mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: Make LexicalPath::relative_path() fallible
This commit is contained in:
parent
f026d495cd
commit
31bf40b659
Notes:
github-actions[bot]
2024-11-09 19:43:30 +00:00
Author: https://github.com/stasoid Commit: https://github.com/LadybirdBrowser/ladybird/commit/31bf40b6595 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1918 Reviewed-by: https://github.com/ADKaster ✅
8 changed files with 12 additions and 14 deletions
|
@ -147,12 +147,10 @@ ByteString LexicalPath::absolute_path(ByteString dir_path, ByteString target)
|
||||||
return LexicalPath::canonicalized_path(join(dir_path, target).string());
|
return LexicalPath::canonicalized_path(join(dir_path, target).string());
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteString LexicalPath::relative_path(StringView a_path, StringView a_prefix)
|
Optional<ByteString> LexicalPath::relative_path(StringView a_path, StringView a_prefix)
|
||||||
{
|
{
|
||||||
if (!a_path.starts_with('/') || !a_prefix.starts_with('/')) {
|
if (!a_path.starts_with('/') || !a_prefix.starts_with('/'))
|
||||||
// FIXME: This should probably VERIFY or return an Optional<ByteString>.
|
return {};
|
||||||
return ""sv;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (a_path == a_prefix)
|
if (a_path == a_prefix)
|
||||||
return ".";
|
return ".";
|
||||||
|
|
|
@ -46,7 +46,7 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] static ByteString canonicalized_path(ByteString);
|
[[nodiscard]] static ByteString canonicalized_path(ByteString);
|
||||||
[[nodiscard]] static ByteString absolute_path(ByteString dir_path, ByteString target);
|
[[nodiscard]] static ByteString absolute_path(ByteString dir_path, ByteString target);
|
||||||
[[nodiscard]] static ByteString relative_path(StringView absolute_path, StringView absolute_prefix);
|
[[nodiscard]] static Optional<ByteString> relative_path(StringView absolute_path, StringView absolute_prefix);
|
||||||
|
|
||||||
template<typename... S>
|
template<typename... S>
|
||||||
[[nodiscard]] static LexicalPath join(StringView first, S&&... rest)
|
[[nodiscard]] static LexicalPath join(StringView first, S&&... rest)
|
||||||
|
|
|
@ -115,11 +115,11 @@ ByteString LexicalPath::absolute_path(ByteString dir_path, ByteString target)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns relative version of abs_path (relative to abs_prefix), such that join(abs_prefix, rel_path) == abs_path.
|
// Returns relative version of abs_path (relative to abs_prefix), such that join(abs_prefix, rel_path) == abs_path.
|
||||||
ByteString LexicalPath::relative_path(StringView abs_path, StringView abs_prefix)
|
Optional<ByteString> LexicalPath::relative_path(StringView abs_path, StringView abs_prefix)
|
||||||
{
|
{
|
||||||
if (!is_absolute_path(abs_path) || !is_absolute_path(abs_prefix)
|
if (!is_absolute_path(abs_path) || !is_absolute_path(abs_prefix)
|
||||||
|| abs_path[0] != abs_prefix[0]) // different drives
|
|| abs_path[0] != abs_prefix[0]) // different drives
|
||||||
return "";
|
return {};
|
||||||
|
|
||||||
auto path = canonicalized_path(abs_path);
|
auto path = canonicalized_path(abs_path);
|
||||||
auto prefix = canonicalized_path(abs_prefix);
|
auto prefix = canonicalized_path(abs_prefix);
|
||||||
|
|
|
@ -390,7 +390,7 @@ ErrorOr<void> run_tests(Core::AnonymousBuffer const& theme, Gfx::IntSize window_
|
||||||
outln("Found {} tests...", tests.size());
|
outln("Found {} tests...", tests.size());
|
||||||
|
|
||||||
for (auto const& [i, test] : enumerate(tests))
|
for (auto const& [i, test] : enumerate(tests))
|
||||||
outln("{}/{}: {}", i + 1, tests.size(), LexicalPath::relative_path(test.input_path, app.test_root_path));
|
outln("{}/{}: {}", i + 1, tests.size(), *LexicalPath::relative_path(test.input_path, app.test_root_path));
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
|
@ -295,7 +295,7 @@ static void generate_include_for(auto& generator, auto& path)
|
||||||
for (auto& search_path : g_header_search_paths) {
|
for (auto& search_path : g_header_search_paths) {
|
||||||
if (!path.starts_with(search_path))
|
if (!path.starts_with(search_path))
|
||||||
continue;
|
continue;
|
||||||
auto relative_path = LexicalPath::relative_path(path, search_path);
|
auto relative_path = *LexicalPath::relative_path(path, search_path);
|
||||||
if (relative_path.length() < path_string.length())
|
if (relative_path.length() < path_string.length())
|
||||||
path_string = relative_path;
|
path_string = relative_path;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,8 @@ TEST_CASE(relative_path)
|
||||||
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt"sv, "/"sv), "tmp/abc.txt"sv);
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt"sv, "/"sv), "tmp/abc.txt"sv);
|
||||||
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt"sv, "/usr"sv), "../tmp/abc.txt"sv);
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt"sv, "/usr"sv), "../tmp/abc.txt"sv);
|
||||||
|
|
||||||
EXPECT_EQ(LexicalPath::relative_path("/tmp/foo.txt"sv, "tmp"sv), ""sv);
|
EXPECT(!LexicalPath::relative_path("/tmp/foo.txt"sv, "tmp"sv).has_value());
|
||||||
EXPECT_EQ(LexicalPath::relative_path("tmp/foo.txt"sv, "/tmp"sv), ""sv);
|
EXPECT(!LexicalPath::relative_path("tmp/foo.txt"sv, "/tmp"sv).has_value());
|
||||||
|
|
||||||
EXPECT_EQ(LexicalPath::relative_path("/tmp/foo/bar/baz.txt"sv, "/tmp/bar/foo/"sv), "../../foo/bar/baz.txt"sv);
|
EXPECT_EQ(LexicalPath::relative_path("/tmp/foo/bar/baz.txt"sv, "/tmp/bar/foo/"sv), "../../foo/bar/baz.txt"sv);
|
||||||
}
|
}
|
||||||
|
|
|
@ -186,7 +186,7 @@ TESTJS_RUN_FILE_FUNCTION(ByteString const& test_file, JS::Realm& realm, JS::Exec
|
||||||
}
|
}
|
||||||
|
|
||||||
auto test_result = test_passed ? Test::Result::Pass : Test::Result::Fail;
|
auto test_result = test_passed ? Test::Result::Pass : Test::Result::Fail;
|
||||||
auto test_path = LexicalPath::relative_path(test_file, Test::JS::g_test_root);
|
auto test_path = *LexicalPath::relative_path(test_file, Test::JS::g_test_root);
|
||||||
auto duration_ms = Test::get_time_in_ms() - start_time;
|
auto duration_ms = Test::get_time_in_ms() - start_time;
|
||||||
return Test::JS::JSFileResult {
|
return Test::JS::JSFileResult {
|
||||||
test_path,
|
test_path,
|
||||||
|
|
|
@ -245,7 +245,7 @@ inline void TestRunner::print_test_results_as_json() const
|
||||||
if (name == "__$$TOP_LEVEL$$__"sv)
|
if (name == "__$$TOP_LEVEL$$__"sv)
|
||||||
name = ByteString::empty();
|
name = ByteString::empty();
|
||||||
|
|
||||||
auto path = LexicalPath::relative_path(suite.path, m_test_root);
|
auto path = *LexicalPath::relative_path(suite.path, m_test_root);
|
||||||
|
|
||||||
tests.set(ByteString::formatted("{}/{}::{}", path, name, case_.name), result_name);
|
tests.set(ByteString::formatted("{}/{}::{}", path, name, case_.name), result_name);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue