|
| 1 | +package toolsnaps |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +type dummyTool struct { |
| 14 | + Name string `json:"name"` |
| 15 | + Value int `json:"value"` |
| 16 | +} |
| 17 | + |
| 18 | +// withIsolatedWorkingDir creates a temp dir, changes to it, and restores the original working dir after the test. |
| 19 | +func withIsolatedWorkingDir(t *testing.T) { |
| 20 | + dir := t.TempDir() |
| 21 | + origDir, err := os.Getwd() |
| 22 | + require.NoError(t, err) |
| 23 | + t.Cleanup(func() { require.NoError(t, os.Chdir(origDir)) }) |
| 24 | + require.NoError(t, os.Chdir(dir)) |
| 25 | +} |
| 26 | + |
| 27 | +func TestSnapshotDoesNotExistNotInCI(t *testing.T) { |
| 28 | + withIsolatedWorkingDir(t) |
| 29 | + |
| 30 | + // Given we are not running in CI |
| 31 | + t.Setenv("GITHUB_ACTIONS", "false") // This REALLY is required because the tests run in CI |
| 32 | + tool := dummyTool{"foo", 42} |
| 33 | + |
| 34 | + // When we test the snapshot |
| 35 | + err := Test("dummy", tool) |
| 36 | + |
| 37 | + // Then it should succeed and write the snapshot file |
| 38 | + require.NoError(t, err) |
| 39 | + path := filepath.Join("__toolsnaps__", "dummy.snap") |
| 40 | + _, statErr := os.Stat(path) |
| 41 | + assert.NoError(t, statErr, "expected snapshot file to be written") |
| 42 | +} |
| 43 | + |
| 44 | +func TestSnapshotDoesNotExistInCI(t *testing.T) { |
| 45 | + withIsolatedWorkingDir(t) |
| 46 | + |
| 47 | + // Given we are running in CI |
| 48 | + t.Setenv("GITHUB_ACTIONS", "true") |
| 49 | + tool := dummyTool{"foo", 42} |
| 50 | + |
| 51 | + // When we test the snapshot |
| 52 | + err := Test("dummy", tool) |
| 53 | + |
| 54 | + // Then it should error about missing snapshot in CI |
| 55 | + require.Error(t, err) |
| 56 | + assert.Contains(t, err.Error(), "tool snapshot does not exist", "expected error about missing snapshot in CI") |
| 57 | +} |
| 58 | + |
| 59 | +func TestSnapshotExistsMatch(t *testing.T) { |
| 60 | + withIsolatedWorkingDir(t) |
| 61 | + |
| 62 | + // Given a matching snapshot file exists |
| 63 | + tool := dummyTool{"foo", 42} |
| 64 | + b, _ := json.MarshalIndent(tool, "", " ") |
| 65 | + require.NoError(t, os.MkdirAll("__toolsnaps__", 0700)) |
| 66 | + require.NoError(t, os.WriteFile(filepath.Join("__toolsnaps__", "dummy.snap"), b, 0600)) |
| 67 | + |
| 68 | + // When we test the snapshot |
| 69 | + err := Test("dummy", tool) |
| 70 | + |
| 71 | + // Then it should succeed (no error) |
| 72 | + require.NoError(t, err) |
| 73 | +} |
| 74 | + |
| 75 | +func TestSnapshotExistsDiff(t *testing.T) { |
| 76 | + withIsolatedWorkingDir(t) |
| 77 | + |
| 78 | + // Given a non-matching snapshot file exists |
| 79 | + require.NoError(t, os.MkdirAll("__toolsnaps__", 0700)) |
| 80 | + require.NoError(t, os.WriteFile(filepath.Join("__toolsnaps__", "dummy.snap"), []byte(`{"name":"foo","value":1}`), 0600)) |
| 81 | + tool := dummyTool{"foo", 2} |
| 82 | + |
| 83 | + // When we test the snapshot |
| 84 | + err := Test("dummy", tool) |
| 85 | + |
| 86 | + // Then it should error about the schema diff |
| 87 | + require.Error(t, err) |
| 88 | + assert.Contains(t, err.Error(), "tool schema for dummy has changed unexpectedly", "expected error about diff") |
| 89 | +} |
| 90 | + |
| 91 | +func TestUpdateToolsnaps(t *testing.T) { |
| 92 | + withIsolatedWorkingDir(t) |
| 93 | + |
| 94 | + // Given UPDATE_TOOLSNAPS is set, regardless of whether a matching snapshot file exists |
| 95 | + t.Setenv("UPDATE_TOOLSNAPS", "true") |
| 96 | + require.NoError(t, os.MkdirAll("__toolsnaps__", 0700)) |
| 97 | + require.NoError(t, os.WriteFile(filepath.Join("__toolsnaps__", "dummy.snap"), []byte(`{"name":"foo","value":1}`), 0600)) |
| 98 | + tool := dummyTool{"foo", 42} |
| 99 | + |
| 100 | + // When we test the snapshot |
| 101 | + err := Test("dummy", tool) |
| 102 | + |
| 103 | + // Then it should succeed and write the snapshot file |
| 104 | + require.NoError(t, err) |
| 105 | + path := filepath.Join("__toolsnaps__", "dummy.snap") |
| 106 | + _, statErr := os.Stat(path) |
| 107 | + assert.NoError(t, statErr, "expected snapshot file to be written") |
| 108 | +} |
| 109 | + |
| 110 | +func TestMalformedSnapshotJSON(t *testing.T) { |
| 111 | + withIsolatedWorkingDir(t) |
| 112 | + |
| 113 | + // Given a malformed snapshot file exists |
| 114 | + require.NoError(t, os.MkdirAll("__toolsnaps__", 0700)) |
| 115 | + require.NoError(t, os.WriteFile(filepath.Join("__toolsnaps__", "dummy.snap"), []byte(`not-json`), 0600)) |
| 116 | + tool := dummyTool{"foo", 42} |
| 117 | + |
| 118 | + // When we test the snapshot |
| 119 | + err := Test("dummy", tool) |
| 120 | + |
| 121 | + // Then it should error about malformed snapshot JSON |
| 122 | + require.Error(t, err) |
| 123 | + assert.Contains(t, err.Error(), "failed to parse snapshot JSON for dummy", "expected error about malformed snapshot JSON") |
| 124 | +} |
0 commit comments