"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "pkg/cmd/extensions/command_test.go" between
gh-cli-1.12.1.tar.gz and gh-cli-1.13.1.tar.gz

About: GitHub CLI is GitHub’s official command line tool.

command_test.go  (gh-cli-1.12.1):command_test.go  (gh-cli-1.13.1)
skipping to change at line 14 skipping to change at line 14
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
"testing" "testing"
"github.com/cli/cli/internal/config" "github.com/cli/cli/internal/config"
"github.com/cli/cli/pkg/cmdutil" "github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/extensions" "github.com/cli/cli/pkg/extensions"
"github.com/cli/cli/pkg/iostreams" "github.com/cli/cli/pkg/iostreams"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestNewCmdExtensions(t *testing.T) { func TestNewCmdExtensions(t *testing.T) {
tempDir := t.TempDir() tempDir := t.TempDir()
oldWd, _ := os.Getwd() oldWd, _ := os.Getwd()
assert.NoError(t, os.Chdir(tempDir)) assert.NoError(t, os.Chdir(tempDir))
t.Cleanup(func() { _ = os.Chdir(oldWd) }) t.Cleanup(func() { _ = os.Chdir(oldWd) })
tests := []struct { tests := []struct {
name string name string
args []string args []string
managerStubs func(em *extensions.ExtensionManagerMock) func(*test ing.T) managerStubs func(em *extensions.ExtensionManagerMock) func(*test ing.T)
isTTY bool
wantErr bool wantErr bool
errMsg string
wantStdout string wantStdout string
wantStderr string wantStderr string
}{ }{
{ {
name: "install an extension", name: "install an extension",
args: []string{"install", "owner/gh-some-ext"}, args: []string{"install", "owner/gh-some-ext"},
managerStubs: func(em *extensions.ExtensionManagerMock) f unc(*testing.T) { managerStubs: func(em *extensions.ExtensionManagerMock) f unc(*testing.T) {
em.ListFunc = func() []extensions.Extension {
return []extensions.Extension{}
}
em.InstallFunc = func(s string, out, errOut io.Wr iter) error { em.InstallFunc = func(s string, out, errOut io.Wr iter) error {
return nil return nil
} }
return func(t *testing.T) { return func(t *testing.T) {
calls := em.InstallCalls() installCalls := em.InstallCalls()
assert.Equal(t, 1, len(installCalls))
assert.Equal(t, "https://github.com/owner
/gh-some-ext.git", installCalls[0].URL)
listCalls := em.ListCalls()
assert.Equal(t, 1, len(listCalls))
}
},
},
{
name: "install an extension with same name as existing ex
tension",
args: []string{"install", "owner/gh-existing-ext"},
managerStubs: func(em *extensions.ExtensionManagerMock) f
unc(*testing.T) {
em.ListFunc = func() []extensions.Extension {
e := &Extension{path: "owner2/gh-existing
-ext"}
return []extensions.Extension{e}
}
return func(t *testing.T) {
calls := em.ListCalls()
assert.Equal(t, 1, len(calls)) assert.Equal(t, 1, len(calls))
assert.Equal(t, "https://github.com/owner /gh-some-ext.git", calls[0].URL)
} }
}, },
wantErr: true,
errMsg: "there is already an installed extension that pr
ovides the \"existing-ext\" command",
}, },
{ {
name: "install local extension", name: "install local extension",
args: []string{"install", "."}, args: []string{"install", "."},
managerStubs: func(em *extensions.ExtensionManagerMock) f unc(*testing.T) { managerStubs: func(em *extensions.ExtensionManagerMock) f unc(*testing.T) {
em.InstallLocalFunc = func(dir string) error { em.InstallLocalFunc = func(dir string) error {
return nil return nil
} }
return func(t *testing.T) { return func(t *testing.T) {
calls := em.InstallLocalCalls() calls := em.InstallLocalCalls()
assert.Equal(t, 1, len(calls)) assert.Equal(t, 1, len(calls))
assert.Equal(t, tempDir, normalizeDir(cal ls[0].Dir)) assert.Equal(t, tempDir, normalizeDir(cal ls[0].Dir))
} }
}, },
}, },
{ {
name: "upgrade error", name: "upgrade error",
args: []string{"upgrade"}, args: []string{"upgrade"},
wantErr: true, wantErr: true,
errMsg: "must specify an extension to upgrade",
}, },
{ {
name: "upgrade an extension", name: "upgrade an extension",
args: []string{"upgrade", "hello"}, args: []string{"upgrade", "hello"},
managerStubs: func(em *extensions.ExtensionManagerMock) f unc(*testing.T) { managerStubs: func(em *extensions.ExtensionManagerMock) f unc(*testing.T) {
em.UpgradeFunc = func(name string, out, errOut io .Writer) error { em.UpgradeFunc = func(name string, out, errOut io .Writer) error {
return nil return nil
} }
return func(t *testing.T) { return func(t *testing.T) {
calls := em.UpgradeCalls() calls := em.UpgradeCalls()
skipping to change at line 93 skipping to change at line 118
return nil return nil
} }
return func(t *testing.T) { return func(t *testing.T) {
calls := em.UpgradeCalls() calls := em.UpgradeCalls()
assert.Equal(t, 1, len(calls)) assert.Equal(t, 1, len(calls))
assert.Equal(t, "", calls[0].Name) assert.Equal(t, "", calls[0].Name)
} }
}, },
}, },
{ {
name: "remove extension", name: "remove extension tty",
args: []string{"remove", "hello"}, args: []string{"remove", "hello"},
managerStubs: func(em *extensions.ExtensionManagerMock) f unc(*testing.T) { managerStubs: func(em *extensions.ExtensionManagerMock) f unc(*testing.T) {
em.RemoveFunc = func(name string) error { em.RemoveFunc = func(name string) error {
return nil return nil
} }
return func(t *testing.T) { return func(t *testing.T) {
calls := em.RemoveCalls() calls := em.RemoveCalls()
assert.Equal(t, 1, len(calls)) assert.Equal(t, 1, len(calls))
assert.Equal(t, "hello", calls[0].Name) assert.Equal(t, "hello", calls[0].Name)
} }
}, },
isTTY: true,
wantStdout: "✓ Removed extension hello\n",
},
{
name: "remove extension nontty",
args: []string{"remove", "hello"},
managerStubs: func(em *extensions.ExtensionManagerMock) f
unc(*testing.T) {
em.RemoveFunc = func(name string) error {
return nil
}
return func(t *testing.T) {
calls := em.RemoveCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "hello", calls[0].Name)
}
},
isTTY: false,
wantStdout: "",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ios, _, stdout, stderr := iostreams.Test() ios, _, stdout, stderr := iostreams.Test()
ios.SetStdoutTTY(tt.isTTY)
ios.SetStderrTTY(tt.isTTY)
var assertFunc func(*testing.T) var assertFunc func(*testing.T)
em := &extensions.ExtensionManagerMock{} em := &extensions.ExtensionManagerMock{}
if tt.managerStubs != nil { if tt.managerStubs != nil {
assertFunc = tt.managerStubs(em) assertFunc = tt.managerStubs(em)
} }
f := cmdutil.Factory{ f := cmdutil.Factory{
Config: func() (config.Config, error) { Config: func() (config.Config, error) {
return config.NewBlankConfig(), nil return config.NewBlankConfig(), nil
skipping to change at line 133 skipping to change at line 178
ExtensionManager: em, ExtensionManager: em,
} }
cmd := NewCmdExtensions(&f) cmd := NewCmdExtensions(&f)
cmd.SetArgs(tt.args) cmd.SetArgs(tt.args)
cmd.SetOut(ioutil.Discard) cmd.SetOut(ioutil.Discard)
cmd.SetErr(ioutil.Discard) cmd.SetErr(ioutil.Discard)
_, err := cmd.ExecuteC() _, err := cmd.ExecuteC()
if tt.wantErr { if tt.wantErr {
assert.Error(t, err) assert.EqualError(t, err, tt.errMsg)
} else { } else {
assert.NoError(t, err) assert.NoError(t, err)
} }
if assertFunc != nil { if assertFunc != nil {
assertFunc(t) assertFunc(t)
} }
assert.Equal(t, tt.wantStdout, stdout.String()) assert.Equal(t, tt.wantStdout, stdout.String())
assert.Equal(t, tt.wantStderr, stderr.String()) assert.Equal(t, tt.wantStderr, stderr.String())
}) })
} }
} }
func normalizeDir(d string) string { func normalizeDir(d string) string {
return strings.TrimPrefix(d, "/private") return strings.TrimPrefix(d, "/private")
} }
func Test_checkValidExtension(t *testing.T) {
rootCmd := &cobra.Command{}
rootCmd.AddCommand(&cobra.Command{Use: "help"})
rootCmd.AddCommand(&cobra.Command{Use: "auth"})
m := &extensions.ExtensionManagerMock{
ListFunc: func() []extensions.Extension {
return []extensions.Extension{
&extensions.ExtensionMock{
NameFunc: func() string { return "screens
aver" },
},
&extensions.ExtensionMock{
NameFunc: func() string { return "triage"
},
},
}
},
}
type args struct {
rootCmd *cobra.Command
manager extensions.ExtensionManager
extName string
}
tests := []struct {
name string
args args
wantError string
}{
{
name: "valid extension",
args: args{
rootCmd: rootCmd,
manager: m,
extName: "gh-hello",
},
},
{
name: "invalid extension name",
args: args{
rootCmd: rootCmd,
manager: m,
extName: "gherkins",
},
wantError: "extension repository name must start with `gh
-`",
},
{
name: "clashes with built-in command",
args: args{
rootCmd: rootCmd,
manager: m,
extName: "gh-auth",
},
wantError: "\"auth\" matches the name of a built-in comma
nd",
},
{
name: "clashes with an installed extension",
args: args{
rootCmd: rootCmd,
manager: m,
extName: "gh-triage",
},
wantError: "there is already an installed extension that
provides the \"triage\" command",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := checkValidExtension(tt.args.rootCmd, tt.args.manag
er, tt.args.extName)
if tt.wantError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.wantError)
}
})
}
}
 End of changes. 13 change blocks. 
4 lines changed or deleted 55 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)