Skip to content

Commit

Permalink
Fix codegen config tests: add file closing (#3037)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored May 2, 2024
1 parent 293991e commit 4b559b3
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,25 @@ func TestReadConfig(t *testing.T) {
})

t.Run("malformed config", func(t *testing.T) {
cfgFile, _ := os.Open("testdata/cfg/malformedconfig.yml")
_, err := ReadConfig(cfgFile)
cfgFile, err := os.Open("testdata/cfg/malformedconfig.yml")
require.NoError(t, err)
t.Cleanup(func() { _ = cfgFile.Close() })
_, err = ReadConfig(cfgFile)
require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `asdf` into config.Config")
})

t.Run("unknown keys", func(t *testing.T) {
cfgFile, _ := os.Open("testdata/cfg/unknownkeys.yml")
_, err := ReadConfig(cfgFile)
cfgFile, err := os.Open("testdata/cfg/unknownkeys.yml")
require.NoError(t, err)
t.Cleanup(func() { _ = cfgFile.Close() })
_, err = ReadConfig(cfgFile)
require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 2: field unknown not found in type config.Config")
})

t.Run("globbed filenames", func(t *testing.T) {
cfgFile, _ := os.Open("testdata/cfg/glob.yml")
cfgFile, err := os.Open("testdata/cfg/glob.yml")
require.NoError(t, err)
t.Cleanup(func() { _ = cfgFile.Close() })
c, err := ReadConfig(cfgFile)
require.NoError(t, err)

Expand All @@ -57,8 +63,10 @@ func TestReadConfig(t *testing.T) {
})

t.Run("unwalkable path", func(t *testing.T) {
cfgFile, _ := os.Open("testdata/cfg/unwalkable.yml")
_, err := ReadConfig(cfgFile)
cfgFile, err := os.Open("testdata/cfg/unwalkable.yml")
require.NoError(t, err)
t.Cleanup(func() { _ = cfgFile.Close() })
_, err = ReadConfig(cfgFile)
if runtime.GOOS == "windows" {
require.EqualError(t, err, "failed to walk schema at root not_walkable/: CreateFile not_walkable/: The system cannot find the file specified.")
} else {
Expand Down

0 comments on commit 4b559b3

Please sign in to comment.