diff --git a/parser.go b/parser.go index f23a384..dae8d71 100644 --- a/parser.go +++ b/parser.go @@ -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 @@ -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 "" diff --git a/parser_internal_test.go b/parser_internal_test.go index 6b4bf51..4ef01fe 100644 --- a/parser_internal_test.go +++ b/parser_internal_test.go @@ -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) + } + }) + } +} diff --git a/parser_test.go b/parser_test.go index 07b642d..668dbf5 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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]) + } + }) + } +} diff --git a/testdata/v0/invalid/anchored_mapping_positions/alias_reference.yml b/testdata/v0/invalid/anchored_mapping_positions/alias_reference.yml new file mode 100644 index 0000000..7134c07 --- /dev/null +++ b/testdata/v0/invalid/anchored_mapping_positions/alias_reference.yml @@ -0,0 +1,2 @@ +other: &legal {license: GPL-3.0} +legal: *legal diff --git a/testdata/v0/invalid/anchored_mapping_positions/block_mapping.yml b/testdata/v0/invalid/anchored_mapping_positions/block_mapping.yml new file mode 100644 index 0000000..ecb1a44 --- /dev/null +++ b/testdata/v0/invalid/anchored_mapping_positions/block_mapping.yml @@ -0,0 +1,2 @@ +legal: &legal + license: GPL-3.0 diff --git a/testdata/v0/invalid/anchored_mapping_positions/flow_mapping.yml b/testdata/v0/invalid/anchored_mapping_positions/flow_mapping.yml new file mode 100644 index 0000000..fc9b2d1 --- /dev/null +++ b/testdata/v0/invalid/anchored_mapping_positions/flow_mapping.yml @@ -0,0 +1 @@ +legal: &legal {license: GPL-3.0} diff --git a/testdata/v0/invalid/anchored_mapping_positions/nested_anchors.yml b/testdata/v0/invalid/anchored_mapping_positions/nested_anchors.yml new file mode 100644 index 0000000..a1c24ec --- /dev/null +++ b/testdata/v0/invalid/anchored_mapping_positions/nested_anchors.yml @@ -0,0 +1,3 @@ +description: &descriptions + en: &english + shortDescription: Test diff --git a/testdata/v0/invalid/anchored_mapping_positions/scalar_anchor.yml b/testdata/v0/invalid/anchored_mapping_positions/scalar_anchor.yml new file mode 100644 index 0000000..c48445d --- /dev/null +++ b/testdata/v0/invalid/anchored_mapping_positions/scalar_anchor.yml @@ -0,0 +1 @@ +legal: &legal scalar diff --git a/testdata/v0/invalid/anchored_mapping_positions/sequence_entry.yml b/testdata/v0/invalid/anchored_mapping_positions/sequence_entry.yml new file mode 100644 index 0000000..dd8c6c2 --- /dev/null +++ b/testdata/v0/invalid/anchored_mapping_positions/sequence_entry.yml @@ -0,0 +1,4 @@ +maintenance: + contacts: + - &contact + name: Test diff --git a/testdata/v0/invalid/no-network/legal_license_wrong_type_anchored.yml b/testdata/v0/invalid/no-network/legal_license_wrong_type_anchored.yml new file mode 100644 index 0000000..456c1d9 --- /dev/null +++ b/testdata/v0/invalid/no-network/legal_license_wrong_type_anchored.yml @@ -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 diff --git a/testdata/v0/valid_with_warnings/anchored_mapping_positions/legal_license_deprecated.yml b/testdata/v0/valid_with_warnings/anchored_mapping_positions/legal_license_deprecated.yml new file mode 100644 index 0000000..9dd418f --- /dev/null +++ b/testdata/v0/valid_with_warnings/anchored_mapping_positions/legal_license_deprecated.yml @@ -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 diff --git a/testdata/v0/valid_with_warnings/anchored_mapping_positions/legal_license_deprecated_anchored.yml b/testdata/v0/valid_with_warnings/anchored_mapping_positions/legal_license_deprecated_anchored.yml new file mode 100644 index 0000000..2bd1362 --- /dev/null +++ b/testdata/v0/valid_with_warnings/anchored_mapping_positions/legal_license_deprecated_anchored.yml @@ -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 diff --git a/v0_test.go b/v0_test.go index cd1a4ef..4a3f070 100644 --- a/v0_test.go +++ b/v0_test.go @@ -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:') or a valid URI (URL or URN)", Line: 17, Column: 5},