Skip to content

Commit

Permalink
Merge pull request #3 from gostaticanalysis/add-fs
Browse files Browse the repository at this point in the history
Add WriteFilesFS
  • Loading branch information
tenntenn committed Mar 21, 2021
2 parents 1ddce8d + 694fefd commit 98f62c2
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 6 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/test.yml → .github/workflows/testandvet.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test
name: Test and Vet

on:
push:
Expand All @@ -25,7 +25,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.15.x
go-version: 1.16.x

- name: Checkout code
uses: actions/checkout@v2
Expand All @@ -39,7 +39,13 @@ jobs:
restore-keys: |
go-
- name: Install tennvet
run: |
GOBIN=$(pwd) go install github.com/tenntenn/tennvet@latest
- name: Test and vet
run: |
go vet ./...
go vet -vettool=$(pwd)/tennvet ./...
go test -v -race ./...
43 changes: 40 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package testutil

import (
"testing"
"io/fs"

"golang.org/x/tools/go/analysis/analysistest"
)
Expand All @@ -12,8 +12,9 @@ import (
// and populates it with a GOPATH-style project using filemap (which
// maps file names to contents).
//
// On success it returns the name of the directory and a cleanup function to delete it.
func WriteFiles(t *testing.T, filemap map[string]string) string {
// On success it returns the name of the directory.
// The directory will be deleted by t.Cleanup.
func WriteFiles(t TestingT, filemap map[string]string) string {
t.Helper()
dir, clean, err := analysistest.WriteFiles(filemap)
if err != nil {
Expand All @@ -22,3 +23,39 @@ func WriteFiles(t *testing.T, filemap map[string]string) string {
t.Cleanup(clean)
return dir
}

// WriteFiles wrapper of analysistest.WriteFiles.
//
// WriteFiles is a helper function that creates a temporary directory
// and populates it with a GOPATH-style project using fs.FS.
//
// On success it returns the name of the directory.
// The directory will be deleted by t.Cleanup.
func WriteFilesFS(t TestingT, fsys fs.FS) string {
t.Helper()
filemap := make(map[string]string)
err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
return nil
}

data, err := fs.ReadFile(fsys, path)
if err != nil {
return err
}

filemap[path] = string(data)

return nil
})

if err != nil {
t.Fatalf("unexpected error: %v", err)
}

return WriteFiles(t, filemap)
}
64 changes: 64 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package testutil_test

import (
"os"
"path"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/gostaticanalysis/testutil"
"github.com/josharian/txtarfs"
"golang.org/x/tools/txtar"
)

func TestWriteFilesFS(t *testing.T) {
t.Parallel()

cases := map[string]struct {
files string
fatal bool
}{
"single": {"-- a.txt --\nhello", false},
"multi": {"-- a.txt --\nhello\n-- b.txt --\ngophers", false},
"nested": {"-- a.txt --\nhello\n-- b/b.txt --\ngophers", false},
}

for name, tt := range cases {
tt := tt
t.Run(name, func(t *testing.T) {
var mt MockT
a := txtar.Parse([]byte(tt.files))
fsys := txtarfs.As(a)

dir := testutil.WriteFilesFS(&mt, fsys)

switch {
case tt.fatal && !mt.IsFatal:
t.Fatal("expected fatal does not occur")
case !tt.fatal && mt.IsFatal:
t.Fatal("unexpected fatal")
}

gotA, err := txtarfs.From(os.DirFS(dir))
if err != nil {
t.Fatal("unexpected error:", err)
}
got := string(txtar.Format(gotA))

wantA := &txtar.Archive{
Files: make([]txtar.File, len(a.Files)),
}
for i := range wantA.Files {
wantA.Files[i] = txtar.File{
Name: path.Join("src", a.Files[i].Name),
Data: a.Files[i].Data,
}
}
want := string(txtar.Format(wantA))

if diff := cmp.Diff(want, got); diff != "" {
t.Error(diff)
}
})
}
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module github.com/gostaticanalysis/testutil

go 1.15
go 1.16

require (
github.com/google/go-cmp v0.5.2
github.com/josharian/txtarfs v0.0.0-20210218200122-0702f000015a
github.com/otiai10/copy v1.2.0
github.com/tenntenn/modver v1.0.1
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/josharian/txtarfs v0.0.0-20210218200122-0702f000015a h1:8NZHLa6Gp0hW6xJ0c3F1Kse7dJw30fOcDzHuF9sLbnE=
github.com/josharian/txtarfs v0.0.0-20210218200122-0702f000015a/go.mod h1:izVPOvVRsHiKkeGCT6tYBNWyDVuzj9wAaBb5R9qamfw=
github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k=
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
Expand All @@ -18,6 +20,7 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1 h1:Kvvh58BN8Y9/lBi7hTekvtMpm07eUZ0ck5pRHpsMWrY=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand All @@ -28,6 +31,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -36,6 +40,7 @@ golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.1-0.20210205202024-ef80cdb6ec6d h1:TWciQgVQK3rhquuBn8vdgrXzMaeTaRd/DVJyTONxE7I=
golang.org/x/tools v0.1.1-0.20210205202024-ef80cdb6ec6d/go.mod h1:9bzcO0MWcOuT0tm1iBGzDVPshzfwoVvREIui8C+MHqU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
5 changes: 5 additions & 0 deletions mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ func LatestVersion(t *testing.T, module string, max int) []ModuleVersion {
// Replacing os.Stderr is not thread safe.
// If you want to turn off replacing os.Stderr, you can use ReplaceStderr(false).
func RunWithVersions(t *testing.T, dir string, a *analysis.Analyzer, vers []ModuleVersion, pkg string) map[ModuleVersion][]*analysistest.Result {
t.Helper()

path := filepath.Join(dir, "src", pkg)

results := make(map[ModuleVersion][]*analysistest.Result, len(vers))
Expand All @@ -200,6 +202,7 @@ func RunWithVersions(t *testing.T, dir string, a *analysis.Analyzer, vers []Modu
}

func execCmd(t *testing.T, dir, cmd string, args ...string) io.Reader {
t.Helper()
var stdout, stderr bytes.Buffer
_cmd := exec.Command(cmd, args...)
_cmd.Stdout = &stdout
Expand All @@ -225,6 +228,8 @@ func ReplaceStderr(onoff bool) {
}

func replaceStderr(t *testing.T, old, new string) {
t.Helper()

stderrMutex.RLock()
ok := !doNotUseFilteredStderr
stderrMutex.RUnlock()
Expand Down
8 changes: 8 additions & 0 deletions testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package testutil

type TestingT interface {
Cleanup(func())
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
Helper()
}
15 changes: 15 additions & 0 deletions testing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package testutil_test

import "github.com/gostaticanalysis/testutil"

type MockT struct {
IsErr bool
IsFatal bool
}

func (t *MockT) Cleanup(_ func()) {}
func (t *MockT) Errorf(_ string, _ ...interface{}) { t.IsErr = true }
func (t *MockT) Fatalf(_ string, _ ...interface{}) { t.IsFatal = true }
func (t *MockT) Helper() {}

var _ testutil.TestingT = (*MockT)(nil)

0 comments on commit 98f62c2

Please sign in to comment.