v / cmd / tools / vfmt_test.v
50 lines · 39 sloc · 1.44 KB · f7c88ac2aeb3f19114e4526b9a671b0322cdf770
Raw
1import os
2
3const vexe = @VEXE
4const vfmt_test_tdir = os.join_path(os.vtmp_dir(), 'vfmt_test_25005')
5
6fn testsuite_begin() {
7 os.rmdir_all(vfmt_test_tdir) or {}
8 os.mkdir_all(vfmt_test_tdir)!
9}
10
11fn testsuite_end() {
12 os.rmdir_all(vfmt_test_tdir) or {}
13}
14
15fn test_fmt_keeps_invalid_assert_source_unchanged() {
16 source_path := os.join_path(vfmt_test_tdir, 'invalid_assert_message.v')
17 original := "fn main() {\n\tassert false 'bye'\n}\n"
18 os.write_file(source_path, original)!
19
20 res := os.execute('${os.quoted_path(vexe)} fmt -w ${os.quoted_path(source_path)}')
21
22 assert res.exit_code != 0, res.output
23 assert res.output.contains('unexpected string `bye`, expecting `,`'), res.output
24 assert os.read_file(source_path)! == original
25}
26
27fn test_fmt_preferences_respect_vflags() {
28 source_path := os.join_path(vfmt_test_tdir, 'vflags_backend_js.v')
29 os.write_file(source_path, "fn main() {\n\tx := 'abc'.str\n}\n")!
30
31 old_vflags := os.getenv('VFLAGS')
32 defer {
33 if old_vflags == '' {
34 os.unsetenv('VFLAGS')
35 } else {
36 os.setenv('VFLAGS', old_vflags, true)
37 }
38 }
39
40 os.unsetenv('VFLAGS')
41 warmup_res := os.execute('${os.quoted_path(vexe)} fmt -help')
42 assert warmup_res.exit_code == 0, warmup_res.output
43
44 os.setenv('VFLAGS', '-b js', true)
45 res := os.execute('${os.quoted_path(vexe)} fmt ${os.quoted_path(source_path)}')
46
47 assert res.exit_code == 0, res.output
48 assert res.output.contains("x := 'abc'.str"), res.output
49 assert !res.output.contains("x := c'abc'"), res.output
50}
51