MCPcopy Create free account
hub / github.com/cli/cli / TestDownloadCopilot

Function TestDownloadCopilot

pkg/cmd/copilot/copilot_test.go:402–595  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

400}
401
402func TestDownloadCopilot(t *testing.T) {
403 // Skip on unsupported architectures
404 if runtime.GOARCH != "amd64" && runtime.GOARCH != "arm64" {
405 t.Skip("skipping test on unsupported architecture")
406 }
407
408 t.Run("downloads and extracts tar.gz with valid checksum", func(t *testing.T) {
409 if runtime.GOOS == "windows" {
410 t.Skip("skipping tar.gz test on windows")
411 }
412
413 ios, _, _, stderr := iostreams.Test()
414 tmpDir := t.TempDir()
415 installDir := filepath.Join(tmpDir, "copilot")
416 localPath := filepath.Join(installDir, "copilot")
417
418 // Create mock archive with copilot binary
419 binaryContent := []byte("#!/bin/sh\necho copilot")
420 archive := createTarGzBuffer(t, map[string][]byte{
421 "copilot": binaryContent,
422 })
423
424 // Calculate checksum
425 checksum := sha256.Sum256(archive)
426 checksumHex := hex.EncodeToString(checksum[:])
427 archiveName := fmt.Sprintf("copilot-%s-%s.tar.gz", runtime.GOOS, archString())
428 checksumFile := fmt.Sprintf("%s %s\n", checksumHex, archiveName)
429
430 reg := &httpmock.Registry{}
431 // Register checksum endpoint
432 reg.Register(
433 httpmock.REST("GET", "github/copilot-cli/releases/latest/download/SHA256SUMS.txt"),
434 httpmock.StringResponse(checksumFile),
435 )
436 // Register archive endpoint
437 reg.Register(
438 httpmock.REST("GET", fmt.Sprintf("github/copilot-cli/releases/latest/download/%s", archiveName)),
439 httpmock.BinaryResponse(archive),
440 )
441
442 httpClient := &http.Client{Transport: reg}
443
444 path, err := downloadCopilot(httpClient, ios, installDir, localPath)
445 require.NoError(t, err, "downloadCopilot() error")
446 require.Equal(t, localPath, path, "downloadCopilot() path mismatch")
447
448 // Verify binary was extracted
449 extracted, err := os.ReadFile(localPath)
450 require.NoError(t, err, "failed to read extracted binary")
451 require.Equal(t, binaryContent, extracted, "extracted content mismatch")
452
453 // Verify output messages
454 require.Contains(t, stderr.String(), "installed successfully", "expected success message in stderr")
455 })
456
457 t.Run("fails with checksum mismatch", func(t *testing.T) {
458 if runtime.GOOS == "windows" {
459 t.Skip("skipping tar.gz test on windows")

Callers

nothing calls this directly

Calls 15

RegisterMethod · 0.95
TestFunction · 0.92
RESTFunction · 0.92
StringResponseFunction · 0.92
BinaryResponseFunction · 0.92
StatusStringResponseFunction · 0.92
createTarGzBufferFunction · 0.85
archStringFunction · 0.85
downloadCopilotFunction · 0.85
createZipBufferFunction · 0.85
SkipMethod · 0.80
JoinMethod · 0.80

Tested by

no test coverage detected