forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreserve_test.go
More file actions
123 lines (109 loc) · 2.69 KB
/
preserve_test.go
File metadata and controls
123 lines (109 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package shared
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"testing"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/test"
"github.com/stretchr/testify/assert"
)
func Test_PreserveInput(t *testing.T) {
tests := []struct {
name string
state *IssueMetadataState
err bool
wantErrLine string
wantPreservation bool
}{
{
name: "err, no changes to state",
err: true,
},
{
name: "no err, no changes to state",
err: false,
},
{
name: "no err, changes to state",
state: &IssueMetadataState{
dirty: true,
},
},
{
name: "err, title/body input received",
state: &IssueMetadataState{
dirty: true,
Title: "almost a",
Body: "jill sandwich",
Reviewers: []string{"barry", "chris"},
Labels: []string{"sandwich"},
},
wantErrLine: `X operation failed. To restore: gh issue create --recover .*testfile.*`,
err: true,
wantPreservation: true,
},
{
name: "err, metadata received",
state: &IssueMetadataState{
Reviewers: []string{"barry", "chris"},
Labels: []string{"sandwich"},
},
wantErrLine: `X operation failed. To restore: gh issue create --recover .*testfile.*`,
err: true,
wantPreservation: true,
},
{
name: "err, dirty, pull request",
state: &IssueMetadataState{
dirty: true,
Title: "a pull request",
Type: PRMetadata,
},
wantErrLine: `X operation failed. To restore: gh pr create --recover .*testfile.*`,
err: true,
wantPreservation: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.state == nil {
tt.state = &IssueMetadataState{}
}
io, _, _, errOut := iostreams.Test()
tf, tferr := tmpfile()
assert.NoError(t, tferr)
defer os.Remove(tf.Name())
io.TempFileOverride = tf
var err error
if tt.err {
err = errors.New("error during creation")
}
PreserveInput(io, tt.state, &err)()
_, err = tf.Seek(0, 0)
assert.NoError(t, err)
data, err := ioutil.ReadAll(tf)
assert.NoError(t, err)
if tt.wantPreservation {
//nolint:staticcheck // prefer exact matchers over ExpectLines
test.ExpectLines(t, errOut.String(), tt.wantErrLine)
preserved := &IssueMetadataState{}
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(data), "")
}
})
}
}
func tmpfile() (*os.File, error) {
dir := os.TempDir()
tmpfile, err := ioutil.TempFile(dir, "testfile*")
if err != nil {
return nil, err
}
return tmpfile, nil
}