diff --git a/cmd/print.go b/cmd/print.go index 63ea266d..e05871f0 100755 --- a/cmd/print.go +++ b/cmd/print.go @@ -6,7 +6,7 @@ import ( "os" "os/exec" "os/signal" - "path" + "path/filepath" "syscall" "github.com/pkg/errors" @@ -90,8 +90,8 @@ func print(cmd *cobra.Command, args []string) error { return fmt.Errorf("could not find executable %s", parsedCommand[0]) } // Executable needs to be defined with an absolute path since it will be run within the context of repositories - if !path.IsAbs(executablePath) { - executablePath = path.Join(workingDir, executablePath) + if !filepath.IsAbs(executablePath) { + executablePath = filepath.Join(workingDir, executablePath) } // Set up signal listening to cancel the context and let started runs finish gracefully diff --git a/cmd/run.go b/cmd/run.go index e2e6eaf9..dc1ca675 100755 --- a/cmd/run.go +++ b/cmd/run.go @@ -7,7 +7,7 @@ import ( "os" "os/exec" "os/signal" - "path" + "path/filepath" "strings" "syscall" @@ -135,8 +135,8 @@ func run(cmd *cobra.Command, args []string) error { return fmt.Errorf("could not find executable %s", parsedCommand[0]) } // Executable needs to be defined with an absolute path since it will be run within the context of repositories - if !path.IsAbs(executablePath) { - executablePath = path.Join(workingDir, executablePath) + if !filepath.IsAbs(executablePath) { + executablePath = filepath.Join(workingDir, executablePath) } // Set up signal listening to cancel the context and let started runs finish gracefully diff --git a/internal/git/git.go b/internal/git/git.go index ae6d1bec..eb09f349 100755 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -3,7 +3,6 @@ package git import ( "bytes" "fmt" - "net/url" "time" "github.com/go-git/go-git/v5/config" @@ -28,13 +27,8 @@ type Git struct { // Clone a repository func (g *Git) Clone(baseName, headName string) error { - u, err := url.Parse(g.Repo) - if err != nil { - return err - } - r, err := git.PlainClone(g.Directory, false, &git.CloneOptions{ - URL: u.String(), + URL: g.Repo, RemoteName: "origin", Depth: g.FetchDepth, ReferenceName: plumbing.NewBranchReferenceName(baseName), diff --git a/tests/print_test.go b/tests/print_test.go index f710b27a..6b104a9b 100644 --- a/tests/print_test.go +++ b/tests/print_test.go @@ -4,7 +4,7 @@ import ( "fmt" "io/ioutil" "os" - "path" + "path/filepath" "testing" "github.com/lindell/multi-gitter/cmd" @@ -30,16 +30,16 @@ func TestPrint(t *testing.T) { vcMock.AddRepository(changeRepo2) vcMock.AddRepository(noChangeRepo) - runLogFile := path.Join(tmpDir, "print-log.txt") - outFile := path.Join(tmpDir, "out.txt") - errOutFile := path.Join(tmpDir, "err-out.txt") + runLogFile := filepath.Join(tmpDir, "print-log.txt") + outFile := filepath.Join(tmpDir, "out.txt") + errOutFile := filepath.Join(tmpDir, "err-out.txt") command := cmd.RootCmd() command.SetArgs([]string{"print", - "--log-file", runLogFile, - "--output", outFile, - "--error-output", errOutFile, - fmt.Sprintf(`go run %s`, path.Join(workingDir, "scripts/printer/main.go")), + "--log-file", filepath.ToSlash(runLogFile), + "--output", filepath.ToSlash(outFile), + "--error-output", filepath.ToSlash(errOutFile), + fmt.Sprintf(`go run %s`, filepath.ToSlash(filepath.Join(workingDir, "scripts/printer/main.go"))), }) err = command.Execute() assert.NoError(t, err) diff --git a/tests/repo_helper_test.go b/tests/repo_helper_test.go index b6019dd2..6b36f9f1 100644 --- a/tests/repo_helper_test.go +++ b/tests/repo_helper_test.go @@ -4,7 +4,7 @@ import ( "fmt" "io/ioutil" "os" - "path" + "path/filepath" "testing" "time" @@ -29,7 +29,7 @@ func createRepo(t *testing.T, name, dataInFile string) vcmock.Repository { } func createDummyRepo(dataInFile string) (string, error) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "multi-git-test-") + tmpDir, err := ioutil.TempDir(os.TempDir(), "multi-git-test-*.git") if err != nil { return "", err } @@ -39,7 +39,7 @@ func createDummyRepo(dataInFile string) (string, error) { return "", err } - testFilePath := path.Join(tmpDir, fileName) + testFilePath := filepath.Join(tmpDir, fileName) err = ioutil.WriteFile(testFilePath, []byte(dataInFile), 0600) if err != nil { @@ -100,7 +100,7 @@ func changeTestFile(t *testing.T, basePath string, content string, commitMessage repo, err := git.PlainOpen(basePath) require.NoError(t, err) - testFilePath := path.Join(basePath, fileName) + testFilePath := filepath.Join(basePath, fileName) err = ioutil.WriteFile(testFilePath, []byte(content), 0600) require.NoError(t, err) @@ -122,7 +122,7 @@ func changeTestFile(t *testing.T, basePath string, content string, commitMessage } func readTestFile(t *testing.T, basePath string) string { - testFilePath := path.Join(basePath, fileName) + testFilePath := filepath.Join(basePath, fileName) b, err := ioutil.ReadFile(testFilePath) require.NoError(t, err) diff --git a/tests/setup_test.go b/tests/setup_test.go index 499c019c..a569daca 100644 --- a/tests/setup_test.go +++ b/tests/setup_test.go @@ -3,16 +3,29 @@ package tests import ( "os" "os/exec" + "runtime" "testing" ) +var changerBinaryPath string +var printerBinaryPath string + func TestMain(m *testing.M) { - command := exec.Command("go", "build", "-o", "scripts/changer/main", "scripts/changer/main.go") + switch runtime.GOOS { + case "windows": + changerBinaryPath = "scripts/changer/main.exe" + printerBinaryPath = "scripts/printer/main.exe" + default: + changerBinaryPath = "scripts/changer/main" + printerBinaryPath = "scripts/printer/main" + } + + command := exec.Command("go", "build", "-o", changerBinaryPath, "scripts/changer/main.go") if err := command.Run(); err != nil { panic(err) } - command = exec.Command("go", "build", "-o", "scripts/printer/main", "scripts/printer/main.go") + command = exec.Command("go", "build", "-o", printerBinaryPath, "scripts/printer/main.go") if err := command.Run(); err != nil { panic(err) } diff --git a/tests/story_test.go b/tests/story_test.go index 2fad1a37..da442893 100644 --- a/tests/story_test.go +++ b/tests/story_test.go @@ -3,7 +3,7 @@ package tests import ( "io/ioutil" "os" - "path" + "path/filepath" "testing" "github.com/lindell/multi-gitter/internal/domain" @@ -23,6 +23,11 @@ func TestStory(t *testing.T) { defer os.RemoveAll(tmpDir) assert.NoError(t, err) + workingDir, err := os.Getwd() + assert.NoError(t, err) + + changerBinaryPath := filepath.ToSlash(filepath.Join(workingDir, changerBinaryPath)) + changeRepo := createRepo(t, "should-change", "i like apples") changeRepo2 := createRepo(t, "should-change-2", "i like my apple") noChangeRepo := createRepo(t, "should-not-change", "i like oranges") @@ -30,7 +35,7 @@ func TestStory(t *testing.T) { vcMock.AddRepository(changeRepo2) vcMock.AddRepository(noChangeRepo) - runOutFile := path.Join(tmpDir, "run-log.txt") + runOutFile := filepath.Join(tmpDir, "run-log.txt") command := cmd.RootCmd() command.SetArgs([]string{"run", @@ -39,19 +44,19 @@ func TestStory(t *testing.T) { "--author-email", "test@example.com", "-B", "custom-branch-name", "-m", "test", - "scripts/changer/main", + changerBinaryPath, }) err = command.Execute() assert.NoError(t, err) // Verify that the data of the original branch is intact - data, err := ioutil.ReadFile(path.Join(changeRepo.Path, fileName)) + data, err := ioutil.ReadFile(filepath.Join(changeRepo.Path, fileName)) assert.NoError(t, err) assert.Equal(t, []byte("i like apples"), data) // Verify that the new branch is changed changeBranch(t, changeRepo.Path, "custom-branch-name", false) - data, err = ioutil.ReadFile(path.Join(changeRepo.Path, fileName)) + data, err = ioutil.ReadFile(filepath.Join(changeRepo.Path, fileName)) assert.NoError(t, err) assert.Equal(t, []byte("i like bananas"), data) @@ -68,7 +73,7 @@ Repositories with a successful run: // // Status // - statusOutFile := path.Join(tmpDir, "status-log.txt") + statusOutFile := filepath.Join(tmpDir, "status-log.txt") command = cmd.RootCmd() command.SetArgs([]string{"status", @@ -89,7 +94,7 @@ Repositories with a successful run: // // Merge // - mergeLogFile := path.Join(tmpDir, "merge-log.txt") + mergeLogFile := filepath.Join(tmpDir, "merge-log.txt") command = cmd.RootCmd() command.SetArgs([]string{"merge", @@ -108,7 +113,7 @@ Repositories with a successful run: // // After Merge Status // - afterMergeStatusOutFile := path.Join(tmpDir, "after-merge-status-log.txt") + afterMergeStatusOutFile := filepath.Join(tmpDir, "after-merge-status-log.txt") command = cmd.RootCmd() command.SetArgs([]string{"status", @@ -126,7 +131,7 @@ Repositories with a successful run: // // Close // - closeLogFile := path.Join(tmpDir, "close-log.txt") + closeLogFile := filepath.Join(tmpDir, "close-log.txt") command = cmd.RootCmd() command.SetArgs([]string{"close", @@ -145,7 +150,7 @@ Repositories with a successful run: // // After Close Status // - afterCloseStatusOutFile := path.Join(tmpDir, "after-close-status-log.txt") + afterCloseStatusOutFile := filepath.Join(tmpDir, "after-close-status-log.txt") command = cmd.RootCmd() command.SetArgs([]string{"status", diff --git a/tests/table_test.go b/tests/table_test.go index 75b914c5..38a75f0d 100644 --- a/tests/table_test.go +++ b/tests/table_test.go @@ -5,7 +5,7 @@ import ( "fmt" "io/ioutil" "os" - "path" + "path/filepath" "testing" "time" @@ -26,6 +26,8 @@ func TestTable(t *testing.T) { workingDir, err := os.Getwd() assert.NoError(t, err) + changerBinaryPath := filepath.ToSlash(filepath.Join(workingDir, changerBinaryPath)) + tests := []struct { name string vc *vcmock.VersionController @@ -49,7 +51,7 @@ func TestTable(t *testing.T) { "--author-email", "test@example.com", "-B", "custom-branch-name", "-m", "custom message", - path.Join(workingDir, "scripts/changer/main"), + changerBinaryPath, }, verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) { require.Len(t, vcMock.PullRequests, 1) @@ -80,7 +82,7 @@ func TestTable(t *testing.T) { "--author-email", "test@example.com", "-B", "custom-branch-name", "-m", "custom message", - fmt.Sprintf("go run %s", path.Join(workingDir, "scripts/changer/main.go")), + fmt.Sprintf("go run %s", filepath.ToSlash(filepath.Join(workingDir, "scripts/changer/main.go"))), }, verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) { require.Len(t, vcMock.PullRequests, 1) @@ -111,7 +113,7 @@ func TestTable(t *testing.T) { "-B", "custom-branch-name", "--base-branch", "custom-base-branch", "-m", "custom message", - path.Join(workingDir, "scripts/changer/main"), + changerBinaryPath, }, verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) { require.Len(t, vcMock.PullRequests, 0) @@ -139,7 +141,7 @@ func TestTable(t *testing.T) { "-B", "custom-branch-name", "--base-branch", "custom-base-branch", "-m", "custom message", - path.Join(workingDir, "scripts/changer/main"), + changerBinaryPath, }, verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) { require.Len(t, vcMock.PullRequests, 1) @@ -165,7 +167,7 @@ func TestTable(t *testing.T) { "--author-email", "test@example.com", "-m", "custom message", "-r", "reviewer1,reviewer2", - path.Join(workingDir, "scripts/changer/main"), + changerBinaryPath, }, verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) { require.Len(t, vcMock.PullRequests, 1) @@ -189,7 +191,7 @@ func TestTable(t *testing.T) { "-m", "custom message", "-r", "reviewer1,reviewer2,reviewer3", "--max-reviewers", "2", - path.Join(workingDir, "scripts/changer/main"), + changerBinaryPath, }, verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) { require.Len(t, vcMock.PullRequests, 1) @@ -211,7 +213,7 @@ func TestTable(t *testing.T) { "-m", "custom message", "-B", "custom-branch-name", "--dry-run", - path.Join(workingDir, "scripts/changer/main"), + changerBinaryPath, }, verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) { require.Len(t, vcMock.PullRequests, 0) @@ -230,6 +232,10 @@ func TestTable(t *testing.T) { createRepo(t, "should-change-4", "i like apples"), createRepo(t, "should-change-5", "i like apples"), createRepo(t, "should-change-6", "i like apples"), + createRepo(t, "should-change-7", "i like apples"), + createRepo(t, "should-change-8", "i like apples"), + createRepo(t, "should-change-9", "i like apples"), + createRepo(t, "should-change-10", "i like apples"), }, }, args: []string{ @@ -238,12 +244,12 @@ func TestTable(t *testing.T) { "--author-email", "test@example.com", "-m", "custom message", "-B", "custom-branch-name", - "-C", "3", - fmt.Sprintf("%s -sleep 100ms", path.Join(workingDir, "scripts/changer/main")), + "-C", "7", + fmt.Sprintf("%s -sleep 300ms", changerBinaryPath), }, verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) { - require.Len(t, vcMock.PullRequests, 6) - require.Less(t, runData.took.Milliseconds(), int64(600)) + require.Len(t, vcMock.PullRequests, 10) + require.Less(t, runData.took.Milliseconds(), int64(3000)) }, }, @@ -267,7 +273,7 @@ func TestTable(t *testing.T) { "--author-email", "test@example.com", "-B", "custom-branch-name", "-m", "custom message", - path.Join(workingDir, "scripts/changer/main"), + changerBinaryPath, }, verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) { require.Len(t, vcMock.PullRequests, 1) @@ -304,7 +310,7 @@ Repositories with a successful run: "-B", "custom-branch-name", "-m", "custom message", "--skip-pr", - path.Join(workingDir, "scripts/changer/main"), + changerBinaryPath, }, verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) { fmt.Fprintln(os.Stderr, vcMock.Repositories[0].Path) diff --git a/tests/vcmock/vcmock.go b/tests/vcmock/vcmock.go index 09ca6e36..6fb0985f 100644 --- a/tests/vcmock/vcmock.go +++ b/tests/vcmock/vcmock.go @@ -6,6 +6,7 @@ import ( "context" "errors" "fmt" + "path/filepath" "github.com/lindell/multi-gitter/internal/domain" ) @@ -134,7 +135,7 @@ type Repository struct { // URL return the URL (filepath) of the repository on disk func (r Repository) URL(token string) string { - return "file://" + r.Path + return fmt.Sprintf(`file://"%s"`, filepath.ToSlash(r.Path)) } // DefaultBranch returns "master" diff --git a/tools/readme-docs/main.go b/tools/readme-docs/main.go index c968784c..eda07349 100755 --- a/tools/readme-docs/main.go +++ b/tools/readme-docs/main.go @@ -6,7 +6,6 @@ import ( "fmt" "io/ioutil" "log" - "path" "path/filepath" "regexp" "strings" @@ -137,13 +136,13 @@ func readExamples() ([]exampleCategory, error) { } var examples []example - categoryDir := path.Join(examplesDir, f.Name()) + categoryDir := filepath.Join(examplesDir, f.Name()) exampleFiles, err := ioutil.ReadDir(categoryDir) if err != nil { return nil, err } for _, e := range exampleFiles { - b, err := ioutil.ReadFile(path.Join(categoryDir, e.Name())) + b, err := ioutil.ReadFile(filepath.Join(categoryDir, e.Name())) if err != nil { return nil, err }