From 98e60b7e0123b808603ae83a951e9fd3abdca8e9 Mon Sep 17 00:00:00 2001 From: Xiaoyang Tan Date: Fri, 13 May 2022 15:41:14 -0700 Subject: [PATCH] final format --- go/tools/builders/stdliblist.go | 7 ++-- go/tools/builders/stdliblist_test.go | 55 ++++++---------------------- 2 files changed, 14 insertions(+), 48 deletions(-) diff --git a/go/tools/builders/stdliblist.go b/go/tools/builders/stdliblist.go index a437468918..76fc69e6e7 100644 --- a/go/tools/builders/stdliblist.go +++ b/go/tools/builders/stdliblist.go @@ -20,7 +20,6 @@ import ( "flag" "fmt" "go/build" - "io/ioutil" "os" "path/filepath" "strings" @@ -192,15 +191,15 @@ func stdliblist(args []string) error { return err } - cloneBase, err := ioutil.TempDir(goenv.wd, "rules-go-stdliblist-work-") + cloneBase, cleanup, err := goenv.workDir() if err != nil { return fmt.Errorf("creating temp %v", err) } + defer func() { cleanup() }() cloneGoRoot, err := cloneGoRoot(goenv.wd, goenv.sdk, cloneBase) if err != nil { - return fmt.Errorf("clonging %v %s", err, - fmt.Sprintf("GOROOT is %s \n dot is %s", abs(os.Getenv("GOROOT")), abs("."))) + return fmt.Errorf("failed to clone new go root %v", err) } // Ensure paths are absolute. diff --git a/go/tools/builders/stdliblist_test.go b/go/tools/builders/stdliblist_test.go index 510f92770a..189a0762b3 100644 --- a/go/tools/builders/stdliblist_test.go +++ b/go/tools/builders/stdliblist_test.go @@ -2,59 +2,39 @@ package main import ( "bufio" - "bytes" "encoding/json" "fmt" - "github.com/bazelbuild/rules_go/go/tools/bazel" "io/ioutil" - "log" - "os" "path/filepath" "strings" "testing" + + "github.com/bazelbuild/rules_go/go/tools/bazel" ) func Test_stdliblist(t *testing.T) { testDir := t.TempDir() f, _ := ioutil.TempFile(testDir, "*") - // test files are at TEST_SRCDIR, but this test is run at - // TEST_SRCDIR/TEST_WORKSPACE/... + // test files are at run file directory, but this test is run at + // {runfile directory}/bazel.TestWorkspace() // since -sdk is assumed to be a relative path to execRoot // (go.sdk.root_file.dirname), thus setting wd to - // TEST_SRCDIR so that go_sdk is discoverable - runFilesDir := os.Getenv("TEST_SRCDIR") - runfiles, err := bazel.ListRunfiles() - if err != nil || len(runfiles) == 0 { - t.Errorf("getting runfiles empty or %v", err) - } - paths := "" - for _, r := range runfiles { - paths = paths + "\n" + r.Path + // {runfile directory} so that go_sdk is discoverable + // {runfile directory} is the parent directory of bazel.RunfilesPath() + runFilesPath, err := bazel.RunfilesPath() + if err != nil { + t.Error("failed to find runfiles path") } - runFilesDir = filepath.Dir(filepath.Clean(strings.TrimSuffix(runfiles[len(runfiles)-1].Path, runfiles[len(runfiles)-1].ShortPath))) - test_args := []string{ fmt.Sprintf("-out=%s", f.Name()), fmt.Sprintf("-sdk=%s", "go_sdk"), - fmt.Sprintf("-wd=%s", runFilesDir), - } - testws, err := bazel.TestWorkspace() - if err != nil { - t.Errorf("getting ws %v", err) + fmt.Sprintf("-wd=%s", filepath.Dir(filepath.Clean(runFilesPath))), } err = stdliblist(test_args) if err != nil { - t.Errorf("calling stdliblist got err: %v\n\nlong:%s\nshort:%s\ndetermined run file dir:%s\nbazel testWorkspace: %s\nallrunfilepaths:%s\n", - err, - // ls(filepath.Join(runFilesDir)), - // ls(filepath.Join(runFilesDir, os.Getenv("TEST_WORKSPACE"))), - runfiles[len(runfiles)-1].Path, - runfiles[len(runfiles)-1].ShortPath, - runFilesDir, - testws, - paths) + t.Errorf("calling stdliblist got err: %v", err) } scanner := bufio.NewScanner(f) for scanner.Scan() { @@ -76,16 +56,3 @@ func Test_stdliblist(t *testing.T) { } } } - -func ls(dir string) string { - files, err := ioutil.ReadDir(dir) - if err != nil { - log.Fatal(err) - } - - var buffer bytes.Buffer - for _, file := range files { - buffer.WriteString(fmt.Sprintln(file.Name(), file.IsDir())) - } - return buffer.String() -}