Skip to content

Commit 4a923c0

Browse files
Make CL_CONFORMANCE_RESULTS_FILENAME Bazel-aware (#2629)
Bazel requires that test outputs are put in a specific directory given by $TEST_UNDECLARED_OUTPUTS_DIR when running a test through Bazel test This patch checks for the environment variable $BAZEL_TEST, which Bazel sets when running tests, and prepends the specified directory to the user-provided path. The behaviour when running outside of a bazel test environment is unchanged
1 parent 0338fd9 commit 4a923c0

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

test_common/harness/testHarness.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
// limitations under the License.
1515
//
1616
#include "testHarness.h"
17+
#include "stringHelpers.h"
1718
#include "compat.h"
1819
#include <algorithm>
1920
#include <stdio.h>
2021
#include <stdlib.h>
2122
#include <string.h>
2223
#include <cassert>
2324
#include <deque>
25+
#include <filesystem>
2426
#include <mutex>
2527
#include <set>
2628
#include <stdexcept>
@@ -33,6 +35,8 @@
3335
#include "imageHelpers.h"
3436
#include "parseParameters.h"
3537

38+
namespace fs = std::filesystem;
39+
3640
#if !defined(_WIN32)
3741
#include <sys/utsname.h>
3842
#include <unistd.h>
@@ -95,11 +99,25 @@ static int saveResultsToJson(const char *suiteName, test_definition testList[],
9599
return EXIT_SUCCESS;
96100
}
97101

98-
FILE *file = fopen(fileName, "w");
102+
fs::path file_path(fileName);
103+
104+
// When running under Bazel test, prepend the Bazel output directory to
105+
// the provided path
106+
if (nullptr != getenv("BAZEL_TEST"))
107+
{
108+
char *bazel_output_dir = getenv("TEST_UNDECLARED_OUTPUTS_DIR");
109+
if (nullptr != bazel_output_dir)
110+
{
111+
file_path = fs::path(bazel_output_dir) / file_path;
112+
}
113+
}
114+
115+
auto file_path_str = to_string(file_path.u8string());
116+
FILE *file = fopen(file_path_str.c_str(), "w");
99117
if (NULL == file)
100118
{
101119
log_error("ERROR: Failed to open '%s' for writing results.\n",
102-
fileName);
120+
file_path_str.c_str());
103121
return EXIT_FAILURE;
104122
}
105123

@@ -128,7 +146,8 @@ static int saveResultsToJson(const char *suiteName, test_definition testList[],
128146

129147
int ret = fclose(file) ? EXIT_FAILURE : EXIT_SUCCESS;
130148

131-
log_info("Saving results to %s: %s!\n", fileName, save_map[ret]);
149+
log_info("Saving results to %s: %s!\n", file_path_str.c_str(),
150+
save_map[ret]);
132151

133152
return ret;
134153
}
@@ -309,6 +328,8 @@ int runTestHarnessWithCheck(int argc, const char *argv[], int testNum,
309328
"CL_CONFORMANCE_RESULTS_FILENAME (currently '%s')\n",
310329
fileName != NULL ? fileName : "<undefined>");
311330
log_info("\t to save results to JSON file.\n");
331+
log_info("\t When running in Bazel test this is relative to "
332+
"$TEST_UNDECLARED_OUTPUTS_DIR.\n");
312333

313334
log_info("\n");
314335
log_info("Test names:\n");

0 commit comments

Comments
 (0)