Skip to content

Commit

Permalink
Fix main.go generation when executing on a windows machine
Browse files Browse the repository at this point in the history
When normalizing the `importPath` it will try to write on `main.go` files with imports
like `github.com/caddyserver/xcaddy/c:\xcaddy`
  • Loading branch information
jaysonsantos committed Aug 31, 2020
1 parent 13c49c3 commit 57b3486
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cmd/xcaddy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func runDev(ctx context.Context, args []string) error {
if err != nil {
return fmt.Errorf("unable to determine current directory: %v", err)
}
importPath := path.Join(currentModule, strings.TrimPrefix(cwd, filepath.ToSlash(moduleDir)))
importPath := normalizeImportPath(currentModule, cwd, moduleDir)

// build caddy with this module plugged in
builder := xcaddy.Builder{
Expand Down Expand Up @@ -243,6 +243,10 @@ func runDev(ctx context.Context, args []string) error {
return cmd.Wait()
}

func normalizeImportPath(currentModule string, cwd string, moduleDir string) string {
return path.Join(currentModule, filepath.ToSlash(strings.TrimPrefix(cwd, moduleDir)))
}

func trapSignals(ctx context.Context, cancel context.CancelFunc) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
Expand Down
31 changes: 31 additions & 0 deletions cmd/xcaddy/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,34 @@ func TestSplitWith(t *testing.T) {
}
}
}

func Test_normalizeImportPath(t *testing.T) {
type args struct {
currentModule string
cwd string
moduleDir string
}
tests := []struct {
name string
args args
want string
}{
{"linux-path", args{
currentModule: "github.com/caddyserver/xcaddy",
cwd: "/xcaddy",
moduleDir: "/xcaddy",
}, "github.com/caddyserver/xcaddy"},
{"windows-path", args{
currentModule: "github.com/caddyserver/xcaddy",
cwd: "c:\\xcaddy",
moduleDir: "c:\\xcaddy",
}, "github.com/caddyserver/xcaddy"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := normalizeImportPath(tt.args.currentModule, tt.args.cwd, tt.args.moduleDir); got != tt.want {
t.Errorf("normalizeImportPath() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 57b3486

Please sign in to comment.