Skip to content

Commit

Permalink
refactor: simplify package setup in tests (#13)
Browse files Browse the repository at this point in the history
This PR simplifies package setup in tests by removing temporary
directory management and using
[`T.TempDir`](https://pkg.go.dev/testing#T.TempDir).
  • Loading branch information
alexandear authored Nov 21, 2024
1 parent 32ba7d4 commit dfe7244
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 31 deletions.
24 changes: 8 additions & 16 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ func main() {
}
}
`
tmpdir, pkgs := setupPackages(t, code)
defer teardownPackage(t, tmpdir)
pkgs := setupPackages(t, code)

errs := Run(pkgs, Config{DefaultSignifiesExhaustive: true})
assert.Equal(t, 1, len(errs))
Expand Down Expand Up @@ -57,8 +56,7 @@ func main() {
}
}
`
tmpdir, pkgs := setupPackages(t, code)
defer teardownPackage(t, tmpdir)
pkgs := setupPackages(t, code)

errs := Run(pkgs, Config{DefaultSignifiesExhaustive: true})
assert.Equal(t, 1, len(errs))
Expand Down Expand Up @@ -88,8 +86,7 @@ func main() {
}
}
`
tmpdir, pkgs := setupPackages(t, code)
defer teardownPackage(t, tmpdir)
pkgs := setupPackages(t, code)

errs := Run(pkgs, Config{DefaultSignifiesExhaustive: true})
assert.Equal(t, 1, len(errs))
Expand Down Expand Up @@ -119,8 +116,7 @@ func main() {
}
}
`
tmpdir, pkgs := setupPackages(t, code)
defer teardownPackage(t, tmpdir)
pkgs := setupPackages(t, code)

errs := Run(pkgs, Config{DefaultSignifiesExhaustive: true})
assert.Equal(t, 0, len(errs))
Expand Down Expand Up @@ -149,8 +145,7 @@ func main() {
}
}
`
tmpdir, pkgs := setupPackages(t, code)
defer teardownPackage(t, tmpdir)
pkgs := setupPackages(t, code)

errs := Run(pkgs, Config{DefaultSignifiesExhaustive: true})
assert.Equal(t, 0, len(errs))
Expand Down Expand Up @@ -179,8 +174,7 @@ func main() {
}
}
`
tmpdir, pkgs := setupPackages(t, code)
defer teardownPackage(t, tmpdir)
pkgs := setupPackages(t, code)

errs := Run(pkgs, Config{DefaultSignifiesExhaustive: false})
assert.Equal(t, 1, len(errs))
Expand All @@ -198,8 +192,7 @@ type T interface {}
func main() {}
`
tmpdir, pkgs := setupPackages(t, code)
defer teardownPackage(t, tmpdir)
pkgs := setupPackages(t, code)

errs := Run(pkgs, Config{DefaultSignifiesExhaustive: true})
assert.Equal(t, 1, len(errs))
Expand All @@ -217,8 +210,7 @@ type T struct {}
func main() {}
`
tmpdir, pkgs := setupPackages(t, code)
defer teardownPackage(t, tmpdir)
pkgs := setupPackages(t, code)

errs := Run(pkgs, Config{DefaultSignifiesExhaustive: true})
assert.Equal(t, 1, len(errs))
Expand Down
19 changes: 4 additions & 15 deletions help_test.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
package gochecksumtype

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"golang.org/x/tools/go/packages"
)

func setupPackages(t *testing.T, code string) (string, []*packages.Package) {
tmpdir, err := ioutil.TempDir("", "go-test-sumtype-")
if err != nil {
t.Fatal(err)
}
srcPath := filepath.Join(tmpdir, "src.go")
if err := ioutil.WriteFile(srcPath, []byte(code), 0600); err != nil {
func setupPackages(t *testing.T, code string) []*packages.Package {
srcPath := filepath.Join(t.TempDir(), "src.go")
if err := os.WriteFile(srcPath, []byte(code), 0600); err != nil {
t.Fatal(err)
}
pkgs, err := tycheckAll([]string{srcPath})
if err != nil {
t.Fatal(err)
}
return tmpdir, pkgs
}

func teardownPackage(t *testing.T, dir string) {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
return pkgs
}

func tycheckAll(args []string) ([]*packages.Package, error) {
Expand Down

0 comments on commit dfe7244

Please sign in to comment.