From 1300a82ee07e6461daf2c00d175282b0aa0d0f49 Mon Sep 17 00:00:00 2001 From: mertcandav Date: Wed, 19 Feb 2025 08:49:14 +0300 Subject: [PATCH] std/os: add tests --- .github/workflows/std_tests_clang_macos.yml | 5 + .github/workflows/std_tests_clang_ubuntu.yml | 5 + .github/workflows/std_tests_clang_windows.yml | 5 + .github/workflows/std_tests_gcc_macos.yml | 6 + .github/workflows/std_tests_gcc_ubuntu.yml | 5 + .github/workflows/std_tests_gcc_windows.yml | 5 + std/os/export_windows.test.jule | 6 + std/os/test/path_windows_test.jule | 115 ++++++++++++++++++ 8 files changed, 152 insertions(+) create mode 100644 std/os/export_windows.test.jule create mode 100644 std/os/test/path_windows_test.jule diff --git a/.github/workflows/std_tests_clang_macos.yml b/.github/workflows/std_tests_clang_macos.yml index 8504f40c..9666bf30 100644 --- a/.github/workflows/std_tests_clang_macos.yml +++ b/.github/workflows/std_tests_clang_macos.yml @@ -92,6 +92,11 @@ jobs: julec test --compiler clang -o test std/net/url ./test + - name: Test - std/os + run: | + julec test --compiler clang -o test std/os/test + ./test + - name: Test - std/path run: | julec test --compiler clang -o test std/path diff --git a/.github/workflows/std_tests_clang_ubuntu.yml b/.github/workflows/std_tests_clang_ubuntu.yml index 3108aac5..480bc80e 100644 --- a/.github/workflows/std_tests_clang_ubuntu.yml +++ b/.github/workflows/std_tests_clang_ubuntu.yml @@ -92,6 +92,11 @@ jobs: julec test --compiler clang -o test std/net/url ./test + - name: Test - std/os + run: | + julec test --compiler clang -o test std/os/test + ./test + - name: Test - std/path run: | julec test --compiler clang -o test std/path diff --git a/.github/workflows/std_tests_clang_windows.yml b/.github/workflows/std_tests_clang_windows.yml index a1e568c2..6aec742c 100644 --- a/.github/workflows/std_tests_clang_windows.yml +++ b/.github/workflows/std_tests_clang_windows.yml @@ -92,6 +92,11 @@ jobs: .\bin\julec test --compiler clang -o test std/net/url ./test + - name: Test - std/os + run: | + .\bin\julec test --compiler clang -o test std/os/test + ./test + - name: Test - std/path run: | .\bin\julec test --compiler clang -o test std/path diff --git a/.github/workflows/std_tests_gcc_macos.yml b/.github/workflows/std_tests_gcc_macos.yml index 11cd2d48..4b7d1ba9 100644 --- a/.github/workflows/std_tests_gcc_macos.yml +++ b/.github/workflows/std_tests_gcc_macos.yml @@ -108,6 +108,12 @@ jobs: g++-13 -w --std=c++17 -O0 -Wl,-ld_classic -o test dist/ir.cpp ./test + - name: Test - std/os + run: | + julec test --compiler gcc --compiler-path g++-13 -o test -t std/os/test + g++-13 -w --std=c++17 -O0 -Wl,-ld_classic -o test dist/ir.cpp + ./test + - name: Test - std/path run: | julec test --compiler gcc --compiler-path g++-13 -o test -t std/path diff --git a/.github/workflows/std_tests_gcc_ubuntu.yml b/.github/workflows/std_tests_gcc_ubuntu.yml index 89f95c95..daf12ae4 100644 --- a/.github/workflows/std_tests_gcc_ubuntu.yml +++ b/.github/workflows/std_tests_gcc_ubuntu.yml @@ -88,6 +88,11 @@ jobs: julec test --compiler gcc -o test std/net/url ./test + - name: Test - std/os + run: | + julec test --compiler gcc -o test std/os/test + ./test + - name: Test - std/path run: | julec test --compiler gcc -o test std/path diff --git a/.github/workflows/std_tests_gcc_windows.yml b/.github/workflows/std_tests_gcc_windows.yml index bb12de97..44ef9e80 100644 --- a/.github/workflows/std_tests_gcc_windows.yml +++ b/.github/workflows/std_tests_gcc_windows.yml @@ -88,6 +88,11 @@ jobs: .\bin\julec test --compiler gcc -o test std/net/url ./test + - name: Test - std/os + run: | + .\bin\julec test --compiler gcc -o test std/os/test + ./test + - name: Test - std/path run: | .\bin\julec test --compiler gcc -o test std/path diff --git a/std/os/export_windows.test.jule b/std/os/export_windows.test.jule new file mode 100644 index 00000000..644ec71d --- /dev/null +++ b/std/os/export_windows.test.jule @@ -0,0 +1,6 @@ +// Copyright 2025 The Jule Programming Language. +// Use of this source code is governed by a BSD 3-Clause +// license that can be found in the LICENSE file. + +// Export for testing. +static AddExtendedPrefix = addExtendedPrefix \ No newline at end of file diff --git a/std/os/test/path_windows_test.jule b/std/os/test/path_windows_test.jule new file mode 100644 index 00000000..be6cb47c --- /dev/null +++ b/std/os/test/path_windows_test.jule @@ -0,0 +1,115 @@ +// Copyright 2025 The Jule Programming Language. +// Use of this source code is governed by a BSD 3-Clause +// license that can be found in the LICENSE file. + +use "std/os" +use "std/os/filepath" +use "std/strings" +use "std/testing" + +struct addExtendedPrefixTest { + input: str + want: str +} + +#test +fn testAddExtendedPrefix(t: &testing::T) { + // Test addExtendedPrefix instead of fixLongPath so the path manipulation code + // is exercised even if long path are supported by the system, else the + // function might not be tested at all if/when all test builders support long paths. + mut cwd := os::Getwd()! + drive := strings::ToLower(filepath::VolumeName(cwd)) + cwd = strings::ToLower(cwd[len(drive)+1:]) + // Build a very long pathname. Paths in Go are supposed to be arbitrarily long, + // so let's make a long path which is comfortably bigger than MAX_PATH on Windows + // (256) and thus requires fixLongPath to be correctly interpreted in I/O syscalls. + veryLong := "l" + strings::Repeat("o", 500) + "ng" + for _, test in []addExtendedPrefixTest([ + // Test cases use word substitutions: + // * "long" is replaced with a very long pathname + // * "c:" or "C:" are replaced with the drive of the current directory (preserving case) + // * "cwd" is replaced with the current directory + + // Drive Absolute + {`C:\long\foo.txt`, `\\?\C:\long\foo.txt`}, + {`C:/long/foo.txt`, `\\?\C:\long\foo.txt`}, + {`C:\\\long///foo.txt`, `\\?\C:\long\foo.txt`}, + {`C:\long\.\foo.txt`, `\\?\C:\long\foo.txt`}, + {`C:\long\..\foo.txt`, `\\?\C:\foo.txt`}, + {`C:\long\..\..\foo.txt`, `\\?\C:\foo.txt`}, + + // Drive Relative + {`C:long\foo.txt`, `\\?\C:\cwd\long\foo.txt`}, + {`C:long/foo.txt`, `\\?\C:\cwd\long\foo.txt`}, + {`C:long///foo.txt`, `\\?\C:\cwd\long\foo.txt`}, + {`C:long\.\foo.txt`, `\\?\C:\cwd\long\foo.txt`}, + {`C:long\..\foo.txt`, `\\?\C:\cwd\foo.txt`}, + + // Rooted + {`\long\foo.txt`, `\\?\C:\long\foo.txt`}, + {`/long/foo.txt`, `\\?\C:\long\foo.txt`}, + {`\long///foo.txt`, `\\?\C:\long\foo.txt`}, + {`\long\.\foo.txt`, `\\?\C:\long\foo.txt`}, + {`\long\..\foo.txt`, `\\?\C:\foo.txt`}, + + // Relative + {`long\foo.txt`, `\\?\C:\cwd\long\foo.txt`}, + {`long/foo.txt`, `\\?\C:\cwd\long\foo.txt`}, + {`long///foo.txt`, `\\?\C:\cwd\long\foo.txt`}, + {`long\.\foo.txt`, `\\?\C:\cwd\long\foo.txt`}, + {`long\..\foo.txt`, `\\?\C:\cwd\foo.txt`}, + {`.\long\foo.txt`, `\\?\C:\cwd\long\foo.txt`}, + + // UNC Absolute + {`\\srv\share\long`, `\\?\UNC\srv\share\long`}, + {`//srv/share/long`, `\\?\UNC\srv\share\long`}, + {`/\srv/share/long`, `\\?\UNC\srv\share\long`}, + {`\\srv\share\long\`, `\\?\UNC\srv\share\long\`}, + {`\\srv\share\bar\.\long`, `\\?\UNC\srv\share\bar\long`}, + {`\\srv\share\bar\..\long`, `\\?\UNC\srv\share\long`}, + {`\\srv\share\bar\..\..\long`, `\\?\UNC\srv\share\long`}, // share name is not removed by ".." + + // Local Device + {`\\.\C:\long\foo.txt`, `\\.\C:\long\foo.txt`}, + {`//./C:/long/foo.txt`, `\\.\C:\long\foo.txt`}, + {`/\./C:/long/foo.txt`, `\\.\C:\long\foo.txt`}, + {`\\.\C:\long///foo.txt`, `\\.\C:\long\foo.txt`}, + {`\\.\C:\long\.\foo.txt`, `\\.\C:\long\foo.txt`}, + {`\\.\C:\long\..\foo.txt`, `\\.\C:\foo.txt`}, + + // Misc tests + {`C:\short.txt`, `C:\short.txt`}, + {`C:\`, `C:\`}, + {`C:`, `C:`}, + {`\\srv\path`, `\\srv\path`}, + {`long.txt`, `\\?\C:\cwd\long.txt`}, + {`C:long.txt`, `\\?\C:\cwd\long.txt`}, + {`C:\long\.\bar\baz`, `\\?\C:\long\bar\baz`}, + {`C:long\.\bar\baz`, `\\?\C:\cwd\long\bar\baz`}, + {`C:\long\..\bar\baz`, `\\?\C:\bar\baz`}, + {`C:long\..\bar\baz`, `\\?\C:\cwd\bar\baz`}, + {`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz\`}, + {`C:\long\..`, `\\?\C:\`}, + {`C:\.\long\..\.`, `\\?\C:\`}, + {`\\?\C:\long\foo.txt`, `\\?\C:\long\foo.txt`}, + {`\\?\C:\long/foo.txt`, `\\?\C:\long/foo.txt`}, + ]) { + mut input := strings::ReplaceAll(test.input, "long", veryLong) + input = strings::ToLower(input) + input = strings::ReplaceAll(input, "c:", drive) + + mut want := strings::ReplaceAll(test.want, "long", veryLong) + want = strings::ToLower(want) + want = strings::ReplaceAll(want, "c:", drive) + want = strings::ReplaceAll(want, "cwd", cwd) + + mut got := os::AddExtendedPrefix(input) + got = strings::ToLower(got) + if got != want { + input = strings::ReplaceAll(input, veryLong, "long") + got = strings::ReplaceAll(got, veryLong, "long") + want = strings::ReplaceAll(want, veryLong, "long") + t.Errorf("addExtendedPrefix({}) = {}; want {}", input, got, want) + } + } +} \ No newline at end of file