From 0f7297032d723d86b6fada1075cbffea9fd921d3 Mon Sep 17 00:00:00 2001 From: jeff87218 Date: Mon, 5 Aug 2024 21:39:04 +0800 Subject: [PATCH 1/5] fix: copy .env file error on Windows os. --- console/commands/new_command.go | 46 ++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/console/commands/new_command.go b/console/commands/new_command.go index b0c54ac..2285250 100644 --- a/console/commands/new_command.go +++ b/console/commands/new_command.go @@ -3,6 +3,7 @@ package commands import ( "errors" "fmt" + "io" "os" "os/exec" "path/filepath" @@ -144,11 +145,11 @@ func (receiver *NewCommand) generate(ctx console.Context, name string) error { color.Successln("Goravel installed successfully!") // generate .env file - copyEnv := exec.Command("cp", ".env.example", ".env") - copyEnv.Dir = path err = ctx.Spinner("> @cp .env.example .env", console.SpinnerOption{ Action: func() error { - return copyEnv.Run() + inputFilePath := filepath.Join(path, ".env.example") + outputFilePath := filepath.Join(path, ".env") + return receiver.copyFile(inputFilePath, outputFilePath) }, }) if err != nil { @@ -185,3 +186,42 @@ func (receiver *NewCommand) removeFiles(path string) error { // Remove the .GitHub directory return file.Remove(filepath.Join(path, ".github")) } + +func (receiver *NewCommand) copyFile(inputFilePath, outputFilePath string) (err error) { + // Open .env.example file + in, err := os.Open(inputFilePath) + if err != nil { + return err + } + defer func() { + if cerr := in.Close(); cerr != nil { + if err == nil { + err = cerr + } else { + fmt.Printf("Error closing input file: %v\n", cerr) + } + } + }() + + // Create .env file + out, err := os.Create(outputFilePath) + if err != nil { + return err + } + defer func() { + if cerr := out.Close(); cerr != nil { + if err == nil { + err = cerr + } else { + fmt.Printf("Error closing output file: %v\n", cerr) + } + } + }() + + // Copy .env.example to .env file + if _, err = io.Copy(out, in); err != nil { + return err + } + + return nil +} From cc6d20cbe78cfbf64245fce91f554a386cb0de7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Tue, 6 Aug 2024 16:31:41 +0800 Subject: [PATCH 2/5] chore: optimize code --- console/commands/new_command.go | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/console/commands/new_command.go b/console/commands/new_command.go index 2285250..e8f3f4b 100644 --- a/console/commands/new_command.go +++ b/console/commands/new_command.go @@ -193,35 +193,16 @@ func (receiver *NewCommand) copyFile(inputFilePath, outputFilePath string) (err if err != nil { return err } - defer func() { - if cerr := in.Close(); cerr != nil { - if err == nil { - err = cerr - } else { - fmt.Printf("Error closing input file: %v\n", cerr) - } - } - }() + defer in.Close() // Create .env file out, err := os.Create(outputFilePath) if err != nil { return err } - defer func() { - if cerr := out.Close(); cerr != nil { - if err == nil { - err = cerr - } else { - fmt.Printf("Error closing output file: %v\n", cerr) - } - } - }() + defer out.Close() // Copy .env.example to .env file - if _, err = io.Copy(out, in); err != nil { - return err - } - - return nil + _, err = io.Copy(out, in) + return err } From 72e1e0845d3d1052048f583ae536ded5c4d922d4 Mon Sep 17 00:00:00 2001 From: jeff87218 Date: Tue, 6 Aug 2024 20:31:51 +0800 Subject: [PATCH 3/5] feat: optimize test --- console/commands/new_command_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/console/commands/new_command_test.go b/console/commands/new_command_test.go index 2dbaf3b..6afc393 100644 --- a/console/commands/new_command_test.go +++ b/console/commands/new_command_test.go @@ -3,6 +3,7 @@ package commands import ( "errors" "io" + "path/filepath" "testing" "github.com/goravel/framework/contracts/console" @@ -47,6 +48,12 @@ func TestNewCommand(t *testing.T) { assert.Contains(t, captureOutput, "App key generated successfully!") assert.True(t, file.Exists("example-app")) + // Test copyFile + src := filepath.Join("example-app", ".env.example") + dst := filepath.Join("example-app", ".env") + assert.Nil(t, newCommand.copyFile(src, dst)) + assert.True(t, file.Exists(dst)) + mockContext.On("Argument", 0).Return("example-app").Once() mockContext.On("OptionBool", "force").Return(false).Once() assert.Contains(t, color.CaptureOutput(func(w io.Writer) { From 85ab3f3e1142e8885f457e9fd1ea5c58fc4529ba Mon Sep 17 00:00:00 2001 From: jeff87218 Date: Wed, 7 Aug 2024 00:31:27 +0800 Subject: [PATCH 4/5] feat: optimize test --- console/commands/new_command_test.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/console/commands/new_command_test.go b/console/commands/new_command_test.go index 6afc393..40797e8 100644 --- a/console/commands/new_command_test.go +++ b/console/commands/new_command_test.go @@ -3,6 +3,7 @@ package commands import ( "errors" "io" + "os" "path/filepath" "testing" @@ -47,12 +48,7 @@ func TestNewCommand(t *testing.T) { assert.Contains(t, captureOutput, ".env file generated successfully!") assert.Contains(t, captureOutput, "App key generated successfully!") assert.True(t, file.Exists("example-app")) - - // Test copyFile - src := filepath.Join("example-app", ".env.example") - dst := filepath.Join("example-app", ".env") - assert.Nil(t, newCommand.copyFile(src, dst)) - assert.True(t, file.Exists(dst)) + assert.True(t, file.Exists(filepath.Join("example-app", ".env"))) mockContext.On("Argument", 0).Return("example-app").Once() mockContext.On("OptionBool", "force").Return(false).Once() @@ -63,3 +59,20 @@ func TestNewCommand(t *testing.T) { assert.Nil(t, file.Remove("example-app")) mockContext.AssertExpectations(t) } + +func TestCopyFile(t *testing.T) { + newCommand := &NewCommand{} + + tmpDir, err := os.MkdirTemp("", "test-copy-file") + assert.Nil(t, err) + src := filepath.Join(tmpDir, ".env.example") + dst := filepath.Join(tmpDir, ".env") + + // Create a mock .env.example file for testing + err = os.WriteFile(src, []byte("example env"), os.ModePerm) + assert.Nil(t, err) + assert.True(t, file.Exists(src)) + assert.Nil(t, newCommand.copyFile(src, dst)) + assert.True(t, file.Exists(dst)) + assert.Nil(t, os.Remove(dst)) +} From b4fa72d7b6d8253b3268b93e4bd0ced870c25719 Mon Sep 17 00:00:00 2001 From: Jeff Hong Date: Wed, 7 Aug 2024 11:31:10 +0800 Subject: [PATCH 5/5] feat: optimize test --- console/commands/new_command_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/console/commands/new_command_test.go b/console/commands/new_command_test.go index 40797e8..552a2d4 100644 --- a/console/commands/new_command_test.go +++ b/console/commands/new_command_test.go @@ -74,5 +74,6 @@ func TestCopyFile(t *testing.T) { assert.True(t, file.Exists(src)) assert.Nil(t, newCommand.copyFile(src, dst)) assert.True(t, file.Exists(dst)) + assert.Nil(t, os.Remove(src)) assert.Nil(t, os.Remove(dst)) }