|
| 1 | +package view_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/gruntwork-io/terragrunt/internal/view" |
| 7 | + "github.com/gruntwork-io/terragrunt/internal/view/diagnostic" |
| 8 | + "github.com/gruntwork-io/terragrunt/test/helpers/venvtest" |
| 9 | + "github.com/hashicorp/hcl/v2" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestHumanRenderDiagnosticsWithoutSourceCode(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + testCases := []struct { |
| 17 | + diag *diagnostic.Diagnostic |
| 18 | + name string |
| 19 | + expected string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "range and snippet missing", |
| 23 | + diag: &diagnostic.Diagnostic{ |
| 24 | + Severity: diagnostic.DiagnosticSeverity(hcl.DiagError), |
| 25 | + Summary: "Failed to read configuration", |
| 26 | + Detail: "The file could not be opened.", |
| 27 | + }, |
| 28 | + expected: "╷\n" + |
| 29 | + "│ Error: Failed to read configuration\n" + |
| 30 | + "│\n" + |
| 31 | + "│ The file could not be opened.\n" + |
| 32 | + "╵\n", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "snippet missing", |
| 36 | + diag: &diagnostic.Diagnostic{ |
| 37 | + Range: &diagnostic.Range{ |
| 38 | + Filename: "terragrunt.hcl", |
| 39 | + Start: diagnostic.Pos{Line: 7, Column: 1, Byte: 42}, |
| 40 | + End: diagnostic.Pos{Line: 7, Column: 9, Byte: 50}, |
| 41 | + }, |
| 42 | + Severity: diagnostic.DiagnosticSeverity(hcl.DiagError), |
| 43 | + Summary: "Unsupported block type", |
| 44 | + Detail: "Blocks of type \"foo\" are not expected here.", |
| 45 | + }, |
| 46 | + expected: "╷\n" + |
| 47 | + "│ Error: Unsupported block type\n" + |
| 48 | + "│\n" + |
| 49 | + "│ on terragrunt.hcl line 7:\n" + |
| 50 | + "│ (source code not available)\n" + |
| 51 | + "│ Blocks of type \"foo\" are not expected here.\n" + |
| 52 | + "╵\n", |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tc := range testCases { |
| 57 | + t.Run(tc.name, func(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + |
| 60 | + render := view.NewHumanRender(venvtest.New(), true) |
| 61 | + |
| 62 | + actual, err := render.Diagnostics(diagnostic.Diagnostics{tc.diag}) |
| 63 | + require.NoError(t, err) |
| 64 | + require.Equal(t, tc.expected, actual) |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
0 commit comments