Skip to content

Commit

Permalink
final format
Browse files Browse the repository at this point in the history
  • Loading branch information
xytan0056 committed May 13, 2022
1 parent 597ad9e commit 98e60b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 48 deletions.
7 changes: 3 additions & 4 deletions go/tools/builders/stdliblist.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"flag"
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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.
Expand Down
55 changes: 11 additions & 44 deletions go/tools/builders/stdliblist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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()
}

0 comments on commit 98e60b7

Please sign in to comment.