-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicktest.nosh
executable file
·387 lines (316 loc) · 12 KB
/
quicktest.nosh
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env nosh
# [RUN] ./cycle.sh
def remove_file_assert(*paths):
for path in paths:
remove(path, force=False)
assert(exists(path, dir=False, file=True), xfail=True)
# determine realpath to our tmpdir for cross-plat consistency
git_worktree_toplevel = pwd()
tmpdir_og = "/tmp/nosh-quicktest"
remove(tmpdir_og, force=True)
mkdir(tmpdir_og)
cd(tmpdir_og)
tmpdir = pwd()
remove(tmpdir)
# get (possibly surrounded by single-quotes) path to script
res = run(os.executable(), capture=["stderr->devnull"], check=False)
postfix = str(res)[-4:]
assert(postfix, " (1)")
exec_path = str(res)[:-4]
assert(not exec_path.endswith(" "))
assert(not exec_path.endswith("(1)"))
# TEST print
res = run(os.executable(), "-c", r'print("otto", ["neurath"], stderr=False)', capture=["stderr", "stdout"])
assert(res.stderr, "")
assert(res.stdout, "otto [\"neurath\"]\n")
res = run(os.executable(), "-c", r'print("otto-neurath", stderr=True)', capture=["stderr", "stdout"])
assert(res.stderr, "otto-neurath\n")
assert(res.stdout, "")
# TEST printf
res = run(os.executable(), "-c", r"""
some_list = [1, "toot", {"otto": "neurath"}]
printf(r"%s\n%d\n%s", "asd", 2, some_list)
res = run(os.executable(), "-v", capture=["stdout"])
printf(r"%s", res, stderr=True)
""", capture=["stderr", "stdout"])
assert(res.stderr, exec_path+" -v (0, <stdout>)")
assert(res.stdout, "asd\\n2\\n[1, \"toot\", {\"otto\": \"neurath\"}]")
# TEST assert
assert(7+1, 8)
assert(1, 2, xfail=True)
assert("neurath")
assert(not "")
# test msg-arg
res = run(os.executable(), "-c", 'assert(False, msg="Neurath")', capture=["stderr", "stdout"], check=False)
assert(res.stderr, "Traceback (most recent call last):\n -c:1:7: in <toplevel>\nError in assert: Neurath; False != True\n")
assert(res.stdout, "")
# TEST starlark features
## @XXX f"...{foo}..." formatting strings don't work yet
assert("{}".format("qwe"), "qwe")
# TEST fs.find
found_count = 0
for x in fs.find(git_worktree_toplevel):
found_count+=1
assert(found_count >= 62, True)
# TEST os.distro
res = os.distro()
assert(res.os in ["linux", "windows", "darwin", "freebsd"])
assert(len(res.arch) >= 3)
assert(any((os.isLinux, os.isWindows, os.isDarwin, os.isFreebsd)))
# TEST os.run
res = run(os.executable(), "--version", capture=["stderr", "stdout"])
assert(str(res), exec_path+" --version (0, <stdout>)")
assert(res.stdout, "0.0.1")
assert(res.stderr, "")
assert(res.exit_code, 0)
res = run(os.executable(), "--version", capture=["stderr", "stdout->devnull"])
assert(str(res), exec_path+" --version (0)")
assert(res.stdout, "0.0.1", xfail=True)
assert(res.stderr, "")
assert(res.exit_code, 0)
res = run(os.executable(), "-c", 'print(args)', 'nosh_first_arg', 'nosh_second_arg', capture=["stderr", "stdout"])
assert(res.stderr, "")
assert(str(res), exec_path+" -c 'print(args)' nosh_first_arg nosh_second_arg (0, <stdout>)")
assert(res.exit_code, 0)
assert(res.stdout, '["nosh_first_arg", "nosh_second_arg"]\n')
# TEST setenv/getenv/expand
setenv("nosh_test_env", "nosh_test_env_has_content")
assert("nosh_test_env=nosh_test_env_has_content" in getenv())
assert(getenv("nosh_test_env"), "nosh_test_env_has_content")
assert(expand("$nosh_test_env"), "nosh_test_env_has_content")
# TEST os.run & expand
res = run(os.executable(), "-c", 'print(expand("$nosh_test_env"))', env=["nosh_test_env=nosh_test_env_has_content"], capture=["stderr", "stdout"])
assert(res.stdout, "nosh_test_env_has_content\n") # @TODO `print` should not write to stderr but stdout by default
assert(res.stderr, "")
assert(res.exit_code, 0)
# TEST os.exists
assert(exists(os.executable()))
# TEST fs.path_separator / fs.path_list_separator
sep = fs.path_separator
if os.isWindows:
assert(fs.path_separator, "\\")
assert(fs.path_list_separator, ";")
else:
assert(fs.path_separator, "/")
assert(fs.path_list_separator, ":")
# TEST fs.join
assert(fs.join(tmpdir, "otto", "neurath"), tmpdir+sep+"otto"+sep+"neurath")
# TEST fs.parse
test_file = fs.join(tmpdir, "does-not-exist.txt")
assert(exists(test_file, dir=False, file=True), xfail=True)
test_file = fs.parse(test_file)
assert(test_file.basename == "does-not-exist")
assert(test_file.name == "does-not-exist.txt")
assert(test_file.ext == ".txt")
assert(test_file.dir == tmpdir)
# TEST fs.mkdir / fs.cd / fs.pwd
test_dir = fs.join(tmpdir, "testd")
mkdir(test_dir)
assert(exists(test_dir, dir=True))
cd(test_dir)
assert(pwd(), test_dir)
write("file", "meep", force=False, mkdir=False)
assert(read("file"), "meep")
assert(read(fs.join(test_dir, "file")), "meep")
remove_file_assert(test_dir)
# TEST fs.popd / fs.pwd / dir.stack
tmpdir_otto = fs.join(tmpdir, "/otto")
tmpdir_otto_neurath = fs.join(tmpdir, "/otto/neurath")
mkdir(tmpdir_otto , mkdir=False)
mkdir(tmpdir_otto_neurath , mkdir=False)
assert(exists(tmpdir_otto , dir=True))
assert(exists(tmpdir_otto_neurath , dir=True))
cd(tmpdir)
assert(fs.dir_stack, [], xfail=True)
popd(reset=True)
assert(popd() == None)
assert(fs.dir_stack, [])
# we create a directory, cd to it, then remove it, to make sure the next `cd`
# will fail it's `os.Getwd()` call to also cover cases where PWD's don't exist
missing_dir = fs.join(tmpdir, "missing_dir")
mkdir(missing_dir)
cd(missing_dir)
remove(missing_dir)
cd(tmpdir)
# @TODO unify dir_stack handling on cross platforms, there are currently
# distinct platform behaviors that will make writing scripts behaving the same
# relying on dir_stack a bit of nuisance
# os.Getwd() calls don't fail on some platforms
if os.isDarwin:
start_stack = [tmpdir, missing_dir]
elif os.isWindows:
start_stack = [tmpdir, missing_dir, tmpdir]
else:
start_stack = [tmpdir]
# dir_stack only changes when a new path is pushed
assert(fs.dir_stack, start_stack)
cd(tmpdir)
if os.isDarwin:
start_stack += [tmpdir]
assert(fs.dir_stack, start_stack)
cd(tmpdir)
assert(fs.dir_stack, start_stack)
cd(tmpdir_otto)
assert(fs.dir_stack, start_stack)
cd(tmpdir_otto_neurath)
assert(fs.dir_stack, start_stack + [fs.join(tmpdir, "otto")])
assert(popd(), fs.join(tmpdir, "otto"))
assert(fs.dir_stack, start_stack)
assert(popd(), tmpdir)
assert(len(fs.dir_stack), len(start_stack)-1)
remove_file_assert(tmpdir_otto_neurath, tmpdir_otto)
# TEST fs.exists/fs.touch/fs.remove - basic
test_dir = fs.join(tmpdir, "testd") + sep
test_dir_file = fs.join(tmpdir, "testd", "testf")
test_dir_file_2 = fs.join(tmpdir, "testd", "testf2")
mkdir(test_dir)
assert(exists(test_dir, dir=True, file=False))
touch(test_dir_file)
assert(exists(test_dir_file, dir=False, file=True))
touch(test_dir_file_2)
assert(exists(test_dir_file_2, dir=False, file=True))
remove(test_dir_file_2)
assert(exists(test_dir_file_2, dir=False, file=True), xfail=True)
remove(test_dir)
assert(exists(test_dir_file, dir=False, file=True), xfail=True)
assert(exists(test_dir, dir=True, file=False), xfail=True)
# TEST fs.remove - force (ignores missing files)
test_dir_missing = fs.join(tmpdir, "testd", "missing_directory")
remove(test_dir_missing, force=True)
# TEST fs.write/fs.read
remove(tmpdir, force=True)
otto = fs.join(tmpdir, "otto_neurath")
write(path=otto, contents="oh\n")
write(path=otto, contents="woe\n", append=True)
assert(read(otto), "oh\nwoe\n")
otto_subdir = fs.join(tmpdir, "subdir", "otto_neurath")
write(path=otto_subdir, contents="soe\n")
assert(read(otto_subdir), "soe\n")
remove_file_assert(otto, otto_subdir)
# TEST fs.copy
otto_from = fs.join(tmpdir, "otto_neurath_from")
otto_to = fs.join(tmpdir, "otto_neurath_to")
otto_subdir = fs.join(tmpdir, "subdir")
remove(otto_from, otto_to, otto_subdir, force=True)
write(path=otto_from, contents="neurath")
copy(otto_from, otto_to)
assert(read(otto_from), "neurath")
assert(read(otto_to), "neurath")
write(path=otto_from, contents="otto")
copy(otto_from, otto_to, force=True)
assert(read(otto_from), "otto")
assert(read(otto_to), "otto")
otto_subdir_to = fs.join(otto_subdir, "otto_neurath_to")
copy(otto_from, otto_subdir_to)
assert(read(otto_subdir_to), "otto")
remove_file_assert(otto_from, otto_subdir)
# TEST fs.move
otto_from = fs.join(tmpdir, "otto_neurath_from")
otto_to = fs.join(tmpdir, "otto_neurath_to")
otto_subdir = fs.join(tmpdir, "subdir")
remove(otto_from, otto_to, otto_subdir, force=True)
write(path=otto_from, contents="neurath")
mv(otto_from, otto_to)
assert(not exists(otto_from))
assert(read(otto_to), "neurath")
write(path=otto_from, contents="otto")
mv(otto_from, otto_to, force=True)
assert(not exists(otto_from))
assert(read(otto_to), "otto")
write(path=otto_from, contents="ottrath")
otto_subdir_to = fs.join(otto_subdir, "otto_neurath_to")
mv(otto_from, otto_subdir_to, mkdirs=True)
assert(read(otto_subdir_to), "ottrath")
remove_file_assert(otto_to, otto_subdir)
# TEST fs - Windows specifics
if os.isWindows:
assert(cmp_path("/tmp/qwe/asd", "\\tmp\\qwe\\asd"))
assert(cmp_path("/tmp/qwe/asd", "\\tmp/qwe\\asd"))
ottos = [
tmpdir_og+"/otto/neurath",
tmpdir_og+"/otto\\neurath",
tmpdir_og+"\\otto\\neurath",
tmpdir_og+"/otto//neurath",
tmpdir_og+"\\otto\\neurath",
tmpdir_og+"\\neurath\\otto\\",
tmpdir_og+"/neurath\\otto",
]
write(path=ottos[0], contents="neurath")
assert(read(ottos[1]), "neurath")
write(path=ottos[2], contents="otto", force=True)
assert(read(ottos[3]), "otto")
mv(ottos[2], ottos[0], force=True)
assert(read(ottos[1]), "otto")
write(path=ottos[4], contents="notto", force=True)
assert(read(ottos[4]), "notto")
copy(ottos[4], ottos[2], force=True)
assert(read(ottos[2]), "notto")
assert(not exists(ottos[5], dir=True))
assert(not exists(ottos[6], dir=True))
mkdir(ottos[5])
assert(exists(ottos[5], dir=True))
assert(exists(ottos[6], dir=True))
pre_cd = pwd()
cd(ottos[5])
popd()
assert(pwd(), pre_cd)
remove(ottos[6])
assert(not exists(ottos[5], dir=True))
remove(force=True, *ottos)
# TEST os.run - redir/append
otto = fs.join(tmpdir, "otto_neurath")
remove(otto, force=True)
run(os.executable(), "-c", 'print("otto")', capture=["stdout->{}".format(otto)])
run(os.executable(), "-c", 'print("neurath")', capture=["stdout->>{}".format(otto)])
assert(exists(otto, dir=False, file=True))
assert(read(otto), "otto\nneurath\n")
remove_file_assert(otto)
# TEST os.run - stdin
otto = fs.join(tmpdir, "otto_neurath")
write(path=otto, contents="neurath")
res = run(os.executable(), "-c", 'print(read("<stdin>"))', capture=["stderr", "stdout", "stdin<-{}".format(otto)])
assert(res.stdout, "neurath\n")
assert(res.stderr, "")
remove_file_assert(otto)
# TEST fail
res = run(os.executable(), "-c", 'fail("otto")', capture=["stderr", "stdout"], check=False)
assert(res.stderr, "otto\n")
assert(res.stdout, "")
assert(res.exit_code, 1)
res = run(os.executable(), "-c", 'fail("otto", exit_code=13, throw=False)', capture=["stderr", "stdout"], check=False)
assert(res.stderr, "otto\n")
assert(res.stdout, "")
assert(res.exit_code, 13)
res = run(os.executable(), "-c", 'fail("otto", 2, True)', capture=["stderr", "stdout"], check=False)
assert(res.stderr, "Traceback (most recent call last):\n -c:1:5: in <toplevel>\nError in fail: otto\n")
assert(res.stdout, "")
assert(res.exit_code, 2)
# TEST defer
otto = fs.join(tmpdir, "otto_neurath")
res = run(os.executable(), "-c", r"""
defer(lambda: write(path="{}", contents="otto-neurath"))
result = "otto"
def otto():
print("neurath")
def toot():
return "toot"
defer(lambda: print(toot()))
defer(lambda: otto())
defer(lambda: print(result))
""".format(otto), capture=["stderr", "stdout"], check=False)
assert(res.stderr, "")
assert(res.stdout, "otto\nneurath\ntoot\n")
assert(read(otto), "otto-neurath")
remove(otto)
# TEST json
otto = fs.join(tmpdir, "otto_neurath")
otto_rewrite = fs.join(tmpdir, "otto_neurath_rewrite")
remove(otto, otto_rewrite, force=True)
write(path=otto, contents='{"otto": "neurath"}')
raw = read(otto)
obj = json.decode(raw)
write(path=otto_rewrite, contents=json.indent(json.encode(obj)).replace("\n", "").replace("\t", ""))
raw_rewrite = read(otto_rewrite)
assert(raw, raw_rewrite)
remove_file_assert(otto, otto_rewrite)
print("quicktest passed successfully")