Skip to content

Commit 159b471

Browse files
committed
Add more tests for canonicalize_path
1 parent b4043d6 commit 159b471

File tree

5 files changed

+384
-31
lines changed

5 files changed

+384
-31
lines changed

src/quick-lint-js/temporary-directory.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ void create_directory(const std::string& path);
1515

1616
// Crashes on failure.
1717
void delete_directory_recursive(const std::string& path);
18+
19+
// Crashes on failure.
20+
std::string get_current_working_directory();
21+
22+
// Crashes on failure.
23+
void set_current_working_directory(const char* path);
1824
}
1925

2026
#endif

src/temporary-directory.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <cstdlib>
88
#include <cstring>
99
#include <iostream>
10+
#include <limits.h>
1011
#include <quick-lint-js/have.h>
1112
#include <quick-lint-js/temporary-directory.h>
1213
#include <quick-lint-js/unreachable.h>
@@ -25,6 +26,10 @@
2526
#include <sys/stat.h>
2627
#endif
2728

29+
#if QLJS_HAVE_UNISTD_H
30+
#include <unistd.h>
31+
#endif
32+
2833
namespace quick_lint_js {
2934
#if QLJS_HAVE_MKDTEMP
3035
std::string make_temporary_directory() {
@@ -133,10 +138,37 @@ void delete_directory_recursive(const std::string &path) {
133138
::fts_close(fts);
134139
}
135140
#elif QLJS_HAVE_STD_FILESYSTEM
136-
void delete_directory_recursive(const std::string& path) {
141+
void delete_directory_recursive(const std::string &path) {
137142
std::filesystem::remove_all(std::filesystem::path(path));
138143
}
139144
#endif
145+
146+
std::string get_current_working_directory() {
147+
#if QLJS_HAVE_STD_FILESYSTEM
148+
return std::filesystem::current_path().string();
149+
#else
150+
std::string cwd;
151+
cwd.resize(PATH_MAX);
152+
if (!::getcwd(cwd.data(), cwd.size() + 1)) {
153+
std::fprintf(stderr, "error: failed to get current directory: %s\n",
154+
std::strerror(errno));
155+
std::terminate();
156+
}
157+
return cwd;
158+
#endif
159+
}
160+
161+
void set_current_working_directory(const char *path) {
162+
#if QLJS_HAVE_STD_FILESYSTEM
163+
std::filesystem::current_path(path);
164+
#else
165+
if (::chdir(path) != 0) {
166+
std::fprintf(stderr, "error: failed to set current directory to %s: %s\n",
167+
path, std::strerror(errno));
168+
std::terminate();
169+
}
170+
#endif
171+
}
140172
}
141173

142174
// quick-lint-js finds bugs in JavaScript programs.

test/quick-lint-js/file-matcher.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ inline ::testing::AssertionResult assert_same_file(
6161
return assert_same_file(lhs_expr, rhs_expr, lhs_path.c_str(),
6262
rhs_path.c_str());
6363
}
64+
65+
inline ::testing::AssertionResult assert_same_file(const char* lhs_expr,
66+
const char* rhs_expr,
67+
std::string_view lhs_path,
68+
std::string_view rhs_path) {
69+
return assert_same_file(lhs_expr, rhs_expr, std::string(lhs_path).c_str(),
70+
std::string(rhs_path).c_str());
71+
}
6472
}
6573

6674
#endif

test/test-configuration-loader.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,6 @@ using namespace std::literals::string_view_literals;
3737

3838
namespace quick_lint_js {
3939
namespace {
40-
std::string get_current_working_directory() {
41-
#if QLJS_HAVE_STD_FILESYSTEM
42-
return std::filesystem::current_path().string();
43-
#else
44-
std::string cwd;
45-
cwd.resize(PATH_MAX);
46-
if (!::getcwd(cwd.data(), cwd.size() + 1)) {
47-
std::fprintf(stderr, "error: failed to get current directory: %s\n",
48-
std::strerror(errno));
49-
std::terminate();
50-
}
51-
return cwd;
52-
#endif
53-
}
54-
55-
void set_current_working_directory(const char* path) {
56-
#if QLJS_HAVE_STD_FILESYSTEM
57-
std::filesystem::current_path(path);
58-
#else
59-
if (::chdir(path) != 0) {
60-
std::fprintf(stderr, "error: failed to set current directory to %s: %s\n",
61-
path, std::strerror(errno));
62-
std::terminate();
63-
}
64-
#endif
65-
}
66-
6740
class test_configuration_loader : public ::testing::Test {
6841
public:
6942
std::string make_temporary_directory() {

0 commit comments

Comments
 (0)