use TempFile though the testing is gross
This commit is contained in:
parent
fffd315a7e
commit
f68909b7a8
3 changed files with 50 additions and 26 deletions
|
|
@ -5,7 +5,6 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/cli/cli/pkg/iostreams"
|
||||
)
|
||||
|
|
@ -40,9 +39,17 @@ func PreserveInput(io *iostreams.IOStreams, state *IssueMetadataState, createErr
|
|||
return
|
||||
}
|
||||
|
||||
dp := dumpPath(time.Now().UnixNano())
|
||||
tmpfile, err := io.TempFile(os.TempDir(), "gh*.json")
|
||||
if err != nil {
|
||||
fmt.Fprintf(out, "failed to save input to file: %s\n", err)
|
||||
fmt.Fprintln(out, "would have saved:")
|
||||
fmt.Fprintf(out, "%v\n", state)
|
||||
return
|
||||
}
|
||||
|
||||
err = io.WriteFile(dp, data)
|
||||
tmpfilePath := filepath.Join(os.TempDir(), tmpfile.Name())
|
||||
|
||||
_, err = tmpfile.Write(data)
|
||||
if err != nil {
|
||||
fmt.Fprintf(out, "failed to save input to file: %s\n", err)
|
||||
fmt.Fprintln(out, "would have saved:")
|
||||
|
|
@ -57,8 +64,8 @@ func PreserveInput(io *iostreams.IOStreams, state *IssueMetadataState, createErr
|
|||
issueType = "issue"
|
||||
}
|
||||
|
||||
fmt.Fprintf(out, "%s operation failed. input saved to: %s\n", cs.FailureIcon(), dp)
|
||||
fmt.Fprintf(out, "resubmit with: gh %s create -j@%s\n", issueType, dp)
|
||||
fmt.Fprintf(out, "%s operation failed. input saved to: %s\n", cs.FailureIcon(), tmpfilePath)
|
||||
fmt.Fprintf(out, "resubmit with: gh %s create -j@%s\n", issueType, tmpfilePath)
|
||||
|
||||
// some whitespace before the actual error
|
||||
fmt.Fprintln(out)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package shared
|
|||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
|
@ -12,12 +13,6 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_dumpPath(t *testing.T) {
|
||||
// mostly pointless test
|
||||
var random int64 = 1234567890
|
||||
assert.Equal(t, dumpPath(random), filepath.Join(os.TempDir(), "gh602d2.json"))
|
||||
}
|
||||
|
||||
func Test_PreserveInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
@ -50,8 +45,8 @@ func Test_PreserveInput(t *testing.T) {
|
|||
Labels: []string{"sandwich"},
|
||||
},
|
||||
wantErrLines: []string{
|
||||
`X operation failed. input saved to:.*\.json`,
|
||||
`resubmit with: gh issue create -j@.*\.json`,
|
||||
`X operation failed. input saved to:.*testfile.*`,
|
||||
`resubmit with: gh issue create -j@.*testfile.*`,
|
||||
},
|
||||
err: true,
|
||||
wantPreservation: true,
|
||||
|
|
@ -63,8 +58,8 @@ func Test_PreserveInput(t *testing.T) {
|
|||
Labels: []string{"sandwich"},
|
||||
},
|
||||
wantErrLines: []string{
|
||||
`X operation failed. input saved to:.*\.json`,
|
||||
`resubmit with: gh issue create -j@.*\.json`,
|
||||
`X operation failed. input saved to:.*testfile.*`,
|
||||
`resubmit with: gh issue create -j@.*testfile.*`,
|
||||
},
|
||||
err: true,
|
||||
wantPreservation: true,
|
||||
|
|
@ -77,8 +72,8 @@ func Test_PreserveInput(t *testing.T) {
|
|||
Type: PRMetadata,
|
||||
},
|
||||
wantErrLines: []string{
|
||||
`X operation failed. input saved to:.*\.json`,
|
||||
`resubmit with: gh pr create -j@.*\.json`,
|
||||
`X operation failed. input saved to:.*testfile.*`,
|
||||
`resubmit with: gh pr create -j@.*testfile.*`,
|
||||
},
|
||||
err: true,
|
||||
wantPreservation: true,
|
||||
|
|
@ -90,8 +85,15 @@ func Test_PreserveInput(t *testing.T) {
|
|||
if tt.state == nil {
|
||||
tt.state = &IssueMetadataState{}
|
||||
}
|
||||
|
||||
io, _, _, errOut := iostreams.Test()
|
||||
io.WriteOverride = []byte{}
|
||||
|
||||
tfPath, tf, tferr := tmpfile()
|
||||
assert.NoError(t, tferr)
|
||||
defer os.Remove(tfPath)
|
||||
|
||||
io.TempFileOverride = tf
|
||||
|
||||
var err error
|
||||
if tt.err {
|
||||
err = errors.New("error during creation")
|
||||
|
|
@ -99,16 +101,31 @@ func Test_PreserveInput(t *testing.T) {
|
|||
|
||||
PreserveInput(io, tt.state, &err)()
|
||||
|
||||
tf.Seek(0, 0)
|
||||
|
||||
data, err := ioutil.ReadAll(tf)
|
||||
assert.NoError(t, err)
|
||||
|
||||
if tt.wantPreservation {
|
||||
test.ExpectLines(t, errOut.String(), tt.wantErrLines...)
|
||||
preserved := &IssueMetadataState{}
|
||||
assert.NoError(t, json.Unmarshal(io.WriteOverride, preserved))
|
||||
assert.NoError(t, json.Unmarshal(data, preserved))
|
||||
preserved.dirty = tt.state.dirty
|
||||
assert.Equal(t, preserved, tt.state)
|
||||
} else {
|
||||
assert.Equal(t, errOut.String(), "")
|
||||
assert.Equal(t, string(io.WriteOverride), "")
|
||||
assert.Equal(t, string(data), "")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func tmpfile() (string, *os.File, error) {
|
||||
dir := os.TempDir()
|
||||
tmpfile, err := ioutil.TempFile(dir, "testfile*")
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
return filepath.Join(dir, tmpfile.Name()), tmpfile, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ type IOStreams struct {
|
|||
|
||||
neverPrompt bool
|
||||
|
||||
WriteOverride []byte
|
||||
TempFileOverride *os.File
|
||||
}
|
||||
|
||||
func (s *IOStreams) ColorEnabled() bool {
|
||||
|
|
@ -270,12 +270,12 @@ func (s *IOStreams) ReadUserFile(fn string) ([]byte, error) {
|
|||
return ioutil.ReadAll(r)
|
||||
}
|
||||
|
||||
func (s *IOStreams) WriteFile(fn string, data []byte) error {
|
||||
if s.WriteOverride != nil {
|
||||
s.WriteOverride = data
|
||||
return nil
|
||||
func (s *IOStreams) TempFile(dir, pattern string) (*os.File, error) {
|
||||
if s.TempFileOverride != nil {
|
||||
fmt.Printf("DBG %#v\n", s.TempFileOverride)
|
||||
return s.TempFileOverride, nil
|
||||
}
|
||||
return ioutil.WriteFile(fn, data, 0660)
|
||||
return ioutil.TempFile(dir, pattern)
|
||||
}
|
||||
|
||||
func System() *IOStreams {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue