summaryrefslogtreecommitdiff
path: root/tests/modules/os/test.lua
blob: 2dcab35c696353cc0b6ff0ffcbc9b62fd1c50fd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
function test_cpdir(t)
    -- get mclock
    local tm = os.mclock()
    -- test cpdir
    os.mkdir("test1")
    t:require(os.exists("test1"))
    os.cp("test1","test2")
    t:require(os.exists("test2"))
    os.rmdir("test1")
    t:require_not(os.exists("test1"))
    io.writefile("test2/awd","awd")
    os.rmdir("test2")
    t:require_not(os.exists("test2"))
    -- assert mclock
    t:require(os.mclock() >= tm)
end

function test_rename(t)
    -- get mclock
    local tm = os.mclock()
    -- test rename
    os.mkdir("test1")
    t:require(os.exists("test1"))
    os.mv("test1","test2")
    t:require_not(os.exists("test1"))
    t:require(os.exists("test2"))
    os.rmdir("test2")
    t:require_not(os.exists("test2"))
    -- assert mclock
    t:require(os.mclock() >= tm)
end

function test_cp_mvdir_into_another_dir(t)
    -- get mclock
    local tm = os.mclock()
    -- test cp/mvdir into another dir
    os.mkdir("test1")
    os.mkdir("test2")
    t:require(os.exists("test1"))
    t:require(os.exists("test2"))
    os.cp("test1","test2")
    t:require(os.exists("test2/test1"))
    os.mv("test1","test2/test1")
    t:require_not(os.exists("test1"))
    t:require(os.exists("test2/test1/test1"))
    os.rmdir("test2")
    t:require_not(os.exists("test2"))
    -- assert mclock
    t:require(os.mclock() >= tm)
end

function test_cp_symlink(t)
    if is_host("windows") then
        return
    end
    os.touch("test1")
    os.ln("test1", "test2")
    t:require(os.isfile("test1"))
    t:require(os.isfile("test2"))
    t:require(os.islink("test2"))
    os.cp("test2", "test3")
    t:require(os.isfile("test3"))
    t:require(not os.islink("test3"))
    os.cp("test2", "test4", {symlink = true})
    t:require(os.isfile("test4"))
    t:require(os.islink("test4"))
    os.mkdir("dir")
    os.touch("dir/test1")
    os.cd("dir")
    os.ln("test1", "test2")
    os.cd("-")
    t:require(os.islink("dir/test2"))
    os.cp("dir", "dir2")
    t:require(not os.islink("dir2/test2"))
    os.cp("dir", "dir3", {symlink = true})
    t:require(os.islink("dir3/test2"))
    os.tryrm("test1")
    os.tryrm("test2")
    os.tryrm("test3")
    os.tryrm("test4")
    os.tryrm("dir")
    os.tryrm("dir2")
    os.tryrm("dir3")
    t:require(not os.exists("test1"))
    t:require(not os.exists("test2"))
    t:require(not os.exists("dir"))
end

function test_setenv(t)
    -- get mclock
    local tm = os.mclock()
    -- test setenv
    os.setenv("__AWD","DWA")
    t:are_equal(os.getenv("__AWD"), "DWA")
    os.setenv("__AWD","DWA2")
    t:are_equal(os.getenv("__AWD"), "DWA2")
    -- assert mclock
    t:require(os.mclock() >= tm)
end

function test_argv(t)
    t:are_equal(os.argv(""), {})
    -- $cli aa bb cc
    t:are_equal(os.argv("aa bb cc"), {"aa", "bb", "cc"})
    -- $cli aa --bb=bbb -c
    t:are_equal(os.argv("aa --bb=bbb -c"), {"aa", "--bb=bbb", "-c"})
    -- $cli "aa bb cc" dd
    t:are_equal(os.argv('"aa bb cc" dd'), {"aa bb cc", "dd"})
    -- $cli aa(bb)cc dd
    t:are_equal(os.argv('aa(bb)cc dd'), {"aa(bb)cc", "dd"})
    -- $cli aa\\bb/cc dd
    t:are_equal(os.argv('aa\\bb/cc dd'), {"aa\\bb/cc", "dd"})
    -- $cli "aa\\bb/cc dd" ee
    t:are_equal(os.argv('"aa\\\\bb/cc dd" ee'), {"aa\\bb/cc dd", "ee"})
    -- $cli "aa\\bb/cc (dd)" ee
    t:are_equal(os.argv('"aa\\\\bb/cc (dd)" ee'), {"aa\\bb/cc (dd)", "ee"})
    -- $cli -DTEST=\"hello\"
    t:are_equal(os.argv('-DTEST=\\"hello\\"'), {'-DTEST="hello"'})
    -- $cli -DTEST=\"hello\" -DTEST=\"hello\"
    t:are_equal(os.argv('-DTEST=\\"hello\\" -DTEST2=\\"hello\\"'), {'-DTEST="hello"', '-DTEST2="hello"'})
    -- $cli -DTEST="hello"
    t:are_equal(os.argv('-DTEST="hello"'), {'-DTEST=hello'})
    -- $cli -DTEST="hello world"
    t:are_equal(os.argv('-DTEST="hello world"'), {'-DTEST=hello world'})
    -- $cli -DTEST=\"hello world\"
    t:are_equal(os.argv('-DTEST=\\"hello world\\"'), {'-DTEST="hello', 'world\"'})
    -- $cli "-DTEST=\"hello world\"" "-DTEST2="\hello world2\""
    t:are_equal(os.argv('"-DTEST=\\\"hello world\\\"" "-DTEST2=\\\"hello world2\\\""'), {'-DTEST="hello world"', '-DTEST2="hello world2"'})
    -- $cli '-DTEST="hello world"' '-DTEST2="hello world2"'
    t:are_equal(os.argv("'-DTEST=\"hello world\"' '-DTEST2=\"hello world2\"'"), {'-DTEST="hello world"', '-DTEST2="hello world2"'})
    -- only split
    t:are_equal(os.argv('-DTEST="hello world"', {splitonly = true}), {'-DTEST="hello world"'})
    t:are_equal(os.argv('-DTEST="hello world" -DTEST2="hello world2"', {splitonly = true}), {'-DTEST="hello world"', '-DTEST2="hello world2"'})
end

function test_args(t)
    t:are_equal(os.args({}), "")
    t:are_equal(os.args({"aa", "bb", "cc"}), "aa bb cc")
    t:are_equal(os.args({"aa", "--bb=bbb", "-c"}), "aa --bb=bbb -c")
    t:are_equal(os.args({"aa bb cc", "dd"}), '"aa bb cc" dd')
    t:are_equal(os.args({"aa(bb)cc", "dd"}), 'aa(bb)cc dd')
    t:are_equal(os.args({"aa\\bb/cc", "dd"}), "aa\\bb/cc dd")
    t:are_equal(os.args({"aa\\bb/cc dd", "ee"}), '"aa\\\\bb/cc dd" ee')
    t:are_equal(os.args({"aa\\bb/cc (dd)", "ee"}), '"aa\\\\bb/cc (dd)" ee')
    t:are_equal(os.args({"aa\\bb/cc", "dd"}, {escape = true}), "aa\\\\bb/cc dd")
    t:are_equal(os.args('-DTEST="hello"'), '-DTEST=\\"hello\\"')
    t:are_equal(os.args({'-DTEST="hello"', '-DTEST2="hello"'}), '-DTEST=\\"hello\\" -DTEST2=\\"hello\\"')
    t:are_equal(os.args('-DTEST=hello'), '-DTEST=hello') -- irreversible
    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})

    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})
end

function test_isexec(t)
    local tempdir = "temp/isexec"
    os.tryrm(tempdir)
    os.mkdir(tempdir)

    local programfile = os.programfile()
    if programfile then
        t:require(os.isexec(programfile))
    end

    local filepath = path.join(tempdir, "script")
    io.writefile(filepath, "echo test\n")

    if is_host("windows") then
        local batfile = path.join(tempdir, "a.bat")
        io.writefile(batfile, "echo test\r\n")
        t:require(os.isexec(batfile))

        local comfile = path.join(tempdir, "a.com")
        io.writefile(comfile, "12345678")
        t:require(os.isexec(comfile))

        local suffix = path.join(tempdir, "prog")
        io.writefile(suffix .. ".exe", "")
        t:require(os.isexec(suffix))

        local suffix2 = path.join(tempdir, "prog2")
        io.writefile(suffix2 .. ".com", "")
        t:require(os.isexec(suffix2))
    else
        os.vrunv("chmod", {"-x", filepath})
        t:require_not(os.isexec(filepath))
        os.vrunv("chmod", {"+x", filepath})
        t:require(os.isexec(filepath))
    end

    os.tryrm(tempdir)
end