Skip to content

Commit

Permalink
test: Fix openAndTemplate test
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Oct 6, 2023
1 parent 15659fd commit 01a5c33
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,21 @@ func Test_openAndTemplate(t *testing.T) {
inplaceConf := config.New()
inplaceConf.Inplace = true

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

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

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

return f.Name(), nil
}

type args struct {
Expand All @@ -161,17 +162,19 @@ func Test_openAndTemplate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, cleanup, err := tempFileWith(tt.args.contents)
p, err := tempFileWith(tt.args.contents)
if !assert.NoError(t, err) {
return
}
defer cleanup()
defer func() {
_ = os.RemoveAll(p)
}()

cmd := NewCommand("", "")
var stdoutBuf strings.Builder
cmd.SetOut(&stdoutBuf)

err = openAndTemplate(cmd, tt.args.conf, f.Name())
err = openAndTemplate(cmd, tt.args.conf, p)
if tt.wantErr {
if !assert.Error(t, err) {
return
Expand All @@ -182,9 +185,13 @@ func Test_openAndTemplate(t *testing.T) {
}
}

if _, err := f.Seek(0, io.SeekStart); !assert.NoError(t, err) {
f, err := os.Open(p)
if !assert.NoError(t, err) {
return
}
defer func(f *os.File) {
_ = f.Close()
}(f)

var buf strings.Builder
if _, err := io.Copy(&buf, f); !assert.NoError(t, err) {
Expand Down

0 comments on commit 01a5c33

Please sign in to comment.