|
| 1 | +package runner |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +// writeFile creates path (including parents) with the given contents. |
| 13 | +func writeFile(t *testing.T, path, contents string) { |
| 14 | + t.Helper() |
| 15 | + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) |
| 16 | + require.NoError(t, os.WriteFile(path, []byte(contents), 0o644)) |
| 17 | +} |
| 18 | + |
| 19 | +func TestEngineCacheFileChecksums(t *testing.T) { |
| 20 | + tmpDir := t.TempDir() |
| 21 | + |
| 22 | + writeFile(t, filepath.Join(tmpDir, "main.go"), "package main\n") |
| 23 | + writeFile(t, filepath.Join(tmpDir, "internal", "app.go"), "package internal\n") |
| 24 | + writeFile(t, filepath.Join(tmpDir, "README.md"), "docs\n") // extension not watched |
| 25 | + writeFile(t, filepath.Join(tmpDir, "skip.go"), "package skip\n") // exclude_file |
| 26 | + writeFile(t, filepath.Join(tmpDir, "app_test.go"), "package main\n") // exclude_regex |
| 27 | + writeFile(t, filepath.Join(tmpDir, "tmp", "main.go"), "package tmp\n") |
| 28 | + writeFile(t, filepath.Join(tmpDir, "vendor", "dep.go"), "package dep\n") |
| 29 | + writeFile(t, filepath.Join(tmpDir, ".hidden", "hidden.go"), "package hidden\n") |
| 30 | + |
| 31 | + cfg := defaultConfig() |
| 32 | + cfg.Root = tmpDir |
| 33 | + cfg.TmpDir = "tmp" |
| 34 | + cfg.Build.IncludeExt = []string{"go"} |
| 35 | + cfg.Build.ExcludeDir = []string{"vendor"} |
| 36 | + cfg.Build.ExcludeFile = []string{"skip.go"} |
| 37 | + cfg.Build.ExcludeRegex = []string{"_test.go"} |
| 38 | + require.NoError(t, cfg.preprocess(nil)) |
| 39 | + |
| 40 | + engine, err := NewEngineWithConfig(&cfg, false) |
| 41 | + require.NoError(t, err) |
| 42 | + t.Cleanup(func() { _ = engine.watcher.Close() }) |
| 43 | + |
| 44 | + require.NoError(t, engine.cacheFileChecksums(cfg.Root)) |
| 45 | + |
| 46 | + cached := func(rel string) bool { |
| 47 | + path := filepath.Join(cfg.Root, rel) |
| 48 | + checksum, err := fileChecksum(path) |
| 49 | + require.NoError(t, err) |
| 50 | + // updateFileChecksum reports false when the checksum is already stored. |
| 51 | + return !engine.fileChecksums.updateFileChecksum(path, checksum) |
| 52 | + } |
| 53 | + |
| 54 | + assert.True(t, cached("main.go"), "watched file should be cached") |
| 55 | + assert.True(t, cached(filepath.Join("internal", "app.go")), "watched file in subdir should be cached") |
| 56 | + |
| 57 | + assert.False(t, cached("README.md"), "unwatched extension should not be cached") |
| 58 | + assert.False(t, cached("skip.go"), "exclude_file match should not be cached") |
| 59 | + assert.False(t, cached("app_test.go"), "exclude_regex match should not be cached") |
| 60 | + assert.False(t, cached(filepath.Join("vendor", "dep.go")), "exclude_dir content should not be cached") |
| 61 | + assert.False(t, cached(filepath.Join(".hidden", "hidden.go")), "hidden dir content should not be cached") |
| 62 | +} |
| 63 | + |
| 64 | +func TestEngineCacheFileChecksumsMissingRoot(t *testing.T) { |
| 65 | + tmpDir := t.TempDir() |
| 66 | + |
| 67 | + cfg := defaultConfig() |
| 68 | + cfg.Root = tmpDir |
| 69 | + require.NoError(t, cfg.preprocess(nil)) |
| 70 | + |
| 71 | + engine, err := NewEngineWithConfig(&cfg, false) |
| 72 | + require.NoError(t, err) |
| 73 | + t.Cleanup(func() { _ = engine.watcher.Close() }) |
| 74 | + |
| 75 | + assert.Error(t, engine.cacheFileChecksums(filepath.Join(tmpDir, "does-not-exist"))) |
| 76 | +} |
| 77 | + |
| 78 | +func TestEnginePrimeChecksumIgnoresUnreadableFile(t *testing.T) { |
| 79 | + tmpDir := t.TempDir() |
| 80 | + |
| 81 | + cfg := defaultConfig() |
| 82 | + cfg.Root = tmpDir |
| 83 | + require.NoError(t, cfg.preprocess(nil)) |
| 84 | + |
| 85 | + engine, err := NewEngineWithConfig(&cfg, false) |
| 86 | + require.NoError(t, err) |
| 87 | + t.Cleanup(func() { _ = engine.watcher.Close() }) |
| 88 | + |
| 89 | + missing := filepath.Join(tmpDir, "gone.go") |
| 90 | + engine.primeChecksum(missing) |
| 91 | + |
| 92 | + // Nothing was stored, so the first real update reports a change. |
| 93 | + writeFile(t, missing, "package main\n") |
| 94 | + checksum, err := fileChecksum(missing) |
| 95 | + require.NoError(t, err) |
| 96 | + assert.True(t, engine.fileChecksums.updateFileChecksum(missing, checksum)) |
| 97 | +} |
| 98 | + |
| 99 | +func TestEngineIsExcludeFile(t *testing.T) { |
| 100 | + tmpDir := t.TempDir() |
| 101 | + |
| 102 | + cfg := defaultConfig() |
| 103 | + cfg.Root = tmpDir |
| 104 | + // isExcludeFile matches with filepath.Match, so the directory pattern has |
| 105 | + // to use the platform separator to match on Windows too. |
| 106 | + cfg.Build.ExcludeFile = []string{"skip.go", filepath.Join("docs", "*.md")} |
| 107 | + require.NoError(t, cfg.preprocess(nil)) |
| 108 | + |
| 109 | + engine, err := NewEngineWithConfig(&cfg, false) |
| 110 | + require.NoError(t, err) |
| 111 | + t.Cleanup(func() { _ = engine.watcher.Close() }) |
| 112 | + |
| 113 | + assert.True(t, engine.isExcludeFile(filepath.Join(cfg.Root, "skip.go"))) |
| 114 | + assert.True(t, engine.isExcludeFile(filepath.Join(cfg.Root, "docs", "readme.md"))) |
| 115 | + assert.False(t, engine.isExcludeFile(filepath.Join(cfg.Root, "main.go"))) |
| 116 | + assert.False(t, engine.isExcludeFile(filepath.Join(cfg.Root, "docs", "nested", "readme.md"))) |
| 117 | +} |
0 commit comments