Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix main.go generation when executing on a windows machine #32

Merged
merged 4 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func normalizeImportPath(currentModule string, cwd string, moduleDir string) string {
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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func Test_normalizeImportPath(t *testing.T) {
func TestNormalizeImportPath(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)
}
})
}
}