1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 06:31:14 +02:00

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>
This commit is contained in:
John Ericson 2024-08-05 12:05:29 -04:00
parent 0836888002
commit 6c861b9c51
32 changed files with 616 additions and 97 deletions

View file

@ -0,0 +1,64 @@
#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 "."));
}
}

View file

@ -52,6 +52,7 @@ sources = files(
'closure.cc',
'compression.cc',
'config.cc',
'executable-path.cc',
'file-content-address.cc',
'git.cc',
'hash.cc',

View file

@ -1,4 +1,5 @@
#include <gtest/gtest.h>
#include <rapidcheck/gtest.h>
#include "strings.hh"
@ -231,4 +232,117 @@ TEST(tokenizeString, tokenizeSepEmpty)
ASSERT_EQ(tokenizeString<Strings>(s, ","), expected);
}
/* ----------------------------------------------------------------------------
* splitString
* --------------------------------------------------------------------------*/
TEST(splitString, empty)
{
Strings expected = {""};
ASSERT_EQ(splitString<Strings>("", " \t\n\r"), expected);
}
TEST(splitString, oneSep)
{
Strings expected = {"", ""};
ASSERT_EQ(splitString<Strings>(" ", " \t\n\r"), expected);
}
TEST(splitString, twoSep)
{
Strings expected = {"", "", ""};
ASSERT_EQ(splitString<Strings>(" \n", " \t\n\r"), expected);
}
TEST(splitString, tokenizeSpacesWithSpaces)
{
auto s = "foo bar baz";
Strings expected = {"foo", "bar", "baz"};
ASSERT_EQ(splitString<Strings>(s, " \t\n\r"), expected);
}
TEST(splitString, tokenizeTabsWithDefaults)
{
auto s = "foo\tbar\tbaz";
// Using it like this is weird, but shows the difference with tokenizeString, which also has this test
Strings expected = {"foo", "bar", "baz"};
ASSERT_EQ(splitString<Strings>(s, " \t\n\r"), expected);
}
TEST(splitString, tokenizeTabsSpacesWithDefaults)
{
auto s = "foo\t bar\t baz";
// Using it like this is weird, but shows the difference with tokenizeString, which also has this test
Strings expected = {"foo", "", "bar", "", "baz"};
ASSERT_EQ(splitString<Strings>(s, " \t\n\r"), expected);
}
TEST(splitString, tokenizeTabsSpacesNewlineWithDefaults)
{
auto s = "foo\t\n bar\t\n baz";
// Using it like this is weird, but shows the difference with tokenizeString, which also has this test
Strings expected = {"foo", "", "", "bar", "", "", "baz"};
ASSERT_EQ(splitString<Strings>(s, " \t\n\r"), expected);
}
TEST(splitString, tokenizeTabsSpacesNewlineRetWithDefaults)
{
auto s = "foo\t\n\r bar\t\n\r baz";
// Using it like this is weird, but shows the difference with tokenizeString, which also has this test
Strings expected = {"foo", "", "", "", "bar", "", "", "", "baz"};
ASSERT_EQ(splitString<Strings>(s, " \t\n\r"), expected);
auto s2 = "foo \t\n\r bar \t\n\r baz";
Strings expected2 = {"foo", "", "", "", "", "bar", "", "", "", "", "baz"};
ASSERT_EQ(splitString<Strings>(s2, " \t\n\r"), expected2);
}
TEST(splitString, tokenizeWithCustomSep)
{
auto s = "foo\n,bar\n,baz\n";
Strings expected = {"foo\n", "bar\n", "baz\n"};
ASSERT_EQ(splitString<Strings>(s, ","), expected);
}
TEST(splitString, tokenizeSepAtStart)
{
auto s = ",foo,bar,baz";
Strings expected = {"", "foo", "bar", "baz"};
ASSERT_EQ(splitString<Strings>(s, ","), expected);
}
TEST(splitString, tokenizeSepAtEnd)
{
auto s = "foo,bar,baz,";
Strings expected = {"foo", "bar", "baz", ""};
ASSERT_EQ(splitString<Strings>(s, ","), expected);
}
TEST(splitString, tokenizeSepEmpty)
{
auto s = "foo,,baz";
Strings expected = {"foo", "", "baz"};
ASSERT_EQ(splitString<Strings>(s, ","), expected);
}
// concatStringsSep sep . splitString sep = id if sep is 1 char
RC_GTEST_PROP(splitString, recoveredByConcatStringsSep, (const std::string & s))
{
RC_ASSERT(concatStringsSep("/", splitString<Strings>(s, "/")) == s);
RC_ASSERT(concatStringsSep("a", splitString<Strings>(s, "a")) == s);
}
} // namespace nix