|
1 | | -//go:build e2e |
| 1 | +//go:build e2e && vm |
2 | 2 |
|
3 | 3 | package e2e |
4 | 4 |
|
5 | 5 | import ( |
6 | | - "os" |
7 | | - "os/exec" |
8 | 6 | "strings" |
9 | 7 | "testing" |
10 | 8 |
|
11 | 9 | "github.com/openbootdotdev/openboot/testutil" |
12 | 10 | "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
13 | 12 | ) |
14 | 13 |
|
15 | 14 | func TestE2E_Clean_DryRun_FromFile(t *testing.T) { |
16 | | - binary := testutil.BuildTestBinary(t) |
17 | | - tmpDir := t.TempDir() |
| 15 | + if testing.Short() { |
| 16 | + t.Skip("skipping VM test in short mode") |
| 17 | + } |
| 18 | + |
| 19 | + vm := testutil.NewTartVM(t) |
| 20 | + vmInstallHomebrew(t, vm) |
| 21 | + bin := vmCopyDevBinary(t, vm) |
18 | 22 |
|
19 | 23 | // Create a snapshot with a small set of packages — system will have "extra" |
20 | | - snapshotPath := writeTestSnapshot(t, tmpDir, []string{"git"}, []string{}, []string{}) |
| 24 | + snapshotPath := "/tmp/clean-test-snapshot.json" |
| 25 | + vmWriteTestSnapshot(t, vm, snapshotPath, []string{"git"}, []string{}, []string{}) |
21 | 26 |
|
22 | 27 | // Capture installed packages before |
23 | | - beforePkgs, err := testutil.GetInstalledBrewPackages() |
24 | | - if err != nil { |
25 | | - t.Skipf("cannot list brew packages: %v", err) |
26 | | - } |
27 | | - |
28 | | - cmd := exec.Command(binary, "clean", "--from", snapshotPath, "--dry-run") |
29 | | - output, err := cmd.CombinedOutput() |
30 | | - outStr := string(output) |
| 28 | + beforePkgs := vmBrewList(t, vm) |
31 | 29 |
|
32 | | - assert.NoError(t, err, "clean --dry-run should succeed, output: %s", outStr) |
| 30 | + output, err := vmRunDevBinary(t, vm, bin, "clean --from "+snapshotPath+" --dry-run") |
| 31 | + assert.NoError(t, err, "clean --dry-run should succeed, output: %s", output) |
33 | 32 | assert.True(t, |
34 | | - strings.Contains(outStr, "DRY-RUN") || strings.Contains(outStr, "dry run") || strings.Contains(outStr, "Dry run"), |
35 | | - "output should mention dry-run mode, got: %s", outStr) |
| 33 | + strings.Contains(output, "DRY-RUN") || strings.Contains(output, "dry run") || strings.Contains(output, "Dry run"), |
| 34 | + "output should mention dry-run mode, got: %s", output) |
36 | 35 |
|
37 | 36 | // Verify nothing was actually removed |
38 | | - afterPkgs, err := testutil.GetInstalledBrewPackages() |
39 | | - if err != nil { |
40 | | - t.Skipf("cannot list brew packages: %v", err) |
41 | | - } |
| 37 | + afterPkgs := vmBrewList(t, vm) |
42 | 38 | assert.Equal(t, len(beforePkgs), len(afterPkgs), |
43 | 39 | "dry-run should not remove any packages (before=%d, after=%d)", len(beforePkgs), len(afterPkgs)) |
44 | 40 | } |
45 | 41 |
|
46 | 42 | func TestE2E_Clean_MissingFile(t *testing.T) { |
47 | | - binary := testutil.BuildTestBinary(t) |
| 43 | + if testing.Short() { |
| 44 | + t.Skip("skipping VM test in short mode") |
| 45 | + } |
48 | 46 |
|
49 | | - cmd := exec.Command(binary, "clean", "--from", "/tmp/nonexistent-snapshot-99999.json") |
50 | | - output, err := cmd.CombinedOutput() |
51 | | - outStr := string(output) |
| 47 | + vm := testutil.NewTartVM(t) |
| 48 | + vmInstallHomebrew(t, vm) |
| 49 | + bin := vmCopyDevBinary(t, vm) |
52 | 50 |
|
| 51 | + output, err := vmRunDevBinary(t, vm, bin, "clean --from /tmp/nonexistent-snapshot-99999.json") |
53 | 52 | assert.Error(t, err, "clean with missing file should fail") |
54 | 53 | assert.True(t, |
55 | | - strings.Contains(outStr, "not found") || strings.Contains(outStr, "no such file") || strings.Contains(outStr, "snapshot"), |
56 | | - "error should mention file issue, got: %s", outStr) |
| 54 | + strings.Contains(output, "not found") || strings.Contains(output, "no such file") || strings.Contains(output, "snapshot"), |
| 55 | + "error should mention file issue, got: %s", output) |
57 | 56 | } |
58 | 57 |
|
59 | 58 | func TestE2E_Clean_NoLocalSnapshot(t *testing.T) { |
60 | | - binary := testutil.BuildTestBinary(t) |
| 59 | + if testing.Short() { |
| 60 | + t.Skip("skipping VM test in short mode") |
| 61 | + } |
61 | 62 |
|
62 | | - // Run clean without flags — tries local snapshot |
63 | | - cmd := exec.Command(binary, "clean") |
64 | | - // Set HOME to a temp dir so no local snapshot exists |
65 | | - tmpHome := t.TempDir() |
66 | | - cmd.Env = append(os.Environ(), "HOME="+tmpHome) |
| 63 | + vm := testutil.NewTartVM(t) |
| 64 | + vmInstallHomebrew(t, vm) |
| 65 | + bin := vmCopyDevBinary(t, vm) |
67 | 66 |
|
68 | | - output, err := cmd.CombinedOutput() |
69 | | - outStr := string(output) |
| 67 | + // Run clean without flags — tries local snapshot which won't exist in fresh VM |
| 68 | + // Use a temp HOME dir in the VM with no snapshot |
| 69 | + _, err := vm.Run("mkdir -p /tmp/clean-no-snapshot-home") |
| 70 | + require.NoError(t, err) |
70 | 71 |
|
| 72 | + output, err := vm.RunWithEnv( |
| 73 | + map[string]string{"PATH": brewPath, "HOME": "/tmp/clean-no-snapshot-home"}, |
| 74 | + bin+" clean", |
| 75 | + ) |
71 | 76 | if err != nil { |
72 | 77 | assert.True(t, |
73 | | - strings.Contains(outStr, "snapshot") || strings.Contains(outStr, "--from") || strings.Contains(outStr, "--user"), |
74 | | - "error should guide user, got: %s", outStr) |
| 78 | + strings.Contains(output, "snapshot") || strings.Contains(output, "--from") || strings.Contains(output, "--user"), |
| 79 | + "error should guide user, got: %s", output) |
75 | 80 | } |
76 | 81 | } |
77 | 82 |
|
78 | 83 | func TestE2E_Clean_HelpFlag(t *testing.T) { |
79 | | - binary := testutil.BuildTestBinary(t) |
| 84 | + if testing.Short() { |
| 85 | + t.Skip("skipping VM test in short mode") |
| 86 | + } |
80 | 87 |
|
81 | | - cmd := exec.Command(binary, "clean", "--help") |
82 | | - output, err := cmd.CombinedOutput() |
83 | | - outStr := string(output) |
| 88 | + vm := testutil.NewTartVM(t) |
| 89 | + vmInstallHomebrew(t, vm) |
| 90 | + bin := vmCopyDevBinary(t, vm) |
84 | 91 |
|
| 92 | + output, err := vmRunDevBinary(t, vm, bin, "clean --help") |
85 | 93 | assert.NoError(t, err, "clean --help should succeed") |
86 | | - assert.Contains(t, outStr, "clean") |
87 | | - assert.Contains(t, outStr, "--from") |
88 | | - assert.Contains(t, outStr, "--user") |
89 | | - assert.Contains(t, outStr, "--dry-run") |
| 94 | + assert.Contains(t, output, "clean") |
| 95 | + assert.Contains(t, output, "--from") |
| 96 | + assert.Contains(t, output, "--user") |
| 97 | + assert.Contains(t, output, "--dry-run") |
90 | 98 | } |
0 commit comments