|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestEnsureIncludeDirective(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + initialContent string |
| 14 | + includeFile string |
| 15 | + expectedContent string |
| 16 | + shouldContain string |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "add include to empty file", |
| 20 | + initialContent: `# PostgreSQL configuration file |
| 21 | +listen_addresses = '*' |
| 22 | +port = 5432 |
| 23 | +`, |
| 24 | + includeFile: "postgresql.tune.conf", |
| 25 | + shouldContain: "include_if_exists 'postgresql.tune.conf'", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "include already exists", |
| 29 | + initialContent: `# PostgreSQL configuration file |
| 30 | +listen_addresses = '*' |
| 31 | +port = 5432 |
| 32 | +include_if_exists 'postgresql.tune.conf' |
| 33 | +`, |
| 34 | + includeFile: "postgresql.tune.conf", |
| 35 | + shouldContain: "include_if_exists 'postgresql.tune.conf'", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "include with different syntax already exists", |
| 39 | + initialContent: `# PostgreSQL configuration file |
| 40 | +listen_addresses = '*' |
| 41 | +include 'postgresql.tune.conf' |
| 42 | +port = 5432 |
| 43 | +`, |
| 44 | + includeFile: "postgresql.tune.conf", |
| 45 | + shouldContain: "include 'postgresql.tune.conf'", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "commented include should add new one", |
| 49 | + initialContent: `# PostgreSQL configuration file |
| 50 | +listen_addresses = '*' |
| 51 | +# include 'postgresql.tune.conf' |
| 52 | +port = 5432 |
| 53 | +`, |
| 54 | + includeFile: "postgresql.tune.conf", |
| 55 | + shouldContain: "include_if_exists 'postgresql.tune.conf'", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "file without trailing newline", |
| 59 | + initialContent: `listen_addresses = '*'`, |
| 60 | + includeFile: "postgresql.tune.conf", |
| 61 | + shouldContain: "include_if_exists 'postgresql.tune.conf'", |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tt := range tests { |
| 66 | + t.Run(tt.name, func(t *testing.T) { |
| 67 | + tmpDir := t.TempDir() |
| 68 | + confPath := filepath.Join(tmpDir, "postgresql.conf") |
| 69 | + |
| 70 | + if err := os.WriteFile(confPath, []byte(tt.initialContent), 0644); err != nil { |
| 71 | + t.Fatalf("failed to write test file: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + err := EnsureIncludeDirective(confPath, tt.includeFile) |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("EnsureIncludeDirective() error = %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + resultContent, err := os.ReadFile(confPath) |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("failed to read result file: %v", err) |
| 82 | + } |
| 83 | + |
| 84 | + resultStr := string(resultContent) |
| 85 | + |
| 86 | + if !strings.Contains(resultStr, tt.shouldContain) { |
| 87 | + t.Errorf("Result does not contain expected string.\nExpected to contain: %s\nGot:\n%s", tt.shouldContain, resultStr) |
| 88 | + } |
| 89 | + |
| 90 | + if tt.name == "include already exists" || tt.name == "include with different syntax already exists" { |
| 91 | + count := strings.Count(resultStr, "include") |
| 92 | + if count != 1 { |
| 93 | + t.Errorf("Expected exactly 1 include directive, got %d\nContent:\n%s", count, resultStr) |
| 94 | + } |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestEnsureIncludeDirective_FileNotExist(t *testing.T) { |
| 101 | + tmpDir := t.TempDir() |
| 102 | + confPath := filepath.Join(tmpDir, "nonexistent.conf") |
| 103 | + |
| 104 | + err := EnsureIncludeDirective(confPath, "postgresql.tune.conf") |
| 105 | + if err == nil { |
| 106 | + t.Error("Expected error for non-existent file, got nil") |
| 107 | + } |
| 108 | +} |
0 commit comments