mirror of
https://github.com/NixOS/nix.git
synced 2025-12-23 01:11:07 +01:00
Extract getUnitTestData() to test-data.hh and fix unsafe getenv calls
The nix_api_store.cc tests and derivation-parser-bench.cc were using raw getenv() calls or unsafe .value() calls on optional, which would segfault when passed to std::filesystem::path constructor if the _NIX_TEST_UNIT_DATA environment variable was not set.
This commit is contained in:
parent
b54dfb66dd
commit
de6fdb7da5
5 changed files with 38 additions and 29 deletions
|
|
@ -4,24 +4,11 @@
|
|||
#include <gtest/gtest.h>
|
||||
|
||||
#include "nix/util/types.hh"
|
||||
#include "nix/util/environment-variables.hh"
|
||||
#include "nix/util/file-system.hh"
|
||||
#include "nix/util/tests/test-data.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
/**
|
||||
* The path to the unit test data directory. See the contributing guide
|
||||
* in the manual for further details.
|
||||
*/
|
||||
static inline std::filesystem::path getUnitTestData()
|
||||
{
|
||||
auto data = getEnv("_NIX_TEST_UNIT_DATA");
|
||||
if (!data)
|
||||
throw Error(
|
||||
"_NIX_TEST_UNIT_DATA environment variable is not set. Recommendation: use meson, example: 'meson test -C build --gdb'");
|
||||
return std::filesystem::path(*data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we should update "golden masters" instead of running tests
|
||||
* against them. See the contributing guide in the manual for further
|
||||
|
|
|
|||
|
|
@ -10,4 +10,5 @@ headers = files(
|
|||
'json-characterization.hh',
|
||||
'nix_api_util.hh',
|
||||
'string_callback.hh',
|
||||
'test-data.hh',
|
||||
)
|
||||
|
|
|
|||
24
src/libutil-test-support/include/nix/util/tests/test-data.hh
Normal file
24
src/libutil-test-support/include/nix/util/tests/test-data.hh
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
///@file
|
||||
|
||||
#include <filesystem>
|
||||
#include "nix/util/environment-variables.hh"
|
||||
#include "nix/util/error.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
/**
|
||||
* The path to the unit test data directory. See the contributing guide
|
||||
* in the manual for further details.
|
||||
*/
|
||||
static inline std::filesystem::path getUnitTestData()
|
||||
{
|
||||
auto data = getEnv("_NIX_TEST_UNIT_DATA");
|
||||
if (!data)
|
||||
throw Error(
|
||||
"_NIX_TEST_UNIT_DATA environment variable is not set. "
|
||||
"Recommendation: use meson, example: 'meson test -C build --gdb'");
|
||||
return std::filesystem::path(*data);
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
Loading…
Add table
Add a link
Reference in a new issue