From f5d269ebad0be6fcf42a0e76a87cc887f3ab37bd Mon Sep 17 00:00:00 2001 From: vilmibm Date: Mon, 20 Sep 2021 17:02:34 -0500 Subject: [PATCH] WIP refactoring --- pkg/cmd/extension/manager.go | 1 + pkg/cmd/extension/manager_test.go | 48 +++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/extension/manager.go b/pkg/cmd/extension/manager.go index ddfbde3fc..75cb4cfd9 100644 --- a/pkg/cmd/extension/manager.go +++ b/pkg/cmd/extension/manager.go @@ -185,6 +185,7 @@ type BinManifest struct { Name string Host string // TODO I may end up not using this; just thinking ahead to local installs + // TODO track version Path string } diff --git a/pkg/cmd/extension/manager_test.go b/pkg/cmd/extension/manager_test.go index 80a535c40..f0a930dcd 100644 --- a/pkg/cmd/extension/manager_test.go +++ b/pkg/cmd/extension/manager_test.go @@ -12,8 +12,10 @@ import ( "testing" "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/httpmock" + "github.com/cli/cli/v2/pkg/iostreams" "github.com/stretchr/testify/assert" "gopkg.in/yaml.v3" ) @@ -197,25 +199,57 @@ func TestManager_Upgrade_NoExtensions(t *testing.T) { assert.Equal(t, "", stderr.String()) } -func TestManager_InstallGit(t *testing.T) { +func TestManager_Install_git(t *testing.T) { tempDir := t.TempDir() m := newTestManager(tempDir) - stdout := &bytes.Buffer{} - stderr := &bytes.Buffer{} - err := m.InstallGit("https://github.com/owner/gh-some-ext.git", stdout, stderr) + reg := httpmock.Registry{} + defer reg.Verify(t) + client := http.Client{Transport: ®} + + reg.Register( + httpmock.REST("GET", "repos/owner/gh-some-ext/releases/latest"), + httpmock.JSONResponse( + release{ + Assets: []releaseAsset{ + { + Name: "not-a-binary", + APIURL: "https://example.com/release/cool", + }, + }, + })) + reg.Register( + httpmock.REST("GET", "repos/owner/gh-some-ext/contents/gh-some-ext"), + httpmock.StringResponse("script")) + + io, _, stdout, stderr := iostreams.Test() + + repo := ghrepo.New("owner", "gh-some-ext") + + err := m.Install(&client, repo, io, config.NewBlankConfig()) assert.NoError(t, err) assert.Equal(t, fmt.Sprintf("[git clone https://github.com/owner/gh-some-ext.git %s]\n", filepath.Join(tempDir, "extensions", "gh-some-ext")), stdout.String()) assert.Equal(t, "", stderr.String()) } -func TestManager_InstallBin(t *testing.T) { +func TestManager_Install_binary(t *testing.T) { repo := ghrepo.NewWithHost("owner", "gh-bin-ext", "example.com") reg := httpmock.Registry{} defer reg.Verify(t) client := http.Client{Transport: ®} + reg.Register( + httpmock.REST("GET", "api/v3/repos/owner/gh-bin-ext/releases/latest"), + httpmock.JSONResponse( + release{ + Assets: []releaseAsset{ + { + Name: "gh-bin-ext-windows-amd64", + APIURL: "https://example.com/release/cool", + }, + }, + })) reg.Register( httpmock.REST("GET", "api/v3/repos/owner/gh-bin-ext/releases/latest"), httpmock.JSONResponse( @@ -234,7 +268,9 @@ func TestManager_InstallBin(t *testing.T) { tempDir := t.TempDir() m := newTestManager(tempDir) - err := m.InstallBin(&client, repo) + io, _, _, _ := iostreams.Test() + + err := m.Install(&client, repo, io, config.NewBlankConfig()) assert.NoError(t, err) manifest, err := os.ReadFile(filepath.Join(tempDir, "extensions/gh-bin-ext/manifest.yml"))