|
@@ -1,5 +1,6 @@
|
|
package test_os2
|
|
package test_os2
|
|
|
|
|
|
|
|
+import "core:os"
|
|
import "core:fmt"
|
|
import "core:fmt"
|
|
import "core:os/os2"
|
|
import "core:os/os2"
|
|
import "core:testing"
|
|
import "core:testing"
|
|
@@ -22,7 +23,7 @@ when ODIN_TEST {
|
|
TEST_count += 1
|
|
TEST_count += 1
|
|
ok := value == expected
|
|
ok := value == expected
|
|
if !ok {
|
|
if !ok {
|
|
- fmt.printf("expected %v, got %v", expected, value)
|
|
|
|
|
|
+ fmt.printf("expected %v, got %v\n", expected, value)
|
|
TEST_fail += 1
|
|
TEST_fail += 1
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -45,12 +46,13 @@ when ODIN_TEST {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-main :: proc()
|
|
|
|
|
|
+main :: proc()
|
|
{
|
|
{
|
|
t: testing.T
|
|
t: testing.T
|
|
file_test(&t)
|
|
file_test(&t)
|
|
path_test(&t)
|
|
path_test(&t)
|
|
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
|
|
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
|
|
|
|
+ os.exit(TEST_fail > 0 ? 1 : 0)
|
|
}
|
|
}
|
|
|
|
|
|
@private
|
|
@private
|
|
@@ -59,7 +61,7 @@ _expect_no_error :: proc(t: ^testing.T, e: os2.Error, loc := #caller_location) {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-F_OK :: 0 // Test for file existance
|
|
|
|
|
|
+F_OK :: 0 // Test for file existence
|
|
X_OK :: 1 // Test for execute permission
|
|
X_OK :: 1 // Test for execute permission
|
|
W_OK :: 2 // Test for write permission
|
|
W_OK :: 2 // Test for write permission
|
|
R_OK :: 4 // Test for read permission
|
|
R_OK :: 4 // Test for read permission
|
|
@@ -84,44 +86,92 @@ file_test :: proc(t: ^testing.T) {
|
|
expect(t, err != nil, "missing error")
|
|
expect(t, err != nil, "missing error")
|
|
expect_value(t, fd, os2.INVALID_HANDLE)
|
|
expect_value(t, fd, os2.INVALID_HANDLE)
|
|
|
|
|
|
- fd, err = os2.open("write.txt", {.Write, .Create, .Trunc}, 0o664)
|
|
|
|
|
|
+ // NOTE: no executable permissions here
|
|
|
|
+ fd, err = os2.open("file.txt", {.Write, .Create, .Trunc}, 0o664)
|
|
_expect_no_error(t, err)
|
|
_expect_no_error(t, err)
|
|
expect(t, fd != os2.INVALID_HANDLE, "unexpected handle")
|
|
expect(t, fd != os2.INVALID_HANDLE, "unexpected handle")
|
|
|
|
|
|
- s1 := "hello"
|
|
|
|
- b1 := transmute([]u8)s1
|
|
|
|
-
|
|
|
|
|
|
+ s := "hello"
|
|
n: int
|
|
n: int
|
|
- n, err = os2.write_at(fd, b1, 10)
|
|
|
|
|
|
+ n, err = os2.write_at(fd, transmute([]u8)s, 10)
|
|
_expect_no_error(t, err)
|
|
_expect_no_error(t, err)
|
|
expect_value(t, n, 5)
|
|
expect_value(t, n, 5)
|
|
|
|
|
|
- s2 := "abcdefghij"
|
|
|
|
- b2 := transmute([]u8)s2
|
|
|
|
-
|
|
|
|
- n, err = os2.write(fd, b2)
|
|
|
|
|
|
+ s = "abcdefghij"
|
|
|
|
+ n, err = os2.write(fd, transmute([]u8)s)
|
|
_expect_no_error(t, err)
|
|
_expect_no_error(t, err)
|
|
expect_value(t, n, 10)
|
|
expect_value(t, n, 10)
|
|
|
|
|
|
|
|
+ // seek to the "ll" in "hello"
|
|
|
|
+ n64: i64
|
|
|
|
+ n64, err = os2.seek(fd, 12, .Start)
|
|
|
|
+ _expect_no_error(t, err)
|
|
|
|
+ expect_value(t, n64, 12)
|
|
|
|
+
|
|
|
|
+ s = "11"
|
|
|
|
+ n, err = os2.write(fd, transmute([]u8)s)
|
|
|
|
+ _expect_no_error(t, err)
|
|
|
|
+ expect_value(t, n, 2)
|
|
|
|
+
|
|
|
|
+ // seek to the "e" in "he11o"
|
|
|
|
+ n64, err = os2.seek(fd, -3, .Current)
|
|
|
|
+ _expect_no_error(t, err)
|
|
|
|
+ expect_value(t, n64, 11)
|
|
|
|
+
|
|
|
|
+ s = "3"
|
|
|
|
+ n, err = os2.write(fd, transmute([]u8)s)
|
|
|
|
+ _expect_no_error(t, err)
|
|
|
|
+ expect_value(t, n, 1)
|
|
|
|
+
|
|
|
|
+ // seek to the "o" in "h311o"
|
|
|
|
+ n64, err = os2.seek(fd, -1, .End)
|
|
|
|
+ _expect_no_error(t, err)
|
|
|
|
+ expect_value(t, n64, 14)
|
|
|
|
+
|
|
|
|
+ s = "0"
|
|
|
|
+ n, err = os2.write(fd, transmute([]u8)s)
|
|
|
|
+ _expect_no_error(t, err)
|
|
|
|
+ expect_value(t, n, 1)
|
|
|
|
+
|
|
_expect_no_error(t, os2.sync(fd))
|
|
_expect_no_error(t, os2.sync(fd))
|
|
|
|
+
|
|
|
|
+ // Add executable permissions to current file (as well as read/write to all)
|
|
|
|
+ err = os2.chmod(fd, 0o766)
|
|
|
|
+ _expect_no_error(t, err)
|
|
|
|
+
|
|
|
|
+ when ODIN_OS == .Linux {
|
|
|
|
+ expect(t, unix.sys_access("file.txt", X_OK) == 0, "expected exec permission")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // NOTE: chown not possible without root user
|
|
|
|
+ //_expect_no_error(t, os2.chown(fd, 0, 0))
|
|
_expect_no_error(t, os2.close(fd))
|
|
_expect_no_error(t, os2.close(fd))
|
|
|
|
|
|
- fd, err = os2.open("write.txt")
|
|
|
|
|
|
+
|
|
|
|
+ fd, err = os2.open("file.txt")
|
|
_expect_no_error(t, err)
|
|
_expect_no_error(t, err)
|
|
|
|
|
|
buf: [32]u8
|
|
buf: [32]u8
|
|
-
|
|
|
|
|
|
+
|
|
n, err = os2.read(fd, buf[:])
|
|
n, err = os2.read(fd, buf[:])
|
|
_expect_no_error(t, err)
|
|
_expect_no_error(t, err)
|
|
expect_value(t, n, 15)
|
|
expect_value(t, n, 15)
|
|
- expect_value(t, string(buf[:n]), "abcdefghijhello")
|
|
|
|
|
|
+ expect_value(t, string(buf[:n]), "abcdefghijh3110")
|
|
|
|
|
|
n, err = os2.read_at(fd, buf[0:2], 1)
|
|
n, err = os2.read_at(fd, buf[0:2], 1)
|
|
_expect_no_error(t, err)
|
|
_expect_no_error(t, err)
|
|
expect_value(t, n, 2)
|
|
expect_value(t, n, 2)
|
|
expect_value(t, string(buf[0:2]), "bc")
|
|
expect_value(t, string(buf[0:2]), "bc")
|
|
|
|
|
|
|
|
+ n64, err = os2.file_size(fd)
|
|
|
|
+ _expect_no_error(t, err)
|
|
|
|
+ expect_value(t, n64, 15)
|
|
|
|
+
|
|
_expect_no_error(t, os2.close(fd))
|
|
_expect_no_error(t, os2.close(fd))
|
|
|
|
+
|
|
|
|
+ _expect_no_error(t, os2.remove("file.txt"))
|
|
|
|
+ _expect_no_error(t, os2.mkdir("empty dir", 0))
|
|
|
|
+ _expect_no_error(t, os2.remove("empty dir"))
|
|
}
|
|
}
|
|
|
|
|
|
@test
|
|
@test
|
|
@@ -131,7 +181,7 @@ path_test :: proc(t: ^testing.T) {
|
|
err = os2.remove_all("a")
|
|
err = os2.remove_all("a")
|
|
_expect_no_error(t, err)
|
|
_expect_no_error(t, err)
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
err = os2.mkdir_all("a/b/c/d", 0)
|
|
err = os2.mkdir_all("a/b/c/d", 0)
|
|
_expect_no_error(t, err)
|
|
_expect_no_error(t, err)
|
|
|
|
|
|
@@ -141,23 +191,25 @@ path_test :: proc(t: ^testing.T) {
|
|
fd, err = os2.create("a/b/c/file.txt", 0o644)
|
|
fd, err = os2.create("a/b/c/file.txt", 0o644)
|
|
_expect_no_error(t, err)
|
|
_expect_no_error(t, err)
|
|
|
|
|
|
- err = os2.close(fd)
|
|
|
|
- _expect_no_error(t, err)
|
|
|
|
|
|
|
|
when ODIN_OS == .Linux {
|
|
when ODIN_OS == .Linux {
|
|
expect(t, unix.sys_access("a/b/c/file.txt", X_OK) < 0, "unexpected exec permission")
|
|
expect(t, unix.sys_access("a/b/c/file.txt", X_OK) < 0, "unexpected exec permission")
|
|
|
|
+ } else {
|
|
|
|
+ expect(t, os2.exists("a/b/c/file.txt"), "file does not exist")
|
|
}
|
|
}
|
|
|
|
|
|
err = os2.rename("a/b/c/file.txt", "a/b/file.txt")
|
|
err = os2.rename("a/b/c/file.txt", "a/b/file.txt")
|
|
_expect_no_error(t, err)
|
|
_expect_no_error(t, err)
|
|
|
|
|
|
when ODIN_OS == .Linux {
|
|
when ODIN_OS == .Linux {
|
|
- expect(t, unix.sys_access("a/b/c/file.txt", F_OK) < 0, "unexpected exec permission")
|
|
|
|
|
|
+ expect(t, unix.sys_access("a/b/c/file.txt", F_OK) < 0, "unexpected file existence")
|
|
|
|
+ } else {
|
|
|
|
+ expect(t, !os2.exists("a/b/c/file.txt"), "unexpected file existence")
|
|
}
|
|
}
|
|
|
|
|
|
err = os2.symlink("b/c/d", "a/symlink_to_d")
|
|
err = os2.symlink("b/c/d", "a/symlink_to_d")
|
|
_expect_no_error(t, err)
|
|
_expect_no_error(t, err)
|
|
-
|
|
|
|
|
|
+
|
|
symlink: string
|
|
symlink: string
|
|
symlink, err = os2.read_link("a/symlink_to_d")
|
|
symlink, err = os2.read_link("a/symlink_to_d")
|
|
_expect_no_error(t, err)
|
|
_expect_no_error(t, err)
|
|
@@ -171,8 +223,10 @@ path_test :: proc(t: ^testing.T) {
|
|
|
|
|
|
when ODIN_OS == .Linux {
|
|
when ODIN_OS == .Linux {
|
|
expect_value(t, unix.sys_access("a/b/c/d/shnt.txt", X_OK | R_OK | W_OK), 0)
|
|
expect_value(t, unix.sys_access("a/b/c/d/shnt.txt", X_OK | R_OK | W_OK), 0)
|
|
|
|
+ } else {
|
|
|
|
+ expect(t, os2.exists("a/b/c/d/shnt.txt"), "file does not exist")
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
err = os2.remove_all("a")
|
|
err = os2.remove_all("a")
|
|
_expect_no_error(t, err)
|
|
_expect_no_error(t, err)
|
|
|
|
|