Stub hasRosetta for tests

This commit is contained in:
William Martin 2024-09-12 20:29:59 +02:00 committed by Tyler McGoffin
parent 5296d60832
commit e0fb793b44
2 changed files with 53 additions and 1 deletions

View file

@ -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
}

View file

@ -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}
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")