Fix repetitive diagnosis output in init response (#1890)

Signed-off-by: buraksenn <buraksenb@gmail.com>
This commit is contained in:
Burak Şen 2024-09-05 14:36:18 +03:00 committed by GitHub
parent 1741058111
commit 7a02fad996
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 75 additions and 2 deletions

View File

@ -292,8 +292,7 @@ func (c *InitCommand) Run(args []string) int {
// Now, we can check the diagnostics from the early configuration and the
// backend.
diags = diags.Append(earlyConfDiags)
diags = diags.Append(backDiags)
diags = diags.Append(earlyConfDiags.StrictDeduplicateMerge(backDiags))
if earlyConfDiags.HasErrors() {
c.Ui.Error(strings.TrimSpace(errInitConfigError))
c.showDiagnostics(diags)

View File

@ -2995,6 +2995,33 @@ func TestInit_moduleVersion(t *testing.T) {
})
}
func TestInit_invalidExtraLabel(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("init-syntax-invalid-extra-label"), td)
defer testChdir(t, td)()
ui := cli.NewMockUi()
view, _ := testView(t)
m := Meta{
Ui: ui,
View: view,
}
c := &InitCommand{
Meta: m,
}
if code := c.Run(nil); code == 0 {
t.Fatalf("succeeded, but was expecting error\nstdout:\n%s\nstderr:\n%s", ui.OutputWriter, ui.ErrorWriter)
}
errStr := ui.ErrorWriter.String()
splitted := strings.Split(errStr, "Error: Unsupported block type")
if len(splitted) != 2 {
t.Fatalf("want exactly one unsupported block type errors but got: %d\nstderr:\n%s\n\nstdout:\n%s", len(splitted)-1, errStr, ui.OutputWriter.String())
}
}
// newMockProviderSource is a helper to succinctly construct a mock provider
// source that contains a set of packages matching the given provider versions
// that are available for installation (from temporary local files).

View File

@ -0,0 +1,10 @@
terraform {
extra_label{
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0.2"
}
}
}
}

View File

@ -58,11 +58,19 @@ type Description struct {
Detail string
}
func (d Description) Equal(other Description) bool {
return d.Address == other.Address && d.Summary == other.Summary && d.Detail == other.Detail
}
type Source struct {
Subject *SourceRange
Context *SourceRange
}
func (s Source) Equal(other Source) bool {
return s.Subject.Equal(other.Subject) && s.Context.Equal(other.Context)
}
type FromExpr struct {
Expression hcl.Expression
EvalContext *hcl.EvalContext

View File

@ -101,6 +101,23 @@ func (diags Diagnostics) Append(new ...interface{}) Diagnostics {
return diags
}
func (diags Diagnostics) StrictDeduplicateMerge(other Diagnostics) Diagnostics {
if len(diags) == 0 {
return other
}
for _, d := range diags {
for _, o := range other {
isEqual := d.Description().Equal(o.Description()) && d.Severity() == o.Severity() && d.Source().Equal(o.Source())
if DoNotConsolidateDiagnostic(d) || DoNotConsolidateDiagnostic(o) || !isEqual {
diags = append(diags, o)
}
}
}
return diags
}
// HasErrors returns true if any of the diagnostics in the list have
// a severity of Error.
func (diags Diagnostics) HasErrors() bool {

View File

@ -16,10 +16,22 @@ type SourceRange struct {
Start, End SourcePos
}
func (r *SourceRange) Equal(other *SourceRange) bool {
if r == nil || other == nil {
return r == other
}
return r.Filename == other.Filename && r.Start.Equal(other.Start) && r.End.Equal(other.End)
}
type SourcePos struct {
Line, Column, Byte int
}
func (p SourcePos) Equal(other SourcePos) bool {
return p.Line == other.Line && p.Column == other.Column && p.Byte == other.Byte
}
// StartString returns a string representation of the start of the range,
// including the filename and the line and column numbers.
func (r SourceRange) StartString() string {