From ada3df303e115feb1ce8ce2fd92c01f2f7472f62 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Thu, 8 Aug 2024 17:46:00 +0200 Subject: [PATCH 1/2] Allow symlink test to work irrespective of git settings. --- core/sys/windows/kernel32.odin | 4 +++ core/sys/windows/types.odin | 6 ++++ tests/core/os/dir/alink.txt | 1 - tests/core/os/os.odin | 54 +++++++++++++++++++++++++--------- 4 files changed, 50 insertions(+), 15 deletions(-) delete mode 120000 tests/core/os/dir/alink.txt diff --git a/core/sys/windows/kernel32.odin b/core/sys/windows/kernel32.odin index f2ca2e507..2f2ebbf17 100755 --- a/core/sys/windows/kernel32.odin +++ b/core/sys/windows/kernel32.odin @@ -85,6 +85,10 @@ foreign kernel32 { lpTargetFileName: LPCWSTR, lpSecurityAttributes: LPSECURITY_ATTRIBUTES) -> BOOL --- + CreateSymbolicLinkW :: proc(lpSymlinkFileName: LPCWSTR, + lpTargetFileName: LPCWSTR, + dwFlags: SYMBOLIC_LINK_FLAGS) -> BOOLEAN --- + GetFileInformationByHandleEx :: proc(hFile: HANDLE, fileInfoClass: FILE_INFO_BY_HANDLE_CLASS, lpFileInformation: LPVOID, diff --git a/core/sys/windows/types.odin b/core/sys/windows/types.odin index e10e53cf9..bd75f3ebc 100644 --- a/core/sys/windows/types.odin +++ b/core/sys/windows/types.odin @@ -1276,6 +1276,12 @@ FOF_NO_UI :: (FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FILEOP_FLAGS :: WORD +SYMBOLIC_LINK_FLAG :: enum DWORD { + DIRECTORY = 0, + ALLOW_UNPRIVILGED_CREATE = 1, +} +SYMBOLIC_LINK_FLAGS :: bit_set[SYMBOLIC_LINK_FLAG; DWORD] + DEVMODEW :: struct { dmDeviceName: [32]wchar_t, dmSpecVersion: WORD, diff --git a/tests/core/os/dir/alink.txt b/tests/core/os/dir/alink.txt deleted file mode 120000 index 1891a26c0..000000000 --- a/tests/core/os/dir/alink.txt +++ /dev/null @@ -1 +0,0 @@ -./a.txt \ No newline at end of file diff --git a/tests/core/os/os.odin b/tests/core/os/os.odin index 97ceaeb1e..95aa22223 100644 --- a/tests/core/os/os.odin +++ b/tests/core/os/os.odin @@ -1,12 +1,35 @@ package tests_core_os +import "core:c/libc" +import win32 "core:sys/windows" import "core:os" import "core:slice" - import "core:testing" +import "core:log" + +_ :: libc +_ :: win32 @(test) read_dir :: proc(t: ^testing.T) { + when ODIN_OS == .Windows { + link := win32.utf8_to_wstring(#directory + "dir/alink.txt") + target := win32.utf8_to_wstring(#directory + "dir/a.txt") + sym_err := win32.CreateSymbolicLinkW(link, target, {.ALLOW_UNPRIVILGED_CREATE}) + + if !sym_err { + log.infof("Unable to create symlink, skipping test. Error: %v", win32.GetLastError()) + return + } + } else { + sym_err := libc.system("ln -s " + #directory + "dir/a.txt " + #directory + "dir/alink.txt") + if sym_err != 0 { + log.infof("Unable to create symlink, skipping test. Error: %v", sym_err) + return + } + } + defer os.remove(#directory + "dir/alink.txt") + fd, err := os.open(#directory + "/dir") testing.expect_value(t, err, nil) defer os.close(fd) @@ -19,17 +42,20 @@ read_dir :: proc(t: ^testing.T) { testing.expect_value(t, len(dir), 3) - testing.expect_value(t, dir[0].name, "alink.txt") - testing.expect(t, !dir[0].is_dir, "is a directory") - when ODIN_OS == .Windows { - testing.expect(t, dir[0].mode & os.File_Mode_Sym_Link != 0, "not a symlink") - } else { - testing.expect(t, os.S_ISLNK(auto_cast dir[0].mode), "not a symlink") + if len(dir) > 0 { + testing.expect_value(t, dir[0].name, "alink.txt") + testing.expect(t, !dir[0].is_dir, "is a directory") + when ODIN_OS == .Windows { + testing.expect(t, dir[0].mode & os.File_Mode_Sym_Link != 0, "not a symlink") + } else { + testing.expect(t, os.S_ISLNK(auto_cast dir[0].mode), "not a symlink") + } } - - testing.expect_value(t, dir[1].name, "b.txt") - - testing.expect_value(t, dir[2].name, "sub") - testing.expect(t, dir[2].is_dir, "is not a directory") -} - + if len(dir) > 1 { + testing.expect_value(t, dir[1].name, "b.txt") + } + if len(dir) > 2 { + testing.expect_value(t, dir[2].name, "sub") + testing.expect(t, dir[2].is_dir, "is not a directory") + } +} \ No newline at end of file From d93f3c63d8d8a8dd279960b3aa2948e4a2dea1bf Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Thu, 8 Aug 2024 17:49:08 +0200 Subject: [PATCH 2/2] Rename package to test_core_os to fit with the rest of test_* --- tests/core/os/os.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/core/os/os.odin b/tests/core/os/os.odin index 95aa22223..c7a0dce4c 100644 --- a/tests/core/os/os.odin +++ b/tests/core/os/os.odin @@ -1,4 +1,4 @@ -package tests_core_os +package test_core_os import "core:c/libc" import win32 "core:sys/windows"