Skip to content

Commit 6e72b27

Browse files
display origin of deprecated value in diagnostic
1 parent d85121f commit 6e72b27

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

internal/command/format/diagnostic.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,16 @@ func (f *snippetFormatter) write() {
253253
}
254254
fmt.Fprintf(buf, " on %s line %d%s:\n", diag.Range.Filename, diag.Range.Start.Line, contextStr)
255255
f.writeSnippet(snippet, code)
256+
257+
if diag.DeprecationOriginRange != nil && diag.DeprecationOriginSnippet != nil {
258+
var depContextStr string
259+
if diag.DeprecationOriginSnippet.Context != nil {
260+
depContextStr = fmt.Sprintf(", in %s", *snippet.Context)
261+
}
262+
buf.WriteByte('\n')
263+
fmt.Fprintf(buf, " (origin of deprecation on %s line %d%s):\n", diag.DeprecationOriginRange.Filename, diag.DeprecationOriginRange.Start.Line, depContextStr)
264+
f.writeSnippet(diag.DeprecationOriginSnippet, diag.DeprecationOriginSnippet.Code)
265+
}
256266
}
257267

258268
buf.WriteByte('\n')

internal/command/format/diagnostic_test.go

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,44 @@ func TestDiagnostic(t *testing.T) {
405405
[red]│[reset]
406406
[red]│[reset] Example crash
407407
[red]╵[reset]
408+
`,
409+
},
410+
"warning from deprecation": {
411+
&hcl.Diagnostic{
412+
Severity: hcl.DiagWarning,
413+
Summary: "Deprecation detected",
414+
Detail: "Countermeasures must be taken.",
415+
Subject: &hcl.Range{
416+
Filename: "test.tf",
417+
Start: hcl.Pos{Line: 1, Column: 6, Byte: 5},
418+
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
419+
},
420+
Extra: &tfdiags.DeprecationOriginDiagnosticExtra{
421+
Origin: &tfdiags.SourceRange{
422+
Filename: "deprecated.tf",
423+
Start: tfdiags.SourcePos{Line: 1, Column: 11, Byte: 10},
424+
End: tfdiags.SourcePos{Line: 1, Column: 22, Byte: 21},
425+
},
426+
},
427+
},
428+
`[yellow]╷[reset]
429+
[yellow]│[reset] [bold][yellow]Warning: [reset][bold]Deprecation detected[reset]
430+
[yellow]│[reset]
431+
[yellow]│[reset] on test.tf line 1:
432+
[yellow]│[reset] 1: test [underline]source[reset] code
433+
[yellow]│[reset]
434+
[yellow]│[reset] (origin of deprecation on deprecated.tf line 1):
435+
[yellow]│[reset] 1: source of [underline]deprecation[reset]
436+
[yellow]│[reset]
437+
[yellow]│[reset] Countermeasures must be taken.
438+
[yellow]╵[reset]
408439
`,
409440
},
410441
}
411442

412443
sources := map[string][]byte{
413-
"test.tf": []byte(`test source code`),
444+
"test.tf": []byte(`test source code`),
445+
"deprecated.tf": []byte(`source of deprecation`),
414446
}
415447

416448
// This empty Colorize just passes through all of the formatting codes
@@ -424,8 +456,9 @@ func TestDiagnostic(t *testing.T) {
424456
diag := diags[0]
425457
got := strings.TrimSpace(Diagnostic(diag, sources, colorize, 40))
426458
want := strings.TrimSpace(test.Want)
427-
if got != want {
428-
t.Errorf("wrong result\ngot:\n%s\n\nwant:\n%s\n\n", got, want)
459+
460+
if diff := cmp.Diff(got, want); diff != "" {
461+
t.Errorf("wrong result\ngot:\n%s\n\nwant:\n%s\n\ndiff:\n%s\n\n", got, want, diff)
429462
}
430463
})
431464
}
@@ -713,12 +746,44 @@ Error: Bad bad bad
713746
1: test source code
714747
715748
Whatever shall we do?
749+
`,
750+
},
751+
752+
"warning from deprecation": {
753+
&hcl.Diagnostic{
754+
Severity: hcl.DiagWarning,
755+
Summary: "Deprecation detected",
756+
Detail: "Countermeasures must be taken.",
757+
Subject: &hcl.Range{
758+
Filename: "test.tf",
759+
Start: hcl.Pos{Line: 1, Column: 6, Byte: 5},
760+
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
761+
},
762+
Extra: &tfdiags.DeprecationOriginDiagnosticExtra{
763+
Origin: &tfdiags.SourceRange{
764+
Filename: "deprecated.tf",
765+
Start: tfdiags.SourcePos{Line: 1, Column: 11, Byte: 10},
766+
End: tfdiags.SourcePos{Line: 1, Column: 22, Byte: 21},
767+
},
768+
},
769+
},
770+
`
771+
Warning: Deprecation detected
772+
773+
on test.tf line 1:
774+
1: test source code
775+
776+
(origin of deprecation on deprecated.tf line 1):
777+
1: source of deprecation
778+
779+
Countermeasures must be taken.
716780
`,
717781
},
718782
}
719783

720784
sources := map[string][]byte{
721-
"test.tf": []byte(`test source code`),
785+
"test.tf": []byte(`test source code`),
786+
"deprecated.tf": []byte(`source of deprecation`),
722787
}
723788

724789
for name, test := range tests {
@@ -728,8 +793,8 @@ Whatever shall we do?
728793
diag := diags[0]
729794
got := strings.TrimSpace(DiagnosticPlain(diag, sources, 40))
730795
want := strings.TrimSpace(test.Want)
731-
if got != want {
732-
t.Errorf("wrong result\ngot:\n%s\n\nwant:\n%s\n\n", got, want)
796+
if diff := cmp.Diff(got, want); diff != "" {
797+
t.Errorf("wrong result\ngot:\n%s\n\nwant:\n%s\n\n,diff:\n%s\n\n", got, want, diff)
733798
}
734799
})
735800
}

0 commit comments

Comments
 (0)