Permalink
Browse files

Merge TestGenerateErrorOnInputError into TestGenerate_buildAndGofmt.

This is simpler. It keeps all the Generate test cases closer together,
making them easier to read and understand.
  • Loading branch information...
dmitshur committed Sep 9, 2018
1 parent b0359c4 commit b28edef6e7979be7e55162f9f5bb5b9d3e3336db
Showing with 19 additions and 20 deletions.
  1. +19 −20 generator_test.go
View
@@ -46,14 +46,22 @@ func TestGenerate_buildAndGofmt(t *testing.T) {
}()
tests := []struct {
filename string
fs http.FileSystem
filename string
fs http.FileSystem
wantError func(error) bool // Nil function means want nil error.
}{
{
// Empty.
filename: "empty.go",
fs: union.New(nil),
},
{
// Test that vfsgen.Generate returns an error when there is
// an error reading from the input filesystem.
filename: "notexist.go",
fs: http.Dir("notexist"),
wantError: os.IsNotExist,
},
{
// No compressed files.
filename: "nocompressed.go",
@@ -81,12 +89,18 @@ func TestGenerate_buildAndGofmt(t *testing.T) {
for _, test := range tests {
filename := filepath.Join(tempDir, test.filename)
err = vfsgen.Generate(test.fs, vfsgen.Options{
err := vfsgen.Generate(test.fs, vfsgen.Options{
Filename: filename,
PackageName: "test",
})
if err != nil {
t.Fatalf("vfsgen.Generate() failed: %v", err)
switch {
case test.wantError == nil && err != nil:
t.Fatalf("%s: vfsgen.Generate returned non-nil error: %v", test.filename, err)
case test.wantError != nil && !test.wantError(err):
t.Fatalf("%s: vfsgen.Generate returned wrong error: %v", test.filename, err)
}
if test.wantError != nil {
continue
}
if out, err := exec.Command("go", "build", filename).CombinedOutput(); err != nil {
@@ -97,18 +111,3 @@ func TestGenerate_buildAndGofmt(t *testing.T) {
}
}
}
// TestGenerateErrorOnInputError tests that vfsgen.Generate returns an error
// when there is an error reading from the input filesystem.
func TestGenerateErrorOnInputError(t *testing.T) {
badFS := http.Dir("notexist")
err := vfsgen.Generate(badFS, vfsgen.Options{
Filename: filepath.Join(os.TempDir(), "test_vfsdata.go"),
})
if err == nil {
t.Fatal("got error: nil, want: non-nil")
}
if got, want := os.IsNotExist(err), true; got != want {
t.Errorf("got os.IsNotExist(err): %v, want: %v", got, want)
}
}

0 comments on commit b28edef

Please sign in to comment.