From f0ad0b024e6081ec849fd3f9c7f5673d80dc7628 Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Sat, 19 Jun 2021 14:36:12 -0400 Subject: [PATCH] v init: create a .gitignore in existing git repo if it does not exist yet (#10488) --- cmd/tools/vcreate.v | 14 +++++------ cmd/tools/vcreate_test.v | 50 ++++++++++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/cmd/tools/vcreate.v b/cmd/tools/vcreate.v index 6adaeac0b9..af327ab915 100644 --- a/cmd/tools/vcreate.v +++ b/cmd/tools/vcreate.v @@ -109,14 +109,14 @@ fn (c &Create) create_git_repo(dir string) { cerror('Unable to create git repo') exit(4) } - if !os.exists('$dir/.gitignore') { - mut fl := os.create('$dir/.gitignore') or { - // We don't really need a .gitignore, it's just a nice-to-have - return - } - fl.write_string(gen_gitignore(c.name)) or { panic(err) } - fl.close() + } + if !os.exists('$dir/.gitignore') { + mut fl := os.create('$dir/.gitignore') or { + // We don't really need a .gitignore, it's just a nice-to-have + return } + fl.write_string(gen_gitignore(c.name)) or { panic(err) } + fl.close() } } diff --git a/cmd/tools/vcreate_test.v b/cmd/tools/vcreate_test.v index b4db89a5b5..2b5b562cf8 100644 --- a/cmd/tools/vcreate_test.v +++ b/cmd/tools/vcreate_test.v @@ -2,15 +2,7 @@ import os const test_path = 'vcreate_test' -fn test_v_init() ? { - dir := os.join_path(os.temp_dir(), test_path) - os.rmdir_all(dir) or {} - os.mkdir(dir) ? - defer { - os.rmdir_all(dir) or {} - } - os.chdir(dir) - +fn init_and_check() ? { vexe := os.getenv('VEXE') os.execute_or_panic('$vexe init') @@ -45,3 +37,43 @@ fn test_v_init() ? { '', ].join('\n') } + +fn test_v_init() ? { + dir := os.join_path(os.temp_dir(), test_path) + os.rmdir_all(dir) or {} + os.mkdir(dir) ? + defer { + os.rmdir_all(dir) or {} + } + os.chdir(dir) + + init_and_check() ? +} + +fn test_v_init_in_git_dir() ? { + dir := os.join_path(os.temp_dir(), test_path) + os.rmdir_all(dir) or {} + os.execute_or_panic('git init $dir') + defer { + os.rmdir_all(dir) or {} + } + os.chdir(dir) + + init_and_check() ? +} + +fn test_v_init_no_overwrite_gitignore() ? { + dir := os.join_path(os.temp_dir(), test_path) + os.rmdir_all(dir) or {} + os.mkdir(dir) or {} + os.write_file('$dir/.gitignore', 'blah') ? + defer { + os.rmdir_all(dir) or {} + } + os.chdir(dir) + + vexe := os.getenv('VEXE') + os.execute_or_panic('$vexe init') + + assert os.read_file('.gitignore') ? == 'blah' +}