From db3e1bc9731ee878bf2c1bae844f9b09728b8e8d Mon Sep 17 00:00:00 2001 From: Miguel Molina Date: Wed, 12 Jul 2017 16:32:05 +0200 Subject: [PATCH] fix pathToFileURL in windows Signed-off-by: Miguel Molina --- .travis.yml | 3 +- Makefile | 4 +-- generator/cli/kallax/cmd/migrate.go | 12 ++++---- generator/cli/kallax/cmd/migrate_test.go | 9 +++++- .../cli/kallax/cmd/migrate_windows_test.go | 28 +++++++++++++++++++ 5 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 generator/cli/kallax/cmd/migrate_windows_test.go diff --git a/.travis.yml b/.travis.yml index 204f6ce..2628b30 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,8 +26,7 @@ install: - mkdir -p $GOPATH/src/gopkg.in/src-d - mv $PWD $GOPATH/src/gopkg.in/src-d/go-kallax.v1 - cd $GOPATH/src/gopkg.in/src-d/go-kallax.v1 - - go get -v -t . - - go get -v -t ./generator/... + - go get -v -t ./... script: - make test diff --git a/Makefile b/Makefile index b448f6c..2d7bb08 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,8 @@ test: tail -n +2 $(COVERAGE_PROFILE) >> $(COVERAGE_REPORT); \ rm $(COVERAGE_PROFILE); \ fi; \ - for dir in `find . -name "*.go" | grep -o '.*/' | sort -u | grep -v './tests/' | grep -v './fixtures/' | grep -v './benchmarks/'`; do \ - go test $$dir -coverprofile=$(COVERAGE_PROFILE) -covermode=$(COVERAGE_MODE); \ + for dir in `go list ./... | grep -v '/tests' | grep -v '/fixtures' | grep -v '/benchmarks'`; do \ + go test -v $$dir -coverprofile=$(COVERAGE_PROFILE) -covermode=$(COVERAGE_MODE); \ if [ $$? != 0 ]; then \ exit 2; \ fi; \ diff --git a/generator/cli/kallax/cmd/migrate.go b/generator/cli/kallax/cmd/migrate.go index 02f9153..96390bc 100644 --- a/generator/cli/kallax/cmd/migrate.go +++ b/generator/cli/kallax/cmd/migrate.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" "path/filepath" - "strings" "github.com/mattes/migrate" _ "github.com/mattes/migrate/database/postgres" @@ -157,11 +156,14 @@ func runMigrationAction(fn runMigrationFunc) cli.ActionFunc { } func pathToFileURL(path string) string { - path = strings.Replace(path, "\\", "/", -1) - if !strings.HasPrefix(path, "/") { - path = "/" + path + if !filepath.IsAbs(path) { + var err error + path, err = filepath.Abs(path) + if err != nil { + return "" + } } - return fmt.Sprintf("file://%s", path) + return fmt.Sprintf("file://%s", filepath.ToSlash(path)) } func migrateAction(c *cli.Context) error { diff --git a/generator/cli/kallax/cmd/migrate_test.go b/generator/cli/kallax/cmd/migrate_test.go index 6fe0169..0b5d6e1 100644 --- a/generator/cli/kallax/cmd/migrate_test.go +++ b/generator/cli/kallax/cmd/migrate_test.go @@ -1,18 +1,25 @@ +// +build !windows + package cmd import ( + "os" + "path/filepath" "testing" "github.com/stretchr/testify/require" ) func TestPathToFileURL(t *testing.T) { + wd, err := os.Getwd() + require.NoError(t, err) + cases := []struct { input string expected string }{ - {`c:\foo\bar\baz`, "file:///c:/foo/bar/baz"}, {"/foo/bar/baz", "file:///foo/bar/baz"}, + {"foo/bar", "file://" + filepath.Join(wd, "foo/bar")}, } for _, tt := range cases { diff --git a/generator/cli/kallax/cmd/migrate_windows_test.go b/generator/cli/kallax/cmd/migrate_windows_test.go new file mode 100644 index 0000000..2592068 --- /dev/null +++ b/generator/cli/kallax/cmd/migrate_windows_test.go @@ -0,0 +1,28 @@ +// +build windows + +package cmd + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestPathToFileURL(t *testing.T) { + wd, err := os.Getwd() + require.NoError(t, err) + + cases := []struct { + input string + expected string + }{ + {"c:\\foo\\bar\\baz", "file://c:/foo/bar/baz"}, + {"foo\\bar", "file://" + filepath.ToSlash(filepath.Join(wd, "foo", "bar"))}, + } + + for _, tt := range cases { + require.Equal(t, tt.expected, pathToFileURL(tt.input), tt.input) + } +}