summaryrefslogtreecommitdiff
path: root/tests/modules
diff options
context:
space:
mode:
authorruki <[email protected]>2025-11-09 08:28:01 +0800
committerGitHub <[email protected]>2025-11-09 08:28:01 +0800
commita1b0ec856e2c22aac84022feb05b6687912e3d6e (patch)
treea4679af3be46069a480be76bdc322eeeefcc4049 /tests/modules
parent0b91f564b00bf22d611a70c17088ba3a155b8a84 (diff)
parent91ef5fa9376814db12d35a020008a0d692883945 (diff)
Merge pull request #6989 from xmake-io/async
Add async support in os APIs
Diffstat (limited to 'tests/modules')
-rw-r--r--tests/modules/os/async_copy.lua34
-rw-r--r--tests/modules/os/async_scheduler.lua32
-rw-r--r--tests/modules/os/test.lua22
3 files changed, 88 insertions, 0 deletions
diff --git a/tests/modules/os/async_copy.lua b/tests/modules/os/async_copy.lua
new file mode 100644
index 000000000..751a940ae
--- /dev/null
+++ b/tests/modules/os/async_copy.lua
@@ -0,0 +1,34 @@
+local function _prepare_source(dir)
+ os.mkdir(dir)
+ io.writefile(path.join(dir, "foo.txt"), "foo")
+ io.writefile(path.join(dir, "bar.txt"), "bar")
+end
+
+function main()
+ local root = os.tmpfile() .. ".os_async_copy"
+ local srcdir = path.join(root, "src")
+ local dstdir = path.join(root, "dst")
+
+ _prepare_source(srcdir)
+ print("source prepared: %s", srcdir)
+
+ local files = os.files(path.join(srcdir, "*.txt"), {async = true})
+ assert(files and #files == 2)
+ print("async enumerate: %d files", #files)
+
+ os.cp(srcdir, dstdir, {async = true})
+ assert(os.isdir(dstdir))
+ print("async copy done: %s", dstdir)
+
+ files = os.files(path.join(dstdir, "*.txt"), {async = true})
+ assert(files and #files == 2)
+
+ os.rm(dstdir, {async = true})
+ assert(not os.isdir(dstdir))
+ print("dst removed async")
+
+ os.rm(srcdir, {async = true})
+ os.tryrm(root)
+ print("async copy test finished")
+end
+
diff --git a/tests/modules/os/async_scheduler.lua b/tests/modules/os/async_scheduler.lua
new file mode 100644
index 000000000..0a201f8f7
--- /dev/null
+++ b/tests/modules/os/async_scheduler.lua
@@ -0,0 +1,32 @@
+import("core.base.scheduler")
+
+local function _prepare_workspace(root)
+ os.mkdir(root)
+ for idx = 1, 4 do
+ io.writefile(path.join(root, string.format("file%d.txt", idx)), "xmake")
+ end
+end
+
+function main()
+ local root = os.tmpfile() .. ".os_async_sched"
+ _prepare_workspace(root)
+ print("workspace prepared: %s", root)
+
+ local group = "os_async_scheduler"
+ scheduler.co_group_begin(group, function ()
+ for idx = 1, 4 do
+ scheduler.co_start(function ()
+ local matches = os.files(path.join(root, string.format("file%d.txt", idx)), {async = true})
+ assert(matches and #matches == 1)
+ print("async match %d finished", idx)
+ end)
+ end
+
+ scheduler.co_start(function ()
+ print("async find all files in programdir...")
+ local files = os.files(path.join(os.programdir(), "**"), {async = true})
+ print("files: %d", #files)
+ print("async find all finished")
+ end)
+ end)
+end
diff --git a/tests/modules/os/test.lua b/tests/modules/os/test.lua
index 92890a234..1207cb615 100644
--- a/tests/modules/os/test.lua
+++ b/tests/modules/os/test.lua
@@ -149,3 +149,25 @@ function test_args(t)
t:are_equal(os.args({'-DTEST="hello world"', '-DTEST2="hello world2"'}), '"-DTEST=\\\"hello world\\\"" "-DTEST2=\\\"hello world2\\\""')
end
+function test_async(t)
+ local tmpdir = os.tmpfile() .. ".dir"
+ local tmpdir2 = os.tmpfile() .. ".dir"
+ io.writefile(path.join(tmpdir, "foo.txt"), "foo")
+ io.writefile(path.join(tmpdir, "bar.txt"), "bar")
+ local files = os.files(path.join(tmpdir, "*.txt"), {async = true})
+ t:require(files and #files == 2)
+
+ os.cp(tmpdir, tmpdir2, {async = true, detach = true})
+ t:require(not os.isdir(tmpdir2))
+
+ os.cp(tmpdir, tmpdir2, {async = true})
+ t:require(os.isdir(tmpdir2))
+
+ t:require(os.isdir(tmpdir))
+ os.rm(tmpdir, {async = true})
+ t:require(not os.isdir(tmpdir))
+
+ t:require(os.isdir(tmpdir2))
+ os.rm(tmpdir2, {async = true, detach = true})
+ t:require(os.isdir(tmpdir2))
+end