diff options
| author | Jonathan Amsterdam <jba@google.com> | 2022-01-12 12:27:39 -0500 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2022-01-13 12:13:38 +0000 |
| commit | de2b3089947146212b9ca3735befb3e2c93df6e7 (patch) | |
| tree | 937838ae32c67339e2601223d87033c0ca471c19 /internal/stdlib/stdlib_test.go | |
| parent | 5e4fb81f1c5c43697f71e4e7850925f852441b1d (diff) | |
| download | go-x-pkgsite-de2b3089947146212b9ca3735befb3e2c93df6e7.tar.xz | |
internal/stdlib: refactor go repo logic
Add a goRepo interface, implemented for remote, local and test repos.
Functions of stdlib use a global instance that interface instead of if
statements.
Also, improve and rewrite some tests.
For golang/go#50229
Change-Id: I73641813e6c7a6bb9667f44a672e37dce964b17a
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/378094
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
Diffstat (limited to 'internal/stdlib/stdlib_test.go')
| -rw-r--r-- | internal/stdlib/stdlib_test.go | 88 |
1 files changed, 58 insertions, 30 deletions
diff --git a/internal/stdlib/stdlib_test.go b/internal/stdlib/stdlib_test.go index 969be4f2..7cd43aa4 100644 --- a/internal/stdlib/stdlib_test.go +++ b/internal/stdlib/stdlib_test.go @@ -124,8 +124,7 @@ func TestMajorVersionForVersion(t *testing.T) { } func TestContentDir(t *testing.T) { - UseTestData = true - defer func() { UseTestData = false }() + defer WithTestData()() for _, resolvedVersion := range []string{ "v1.3.2", "v1.12.5", @@ -154,10 +153,6 @@ func TestContentDir(t *testing.T) { } func TestContentDirCloneAndOpen(t *testing.T) { - if !*clone && *repoPath == "" { - t.Skip("-clone and -path not supplied") - } - run := func(t *testing.T) { for _, resolvedVersion := range []string{ "v1.3.2", @@ -175,13 +170,24 @@ func TestContentDirCloneAndOpen(t *testing.T) { } } - if *clone { - t.Run("clone", run) - } - if *repoPath != "" { - SetGoRepoPath(*repoPath) - t.Run("open", run) - } + t.Run("clone", func(t *testing.T) { + if !*clone { + t.Skip("-clone not supplied") + } + defer withGoRepo(&remoteGoRepo{})() + run(t) + }) + t.Run("local", func(t *testing.T) { + if *repoPath == "" { + t.Skip("-path not supplied") + } + lgr, err := newLocalGoRepo(*repoPath) + if err != nil { + t.Fatal(err) + } + defer withGoRepo(lgr)() + run(t) + }) } func checkContentDirFiles(t *testing.T, cdir fs.FS, resolvedVersion string) { @@ -219,8 +225,7 @@ func checkContentDirFiles(t *testing.T, cdir fs.FS, resolvedVersion string) { } func TestZipInfo(t *testing.T) { - UseTestData = true - defer func() { UseTestData = false }() + defer WithTestData()() for _, tc := range []struct { requestedVersion string @@ -246,28 +251,51 @@ func TestZipInfo(t *testing.T) { } func TestVersions(t *testing.T) { - UseTestData = true - defer func() { UseTestData = false }() - - got, err := Versions() - if err != nil { - t.Fatal(err) - } - gotmap := map[string]bool{} - for _, g := range got { - gotmap[g] = true + testVersions := func(wants []string) { + got, err := Versions() + if err != nil { + t.Fatal(err) + } + gotmap := map[string]bool{} + for _, g := range got { + gotmap[g] = true + } + for _, w := range wants { + if !gotmap[w] { + t.Errorf("missing %s", w) + } + } } - wants := []string{ + + commonWants := []string{ "v1.4.2", "v1.9.0-rc.1", "v1.11.0", "v1.13.0-beta.1", } - for _, w := range wants { - if !gotmap[w] { - t.Errorf("missing %s", w) + otherWants := append([]string{"v1.17.6"}, commonWants...) + t.Run("test", func(t *testing.T) { + defer WithTestData()() + testVersions(commonWants) + }) + t.Run("local", func(t *testing.T) { + if *repoPath == "" { + t.Skip("-path not supplied") } - } + lgr, err := newLocalGoRepo(*repoPath) + if err != nil { + t.Fatal(err) + } + defer withGoRepo(lgr)() + testVersions(otherWants) + }) + t.Run("remote", func(t *testing.T) { + if !*clone { + t.Skip("-clone not supplied") + } + defer withGoRepo(&remoteGoRepo{})() + testVersions(otherWants) + }) } func TestVersionForTag(t *testing.T) { |
