From e0fb793b44d7060fdff429500382badf030ae880 Mon Sep 17 00:00:00 2001 From: William Martin Date: Thu, 12 Sep 2024 20:29:59 +0200 Subject: [PATCH] Stub hasRosetta for tests --- pkg/cmd/extension/manager.go | 2 +- pkg/cmd/extension/manager_test.go | 52 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/extension/manager.go b/pkg/cmd/extension/manager.go index cf5c44bcd..45186d689 100644 --- a/pkg/cmd/extension/manager.go +++ b/pkg/cmd/extension/manager.go @@ -771,7 +771,7 @@ func possibleDists() []string { } } -func hasRosetta() bool { +var hasRosetta = func() bool { _, err := os.Stat("/Library/Apple/usr/libexec/oah/libRosettaRuntime") return err == nil } diff --git a/pkg/cmd/extension/manager_test.go b/pkg/cmd/extension/manager_test.go index 6d9b63124..f03cc7f73 100644 --- a/pkg/cmd/extension/manager_test.go +++ b/pkg/cmd/extension/manager_test.go @@ -912,6 +912,58 @@ func TestManager_Install_binary_unsupported(t *testing.T) { assert.Equal(t, "", stderr.String()) } +func TestManager_Install_rosetta_fallback_not_found(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-darwin-amd64", + APIURL: "https://example.com/release/cool", + }, + }, + })) + reg.Register( + httpmock.REST("GET", "api/v3/repos/owner/gh-bin-ext/releases/latest"), + httpmock.JSONResponse( + release{ + Tag: "v1.0.1", + Assets: []releaseAsset{ + { + Name: "gh-bin-ext-darwin-amd64", + APIURL: "https://example.com/release/cool", + }, + }, + })) + + ios, _, stdout, stderr := iostreams.Test() + tempDir := t.TempDir() + + m := newTestManager(tempDir, &client, nil, ios) + m.platform = func() (string, string) { + return "darwin-arm64", "" + } + + originalHasRosetta := hasRosetta + t.Cleanup(func() { hasRosetta = originalHasRosetta }) + hasRosetta = func() bool { + return false + } + + err := m.Install(repo, "") + assert.EqualError(t, err, "gh-bin-ext unsupported for darwin-arm64. Install Rosetta with `softwareupdate --install-rosetta` to use the available darwin-amd64 binary, or open an issue: `gh issue create -R owner/gh-bin-ext -t'Support darwin-arm64'`") + + assert.Equal(t, "", stdout.String()) + assert.Equal(t, "", stderr.String()) +} + func TestManager_Install_binary(t *testing.T) { repo := ghrepo.NewWithHost("owner", "gh-bin-ext", "example.com")