Implement tests for lookupPathForProgram and fix bugs caught by tests

This commit is contained in:
PoweredByPie 2024-06-17 18:44:23 -07:00
parent d7537f6955
commit 4f6e3b9402
2 changed files with 13 additions and 5 deletions

View file

@ -94,7 +94,7 @@ std::string runProgram(
Path lookupPathForProgram(const Path & program) Path lookupPathForProgram(const Path & program)
{ {
if (program.find('/') != program.npos || program.find('\\') != program.npos) { if (program.find('/') != program.npos || program.find('\\') != program.npos) {
throw Error("program '%1%' partially specifies its path", program); throw UsageError("program '%1%' partially specifies its path", program);
} }
// Possible extensions. // Possible extensions.
@ -107,8 +107,9 @@ Path lookupPathForProgram(const Path & program)
} }
// Look through each directory listed in $PATH. // Look through each directory listed in $PATH.
for (const std::string & dir : tokenizeString<Strings>(*getEnv("PATH"), ";")) { for (const std::string & dir : tokenizeString<Strings>(*path, ";")) {
Path candidate = canonPath(dir) + '/' + program; // TODO: This should actually be canonPath(dir), but that ends up appending two drive paths
Path candidate = dir + "/" + program;
for (const auto ext : exts) { for (const auto ext : exts) {
if (pathExists(candidate + ext)) { if (pathExists(candidate + ext)) {
return candidate; return candidate;
@ -408,5 +409,4 @@ bool statusOk(int status)
{ {
return status == 0; return status == 0;
} }
} }

View file

@ -6,11 +6,19 @@ namespace nix {
/* /*
TEST(SpawnTest, spawnEcho) TEST(SpawnTest, spawnEcho)
{ {
auto output = runProgram(RunOptions{.program = "echo", .args = {}}); auto output = runProgram(RunOptions{.program = "cmd", .lookupPath = true, .args = {"/C", "echo \"hello world\""}});
std::cout << output.second << std::endl;
} }
*/ */
#ifdef _WIN32 #ifdef _WIN32
Path lookupPathForProgram(const Path & program);
TEST(SpawnTest, pathSearch)
{
ASSERT_NO_THROW(lookupPathForProgram("cmd"));
ASSERT_NO_THROW(lookupPathForProgram("cmd.exe"));
ASSERT_THROW(lookupPathForProgram("C:/System32/cmd.exe"), UsageError);
}
std::string windowsEscape(const std::string & str, bool cmd); std::string windowsEscape(const std::string & str, bool cmd);
TEST(SpawnTest, windowsEscape) TEST(SpawnTest, windowsEscape)