MCPcopy Create free account
hub / github.com/cli/cli / TestCmdWrite

Function TestCmdWrite

script/ghmock/gitcmd_test.go:126–166  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

124}
125
126func TestCmdWrite(t *testing.T) {
127 tests := []struct {
128 name string
129 old string
130 text string
131 link bool
132 }{
133 {name: "new directories and file", text: "one\n"},
134 {name: "replace contents", old: "one\n", text: "two\n"},
135 {name: "empty file", old: "one\n"},
136 {name: "refuse an outside symlink", text: "changed", link: true},
137 }
138 for _, tt := range tests {
139 t.Run(tt.name, func(t *testing.T) {
140 t.Setenv("GHMOCK_DIR", t.TempDir())
141 st, err := openState()
142 require.NoError(t, err)
143 world, err := st.ensureGitFixture()
144 require.NoError(t, err)
145 path := filepath.Join(world, "work", "main", "a.txt")
146 if tt.old != "" {
147 require.NoError(t, cmdWrite([]string{"main/a.txt", "--text", tt.old}, io.Discard))
148 }
149 if tt.link {
150 require.NoError(t, os.Symlink(t.TempDir(), filepath.Join(world, "work", "main")))
151 }
152 var out bytes.Buffer
153 err = cmdWrite([]string{"main/a.txt", "--text", tt.text}, &out)
154 if tt.link {
155 require.Error(t, err)
156 assert.Empty(t, out.String())
157 return
158 }
159 require.NoError(t, err)
160 data, err := os.ReadFile(path)
161 require.NoError(t, err)
162 assert.Equal(t, tt.text, string(data))
163 assert.Equal(t, fmt.Sprintf("Wrote main/a.txt (%d bytes)\n", len(tt.text)), out.String())
164 })
165 }
166}
167
168func TestParseGitArgs(t *testing.T) {
169 tests := []struct {

Callers

nothing calls this directly

Calls 9

openStateFunction · 0.85
cmdWriteFunction · 0.85
ensureGitFixtureMethod · 0.80
JoinMethod · 0.80
EmptyMethod · 0.80
EqualMethod · 0.80
RunMethod · 0.65
StringMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected