Skip to content

Commit

Permalink
test(cmd): Clean up temp file creation func
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Sep 20, 2024
1 parent f1ae7d4 commit adf063a
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,18 @@ func Test_openAndTemplateFile(t *testing.T) {
inplaceConf := config.New()
inplaceConf.Inplace = true

tempFileWith := func(contents string) (string, error) {
tempFile := func(t *testing.T, contents string) string {
f, err := os.CreateTemp("", "")
if err != nil {
return f.Name(), err
}

if _, err := f.WriteString(contents); err != nil {
return f.Name(), err
}

if err := f.Close(); err != nil {
return f.Name(), err
}
require.NoError(t, err)
t.Cleanup(func() {
_ = f.Close()
_ = os.Remove(f.Name())
})

return f.Name(), nil
_, err = f.WriteString(contents)
require.NoError(t, err)
require.NoError(t, f.Close())
return f.Name()
}

type args struct {
Expand All @@ -168,15 +165,10 @@ func Test_openAndTemplateFile(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p, err := tempFileWith(tt.args.contents)
t.Cleanup(func() {
_ = os.RemoveAll(p)
})
require.NoError(t, err)
p := tempFile(t, tt.args.contents)

var stdoutBuf strings.Builder
err = openAndTemplateFile(tt.args.conf, &stdoutBuf, p, p, false)
tt.wantErr(t, err)
tt.wantErr(t, openAndTemplateFile(tt.args.conf, &stdoutBuf, p, p, false))

fileContents, err := os.ReadFile(p)
require.NoError(t, err)
Expand Down

0 comments on commit adf063a

Please sign in to comment.