Skip to content

Commit

Permalink
Fix main.go generation when executing on a windows machine (#32)
Browse files Browse the repository at this point in the history
* Fix main.go generation when executing on a windows machine

When normalizing the `importPath` it will try to write on `main.go` files with imports
like `github.com/caddyserver/xcaddy/c:\xcaddy`

* Apply CR suggestions

* Add test cases for sub-paths on normalizeImportPath

* Run windows tests only when GOOS matches
  • Loading branch information
jaysonsantos committed Sep 25, 2020
1 parent 13c49c3 commit 4f3e0ef
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
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, cwd, 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
55 changes: 54 additions & 1 deletion cmd/xcaddy/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "testing"
import (
"runtime"
"testing"
)

func TestSplitWith(t *testing.T) {
for i, tc := range []struct {
Expand Down Expand Up @@ -71,3 +74,53 @@ func TestSplitWith(t *testing.T) {
}
}
}

func TestNormalizeImportPath(t *testing.T) {
type (
args struct {
currentModule string
cwd string
moduleDir string
}
testCaseType []struct {
name string
args args
want string
}
)

tests := testCaseType{
{"linux-path", args{
currentModule: "github.com/caddyserver/xcaddy",
cwd: "/xcaddy",
moduleDir: "/xcaddy",
}, "github.com/caddyserver/xcaddy"},
{"linux-subpath", args{
currentModule: "github.com/caddyserver/xcaddy",
cwd: "/xcaddy/subdir",
moduleDir: "/xcaddy",
}, "github.com/caddyserver/xcaddy/subdir"},
}
windowsTests := testCaseType{
{"windows-path", args{
currentModule: "github.com/caddyserver/xcaddy",
cwd: "c:\\xcaddy",
moduleDir: "c:\\xcaddy",
}, "github.com/caddyserver/xcaddy"},
{"windows-subpath", args{
currentModule: "github.com/caddyserver/xcaddy",
cwd: "c:\\xcaddy\\subdir",
moduleDir: "c:\\xcaddy",
}, "github.com/caddyserver/xcaddy/subdir"},
}
if runtime.GOOS == "windows" {
tests = append(tests, windowsTests...)
}
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 4f3e0ef

Please sign in to comment.