updating tests WIP

This commit is contained in:
Gowtham Munukutla 2021-02-24 09:44:11 +05:30
parent a60a6d854b
commit a6fa14866b
2 changed files with 9 additions and 106 deletions

View file

@ -7,7 +7,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
@ -121,8 +120,6 @@ func editRun(opts *EditOptions) error {
return err
}
fmt.Printf("%v", files)
gist.Files = files
err = updateGist(apiClient, ghinstance.OverridableDefault(), gist)
if err != nil {
@ -263,70 +260,22 @@ func updateGist(apiClient *api.Client, hostname string, gist *shared.Gist) error
func getFilesToAdd(file string, opts *EditOptions) (map[string]*shared.GistFile, error) {
cs := opts.IO.ColorScheme()
fileExists, err := fileExists(file)
if err != nil {
return nil, fmt.Errorf("%s %s", cs.Red("!"), err)
}
filesToAdd := map[string]*shared.GistFile{}
filename := filepath.Base(file)
if fileExists {
content, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("%s failed to read file %s: %w", cs.FailureIcon(), file, err)
}
if string(content) == "" {
return nil, fmt.Errorf("%s Contents can't be empty", cs.FailureIcon())
}
filesToAdd[filename] = &shared.GistFile{
Filename: filename,
Content: string(content),
}
return filesToAdd, nil
} else {
editorCommand, err := cmdutil.DetermineEditor(opts.Config)
if err != nil {
return nil, err
}
text, err := opts.Add(editorCommand, filename, opts.IO)
if err != nil {
return nil, err
}
if text == "" {
return nil, fmt.Errorf("%s Contents can't be empty", cs.Red("!"))
}
filesToAdd[filename] = &shared.GistFile{
Filename: filename,
Content: text,
}
return filesToAdd, nil
}
}
func fileExists(filename string) (bool, error) {
fi, err := os.Stat(filename)
content, err := ioutil.ReadFile(file)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return nil, fmt.Errorf("%s failed to read file %s: %w", cs.FailureIcon(), file, err)
}
switch mode := fi.Mode(); {
case mode.IsDir():
return false, fmt.Errorf("found directory %s", filename)
case mode.IsRegular():
return true, nil
if string(content) == "" {
return nil, fmt.Errorf("%s Contents can't be empty", cs.FailureIcon())
}
return false, nil
filesToAdd[filename] = &shared.GistFile{
Filename: filename,
Content: string(content),
}
return filesToAdd, nil
}

View file

@ -20,22 +20,8 @@ import (
const (
fixtureFile = "../fixture.txt"
nonExistentFile = "../file.txt"
)
func Test_fileExists(t *testing.T) {
fixtureFileExists, err := fileExists(fixtureFile)
if err != nil {
t.Fatalf("unexpected error processing files: %s", err)
}
assert.Equal(t, true, fixtureFileExists)
neFileExists, err := fileExists(nonExistentFile)
assert.Equal(t, nil, err)
assert.Equal(t, false, neFileExists)
}
func Test_getFilesToAdd(t *testing.T) {
gf, err := getFilesToAdd(fixtureFile, &EditOptions{
AddFilename: fixtureFile,
@ -256,38 +242,6 @@ func Test_editRun(t *testing.T) {
wantErr: true,
wantStderr: "You do not own this gist.",
},
{
name: "add file to existing gist",
gist: &shared.Gist{
ID: "1234",
Files: map[string]*shared.GistFile{
"sample.txt": {
Filename: "sample.txt",
Content: "bwhiizzzbwhuiiizzzz",
Type: "text/plain",
},
},
Owner: &shared.GistOwner{Login: "octocat"},
},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("POST", "gists/1234"),
httpmock.StatusStringResponse(201, "{}"))
},
opts: &EditOptions{
AddFilename: "foo.txt",
},
wantParams: map[string]interface{}{
"description": "",
"updated_at": "0001-01-01T00:00:00Z",
"public": false,
"files": map[string]interface{}{
"foo.txt": map[string]interface{}{
"content": "new content to existing gist",
"filename": "foo.txt",
},
},
},
},
{
name: "add file to existing gist with absolute path",
gist: &shared.Gist{