Skip to content

Commit a6fa148

Browse files
committed
updating tests WIP
1 parent a60a6d8 commit a6fa148

File tree

2 files changed

+9
-106
lines changed

2 files changed

+9
-106
lines changed

pkg/cmd/gist/edit/edit.go

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"io/ioutil"
99
"net/http"
10-
"os"
1110
"path/filepath"
1211
"sort"
1312
"strings"
@@ -121,8 +120,6 @@ func editRun(opts *EditOptions) error {
121120
return err
122121
}
123122

124-
fmt.Printf("%v", files)
125-
126123
gist.Files = files
127124
err = updateGist(apiClient, ghinstance.OverridableDefault(), gist)
128125
if err != nil {
@@ -263,70 +260,22 @@ func updateGist(apiClient *api.Client, hostname string, gist *shared.Gist) error
263260
func getFilesToAdd(file string, opts *EditOptions) (map[string]*shared.GistFile, error) {
264261
cs := opts.IO.ColorScheme()
265262

266-
fileExists, err := fileExists(file)
267-
if err != nil {
268-
return nil, fmt.Errorf("%s %s", cs.Red("!"), err)
269-
}
270-
271263
filesToAdd := map[string]*shared.GistFile{}
272264

273265
filename := filepath.Base(file)
274266

275-
if fileExists {
276-
content, err := ioutil.ReadFile(file)
277-
if err != nil {
278-
return nil, fmt.Errorf("%s failed to read file %s: %w", cs.FailureIcon(), file, err)
279-
}
280-
281-
if string(content) == "" {
282-
return nil, fmt.Errorf("%s Contents can't be empty", cs.FailureIcon())
283-
}
284-
285-
filesToAdd[filename] = &shared.GistFile{
286-
Filename: filename,
287-
Content: string(content),
288-
}
289-
return filesToAdd, nil
290-
} else {
291-
editorCommand, err := cmdutil.DetermineEditor(opts.Config)
292-
if err != nil {
293-
return nil, err
294-
}
295-
296-
text, err := opts.Add(editorCommand, filename, opts.IO)
297-
if err != nil {
298-
return nil, err
299-
}
300-
301-
if text == "" {
302-
return nil, fmt.Errorf("%s Contents can't be empty", cs.Red("!"))
303-
}
304-
305-
filesToAdd[filename] = &shared.GistFile{
306-
Filename: filename,
307-
Content: text,
308-
}
309-
310-
return filesToAdd, nil
311-
}
312-
}
313-
314-
func fileExists(filename string) (bool, error) {
315-
316-
fi, err := os.Stat(filename)
317-
267+
content, err := ioutil.ReadFile(file)
318268
if err != nil {
319-
if os.IsNotExist(err) {
320-
return false, nil
321-
}
269+
return nil, fmt.Errorf("%s failed to read file %s: %w", cs.FailureIcon(), file, err)
322270
}
323271

324-
switch mode := fi.Mode(); {
325-
case mode.IsDir():
326-
return false, fmt.Errorf("found directory %s", filename)
327-
case mode.IsRegular():
328-
return true, nil
272+
if string(content) == "" {
273+
return nil, fmt.Errorf("%s Contents can't be empty", cs.FailureIcon())
329274
}
330275

331-
return false, nil
276+
filesToAdd[filename] = &shared.GistFile{
277+
Filename: filename,
278+
Content: string(content),
279+
}
280+
return filesToAdd, nil
332281
}

pkg/cmd/gist/edit/edit_test.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,8 @@ import (
2020

2121
const (
2222
fixtureFile = "../fixture.txt"
23-
nonExistentFile = "../file.txt"
2423
)
2524

26-
func Test_fileExists(t *testing.T) {
27-
fixtureFileExists, err := fileExists(fixtureFile)
28-
if err != nil {
29-
t.Fatalf("unexpected error processing files: %s", err)
30-
}
31-
32-
assert.Equal(t, true, fixtureFileExists)
33-
34-
neFileExists, err := fileExists(nonExistentFile)
35-
assert.Equal(t, nil, err)
36-
assert.Equal(t, false, neFileExists)
37-
}
38-
3925
func Test_getFilesToAdd(t *testing.T) {
4026
gf, err := getFilesToAdd(fixtureFile, &EditOptions{
4127
AddFilename: fixtureFile,
@@ -256,38 +242,6 @@ func Test_editRun(t *testing.T) {
256242
wantErr: true,
257243
wantStderr: "You do not own this gist.",
258244
},
259-
{
260-
name: "add file to existing gist",
261-
gist: &shared.Gist{
262-
ID: "1234",
263-
Files: map[string]*shared.GistFile{
264-
"sample.txt": {
265-
Filename: "sample.txt",
266-
Content: "bwhiizzzbwhuiiizzzz",
267-
Type: "text/plain",
268-
},
269-
},
270-
Owner: &shared.GistOwner{Login: "octocat"},
271-
},
272-
httpStubs: func(reg *httpmock.Registry) {
273-
reg.Register(httpmock.REST("POST", "gists/1234"),
274-
httpmock.StatusStringResponse(201, "{}"))
275-
},
276-
opts: &EditOptions{
277-
AddFilename: "foo.txt",
278-
},
279-
wantParams: map[string]interface{}{
280-
"description": "",
281-
"updated_at": "0001-01-01T00:00:00Z",
282-
"public": false,
283-
"files": map[string]interface{}{
284-
"foo.txt": map[string]interface{}{
285-
"content": "new content to existing gist",
286-
"filename": "foo.txt",
287-
},
288-
},
289-
},
290-
},
291245
{
292246
name: "add file to existing gist with absolute path",
293247
gist: &shared.Gist{

0 commit comments

Comments
 (0)