Skip to content

Commit

Permalink
[libcxx] [test] Fix all remaining issues with fs::path::string_type b…
Browse files Browse the repository at this point in the history
…eing wstring

Use fs::path as variable type instead of std::string, when the input
potentially is a path, as they can't be implicitly converted back to
string.

Differential Revision: https://reviews.llvm.org/D89674
  • Loading branch information
mstorsjo committed Oct 19, 2020
1 parent 5c39eeb commit 81db3c3
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ void checkIteratorConcepts() {
ASSERT_SAME_TYPE(It, decltype(it--));
ASSERT_SAME_TYPE(Traits::reference, decltype(*it));
ASSERT_SAME_TYPE(Traits::pointer, decltype(it.operator->()));
#ifdef _WIN32
ASSERT_SAME_TYPE(std::wstring const&, decltype(it->native()));
#else
ASSERT_SAME_TYPE(std::string const&, decltype(it->native()));
#endif
ASSERT_SAME_TYPE(bool, decltype(it == it));
ASSERT_SAME_TYPE(bool, decltype(it != it));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ int main(int, char**) {
using namespace fs;
path p("abc");
p = {};
#ifdef _WIN32
assert(p.native() == L"");
#else
assert(p.native() == "");
#endif

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
template <class CharT>
void RunTestCase(MultiStringType const& MS) {
using namespace fs;
const char* Expect = MS;
const fs::path::value_type* Expect = MS;
const CharT* TestPath = MS;
const CharT* TestPathEnd = StrEnd(TestPath);
const std::size_t Size = TestPathEnd - TestPath;
Expand Down Expand Up @@ -211,9 +211,9 @@ void test_sfinae() {
}
}

void RunStringMoveTest(const char* Expect) {
void RunStringMoveTest(const fs::path::value_type* Expect) {
using namespace fs;
std::string ss(Expect);
fs::path::string_type ss(Expect);
path p;
{
DisableAllocationGuard g; ((void)g);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
#include "verbose_assert.h"

struct ComparePathExact {
bool operator()(std::string const& LHS, std::string const& RHS) const {
return LHS == RHS;
bool operator()(fs::path const& LHS, std::string const& RHS) const {
return LHS.string() == RHS;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int main(int, char**) {
" Input: '%s'\n"
" Expected: '%s'\n"
" Output: '%s'\n",
ID, TC.input.c_str(), TC.expect.c_str(), output.native().c_str());
ID, TC.input.c_str(), TC.expect.c_str(), output.string().c_str());
}
}
return Failed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int main(int, char**) {
" Expected: '%s'\n"
" Output: '%s'\n",
ID, Testing, TC.input.c_str(), TC.base.c_str(),
Expected.c_str(), Output.native().c_str());
Expected.string().c_str(), Output.string().c_str());
};
if (!PathEq(output, TC.expect))
ReportErr("path::lexically_relative", output, TC.expect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ TEST_CASE(test_PR35078)
ExceptionChecker Checker(std::errc::permission_denied,
"recursive_directory_iterator::operator++()",
format_string("attempting recursion into \"%s\"",
nestedDir.native()));
nestedDir.string().c_str()));
TEST_CHECK_THROW_RESULT(filesystem_error, Checker, ++it);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TEST_CASE(basic_test)
const fs::path cwd = fs::current_path();
const struct {
std::string input;
std::string expect;
fs::path expect;
} TestCases [] = {
{"", cwd / ""},
{"foo", cwd / "foo"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ struct Times {

Times GetTimes(path const& p) {
StatT st;
if (::stat(p.c_str(), &st) == -1) {
if (::stat(p.string().c_str(), &st) == -1) {
std::error_code ec(errno, std::generic_category());
#ifndef TEST_HAS_NO_EXCEPTIONS
throw ec;
Expand All @@ -123,7 +123,7 @@ TimeSpec LastWriteTime(path const& p) { return GetTimes(p).write; }

Times GetSymlinkTimes(path const& p) {
StatT st;
if (::lstat(p.c_str(), &st) == -1) {
if (::lstat(p.string().c_str(), &st) == -1) {
std::error_code ec(errno, std::generic_category());
#ifndef TEST_HAS_NO_EXCEPTIONS
throw ec;
Expand Down Expand Up @@ -395,7 +395,7 @@ TEST_CASE(get_last_write_time_dynamic_env_test)
SleepFor(Sec(2));

// update file and add a file to the directory. Make sure the times increase.
std::FILE* of = std::fopen(file.c_str(), "a");
std::FILE* of = std::fopen(file.string().c_str(), "a");
std::fwrite("hello", 1, sizeof("hello"), of);
std::fclose(of);
env.create_file("dir/file1", 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ TEST_CASE(basic_test) {
path relative_cwd = cwd.native().substr(1);
// clang-format off
struct {
std::string input;
std::string base;
std::string expect;
fs::path input;
fs::path base;
fs::path expect;
} TestCases[] = {
{"", "", "."},
{cwd, "a", ".."},
Expand Down Expand Up @@ -104,7 +104,8 @@ TEST_CASE(basic_test) {
" Input: '%s'\n"
" Base: '%s'\n"
" Expected: '%s'\n",
ID, TC.input.c_str(), TC.base.c_str(), TC.expect.c_str());
ID, TC.input.string().c_str(), TC.base.string().c_str(),
TC.expect.string().c_str());
} else if (!PathEq(output, TC.expect)) {
TEST_CHECK(PathEq(output, TC.expect));

Expand All @@ -119,9 +120,10 @@ TEST_CASE(basic_test) {
" Lex Prox: '%s'\n"
" Canon Input: '%s'\n"
" Canon Base: '%s'\n",
ID, TC.input.c_str(), TC.base.c_str(), TC.expect.c_str(),
output.native().c_str(), lexically_p.native().c_str(),
canon_input.c_str(), canon_base.c_str());
ID, TC.input.string().c_str(), TC.base.string().c_str(),
TC.expect.string().c_str(), output.string().c_str(),
lexically_p.string().c_str(), canon_input.string().c_str(),
canon_base.string().c_str());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ TEST_CASE(basic_space_test)
// should have the same expected result. Compute this expected result
// one and check that it looks semi-sane.
struct statvfs expect;
TEST_REQUIRE(::statvfs(static_env.Dir.c_str(), &expect) != -1);
TEST_REQUIRE(::statvfs(static_env.Dir.string().c_str(), &expect) != -1);
TEST_CHECK(expect.f_bavail > 0);
TEST_CHECK(expect.f_bfree > 0);
TEST_CHECK(expect.f_bsize > 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

using namespace fs;

void PutEnv(std::string var, std::string value) {
assert(::setenv(var.c_str(), value.c_str(), /* overwrite */ 1) == 0);
void PutEnv(std::string var, fs::path value) {
assert(::setenv(var.c_str(), value.string().c_str(), /* overwrite */ 1) == 0);
}

void UnsetEnv(std::string var) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ int main(int, char**) {

// clang-format off
struct {
std::string input;
std::string expect;
fs::path input;
fs::path expect;
} TestCases[] = {
{"", fs::current_path()},
{".", fs::current_path()},
Expand Down Expand Up @@ -69,7 +69,8 @@ int main(int, char**) {
" Input: '%s'\n"
" Expected: '%s'\n"
" Output: '%s'\n",
ID, TC.input.c_str(), TC.expect.c_str(), output.native().c_str());
ID, TC.input.string().c_str(), TC.expect.string().c_str(),
output.string().c_str());
}
}
return Failed;
Expand Down

0 comments on commit 81db3c3

Please sign in to comment.