|
| 1 | +package lint |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestCopyRulesFromPath_SameSourceAndTarget_NoOp(t *testing.T) { |
| 10 | + tempDir := t.TempDir() |
| 11 | + rulesDir := filepath.Join(tempDir, "rules") |
| 12 | + if err := os.MkdirAll(filepath.Join(rulesDir, "sample"), 0755); err != nil { |
| 13 | + t.Fatalf("failed to create rules directory: %v", err) |
| 14 | + } |
| 15 | + rulePath := filepath.Join(rulesDir, "sample", "rule.js") |
| 16 | + original := []byte("const metadata = { title: 'a', description: 'b', custom: { rulenumber: '001_0001' } };") |
| 17 | + if err := os.WriteFile(rulePath, original, 0644); err != nil { |
| 18 | + t.Fatalf("failed to write sample rule: %v", err) |
| 19 | + } |
| 20 | + |
| 21 | + if err := copyRulesFromPath(rulesDir, rulesDir); err != nil { |
| 22 | + t.Fatalf("copyRulesFromPath should no-op for identical source/target: %v", err) |
| 23 | + } |
| 24 | + |
| 25 | + got, err := os.ReadFile(rulePath) |
| 26 | + if err != nil { |
| 27 | + t.Fatalf("failed to read sample rule after copy: %v", err) |
| 28 | + } |
| 29 | + if string(got) != string(original) { |
| 30 | + t.Fatalf("expected rule content unchanged, got %q", string(got)) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestCopyFile_SameSourceAndDestination_NoOp(t *testing.T) { |
| 35 | + tempDir := t.TempDir() |
| 36 | + filePath := filepath.Join(tempDir, "rule.rego") |
| 37 | + original := []byte("# METADATA\n# title: x\n") |
| 38 | + if err := os.WriteFile(filePath, original, 0644); err != nil { |
| 39 | + t.Fatalf("failed to write source file: %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + if err := copyFile(filePath, filePath); err != nil { |
| 43 | + t.Fatalf("copyFile should no-op for identical source/destination: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + got, err := os.ReadFile(filePath) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("failed to read file after copy: %v", err) |
| 49 | + } |
| 50 | + if string(got) != string(original) { |
| 51 | + t.Fatalf("expected file content unchanged, got %q", string(got)) |
| 52 | + } |
| 53 | +} |
0 commit comments