From 4f3e0ef756d40d109b2d35e2f06d807fb852d17e Mon Sep 17 00:00:00 2001 From: Jayson Reis Date: Sat, 26 Sep 2020 01:39:48 +0200 Subject: [PATCH] Fix main.go generation when executing on a windows machine (#32) * 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 --- cmd/xcaddy/main.go | 6 ++++- cmd/xcaddy/main_test.go | 55 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/cmd/xcaddy/main.go b/cmd/xcaddy/main.go index 43c150f..acf4658 100644 --- a/cmd/xcaddy/main.go +++ b/cmd/xcaddy/main.go @@ -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{ @@ -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) diff --git a/cmd/xcaddy/main_test.go b/cmd/xcaddy/main_test.go index 0339fd4..63800c0 100644 --- a/cmd/xcaddy/main_test.go +++ b/cmd/xcaddy/main_test.go @@ -1,6 +1,9 @@ package main -import "testing" +import ( + "runtime" + "testing" +) func TestSplitWith(t *testing.T) { for i, tc := range []struct { @@ -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) + } + }) + } +}