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

Function TestDownloadCopilot

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

Source from the content-addressed store, hash-verified

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