Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: copy .env file error on Windows os. #9

Merged
merged 7 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions console/commands/new_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -185,3 +186,23 @@ 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test case for this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the unit tests. Please check them out.

// Open .env.example file
in, err := os.Open(inputFilePath)
if err != nil {
return err
}
defer in.Close()

// Create .env file
out, err := os.Create(outputFilePath)
if err != nil {
return err
}
defer out.Close()

// Copy .env.example to .env file
_, err = io.Copy(out, in)
return err
}
21 changes: 21 additions & 0 deletions console/commands/new_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package commands
import (
"errors"
"io"
"os"
"path/filepath"
"testing"

"github.com/goravel/framework/contracts/console"
Expand Down Expand Up @@ -46,6 +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"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.True(t, file.Exists("example-app"))
assert.True(t, file.Exists("example-app"))
assert.True(t, file.Exists("example-app/.env"))

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()
Expand All @@ -56,3 +59,21 @@ 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(src))
assert.Nil(t, os.Remove(dst))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove src, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure!

}
Loading