add docstring to lookupFileArg

Change-Id: Ifc149764f5a15725d3d630677c6da29def4b0f3e
This commit is contained in:
Qyriad 2024-05-20 13:16:09 -06:00
parent c46083426d
commit d4491263cb
3 changed files with 105 additions and 1 deletions

View file

@ -28,6 +28,26 @@ private:
std::map<std::string, std::string> autoArgs; std::map<std::string, std::string> autoArgs;
}; };
SourcePath lookupFileArg(EvalState & state, std::string_view s); /** @brief Resolve an argument that is generally a file, but could be something that
* is easy to resolve to a file, like a <lookup path> or a tarball URL.
*
* In particular, this will resolve and fetch pseudo-URLs starting with
* @c channel:, flakerefs starting with @c flake:, and anything that
* @ref nix::fetchers::downloadTarball() can take.
*
* Non-absolute files are looked up relative to the current directory(?)
* FIXME: the process's current directory or EvalState's current directory?
*
* @param state The nix::EvalState to base settings, store, and nixPath from.
*
* @param fileArg The the path-ish to resolve.
*
* @return A nix::SourcePath to the resolved and fetched file.
*
* @exception nix::FileTransferError from nix::fetchers::downloadTarball(). Probably others.
*
* @exception nix::ThrownError for failed search path lookup. Probably others.
*/
SourcePath lookupFileArg(EvalState & state, std::string_view fileArg);
} }

57
tests/unit/libcmd/args.cc Normal file
View file

@ -0,0 +1,57 @@
#include <iostream>
#include <memory>
#include <string_view>
#include <boost/core/demangle.hpp>
#include <gtest/gtest.h>
#include "common-eval-args.hh"
#include "eval.hh"
#include "filetransfer.hh"
#include "shared.hh"
#include "store-api.hh"
#include "util.hh"
constexpr std::string_view INVALID_CHANNEL = "channel:example";
constexpr std::string_view CHANNEL_URL = "https://nixos.org/channels/example/nixexprs.tar.xz";
namespace nix
{
TEST(Arguments, lookupFileArg) {
initNix();
initGC();
std::string const unitDataPath = getEnv("_NIX_TEST_UNIT_DATA").value();
// Meson should be allowed to pass us a relative path here tbh.
auto const canonDataPath = CanonPath::fromCwd(unitDataPath);
std::string const searchPathElem = fmt("example=%s", unitDataPath);
SearchPath searchPath;
searchPath.elements.push_back(SearchPath::Elem::parse(searchPathElem));
auto store = openStore("dummy://");
auto statePtr = std::make_shared<EvalState>(searchPath, store, store);
auto & state = *statePtr;
SourcePath const foundUnitData = lookupFileArg(state, "<example>");
EXPECT_EQ(foundUnitData.path, canonDataPath);
// lookupFileArg should not resolve <search paths> if anything else is before or after it.
SourcePath const yepEvenSpaces = lookupFileArg(state, " <example>");
EXPECT_EQ(yepEvenSpaces.path, CanonPath::fromCwd(" <example>"));
EXPECT_EQ(lookupFileArg(state, "<example>/nixos").path, CanonPath::fromCwd("<example>/nixos"));
try {
lookupFileArg(state, INVALID_CHANNEL);
} catch (FileTransferError const & ex) {
std::string_view msg(ex.what());
EXPECT_NE(msg.find(CHANNEL_URL), msg.npos);
}
SourcePath const normalFile = lookupFileArg(state, unitDataPath);
EXPECT_EQ(normalFile.path, canonDataPath);
}
}

View file

@ -212,3 +212,30 @@ test(
protocol : 'gtest', protocol : 'gtest',
verbose : true, verbose : true,
) )
libcmd_tester = executable(
'liblixcmd-tests',
files('libcmd/args.cc'),
dependencies : [
liblixcmd,
liblixutil,
liblixmain,
liblixexpr,
liblixstore,
gtest,
boost,
],
)
test(
'libcmd-eval-args',
libcmd_tester,
args : [tests_args, 'TestEvalArgs'],
env : {
# No special meaning here, it's just a file laying around that is unlikely to go anywhere
# any time soon.
'_NIX_TEST_UNIT_DATA': meson.project_source_root() / 'src/nix-env/buildenv.nix',
},
suite : 'check',
protocol : 'gtest',
)