Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,8 @@ func findKeyPos(node ast.Node, parts []string) (int, int) {
}
case *ast.DocumentNode:
return findKeyPos(n.Body, parts)
case *ast.AnchorNode:
return findKeyPos(n.Value, parts)
default:
// Node is a scalar or unexpected type
// Return the node's own token so the position points to the
Expand Down Expand Up @@ -905,6 +907,8 @@ func findKeyAtLine(node ast.Node, targetLine int, prefix string) string {
}
case *ast.DocumentNode:
return findKeyAtLine(n.Body, targetLine, prefix)
case *ast.AnchorNode:
return findKeyAtLine(n.Value, targetLine, prefix)
}

return ""
Expand Down
33 changes: 33 additions & 0 deletions parser_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,36 @@ func TestDecodeUnknownFieldAliasChain(t *testing.T) {
t.Fatalf("unexpected decoded result: name=%q, wrong types=%v, errors=%v", document.Name, wrongTypes, results)
}
}

func TestGetPositionInAnchoredMapping(t *testing.T) {
tests := []struct {
name string
fixture string
key string
line, column int
}{
{"block mapping", "testdata/v0/invalid/anchored_mapping_positions/block_mapping.yml", "legal.license", 2, 3},
{"flow mapping", "testdata/v0/invalid/anchored_mapping_positions/flow_mapping.yml", "legal.license", 1, 16},
{"nested anchors", "testdata/v0/invalid/anchored_mapping_positions/nested_anchors.yml", "description[en].shortDescription", 3, 5},
{"sequence entry", "testdata/v0/invalid/anchored_mapping_positions/sequence_entry.yml", "maintenance.contacts[0].name", 4, 7},
{"scalar anchor", "testdata/v0/invalid/anchored_mapping_positions/scalar_anchor.yml", "legal.license", 1, 15},
{"alias reference", "testdata/v0/invalid/anchored_mapping_positions/alias_reference.yml", "legal.license", 2, 8},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
source, err := os.ReadFile(test.fixture)
if err != nil {
t.Fatal(err)
}

file, err := parser.ParseBytes(source, 0)
if err != nil {
t.Fatal(err)
}
line, column := getPositionInFile(test.key, file)
if line != test.line || column != test.column {
t.Errorf("got %d:%d, want %d:%d", line, column, test.line, test.column)
}
})
}
}
20 changes: 20 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,23 @@ func TestParseReportsInputFileNameOnWarnings(t *testing.T) {
t.Fatalf("expected warnings, got %v", results)
}
}

func TestAnchoredMappingDiagnosticPosition(t *testing.T) {
for _, fixture := range []string{"legal_license_deprecated.yml", "legal_license_deprecated_anchored.yml"} {
t.Run(fixture, func(t *testing.T) {
p, err := NewParser(ParserConfig{DisableNetwork: true})
if err != nil {
t.Fatal(err)
}
_, err = p.Parse("testdata/v0/valid_with_warnings/anchored_mapping_positions/" + fixture)
var results ValidationResults
if !errors.As(err, &results) || len(results) != 1 {
t.Fatalf("expected one license warning, got %v", err)
}
warning, ok := errors.AsType[ValidationWarning](results[0])
if !ok || warning.Key != "legal.license" || warning.Line != 39 || warning.Column != 3 {
t.Fatalf("expected legal.license warning at 39:3, got %#v", results[0])
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
other: &legal {license: GPL-3.0}
legal: *legal
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
legal: &legal
license: GPL-3.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
legal: &legal {license: GPL-3.0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description: &descriptions
en: &english
shortDescription: Test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
legal: &legal scalar
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
maintenance:
contacts:
- &contact
name: Test
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
publiccodeYmlVersion: "0"

name: Medusa
url: "https://example.com/software.git"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

description:
en-GB:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Just one feature

legal: &legal
license: [GPL-3.0]

maintenance:
type: "community"

contacts:
- name: Alex Example

localisation:
localisationReady: true
availableLanguages:
- en
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
publiccodeYmlVersion: "0"

name: Medusa
url: "https://example.com/software.git"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

description:
en-GB:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Just one feature

legal:
license: GPL-3.0

maintenance:
type: "community"

contacts:
- name: Alex Example

localisation:
localisationReady: true
availableLanguages:
- en
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
publiccodeYmlVersion: "0"

name: Medusa
url: "https://example.com/software.git"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

description:
en-GB:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Just one feature

legal: &legal
license: GPL-3.0

maintenance:
type: "community"

contacts:
- name: Alex Example

localisation:
localisationReady: true
availableLanguages:
- en
5 changes: 5 additions & 0 deletions v0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ func TestInvalidTestcasesV0_NoNetwork(t *testing.T) {
ValidationError{File: "", Key: "maintenance.zzzMaintenance", Description: "unknown field \"zzzMaintenance\"", Line: 44, Column: 3},
},

// legal
"legal_license_wrong_type_anchored.yml": ValidationResults{
ValidationError{File: "", Key: "legal.license", Description: "wrong type for this field", Line: 39, Column: 3},
},

// supports
"supports_id_invalid.yml": ValidationResults{
ValidationError{File: "", Key: "supports[0].id", Description: "id must be a known alias ('alias:<name>') or a valid URI (URL or URN)", Line: 17, Column: 5},
Expand Down