add unit test for #43
This commit is contained in:
parent
7848a4ae06
commit
0efacf0ca9
@ -60,3 +60,31 @@ func TestGeneratedBinary(t *testing.T) {
|
|||||||
cnt = strings.Count(string(out), "GoCover")
|
cnt = strings.Count(string(out), "GoCover")
|
||||||
assert.Equal(t, cnt > 0, true, "GoCover varibale should be in the binary")
|
assert.Equal(t, cnt > 0, true, "GoCover varibale should be in the binary")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuildBinaryName(t *testing.T) {
|
||||||
|
startTime := time.Now()
|
||||||
|
|
||||||
|
workingDir := filepath.Join(baseDir, "../tests/samples/simple_project2")
|
||||||
|
gopath := ""
|
||||||
|
|
||||||
|
os.Setenv("GOPATH", gopath)
|
||||||
|
os.Setenv("GO111MODULE", "on")
|
||||||
|
|
||||||
|
buildFlags, buildOutput = "", ""
|
||||||
|
args := []string{"."}
|
||||||
|
runBuild(args, workingDir)
|
||||||
|
|
||||||
|
obj := filepath.Join(workingDir, "simple-project")
|
||||||
|
fInfo, err := os.Lstat(obj)
|
||||||
|
assert.Equal(t, err, nil, "the binary should be generated.")
|
||||||
|
assert.Equal(t, startTime.Before(fInfo.ModTime()), true, obj+"new binary should be generated, not the old one")
|
||||||
|
|
||||||
|
cmd := exec.Command("go", "tool", "objdump", "simple-project")
|
||||||
|
cmd.Dir = workingDir
|
||||||
|
out, _ := cmd.CombinedOutput()
|
||||||
|
cnt := strings.Count(string(out), "main.registerSelf")
|
||||||
|
assert.Equal(t, cnt > 0, true, "main.registerSelf function should be in the binary")
|
||||||
|
|
||||||
|
cnt = strings.Count(string(out), "GoCover")
|
||||||
|
assert.Equal(t, cnt > 0, true, "GoCover varibale should be in the binary")
|
||||||
|
}
|
||||||
|
@ -115,21 +115,18 @@ func (b *Build) determineOutputDir(outputDir string) (string, error) {
|
|||||||
return "", fmt.Errorf("can only be called after Build.MvProjectsToTmp(): %w", ErrWrongCallSequence)
|
return "", fmt.Errorf("can only be called after Build.MvProjectsToTmp(): %w", ErrWrongCallSequence)
|
||||||
}
|
}
|
||||||
|
|
||||||
if outputDir == "" {
|
// fix #43
|
||||||
_, last := filepath.Split(b.WorkingDir)
|
// use target name from `go list -json ./...` of the main module
|
||||||
if b.IsMod {
|
targetName := ""
|
||||||
// in mod, special rule
|
for _, pkg := range b.Pkgs {
|
||||||
// replace "_" with "-" in the import path
|
if pkg.Name == "main" {
|
||||||
last = strings.ReplaceAll(last, "_", "-")
|
_, file := filepath.Split(pkg.Target)
|
||||||
|
targetName = file
|
||||||
|
break
|
||||||
}
|
}
|
||||||
return filepath.Join(b.WorkingDir, last), nil
|
|
||||||
}
|
}
|
||||||
abs, err := filepath.Abs(outputDir)
|
|
||||||
if err != nil {
|
return filepath.Join(b.WorkingDir, targetName), nil
|
||||||
log.Errorf("Fail to transform the path: %v to absolute path: %v", outputDir, err)
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return abs, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// validatePackageForBuild only allow . as package name
|
// validatePackageForBuild only allow . as package name
|
||||||
|
Loading…
Reference in New Issue
Block a user