TestFormatter_FormatFile tests single file formatting
(t *testing.T)
| 24 | |
| 25 | // TestFormatter_FormatFile tests single file formatting |
| 26 | func TestFormatter_FormatFile(t *testing.T) { |
| 27 | tmpDir := t.TempDir() |
| 28 | |
| 29 | tests := []struct { |
| 30 | name string |
| 31 | filename string |
| 32 | content string |
| 33 | expectChanged bool |
| 34 | expectError bool |
| 35 | errorContains string |
| 36 | }{ |
| 37 | { |
| 38 | name: "valid SQL file - basic SELECT", |
| 39 | filename: "query.sql", |
| 40 | content: "SELECT * FROM users WHERE active = true", |
| 41 | expectChanged: true, // Formatter adds indentation and newlines |
| 42 | expectError: false, |
| 43 | }, |
| 44 | { |
| 45 | name: "valid empty file", |
| 46 | filename: "empty.sql", |
| 47 | content: "", |
| 48 | expectChanged: false, |
| 49 | expectError: false, |
| 50 | }, |
| 51 | { |
| 52 | name: "SQL needing formatting", |
| 53 | filename: "unformatted.sql", |
| 54 | content: "select*from users", |
| 55 | expectChanged: true, |
| 56 | expectError: false, |
| 57 | }, |
| 58 | { |
| 59 | name: "invalid SQL", |
| 60 | filename: "invalid.sql", |
| 61 | content: "SELECT * FROM", |
| 62 | expectChanged: false, |
| 63 | expectError: true, |
| 64 | errorContains: "parsing failed", |
| 65 | }, |
| 66 | } |
| 67 | |
| 68 | for _, tt := range tests { |
| 69 | t.Run(tt.name, func(t *testing.T) { |
| 70 | // Create test file |
| 71 | filepath := filepath.Join(tmpDir, tt.filename) |
| 72 | if err := os.WriteFile(filepath, []byte(tt.content), 0644); err != nil { |
| 73 | t.Fatalf("Failed to create test file: %v", err) |
| 74 | } |
| 75 | |
| 76 | // Create formatter |
| 77 | var outBuf, errBuf bytes.Buffer |
| 78 | formatter := NewFormatter(&outBuf, &errBuf, CLIFormatterOptions{ |
| 79 | IndentSize: 2, |
| 80 | Uppercase: true, |
| 81 | Compact: false, |
| 82 | }) |
| 83 |
nothing calls this directly
no test coverage detected