(t *testing.T)
| 396 | } |
| 397 | |
| 398 | func 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") |
nothing calls this directly
no test coverage detected