Introduce path_normalize

This new function performs normalization of paths including dropping
/./ segments, and resolving /../ segments, in preparation for switching
fish to a "virtual" PWD.
This commit is contained in:
ridiculousfish
2018-09-16 17:55:15 -07:00
parent 767b7a2180
commit 6ad4d94e12
3 changed files with 57 additions and 0 deletions

View File

@@ -4664,6 +4664,22 @@ void test_layout_cache() {
do_test(seqs.find_prompt_layout(L"whatever")->line_count == 100);
}
void test_normalize_path() {
say(L"Testing path normalization");
do_test(normalize_path(L"") == L".");
do_test(normalize_path(L"..") == L"..");
do_test(normalize_path(L"./") == L".");
do_test(normalize_path(L"////abc") == L"//abc");
do_test(normalize_path(L"/abc") == L"/abc");
do_test(normalize_path(L"/abc/") == L"/abc");
do_test(normalize_path(L"/abc/..def/") == L"/abc/..def");
do_test(normalize_path(L"//abc/../def/") == L"//def");
do_test(normalize_path(L"abc/../abc/../abc/../abc") == L"abc");
do_test(normalize_path(L"../../") == L"../..");
do_test(normalize_path(L"foo/./bar") == L"foo/bar");
do_test(normalize_path(L"foo/././bar/.././baz") == L"foo/baz");
}
/// Main test.
int main(int argc, char **argv) {
UNUSED(argc);
@@ -4762,6 +4778,7 @@ int main(int argc, char **argv) {
if (should_test_function("illegal_command_exit_code")) test_illegal_command_exit_code();
if (should_test_function("maybe")) test_maybe();
if (should_test_function("layout_cache")) test_layout_cache();
if (should_test_function("normalize")) test_normalize_path();
// history_tests_t::test_history_speed();
say(L"Encountered %d errors in low-level tests", err_count);