Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run unit tests with clean config #10562

Merged
merged 4 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libmain/shared.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ static void sigHandler(int signo) { }
#endif


void initNix()
void initNix(bool loadConfig)
{
/* Turn on buffering for cerr. */
#if HAVE_PUBSETBUF
static char buf[1024];
std::cerr.rdbuf()->pubsetbuf(buf, sizeof(buf));
#endif

initLibStore();
initLibStore(loadConfig);

#ifndef _WIN32
unix::startSignalHandlerThread();
Expand Down
3 changes: 2 additions & 1 deletion src/libmain/shared.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ int handleExceptions(const std::string & programName, std::function<void()> fun)

/**
* Don't forget to call initPlugins() after settings are initialized!
* @param loadConfig Whether to load configuration from `nix.conf`, `NIX_CONFIG`, etc. May be disabled for unit tests.
*/
void initNix();
void initNix(bool loadConfig = true);

void parseCmdLine(int argc, char * * argv,
std::function<bool(Strings::iterator & arg, const Strings::iterator & end)> parseArg);
Expand Down
10 changes: 10 additions & 0 deletions src/libstore-c/nix_api_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ nix_err nix_libstore_init(nix_c_context * context)
NIXC_CATCH_ERRS
}

nix_err nix_libstore_init_no_load_config(nix_c_context * context)
{
if (context)
context->last_err_code = NIX_OK;
try {
nix::initLibStore(false);
}
NIXC_CATCH_ERRS
}

nix_err nix_init_plugins(nix_c_context * context)
{
if (context)
Expand Down
7 changes: 7 additions & 0 deletions src/libstore-c/nix_api_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ typedef struct StorePath StorePath;
*/
nix_err nix_libstore_init(nix_c_context * context);

/**
* @brief Like nix_libstore_init, but does not load the Nix configuration.
*
* This is useful when external configuration is not desired, such as when running unit tests.
*/
nix_err nix_libstore_init_no_load_config(nix_c_context * context);

/**
* @brief Loads the plugins specified in Nix's plugin-files setting.
*
Expand Down
5 changes: 3 additions & 2 deletions src/libstore/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,13 @@ void assertLibStoreInitialized() {
};
}

void initLibStore() {
void initLibStore(bool loadConfig) {
if (initLibStoreDone) return;

initLibUtil();

loadConfFile();
if (loadConfig)
loadConfFile();

preloadNSS();

Expand Down
5 changes: 3 additions & 2 deletions src/libstore/globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -1279,9 +1279,10 @@ std::vector<Path> getUserConfigFiles();
extern const std::string nixVersion;

/**
* NB: This is not sufficient. You need to call initNix()
* @param loadConfig Whether to load configuration from `nix.conf`, `NIX_CONFIG`, etc. May be disabled for unit tests.
* @note When using libexpr, and/or libmain, This is not sufficient. See initNix().
*/
void initLibStore();
void initLibStore(bool loadConfig = true);

/**
* It's important to initialize before doing _anything_, which is why we
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/libstore-support/tests/libstore.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace nix {
class LibStoreTest : public virtual ::testing::Test {
public:
static void SetUpTestSuite() {
initLibStore();
initLibStore(false);
}

protected:
Expand Down
Loading