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

tools/ut.go: replace path with filepath #55122

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 21 additions & 19 deletions tools/check/ut.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"math/rand"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
Expand Down Expand Up @@ -88,7 +87,7 @@ ut run --short`
return true
}

const modulePath = "github.com/pingcap/tidb"
var modulePath = filepath.Join("github.com", "pingcap", "tidb")

type task struct {
pkg string
Expand Down Expand Up @@ -546,7 +545,7 @@ func collectCoverProfileFile() {
}

func collectOneCoverProfileFile(result map[string]*cover.Profile, file os.DirEntry) {
f, err := os.Open(path.Join(coverFileTempDir, file.Name()))
f, err := os.Open(filepath.Join(coverFileTempDir, file.Name()))
if err != nil {
fmt.Println("open temp cover file error:", err)
os.Exit(-1)
Expand Down Expand Up @@ -702,7 +701,8 @@ func filterTestCases(tasks []task, arg1 string) ([]task, error) {
}

func listPackages() ([]string, error) {
cmd := exec.Command("go", "list", "./...")
listPath := strings.Join([]string{".", "..."}, string(filepath.Separator))
cmd := exec.Command("go", "list", listPath)
ss, err := cmdToLines(cmd)
if err != nil {
return nil, withTrace(err)
Expand Down Expand Up @@ -749,7 +749,7 @@ type testResult struct {
func (n *numa) runTestCase(pkg string, fn string) testResult {
res := testResult{
JUnitTestCase: JUnitTestCase{
Classname: path.Join(modulePath, pkg),
Classname: filepath.Join(modulePath, pkg),
Name: fn,
},
}
Expand All @@ -759,7 +759,7 @@ func (n *numa) runTestCase(pkg string, fn string) testResult {
var start time.Time
for i := 0; i < 3; i++ {
cmd := n.testCommand(pkg, fn)
cmd.Dir = path.Join(workDir, pkg)
cmd.Dir = filepath.Join(workDir, pkg)
// Combine the test case output, so the run result for failed cases can be displayed.
cmd.Stdout = &buf
cmd.Stderr = &buf
Expand Down Expand Up @@ -846,10 +846,11 @@ func failureCases(input []JUnitTestCase) int {

func (n *numa) testCommand(pkg string, fn string) *exec.Cmd {
args := make([]string, 0, 10)
exe := "./" + testFileName(pkg)
exe := strings.Join([]string{".", testFileName(pkg)}, string(filepath.Separator))

if coverprofile != "" {
fileName := strings.ReplaceAll(pkg, "/", "_") + "." + fn
tmpFile := path.Join(coverFileTempDir, fileName)
fileName := strings.ReplaceAll(pkg, string(filepath.Separator), "_") + "." + fn
tmpFile := filepath.Join(coverFileTempDir, fileName)
args = append(args, "-test.coverprofile", tmpFile)
}
args = append(args, "-test.cpu", "1")
Expand All @@ -867,7 +868,8 @@ func (n *numa) testCommand(pkg string, fn string) *exec.Cmd {
}

func skipDIR(pkg string) bool {
skipDir := []string{"br", "lightning", "pkg/lightning", "cmd", "dumpling", "tests", "tools/check", "build"}
skipDir := []string{"br", "lightning", filepath.Join("pkg", "lightning"),
"cmd", "dumpling", "tests", filepath.Join("tools", "check"), "build"}
for _, ignore := range skipDir {
if strings.HasPrefix(pkg, ignore) {
return true
Expand All @@ -888,7 +890,7 @@ func buildTestBinary(pkg string) error {
if short {
cmd.Args = append(cmd.Args, "--test.short")
}
cmd.Dir = path.Join(workDir, pkg)
cmd.Dir = filepath.Join(workDir, pkg)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
Expand All @@ -900,9 +902,9 @@ func buildTestBinary(pkg string) error {
func generateBuildCache() error {
// cd cmd/tidb-server && go test -tags intest -exec true -vet off -toolexec=go-compile-without-link
cmd := exec.Command("go", "test", "-tags=intest", "-exec=true", "-vet=off")
goCompileWithoutLink := fmt.Sprintf("-toolexec=%s/tools/check/go-compile-without-link.sh", workDir)
goCompileWithoutLink := fmt.Sprintf("-toolexec=%s", filepath.Join(workDir, "tools", "check", "go-compile-without-link.sh"))
cmd.Args = append(cmd.Args, goCompileWithoutLink)
cmd.Dir = path.Join(workDir, "cmd/tidb-server")
cmd.Dir = filepath.Join(workDir, "cmd", "tidb-server")
if err := cmd.Run(); err != nil {
return withTrace(err)
}
Expand All @@ -918,10 +920,10 @@ func buildTestBinaryMulti(pkgs []string) error {
}

// go test --exec=xprog -cover -vet=off --count=0 $(pkgs)
xprogPath := path.Join(workDir, "tools/bin/xprog")
xprogPath := filepath.Join(workDir, "tools", "bin", "xprog")
packages := make([]string, 0, len(pkgs))
for _, pkg := range pkgs {
packages = append(packages, path.Join(modulePath, pkg))
packages = append(packages, filepath.Join(modulePath, pkg))
}

var cmd *exec.Cmd
Expand Down Expand Up @@ -957,20 +959,20 @@ func testBinaryExist(pkg string) (bool, error) {
}

func testFileName(pkg string) string {
_, file := path.Split(pkg)
_, file := filepath.Split(pkg)
return file + ".test.bin"
}

func testFileFullPath(pkg string) string {
return path.Join(workDir, pkg, testFileName(pkg))
return filepath.Join(workDir, pkg, testFileName(pkg))
}

func listNewTestCases(pkg string) ([]string, error) {
exe := "./" + testFileName(pkg)
exe := strings.Join([]string{".", testFileName(pkg)}, string(filepath.Separator))

// session.test -test.list Test
cmd := exec.Command(exe, "-test.list", "Test")
cmd.Dir = path.Join(workDir, pkg)
cmd.Dir = filepath.Join(workDir, pkg)
var buf bytes.Buffer
cmd.Stdout = &buf
err := cmd.Run()
Expand Down
4 changes: 2 additions & 2 deletions tools/check/xprog.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func main() {

// Extract the current work directory
cwd := os.Args[0]
cwd = cwd[:len(cwd)-len("tools/bin/xprog")]
cwd = cwd[:len(cwd)-len(filepath.Join("tools", "bin", "xprog"))]

testBinaryPath := filepath.Clean(os.Args[1])
dir, _ := filepath.Split(testBinaryPath)

// Extract the package info from /tmp/go-build2662369829/b1382/importcfg.link
pkg := getPackageInfo(dir)

const prefix = "github.com/pingcap/tidb/"
var prefix = filepath.Join("github.com", "pingcap", "tidb")
if !strings.HasPrefix(pkg, prefix) {
os.Exit(-3)
}
Expand Down