nix/tests/unit/libutil/executable-path.cc
John Ericson 6c861b9c51 Factor out lookupExecutable and other PATH improvments
This ended up motivating a good deal of other infra improvements in
order to get Windows right:

- `OsString` to complement `std::filesystem::path`

- env var code for working with the underlying `OsString`s

- Rename `PATHNG_LITERAL` to `OS_STR`

- `NativePathTrait` renamed to `OsPathTrait`, given a character template
  parameter until #9205 is complete.

Split `tests.cc` matching split of `util.{cc,hh}` last year.

Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
2024-08-07 18:12:58 -04:00

65 lines
1.5 KiB
C++

#include <gtest/gtest.h>
#include "executable-path.hh"
namespace nix {
#ifdef WIN32
# define PATH_VAR_SEP L";"
#else
# define PATH_VAR_SEP ":"
#endif
#define PATH_ENV_ROUND_TRIP(NAME, STRING_LIT, CXX_LIT) \
TEST(ExecutablePath, NAME) \
{ \
OsString s = STRING_LIT; \
auto v = ExecutablePath::parse(s); \
EXPECT_EQ(v, (ExecutablePath CXX_LIT)); \
auto s2 = v.render(); \
EXPECT_EQ(s2, s); \
}
PATH_ENV_ROUND_TRIP(emptyRoundTrip, OS_STR(""), ({}))
PATH_ENV_ROUND_TRIP(
oneElemRoundTrip,
OS_STR("/foo"),
({
OS_STR("/foo"),
}))
PATH_ENV_ROUND_TRIP(
twoElemsRoundTrip,
OS_STR("/foo" PATH_VAR_SEP "/bar"),
({
OS_STR("/foo"),
OS_STR("/bar"),
}))
PATH_ENV_ROUND_TRIP(
threeElemsRoundTrip,
OS_STR("/foo" PATH_VAR_SEP "." PATH_VAR_SEP "/bar"),
({
OS_STR("/foo"),
OS_STR("."),
OS_STR("/bar"),
}))
TEST(ExecutablePath, elementyElemNormalize)
{
auto v = ExecutablePath::parse(PATH_VAR_SEP PATH_VAR_SEP PATH_VAR_SEP);
EXPECT_EQ(
v,
(ExecutablePath{{
OS_STR("."),
OS_STR("."),
OS_STR("."),
OS_STR("."),
}}));
auto s2 = v.render();
EXPECT_EQ(s2, OS_STR("." PATH_VAR_SEP "." PATH_VAR_SEP "." PATH_VAR_SEP "."));
}
}