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

Function TestDownloadCopilot

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

Source from the content-addressed store, hash-verified

510}
511
512func TestDownloadCopilot(t *testing.T) {
513 // Skip on unsupported architectures
514 if runtime.GOARCH != "amd64" && runtime.GOARCH != "arm64" {
515 t.Skip("skipping test on unsupported architecture")
516 }
517
518 t.Run("downloads and extracts tar.gz with valid checksum", func(t *testing.T) {
519 if runtime.GOOS == "windows" {
520 t.Skip("skipping tar.gz test on windows")
521 }
522
523 ios, _, _, stderr := iostreams.Test()
524 tmpDir := t.TempDir()
525 installDir := filepath.Join(tmpDir, "copilot")
526 localPath := filepath.Join(installDir, "copilot")
527
528 // Create mock archive with copilot binary
529 binaryContent := []byte("#!/bin/sh\necho copilot")
530 archive := createTarGzBuffer(t, map[string][]byte{
531 "copilot": binaryContent,
532 })
533
534 // Calculate checksum
535 checksum := sha256.Sum256(archive)
536 checksumHex := hex.EncodeToString(checksum[:])
537 archiveName := fmt.Sprintf("copilot-%s-%s.tar.gz", runtime.GOOS, archString())
538 checksumFile := fmt.Sprintf("%s %s\n", checksumHex, archiveName)
539
540 reg := &httpmock.Registry{}
541 // Register checksum endpoint
542 reg.Register(
543 httpmock.REST("GET", "github/copilot-cli/releases/latest/download/SHA256SUMS.txt"),
544 httpmock.StringResponse(checksumFile),
545 )
546 // Register archive endpoint
547 reg.Register(
548 httpmock.REST("GET", fmt.Sprintf("github/copilot-cli/releases/latest/download/%s", archiveName)),
549 httpmock.BinaryResponse(archive),
550 )
551
552 httpClient := &http.Client{Transport: reg}
553
554 path, err := downloadCopilot(httpClient, ios, installDir, localPath)
555 require.NoError(t, err, "downloadCopilot() error")
556 require.Equal(t, localPath, path, "downloadCopilot() path mismatch")
557
558 // Verify binary was extracted
559 extracted, err := os.ReadFile(localPath)
560 require.NoError(t, err, "failed to read extracted binary")
561 require.Equal(t, binaryContent, extracted, "extracted content mismatch")
562
563 // Verify output messages
564 require.Contains(t, stderr.String(), "installed successfully", "expected success message in stderr")
565 })
566
567 t.Run("fails with checksum mismatch", func(t *testing.T) {
568 if runtime.GOOS == "windows" {
569 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