Skip to content

Commit

Permalink
test: check for unexpected files
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Jul 15, 2024
1 parent 63e3c13 commit f29eb68
Showing 1 changed file with 50 additions and 25 deletions.
75 changes: 50 additions & 25 deletions pkg/recipe/saver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,17 @@ func TestSaveRecipe(t *testing.T) {
}

expectedFiles := []string{
filepath.Join(dir, RecipeFileName+YAMLExtension),
filepath.Join(dir, "templates", "foo.md"),
filepath.Join(dir, "templates", "foo", "bar.md"),
filepath.Join(dir, "templates", "foo", "bar", "baz.md"),
filepath.Join(dir, "tests", re.Tests[0].Name, RecipeTestMetaFileName+YAMLExtension),
filepath.Join(dir, "tests", re.Tests[0].Name, RecipeTestFilesDirName, "foo.md"),
filepath.Join(dir, "tests", re.Tests[0].Name, RecipeTestFilesDirName, "foo", "bar.md"),
filepath.Join(dir, "tests", re.Tests[0].Name, RecipeTestFilesDirName, "foo", "bar", "baz.md"),
}

// TODO: check that these are _only_ files existing
for _, expectedFile := range expectedFiles {
if _, err := os.Stat(expectedFile); os.IsNotExist(err) {
t.Fatalf("expected file '%s' did not exist", expectedFile)
}
filepath.Join(RecipeFileName + YAMLExtension),
filepath.Join("templates", "foo.md"),
filepath.Join("templates", "foo", "bar.md"),
filepath.Join("templates", "foo", "bar", "baz.md"),
filepath.Join("tests", re.Tests[0].Name, RecipeTestMetaFileName+YAMLExtension),
filepath.Join("tests", re.Tests[0].Name, RecipeTestFilesDirName, "foo.md"),
filepath.Join("tests", re.Tests[0].Name, RecipeTestFilesDirName, "foo", "bar.md"),
filepath.Join("tests", re.Tests[0].Name, RecipeTestFilesDirName, "foo", "bar", "baz.md"),
}

checkFiles(t, dir, expectedFiles)
}

func TestSaveSauce(t *testing.T) {
Expand Down Expand Up @@ -105,18 +100,13 @@ func TestSaveSauce(t *testing.T) {
}

expectedFiles := []string{
filepath.Join(dir, SauceDirName, SaucesFileName+YAMLExtension),
filepath.Join(dir, "foo.md"),
filepath.Join(dir, "foo", "bar.md"),
filepath.Join(dir, "foo", "bar", "baz.md"),
filepath.Join(SauceDirName, SaucesFileName+YAMLExtension),
filepath.Join("foo.md"),
filepath.Join("foo", "bar.md"),
filepath.Join("foo", "bar", "baz.md"),
}

// TODO: check that these are _only_ files existing
for _, expectedFile := range expectedFiles {
if _, err := os.Stat(expectedFile); os.IsNotExist(err) {
t.Fatalf("expected file '%s' did not exist", expectedFile)
}
}
checkFiles(t, dir, expectedFiles)
}

func TestSaveSauceDoesNotWriteOutsideDest(t *testing.T) {
Expand Down Expand Up @@ -152,3 +142,38 @@ func TestSaveSauceDoesNotWriteOutsideDest(t *testing.T) {
t.Fatalf("error received was not expected: %s", err)
}
}

func checkFiles(t *testing.T, dir string, expectedFiles []string) {
files := make(map[string]bool, len(expectedFiles))
for _, file := range expectedFiles {
files[filepath.Join(dir, file)] = false
}

err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
return nil
}

if _, found := files[path]; !found {
t.Fatalf("unexpected file '%s' found", path)
} else {
files[path] = true
}

return nil
})

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

for file, found := range files {
if !found {
t.Fatalf("expected file '%s' not found", file)
}
}
}

0 comments on commit f29eb68

Please sign in to comment.