Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix codegen config tests: add file closing #3037

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading