-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from gostaticanalysis/add-fs
Add WriteFilesFS
- Loading branch information
Showing
8 changed files
with
148 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |