v / cmd / tools / vcreate / vcreate_new_test.v
92 lines · 85 sloc · 2.84 KB · eeaaff218bbe695c655a6380963e2f82deadcb94
Raw
1// vtest retry: 3
2import os
3import v.vmod
4
5const vroot = @VEXEROOT
6// Expect has to be installed for the test.
7const expect_exe = os.quoted_path(os.find_abs_path_of_executable('expect') or {
8 eprintln('skipping test, since expect is missing')
9 exit(0)
10})
11// Directory that contains the Expect scripts used in the test.
12const expect_tests_path = os.join_path(@VEXEROOT, 'cmd', 'tools', 'vcreate', 'tests')
13// Running tests appends a tsession path to VTMP, which is automatically cleaned up after the test.
14// The following will result in e.g. `$VTMP/tsession_7fe8e93bd740_1612958707536/test_vcreate_input/`.
15const test_module_path = os.join_path(os.vtmp_dir(), 'test_vcreate_input')
16
17fn testsuite_begin() {
18 dump(expect_exe)
19 dump(test_module_path)
20 dump(expect_tests_path)
21}
22
23fn testsuite_end() {
24 os.rmdir_all(test_module_path) or {}
25}
26
27fn prepare_test_path() ! {
28 os.rmdir_all(test_module_path) or {}
29 os.mkdir_all(test_module_path) or {}
30 os.chdir(test_module_path)!
31}
32
33fn test_new_with_no_arg_input() {
34 prepare_test_path()!
35 project_name := 'my_project'
36 cmd := '${expect_exe} ${os.join_path(expect_tests_path, 'new_with_no_arg.expect')} ${vroot} ${project_name}'
37 os.execute_opt(cmd) or {
38 dump(cmd)
39 assert false, err.msg()
40 }
41 // Assert mod data set in `new_no_arg.expect`.
42 mod := vmod.from_file(os.join_path(test_module_path, project_name, 'v.mod')) or {
43 assert false, err.str()
44 return
45 }
46 assert mod.name == project_name
47 assert mod.description == 'My Awesome V Project.'
48 assert mod.version == '0.1.0'
49 assert mod.license == 'GPL'
50}
51
52fn test_new_with_name_arg_input() {
53 prepare_test_path()!
54 project_name := 'my_other_project'
55 cmd := '${expect_exe} ${os.join_path(expect_tests_path, 'new_with_name_arg.expect')} ${vroot} ${project_name}'
56 os.execute_opt(cmd) or {
57 dump(cmd)
58 assert false, err.msg()
59 }
60 // Assert mod data set in `new_with_name_arg.expect`.
61 mod := vmod.from_file(os.join_path(test_module_path, project_name, 'v.mod')) or {
62 assert false, err.str()
63 return
64 }
65 assert mod.name == project_name
66 assert mod.description == ''
67 assert mod.version == '0.0.0'
68 assert mod.license == 'MIT'
69}
70
71fn test_new_with_model_arg_input() {
72 prepare_test_path()!
73 project_name := 'my_lib'
74 model := '--lib'
75 cmd := '${expect_exe} ${os.join_path(expect_tests_path, 'new_with_model_arg.expect')} ${vroot} ${model} ${project_name}'
76 os.execute_opt(cmd) or {
77 dump(cmd)
78 assert false, err.msg()
79 }
80 project_path := os.join_path(test_module_path, project_name)
81 // Assert mod data set in `new_with_model_arg.expect`.
82 mod := vmod.from_file(os.join_path(project_path, 'v.mod')) or {
83 assert false, err.str()
84 return
85 }
86 assert mod.name == project_name
87 assert mod.description == 'My Awesome V Project.'
88 assert mod.version == '0.0.1'
89 assert mod.license == 'MIT'
90 // Assert existence of a model-specific file.
91 assert os.exists(os.join_path(project_path, 'tests', 'square_test.v'))
92}
93