v / cmd / tools
Raw file | 163 loc (138 sloc) | 2.12 KB | Latest commit hash f427a5241
1import os
2
3const vexe = @VEXE
4
5const tfolder = os.join_path(os.vtmp_dir(), 'v', 'vbump')
6
7fn testsuite_begin() {
8 os.mkdir_all(tfolder) or {}
9}
10
11fn testsuite_end() {
12 os.rmdir_all(tfolder) or {}
13}
14
15struct BumpTestCase {
16 file_name string
17 contents string
18 line int
19 expected_patch string
20 expected_minor string
21 expected_major string
22}
23
24const test_cases = [
25 BumpTestCase{
26 file_name: 'v.mod'
27 contents: "Module {
28 name: 'Sample'
29 description: 'Sample project'
30 version: '1.2.6'
31 license: 'MIT'
32 dependencies: []
33}
34
35"
36 line: 3
37 expected_patch: " version: '1.2.7'"
38 expected_minor: " version: '1.3.0'"
39 expected_major: " version: '2.0.0'"
40 },
41 BumpTestCase{
42 file_name: 'random_versions.vv'
43 contents: "
441.1.2
451.2.5
463.21.73
47version = '1.5.1'
48
49"
50 line: 4
51 expected_patch: "version = '1.5.2'"
52 expected_minor: "version = '1.6.0'"
53 expected_major: "version = '2.0.0'"
54 },
55 BumpTestCase{
56 file_name: 'sample_tool.v'
57 contents: "// Module comment and copyright information
58import os
59import flag
60
61const (
62 tool_name = os.file_name(os.executable())
63 tool_version = '0.1.33'
64)
65fn main() {
66 // stuff
67}
68 "
69 line: 6
70 expected_patch: " tool_version = '0.1.34'"
71 expected_minor: " tool_version = '0.2.0'"
72 expected_major: " tool_version = '1.0.0'"
73 },
74]
75
76fn run_individual_test(case BumpTestCase) ! {
77 test_file := os.join_path_single(tfolder, case.file_name)
78
79 os.rm(test_file) or {}
80 os.write_file(test_file, case.contents)!
81
82 os.execute_or_exit('${os.quoted_path(vexe)} bump --patch ${os.quoted_path(test_file)}')
83 patch_lines := os.read_lines(test_file)!
84 assert patch_lines[case.line] == case.expected_patch
85
86 os.execute_or_exit('${os.quoted_path(vexe)} bump --minor ${os.quoted_path(test_file)}')
87 minor_lines := os.read_lines(test_file)!
88 assert minor_lines[case.line] == case.expected_minor
89
90 os.execute_or_exit('${os.quoted_path(vexe)} bump --major ${os.quoted_path(test_file)}')
91 major_lines := os.read_lines(test_file)!
92 assert major_lines[case.line] == case.expected_major
93
94 os.rm(test_file)!
95}
96
97fn test_all_bump_cases() {
98 for case in test_cases {
99 run_individual_test(case) or { panic(err) }
100 }
101}
102
103struct SkipTestCase {
104 file_name string
105 contents string
106 skip string
107 line int
108 expected_patch string
109 expected_minor string
110 expected_major string
111}
112
113const skip_test_cases = [
114 SkipTestCase{
115 file_name: 'CITATION.cff'
116 contents: 'abstract: A sample CLI tool made in V that prints geometric shapes to the screen.
117authors:
118 - alias: hungrybluedev
119 family-names: Haldar
120 given-names: Subhomoy
121cff-version: 1.2.0
122date-released: 2023-04-20
123license: MIT
124message: Please cite this software using these information.
125repository-code: https://github.com/hungrybluedev/geo
126title: geo
127url: https://github.com/hungrybluedev/geo
128version: 0.2.4
129'
130 line: 12
131 skip: 'cff-version'
132 expected_patch: 'version: 0.2.5'
133 expected_minor: 'version: 0.3.0'
134 expected_major: 'version: 1.0.0'
135 },
136]
137
138fn run_skip_test(case SkipTestCase) ! {
139 test_file := os.join_path_single(tfolder, case.file_name)
140
141 os.rm(test_file) or {}
142 os.write_file(test_file, case.contents)!
143
144 os.execute_or_exit('${os.quoted_path(vexe)} bump --patch --skip="${case.skip}" ${os.quoted_path(test_file)}')
145 patch_lines := os.read_lines(test_file)!
146 assert patch_lines[case.line] == case.expected_patch
147
148 os.execute_or_exit('${os.quoted_path(vexe)} bump --minor --skip="${case.skip}" ${os.quoted_path(test_file)}')
149 minor_lines := os.read_lines(test_file)!
150 assert minor_lines[case.line] == case.expected_minor
151
152 os.execute_or_exit('${os.quoted_path(vexe)} bump --major --skip="${case.skip}" ${os.quoted_path(test_file)}')
153 major_lines := os.read_lines(test_file)!
154 assert major_lines[case.line] == case.expected_major
155
156 os.rm(test_file)!
157}
158
159fn test_all_skip_bump_cases() ! {
160 for case in skip_test_cases {
161 run_skip_test(case) or { panic(err) }
162 }
163}