mirror of
https://github.com/caddyserver/xcaddy.git
synced 2024-11-08 10:19:59 +00:00
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
This commit is contained in:
parent
13c49c3566
commit
4f3e0ef756
2 changed files with 59 additions and 2 deletions
|
@ -199,7 +199,7 @@ func runDev(ctx context.Context, args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to determine current directory: %v", err)
|
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
|
// build caddy with this module plugged in
|
||||||
builder := xcaddy.Builder{
|
builder := xcaddy.Builder{
|
||||||
|
@ -243,6 +243,10 @@ func runDev(ctx context.Context, args []string) error {
|
||||||
return cmd.Wait()
|
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) {
|
func trapSignals(ctx context.Context, cancel context.CancelFunc) {
|
||||||
sig := make(chan os.Signal, 1)
|
sig := make(chan os.Signal, 1)
|
||||||
signal.Notify(sig, os.Interrupt)
|
signal.Notify(sig, os.Interrupt)
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"runtime"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestSplitWith(t *testing.T) {
|
func TestSplitWith(t *testing.T) {
|
||||||
for i, tc := range []struct {
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue