Skip to content

Commit be273f6

Browse files
committed
tests: make relative config paths absolute when copying
1 parent 9e1a9ed commit be273f6

File tree

4 files changed

+62
-21
lines changed

4 files changed

+62
-21
lines changed

test/integration/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set(INTEGRATION_TEST_CONFIGS
33
anonymous.toml
44
container_enums.toml
55
cycles.toml
6+
ignored.toml
67
inheritance_access.toml
78
inheritance_multiple.toml
89
inheritance_polymorphic.toml
@@ -95,7 +96,9 @@ target_include_directories(integration_test_runner PRIVATE ${CMAKE_CURRENT_SOURC
9596
target_link_libraries(integration_test_runner PRIVATE
9697
${GMOCK_MAIN_LIBS}
9798
Boost::headers
98-
${Boost_LIBRARIES})
99+
${Boost_LIBRARIES}
100+
tomlplusplus::tomlplusplus
101+
)
99102
target_compile_definitions(integration_test_runner PRIVATE
100103
TARGET_EXE_PATH="${CMAKE_CURRENT_BINARY_DIR}/integration_test_target"
101104
OID_EXE_PATH="$<TARGET_FILE:oid>"

test/integration/gen_tests.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,7 @@ def add_oid_integration_test(f, config, case_name, case):
240240
f"\n"
241241
f"TEST_F(OidIntegration, {case_str}) {{\n"
242242
f"{generate_skip(case, 'oid')}"
243-
f' std::string configOptions = R"--(\n'
244-
f"{config_extra}\n"
245-
f' )--";\n'
243+
f' std::string configOptions = R"--({config_extra})--";\n'
246244
f" ba::io_context ctx;\n"
247245
f" auto [target, oid] = runOidOnProcess(\n"
248246
f" {{\n"
@@ -307,15 +305,18 @@ def add_oil_integration_test(f, config, case_name, case):
307305
if case.get("oil_disable", False):
308306
return
309307

308+
config_extra = case.get("config", "")
309+
310310
f.write(
311311
f"\n"
312312
f"TEST_F(OilIntegration, {case_str}) {{\n"
313313
f"{generate_skip(case, 'oil')}"
314+
f' std::string configOptions = R"--({config_extra})--";\n'
314315
f" ba::io_context ctx;\n"
315316
f" auto target = runOilTarget({{\n"
316317
f" .ctx = ctx,\n"
317318
f' .targetArgs = "oil {case_str} 1",\n'
318-
f" }});\n\n"
319+
f" }}, std::move(configOptions));\n\n"
319320
f" ASSERT_EQ(exit_code(target), {exit_code});\n"
320321
f"\n"
321322
f" bpt::ptree result_json;\n"

test/integration/runner_common.cpp

+51-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "runner_common.h"
22

3+
#include <toml++/toml.h>
4+
5+
#include <algorithm>
36
#include <boost/algorithm/string.hpp>
47
#include <boost/asio.hpp>
58
#include <boost/process.hpp>
@@ -119,6 +122,48 @@ int IntegrationBase::exit_code(Proc &proc) {
119122
return proc.proc.exit_code();
120123
}
121124

125+
fs::path IntegrationBase::createCustomConfig(const std::string &extraConfig) {
126+
// If no extra config provided, return the config path unaltered.
127+
if (extraConfig.empty()) {
128+
return configFile;
129+
}
130+
131+
auto customConfigFile = workingDir / "oid.config.toml";
132+
auto config = toml::parse_file(configFile);
133+
134+
// As relative paths are allowed, we must canonicalise the paths before
135+
// moving the file to the temporary directory.
136+
fs::path configDirectory = fs::path(configFile).remove_filename();
137+
138+
if (toml::table *types = config["types"].as_table()) {
139+
if (toml::array *arr = (*types)["containers"].as_array()) {
140+
arr->for_each([&](auto &&el) {
141+
if constexpr (toml::is_string<decltype(el)>) {
142+
el = configDirectory / el.get();
143+
}
144+
});
145+
}
146+
}
147+
if (toml::table *headers = config["headers"].as_table()) {
148+
for (auto &path : {"user_paths", "system_paths"}) {
149+
if (toml::array *arr = (*headers)[path].as_array()) {
150+
arr->for_each([&](auto &&el) {
151+
if constexpr (toml::is_string<decltype(el)>) {
152+
el = configDirectory / el.get();
153+
}
154+
});
155+
}
156+
}
157+
}
158+
159+
std::ofstream customConfig(customConfigFile, std::ios_base::app);
160+
customConfig << config;
161+
customConfig << "\n\n# Test custom config\n\n";
162+
customConfig << extraConfig;
163+
164+
return customConfigFile;
165+
}
166+
122167
std::string OidIntegration::TmpDirStr() {
123168
return std::string("/tmp/oid-integration-XXXXXX");
124169
}
@@ -149,18 +194,7 @@ OidProc OidIntegration::runOidOnProcess(OidOpts opts,
149194
std::ofstream touch(segconfigPath);
150195
}
151196

152-
// Only create a new custom config if we've been provided an extra_config
153-
boost::trim(extra_config);
154-
155-
fs::path customConfigFile = configFile;
156-
if (!extra_config.empty()) {
157-
customConfigFile = workingDir / "oid.config.toml";
158-
fs::copy_file(configFile, customConfigFile);
159-
160-
std::ofstream customConfig(customConfigFile, std::ios_base::app);
161-
customConfig << "\n\n# Test custom config\n\n";
162-
customConfig << extra_config;
163-
}
197+
fs::path thisConfig = createCustomConfig(extra_config);
164198

165199
// Keep PID as the last argument to make it easier for users to directly copy
166200
// and modify the command from the verbose mode output.
@@ -169,7 +203,7 @@ OidProc OidIntegration::runOidOnProcess(OidOpts opts,
169203
"--debug-level=3"s,
170204
"--timeout=20"s,
171205
"--dump-json"s,
172-
"--config-file"s, customConfigFile.string(),
206+
"--config-file"s, thisConfig.string(),
173207
"--script-source"s, opts.scriptSource,
174208
"--pid"s, std::to_string(targetProcess.id()),
175209
};
@@ -302,7 +336,7 @@ std::string OilIntegration::TmpDirStr() {
302336
return std::string("/tmp/oil-integration-XXXXXX");
303337
}
304338

305-
Proc OilIntegration::runOilTarget(OidOpts opts) {
339+
Proc OilIntegration::runOilTarget(OidOpts opts, std::string extra_config) {
306340
std::string targetExe = std::string(TARGET_EXE_PATH) + " " + opts.targetArgs;
307341

308342
if (verbose) {
@@ -341,6 +375,8 @@ Proc OilIntegration::runOilTarget(OidOpts opts) {
341375
// clang-format on
342376
}
343377

378+
fs::path thisConfig = createCustomConfig(extra_config);
379+
344380
/* Spawn target with tracing on and IOs redirected in custom pipes to be read
345381
* later */
346382
// clang-format off
@@ -349,7 +385,7 @@ Proc OilIntegration::runOilTarget(OidOpts opts) {
349385
bp::std_in < bp::null,
350386
bp::std_out > std_out_pipe,
351387
bp::std_err > std_err_pipe,
352-
bp::env["CONFIG_FILE_PATH"] = configFile,
388+
bp::env["CONFIG_FILE_PATH"] = thisConfig.string(),
353389
opts.ctx);
354390
// clang-format on
355391

test/integration/runner_common.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class IntegrationBase : public ::testing::Test {
3838
void TearDown() override;
3939
void SetUp() override;
4040
int exit_code(Proc &proc);
41+
std::filesystem::path createCustomConfig(const std::string &extra);
4142

4243
std::filesystem::path workingDir;
4344

@@ -68,5 +69,5 @@ class OilIntegration : public IntegrationBase {
6869
protected:
6970
std::string TmpDirStr() override;
7071

71-
Proc runOilTarget(OidOpts opts);
72+
Proc runOilTarget(OidOpts opts, std::string extra_config);
7273
};

0 commit comments

Comments
 (0)