| 1 | // vtest retry: 3 |
| 2 | import os |
| 3 | import v.vmod |
| 4 | |
| 5 | const vroot = os.quoted_path(@VEXEROOT) |
| 6 | const vexe = os.quoted_path(@VEXE) |
| 7 | // Expect has to be installed for the test. |
| 8 | const expect_exe = os.quoted_path(os.find_abs_path_of_executable('expect') or { |
| 9 | eprintln('skipping test, since expect is missing') |
| 10 | exit(0) |
| 11 | }) |
| 12 | // Directory that contains the Expect scripts used in the test. |
| 13 | const expect_tests_path = os.join_path(@VEXEROOT, 'cmd', 'tools', 'vcreate', 'tests') |
| 14 | const test_project_dir_name = 'test_project' |
| 15 | // Running tests appends a tsession path to VTMP, which is automatically cleaned up after the test. |
| 16 | // The following will result in e.g. `$VTMP/tsession_7fe8e93bd740_1612958707536/test_project/`. |
| 17 | const test_path = os.join_path(os.vtmp_dir(), test_project_dir_name) |
| 18 | |
| 19 | fn testsuite_end() { |
| 20 | os.rmdir_all(test_path) or {} |
| 21 | } |
| 22 | |
| 23 | fn init_and_check() ! { |
| 24 | os.chdir(test_path)! |
| 25 | |
| 26 | // Keep track of the last modified time of the main file to ensure it is not modified if it already exists. |
| 27 | main_exists := os.exists('main.v') |
| 28 | main_last_modified := if main_exists { os.file_last_mod_unix('main.v') } else { 0 } |
| 29 | |
| 30 | // Initialize project. |
| 31 | os.execute_or_exit('${expect_exe} ${os.join_path(expect_tests_path, 'init.expect')} ${vroot}') |
| 32 | |
| 33 | x := os.execute_or_exit('${vexe} run .') |
| 34 | assert x.output.trim_space() == 'Hello World!' |
| 35 | |
| 36 | if main_exists { |
| 37 | assert main_last_modified == os.file_last_mod_unix('main.v') |
| 38 | } else { |
| 39 | assert os.read_file('main.v')! == [ |
| 40 | 'module main\n', |
| 41 | 'fn main() {', |
| 42 | " println('Hello World!')", |
| 43 | '}', |
| 44 | '', |
| 45 | ].join_lines() |
| 46 | } |
| 47 | |
| 48 | assert os.read_file('v.mod')! == [ |
| 49 | 'Module {', |
| 50 | " name: '${test_project_dir_name}'", |
| 51 | " description: ''", |
| 52 | " version: '0.0.0'", |
| 53 | " license: 'MIT'", |
| 54 | ' dependencies: []', |
| 55 | '}', |
| 56 | '', |
| 57 | ].join_lines() |
| 58 | |
| 59 | assert os.read_file('.gitignore')! == [ |
| 60 | '# Binaries for programs and plugins', |
| 61 | 'main', |
| 62 | '${test_project_dir_name}', |
| 63 | '*.exe', |
| 64 | '*.exe~', |
| 65 | '*.so', |
| 66 | '*.dylib', |
| 67 | '*.dll', |
| 68 | '', |
| 69 | '# Ignore binary output folders', |
| 70 | 'bin/', |
| 71 | '', |
| 72 | '# Ignore common editor/system specific metadata', |
| 73 | '.DS_Store', |
| 74 | '.idea/', |
| 75 | '.vscode/', |
| 76 | '*.iml', |
| 77 | '', |
| 78 | '# ENV', |
| 79 | '.env', |
| 80 | '', |
| 81 | '# Web assets and local databases', |
| 82 | '*.db', |
| 83 | '*.js', |
| 84 | '', |
| 85 | '# Ignore installed modules through `v install --local`:', |
| 86 | 'modules/', |
| 87 | '', |
| 88 | ].join_lines() |
| 89 | |
| 90 | assert os.read_file('.gitattributes')! == [ |
| 91 | '* text=auto eol=lf', |
| 92 | '*.bat eol=crlf', |
| 93 | '', |
| 94 | '*.v linguist-language=V', |
| 95 | '*.vv linguist-language=V', |
| 96 | '*.vsh linguist-language=V', |
| 97 | 'v.mod linguist-language=V', |
| 98 | '.vdocignore linguist-language=ignore', |
| 99 | '', |
| 100 | ].join_lines() |
| 101 | |
| 102 | assert os.read_file('.editorconfig')! == [ |
| 103 | '[*]', |
| 104 | 'charset = utf-8', |
| 105 | 'end_of_line = lf', |
| 106 | 'insert_final_newline = true', |
| 107 | 'trim_trailing_whitespace = true', |
| 108 | '', |
| 109 | '[*.v]', |
| 110 | 'indent_style = tab', |
| 111 | '', |
| 112 | ].join_lines() |
| 113 | } |
| 114 | |
| 115 | fn prepare_test_path() ! { |
| 116 | os.rmdir_all(test_path) or {} |
| 117 | os.mkdir_all(test_path) or {} |
| 118 | os.chdir(test_path)! |
| 119 | } |
| 120 | |
| 121 | fn test_v_init() { |
| 122 | prepare_test_path()! |
| 123 | init_and_check()! |
| 124 | } |
| 125 | |
| 126 | fn test_v_init_in_git_dir() { |
| 127 | prepare_test_path()! |
| 128 | os.execute_or_exit('git init .') |
| 129 | init_and_check()! |
| 130 | } |
| 131 | |
| 132 | fn test_v_init_no_overwrite_gitignore() { |
| 133 | prepare_test_path()! |
| 134 | os.write_file('.gitignore', 'foo')! |
| 135 | os.execute_or_exit('${expect_exe} ${os.join_path(expect_tests_path, 'init.expect')} ${vroot}') |
| 136 | assert os.read_file('.gitignore')! == 'foo' |
| 137 | } |
| 138 | |
| 139 | fn test_v_init_no_overwrite_gitattributes_and_editorconfig() { |
| 140 | git_attributes_content := '*.v linguist-language=V text=auto eol=lf' |
| 141 | editor_config_content := '[*] |
| 142 | charset = utf-8 |
| 143 | end_of_line = lf |
| 144 | insert_final_newline = true |
| 145 | trim_trailing_whitespace = true |
| 146 | |
| 147 | [*.v] |
| 148 | indent_style = tab |
| 149 | ' |
| 150 | prepare_test_path()! |
| 151 | os.write_file('.gitattributes', git_attributes_content)! |
| 152 | os.write_file('.editorconfig', editor_config_content)! |
| 153 | res := |
| 154 | os.execute_or_exit('${expect_exe} ${os.join_path(expect_tests_path, 'init.expect')} ${vroot}') |
| 155 | assert res.output.contains('Created binary (application) project `${test_project_dir_name}`') |
| 156 | assert os.read_file('.gitattributes')! == git_attributes_content |
| 157 | assert os.read_file('.editorconfig')! == editor_config_content |
| 158 | } |
| 159 | |
| 160 | fn test_v_init_in_dir_with_invalid_mod_name_input() { |
| 161 | // A project with a directory name with hyphens, which is invalid for a module name. |
| 162 | dir_name_with_invalid_mod_name := 'my-proj' |
| 163 | corrected_mod_name := 'my_proj' |
| 164 | proj_path := os.join_path(os.vtmp_dir(), dir_name_with_invalid_mod_name) |
| 165 | os.mkdir_all(proj_path) or {} |
| 166 | os.chdir(proj_path)! |
| 167 | os.execute_or_exit('${expect_exe} ${os.join_path(expect_tests_path, |
| 168 | 'init_in_dir_with_invalid_mod_name.expect')} ${vroot} ${dir_name_with_invalid_mod_name} ${corrected_mod_name}') |
| 169 | // Assert mod data set in `new_with_model_arg.expect`. |
| 170 | mod := vmod.from_file(os.join_path(proj_path, 'v.mod')) or { |
| 171 | assert false, err.str() |
| 172 | return |
| 173 | } |
| 174 | assert mod.name == corrected_mod_name |
| 175 | } |
| 176 | |
| 177 | fn test_v_init_with_model_arg_input() { |
| 178 | prepare_test_path()! |
| 179 | model := '--lib' |
| 180 | res := os.execute_or_exit('${expect_exe} ${os.join_path(expect_tests_path, |
| 181 | 'init_with_model_arg.expect')} ${vroot} ${model}') |
| 182 | assert res.output.contains('Created library project `${test_project_dir_name}`'), res.output |
| 183 | project_path := os.join_path(test_path) |
| 184 | mod := vmod.from_file(os.join_path(project_path, 'v.mod')) or { |
| 185 | assert false, err.str() |
| 186 | return |
| 187 | } |
| 188 | assert mod.name == test_project_dir_name |
| 189 | assert mod.description == 'My Awesome V Application.' |
| 190 | assert mod.version == '0.0.1' |
| 191 | assert mod.license == 'MIT' |
| 192 | // Assert existence of a model-specific file. |
| 193 | assert os.exists(os.join_path(project_path, 'tests', 'square_test.v')) |
| 194 | } |
| 195 | |