MCPcopy Index your code
hub / github.com/cli/cli / TestDownloadCopilot

Function TestDownloadCopilot

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

Source from the content-addressed store, hash-verified

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