Skip to content

Commit

Permalink
fix tests on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed May 14, 2024
1 parent 6d3295b commit ce179a2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/xgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func handleBuild(cmd string, args []string) error {
return nil
}
}
instrumentGo := filepath.Join(instrumentGoroot, "bin", "go")
instrumentGo := filepath.Join(instrumentGoroot, "bin", "go"+osinfo.EXE_SUFFIX)
subPaths, mainModule, err := goinfo.ResolveMainModule(projectDir, remainArgs)
if err != nil {
if !errors.Is(err, goinfo.ErrGoModNotFound) && !errors.Is(err, goinfo.ErrGoModDoesNotHaveModule) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/xgo/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ func syncGoroot(goroot string, instrumentGoroot string, fullSyncRecordFile strin
return err
}
var goBinaryChanged bool = true
srcGoBin := filepath.Join(goroot, "bin", "go")
dstGoBin := filepath.Join(instrumentGoroot, "bin", "go")
srcGoBin := filepath.Join(goroot, "bin", "go"+osinfo.EXE_SUFFIX)
dstGoBin := filepath.Join(instrumentGoroot, "bin", "go"+osinfo.EXE_SUFFIX)

srcFile, err := os.Stat(srcGoBin)
if err != nil {
return nil
return err
}
if srcFile.IsDir() {
return fmt.Errorf("bad goroot: %s", goroot)
Expand Down
23 changes: 18 additions & 5 deletions cmd/xgo/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@ package main

import (
"path/filepath"
"runtime"
"testing"
)

// go test -run TestFilePathDir -v ./cmd/xgo
func TestFilePathDir(t *testing.T) {
var testCases = []struct {

type testCase struct {
Name string
Dir string
}{
}
var testCases = []testCase{
{"", "."},
{".", "."},
{"/", "/"},
{"/tmp", "/"},
{"//tmp", "/"},
}
if runtime.GOOS != "windows" {
testCases = append(testCases, []testCase{
{"/", "/"},
{"/tmp", "/"},
{"//tmp", "/"},
}...)
} else {
testCases = append(testCases, []testCase{
{"/", "\\"},
{"/tmp", "\\"},
{"//tmp", "\\\\tmp"},
}...)
}

for _, testCase := range testCases {
Expand Down
9 changes: 7 additions & 2 deletions cmd/xgo/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func loadDependency(goroot string, goBinary string, goVersion *goinfo.GoVersion,
}
vendorModPath := filepath.Join(vendorDir, modPath)
vendorModFile := filepath.Join(vendorModPath, "go.mod")
replaceModFile := filepath.Join(tmpProjectDir, vendorModFile)
replaceModFile := filepath.Join(tmpProjectDir, asSubPath(vendorModFile))
// replace goMod => vendor=>

// NOTE: if replace without require, go will automatically add
Expand Down Expand Up @@ -490,6 +490,11 @@ func addBlankImports(goroot string, goBinary string, projectDir string, pkgArgs
return replace, nil
}

// when doing filepath.Join(a,b),
// on windows, if b has :, everything fails
func asSubPath(path string) string {
return fileutil.CleanSpecial(path)
}
func addBlankImportForPackage(srcDir string, dstDir string, imports []string, files []string, allFile bool) (map[string]string, error) {
if len(files) == 0 {
// no files
Expand All @@ -509,7 +514,7 @@ func addBlankImportForPackage(srcDir string, dstDir string, imports []string, fi
mapping := make(map[string]string, len(files))
for _, file := range files {
srcFile := filepath.Join(srcDir, file)
dstFile := filepath.Join(dstDir, srcFile)
dstFile := filepath.Join(dstDir, asSubPath(srcFile))
err := filecopy.CopyFileAll(srcFile, dstFile)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions runtime/test/mock_stdlib/mock_stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// go run ./script/run-test/ --include go1.22.1 --xgo-runtime-test-only -run TestMockTimeNow -v ./test/mock_stdlib
func TestMockTimeNow(t *testing.T) {
now1 := time.Now()
time.Sleep(1 * time.Millisecond)
now2 := time.Now()

d1 := now2.Sub(now1)
Expand Down

0 comments on commit ce179a2

Please sign in to comment.