Skip to content

Commit

Permalink
fix: fixed windows filepaths (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell authored Mar 31, 2021
1 parent 98fb52f commit cb38fc0
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 56 deletions.
6 changes: 3 additions & 3 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"os/exec"
"os/signal"
"path"
"path/filepath"
"syscall"

"github.com/pkg/errors"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"os/exec"
"os/signal"
"path"
"path/filepath"
"strings"
"syscall"

Expand Down Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package git
import (
"bytes"
"fmt"
"net/url"
"time"

"github.com/go-git/go-git/v5/config"
Expand All @@ -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),
Expand Down
16 changes: 8 additions & 8 deletions tests/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"

"github.com/lindell/multi-gitter/cmd"
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions tests/repo_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
"time"

Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions tests/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
25 changes: 15 additions & 10 deletions tests/story_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tests
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"

"github.com/lindell/multi-gitter/internal/domain"
Expand All @@ -23,14 +23,19 @@ 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")
vcMock.AddRepository(changeRepo)
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",
Expand All @@ -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)

Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
34 changes: 20 additions & 14 deletions tests/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
"time"

Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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{
Expand All @@ -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))
},
},

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit cb38fc0

Please sign in to comment.