This repository was archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwhich_test.cpp
More file actions
63 lines (54 loc) · 1.77 KB
/
Copy pathwhich_test.cpp
File metadata and controls
63 lines (54 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "util/which.hpp"
#include <cstdlib>
#include <fstream>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "util/file.hpp"
namespace {
const std::string test_tmpdir = "/tmp/task_maker_testdir";
void createFile(const std::string& path) { std::ofstream os(path); }
// NOLINTNEXTLINE
TEST(Which, Which) {
util::TempDir tmpdir1(test_tmpdir + "/which");
util::TempDir tmpdir2(test_tmpdir + "/which");
createFile(tmpdir1.Path() + "/cmd");
createFile(tmpdir2.Path() + "/cmd");
createFile(tmpdir2.Path() + "/cmd2");
std::string path = tmpdir1.Path() + ":" + tmpdir2.Path();
setenv("PATH", path.c_str(), 1);
EXPECT_EQ(util::which("cmd"), tmpdir1.Path() + "/cmd");
EXPECT_EQ(util::which("cmd2"), tmpdir2.Path() + "/cmd2");
}
// It's not clear whether this behavior should be expected, on Linux it getenv
// of an unknown variable returns nullptr and the std::string constructor raises
// an exception. On OSX it gets a SIGSEGV. Disabled for now.
//// NOLINTNEXTLINE
//TEST(Which, WhichEmptyPath) {
// unsetenv("PATH");
// EXPECT_THROW(util::which("cmd"), std::exception); // NOLINT
//}
// NOLINTNEXTLINE
TEST(Which, WhichUsesCache) {
std::string path;
{
util::TempDir tmpdir1(test_tmpdir + "/which");
createFile(tmpdir1.Path() + "/cmd");
setenv("PATH", tmpdir1.Path().c_str(), 1);
path = util::which("cmd");
EXPECT_EQ(path, tmpdir1.Path() + "/cmd");
}
EXPECT_EQ(util::which("cmd"), path);
}
// NOLINTNEXTLINE
TEST(Which, WhichCacheDisabled) {
std::string path;
{
util::TempDir tmpdir1(test_tmpdir + "/which");
createFile(tmpdir1.Path() + "/cmd");
setenv("PATH", tmpdir1.Path().c_str(), 1);
path = util::which("cmd");
EXPECT_EQ(path, tmpdir1.Path() + "/cmd");
}
EXPECT_EQ(util::which("cmd", false), "");
}
} // namespace