From 269462425a33432e2a3260a6d4a5f1469042f326 Mon Sep 17 00:00:00 2001 From: Sanath Kumar U Date: Mon, 20 Feb 2023 16:24:38 +0530 Subject: [PATCH] tools: prevent `v init` from overwriting an already existing `src/main.v` file (fix #17362) (#17363) --- cmd/tools/vcreate/vcreate.v | 9 +++++---- cmd/tools/vcreate/vcreate_test.v | 25 ++++++++++++++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/cmd/tools/vcreate/vcreate.v b/cmd/tools/vcreate/vcreate.v index d842a72821..bfef149c11 100644 --- a/cmd/tools/vcreate/vcreate.v +++ b/cmd/tools/vcreate/vcreate.v @@ -133,10 +133,11 @@ fn init_project() { c.write_vmod(false) println('Change the description of your project in `v.mod`') } - - c.files << ProjectFiles{ - path: 'src/main.v' - content: hello_world_content() + if !os.exists('src/main.v') { + c.files << ProjectFiles{ + path: 'src/main.v' + content: hello_world_content() + } } c.create_files_and_directories() c.write_gitattributes(false) diff --git a/cmd/tools/vcreate/vcreate_test.v b/cmd/tools/vcreate/vcreate_test.v index 3031a133d7..44b111a900 100644 --- a/cmd/tools/vcreate/vcreate_test.v +++ b/cmd/tools/vcreate/vcreate_test.v @@ -7,19 +7,30 @@ const test_path = os.join_path(os.vtmp_dir(), 'v', 'test_vcreate') fn init_and_check() ! { os.chdir(test_path)! + + // if main file already exist we should not tamper it + mut main_last_modified_time := i64(0) + is_main_file_preexisting := os.exists('src/main.v') + if is_main_file_preexisting == true { + main_last_modified_time = os.file_last_mod_unix('src/main.v') + } os.execute_or_exit('${os.quoted_path(@VEXE)} init') x := os.execute_or_exit('${os.quoted_path(@VEXE)} run .') assert x.exit_code == 0 assert x.output.trim_space() == 'Hello World!' - assert os.read_file('src/main.v')! == [ - 'module main\n', - 'fn main() {', - " println('Hello World!')", - '}', - '', - ].join_lines() + if is_main_file_preexisting == true { + assert main_last_modified_time == os.file_last_mod_unix('src/main.v') + } else { + assert os.read_file('src/main.v')! == [ + 'module main\n', + 'fn main() {', + " println('Hello World!')", + '}', + '', + ].join_lines() + } assert os.read_file('v.mod')! == [ 'Module {',