Skip to content

Commit 10fc5cc

Browse files
authored
Fix SourceArchive.FindDocumentLinks (#16939)
`SourceArchive.FindDocumentLinks` should return an empty array instead of throwing an exception if no document links are found. Closes #16935. ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/16939)
1 parent 7154384 commit 10fc5cc

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/Bicep.Core.UnitTests/SourceLink/SourceArchiveTests.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,81 @@ public void GetSourceFiles_ShouldGiveError_ForIncompatibleNewerVersion()
720720
ex!.Message.Should().StartWith("This source code was published with a newer, incompatible version of Bicep (0.whatever.0). You are using version ");
721721
}
722722

723+
[TestMethod]
724+
public void FindDocumentLinks_DocumentLinksNotFound_ReturnsEmptyArray()
725+
{
726+
var result = CreateSourceArchiveResult(
727+
(
728+
"__metadata.json",
729+
"""
730+
{
731+
"entryPoint": "child.bicep",
732+
"metadataVersion": 1,
733+
"bicepVersion": "0.34.1",
734+
"sourceFiles": [
735+
{
736+
"path": "child.bicep",
737+
"archivePath": "files/child.bicep",
738+
"kind": "bicep"
739+
}
740+
],
741+
"documentLinks": {}
742+
}
743+
"""
744+
),
745+
(
746+
"child.bicep",
747+
"bicep contents"
748+
)
749+
).UnwrapOrThrow();
750+
751+
var documentLinks = result.FindDocumentLinks("child.bicep");
752+
753+
documentLinks.Should().BeEmpty();
754+
}
755+
756+
[TestMethod]
757+
public void FindDocumentLinks_DocumentLinksFound_ReturnsNonEmptyArray()
758+
{
759+
var result = CreateSourceArchiveResult(
760+
(
761+
"__metadata.json",
762+
"""
763+
{
764+
"entryPoint": "child.bicep",
765+
"metadataVersion": 1,
766+
"bicepVersion": "0.34.1",
767+
"sourceFiles": [
768+
{
769+
"path": "child.bicep",
770+
"archivePath": "files/child.bicep",
771+
"kind": "bicep"
772+
}
773+
],
774+
"documentLinks": {
775+
"dummy.bicep": [
776+
{
777+
"range": "[0:1]-[2:4]",
778+
"target": "foobar.bicep"
779+
}
780+
]
781+
}
782+
}
783+
"""
784+
),
785+
(
786+
"child.bicep",
787+
"bicep contents"
788+
)
789+
).UnwrapOrThrow();
790+
791+
var documentLinks = result.FindDocumentLinks("dummy.bicep");
792+
793+
documentLinks.Should().HaveCount(1);
794+
documentLinks[0].Range.ToString().Should().Be("[0:1]-[2:4]");
795+
documentLinks[0].Target.Should().Be("foobar.bicep");
796+
}
797+
723798
private ResultWithException<SourceArchive> CreateSourceArchiveResult(params (string relativePath, string contents)[] files)
724799
{
725800
var stream = new MemoryStream();

src/Bicep.Core/SourceLink/SourceArchive.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ public static SourceArchive CreateFor(Uri entrypointFileUri, IDirectoryHandle? c
219219
return new SourceArchive(metadata, fileEntries.ToImmutableDictionary());
220220
}
221221

222-
public ImmutableArray<SourceCodeDocumentPathLink> FindDocumentLinks(string path) => this.metadata.DocumentLinks[path];
222+
public ImmutableArray<SourceCodeDocumentPathLink> FindDocumentLinks(string path) =>
223+
this.metadata.DocumentLinks.TryGetValue(path, out var documentLinks) ? documentLinks : [];
223224

224225
public LinkedSourceFile FindSourceFile(string path)
225226
{

0 commit comments

Comments
 (0)