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