Skip to content

Commit 5f0ea43

Browse files
test: run tests in build dir, pass source dir in env
This works around an annoyance with gtest_discover_tests. The test discovery system works by running the test binary with flags to make it produce a JSON file that gives details of all the tests. However, that file is written to the working directory. If we set the working directory for the tests to be the source directory, then the tests can access their test data by relative paths, but the JSON test details files for test discovery get written to the source directory, which is ugly. So, we instead provide the location of the source tree through an environment variable, and then in the tests themselves we change the current working directory to the source tree directory. Why change CWD instead of just using absolute paths for input files? Because error messages produced by jsonnet include the file paths, and we need (or at least want) those to still be relative paths so that we can compare against stable golden outputs.
1 parent 733b9b5 commit 5f0ea43

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,10 @@ if(BUILD_TESTS)
308308
add_executable(${TESTNAME} ${ARGN})
309309
target_link_libraries(${TESTNAME} ${JSONNET_LIB_TARGET} gtest gmock gtest_main)
310310
gtest_discover_tests(${TESTNAME}
311-
# Test-data is in the source directory so that's where we run the test.
312-
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
313-
PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
311+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
312+
PROPERTIES
313+
VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
314+
ENVIRONMENT "JSONNET_SOURCE_BASE=${PROJECT_SOURCE_DIR}"
314315
)
315316
set_target_properties(${TESTNAME} PROPERTIES FOLDER tests)
316317
endfunction()

core/libjsonnet_test.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,36 @@ extern "C" {
1818
#include "libjsonnet.h"
1919
}
2020

21+
#include <cstdlib>
22+
#include <filesystem>
23+
2124
#include "gtest/gtest.h"
2225

2326
namespace jsonnet::internal {
2427
namespace {
2528

26-
TEST(JsonnetTest, TestEvaluateSnippet)
29+
class TestInSourceDir : public testing::Test {
30+
protected:
31+
static void SetUpTestSuite() {
32+
original_cwd_ = std::filesystem::current_path();
33+
const char* source_base_env = std::getenv("JSONNET_SOURCE_BASE");
34+
if (source_base_env) {
35+
std::filesystem::path source_base{source_base_env};
36+
std::filesystem::current_path(source_base);
37+
}
38+
}
39+
40+
static void TearDownTestSuite() {
41+
std::filesystem::current_path(original_cwd_);
42+
}
43+
44+
private:
45+
static std::filesystem::path original_cwd_;
46+
};
47+
48+
std::filesystem::path TestInSourceDir::original_cwd_;
49+
50+
TEST_F(TestInSourceDir, TestEvaluateSnippet)
2751
{
2852
const char* snippet = "std.assertEqual(({ x: 1, y: self.x } { x: 2 }).y, 2)";
2953
struct JsonnetVm* vm = jsonnet_make();
@@ -35,13 +59,12 @@ TEST(JsonnetTest, TestEvaluateSnippet)
3559
jsonnet_destroy(vm);
3660
}
3761

38-
TEST(JsonnetTest, TestEvaluateFile)
62+
TEST_F(TestInSourceDir, TestEvaluateFile)
3963
{
40-
const char* file_path = "test_suite/object.jsonnet";
4164
struct JsonnetVm *vm = jsonnet_make();
4265
ASSERT_FALSE(vm == nullptr);
4366
int error = 0;
44-
char* output = jsonnet_evaluate_file(vm, file_path, &error);
67+
char* output = jsonnet_evaluate_file(vm, "test_suite/object.jsonnet", &error);
4568
EXPECT_EQ(0, error);
4669
EXPECT_STREQ("true\n", output);
4770
jsonnet_realloc(vm, output, 0);

cpp/libjsonnet++_test.cpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,48 @@ limitations under the License.
1616

1717
#include "libjsonnet++.h"
1818

19+
#include <cstdlib>
20+
#include <filesystem>
1921
#include <fstream>
2022
#include <streambuf>
2123
#include <string>
2224

2325
#include "gtest/gtest.h"
2426

2527
namespace jsonnet {
26-
std::string readFile(const std::string& filename)
28+
29+
class TestInSourceDir : public testing::Test {
30+
protected:
31+
static void SetUpTestSuite() {
32+
original_cwd_ = std::filesystem::current_path();
33+
const char* source_base_env = std::getenv("JSONNET_SOURCE_BASE");
34+
if (source_base_env) {
35+
std::filesystem::path source_base{source_base_env};
36+
std::filesystem::current_path(source_base);
37+
}
38+
}
39+
40+
static void TearDownTestSuite() {
41+
std::filesystem::current_path(original_cwd_);
42+
}
43+
44+
private:
45+
static std::filesystem::path original_cwd_;
46+
};
47+
48+
std::filesystem::path TestInSourceDir::original_cwd_;
49+
50+
std::string readFile(const std::string& path)
2751
{
28-
std::ifstream in(filename);
52+
std::ifstream in(path);
2953
if (!in.good()){
30-
ADD_FAILURE() << "Could not open: " << filename;
54+
ADD_FAILURE() << "Could not open: " << path;
3155
return "";
3256
}
3357
return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
3458
}
3559

36-
TEST(JsonnetTest, TestEvaluateSnippet)
60+
TEST_F(TestInSourceDir, TestEvaluateSnippet)
3761
{
3862
const std::string input = readFile("cpp/testdata/example.jsonnet");
3963
const std::string expected = readFile("cpp/testdata/example_golden.json");
@@ -46,7 +70,7 @@ TEST(JsonnetTest, TestEvaluateSnippet)
4670
EXPECT_EQ("", jsonnet.lastError());
4771
}
4872

49-
TEST(JsonnetTest, TestEvaluateInvalidSnippet)
73+
TEST_F(TestInSourceDir, TestEvaluateInvalidSnippet)
5074
{
5175
const std::string input = readFile("cpp/testdata/invalid.jsonnet");
5276
const std::string error = readFile("cpp/testdata/invalid.out");
@@ -59,7 +83,7 @@ TEST(JsonnetTest, TestEvaluateInvalidSnippet)
5983
EXPECT_EQ(error, jsonnet.lastError());
6084
}
6185

62-
TEST(JsonnetTest, TestEvaluateFile)
86+
TEST_F(TestInSourceDir, TestEvaluateFile)
6387
{
6488
const std::string expected = readFile("cpp/testdata/example_golden.json");
6589

@@ -71,7 +95,7 @@ TEST(JsonnetTest, TestEvaluateFile)
7195
EXPECT_EQ("", jsonnet.lastError());
7296
}
7397

74-
TEST(JsonnetTest, TestEvaluateInvalidFile)
98+
TEST_F(TestInSourceDir, TestEvaluateInvalidFile)
7599
{
76100
const std::string expected = readFile("cpp/testdata/invalid.out");
77101

@@ -83,7 +107,7 @@ TEST(JsonnetTest, TestEvaluateInvalidFile)
83107
EXPECT_EQ(expected, jsonnet.lastError());
84108
}
85109

86-
TEST(JsonnetTest, TestAddImportPath)
110+
TEST_F(TestInSourceDir, TestAddImportPath)
87111
{
88112
const std::string expected = readFile("cpp/testdata/importing_golden.json");
89113

0 commit comments

Comments
 (0)