Skip to content

Commit bc53d7d

Browse files
work
1 parent e045f3c commit bc53d7d

File tree

3 files changed

+18
-29
lines changed

3 files changed

+18
-29
lines changed

src/Bicep.Cli.IntegrationTests/UseRecentModuleVersionsIntegrationTests.cs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -111,33 +111,11 @@ private async Task<CliResult> Test(Options options)
111111
return await Bicep(settings, "lint", mainFile, options.NoRestore ? "--no-restore" : null);
112112
}
113113

114-
[TestMethod] //asdfgasdfgasdfg2 should be failing exception is swallowed
115-
public async Task IfLevelIsOff_ShouldNotDownloadModuleMetadata()
116-
{
117-
var moduleIndexClientMock = PublicRegistryModuleIndexClientMock.CreateToThrow(new Exception("shouldn't try to download metadata if rule is off"));
118-
var result = await Test(new Options(CacheRoot)
119-
{
120-
Bicep = """
121-
module m1 '{PREFIX}/fake/avm/res/app/container-app:0.2.0' = {
122-
name: 'm1'
123-
}
124-
""".Replace("{PREFIX}", PREFIX),
125-
DiagnosticLevel = "off",
126-
PublishedModules = [$"{PREFIX}/fake/avm/res/app/container-app:0.2.0"],
127-
MetadataClient = moduleIndexClientMock.Object,
128-
});
129-
130-
result.Should().NotHaveStderr();
131-
result.Should().HaveStdout("");
132-
result.Should().Succeed();
133-
134-
moduleIndexClientMock.Verify(client => client.GetModuleIndexAsync(), Times.Never, "shouldn't try to download metadata if rule is off");
135-
}
136-
137114
[TestMethod]
138115
// We don't currently cache to disk, but rather on every check to restore modules.
139-
public async Task IfNoRestoreSpecified_ThenShouldFailBecauseNoCache()
116+
public async Task IfNoRestoreSpecified_ThenShouldNotDownloadMetadata_AndShouldFailBecauseNoCache()
140117
{
118+
var moduleIndexClientMock = PublicRegistryModuleIndexClientMock.CreateToThrow(new Exception("shouldn't try to download metadata --no-restore is set"));
141119
var result = await Test(new Options(CacheRoot)
142120
{
143121
Bicep = """
@@ -147,13 +125,16 @@ public async Task IfNoRestoreSpecified_ThenShouldFailBecauseNoCache()
147125
""".Replace("{PREFIX}", PREFIX),
148126
PublishedModules = [$"{PREFIX}/fake/avm/res/app/container-app:0.2.0"],
149127
ModulesMetadata = [("fake/avm/res/app/container-app", ["0.2.0"])],
128+
MetadataClient = moduleIndexClientMock.Object,
150129
NoRestore = true,
151130
});
152131

153132
result.Should().HaveStderrMatch($"*Error {NotRestoredErrorCode}: The artifact with reference \"br:mcr.microsoft.com/bicep/fake/avm/res/app/container-app:0.2.0\" has not been restored.*");
154133
result.Should().HaveStderrMatch("*Warning use-recent-module-versions: Available module versions have not yet been downloaded. If running from the command line, be sure --no-restore is not specified.*");
155134
result.Should().HaveStdout("");
156135
result.Should().Fail();
136+
137+
moduleIndexClientMock.Verify(client => client.GetModuleIndexAsync(), Times.Never, "shouldn't try to download metadata --no-restore is set");
157138
}
158139

159140
[TestMethod]

src/Bicep.Core/Analyzers/Linter/Rules/UseRecentModuleVersionsRule.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ private static IEnumerable<Failure> GetFailures(SemanticModel model, IServicePro
118118

119119
private static IEnumerable<Failure> AnalyzeBicepModule(IPublicRegistryModuleMetadataProvider publicRegistryModuleMetadataProvider, ModuleDeclarationSyntax moduleSyntax, TextSpan errorSpan, string tag, string publicModulePath)
120120
{
121+
// NOTE: We don't want linter tests to download anything during analysis. So metadata is loaded
122+
// and cached during module restore. So don't use the Get*Async methods of IPublicRegistryModuleMetadataProvider,
123+
// just the GetCached* methods
121124
var availableVersions = publicRegistryModuleMetadataProvider.GetCachedModuleVersions($"{LanguageConstants.BicepPublicMcrPathPrefix}{publicModulePath}")
122125
.Select(v => v.Version)
123126
.ToArray();

src/Bicep.Core/Registry/OciArtifactRegistry.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public sealed class OciArtifactRegistry : ExternalArtifactRegistry<OciArtifactRe
3535

3636
private readonly IFeatureProvider features;
3737

38-
private readonly IPublicRegistryModuleMetadataProvider/*asdfg or indexer?*/ publicRegistryModuleMetadataProvider;
38+
private readonly IPublicRegistryModuleMetadataProvider publicRegistryModuleMetadataProvider;
3939

4040
public OciArtifactRegistry(
4141
IFileResolver FileResolver,
@@ -218,10 +218,15 @@ private OciManifest GetCachedManifest(OciArtifactReference ociArtifactModuleRefe
218218

219219
public override async Task OnRestoreArtifacts(bool forceRestore)
220220
{
221-
// We call this here because we don't want to run this code at all if we're running from the command line and --no-restore has been specified.
222-
// So, if we're restoring artifacts, it's safe to also query for public module metadata
223-
//asdfg use global options instead?
224-
await publicRegistryModuleMetadataProvider.TryAwaitCache(forceRestore); //asdfg why are we waiting for the cache? asdfg should we be using the indexer instead?
221+
// We don't want linter tests to download anything during analysis. So we are downloading
222+
// metadata here to avoid downloading during analysis, and tests can use cached data if it
223+
// exists (e.g. IRegistryModuleMetadataProvider.GetCached* methods).
224+
// If --no-restore has been specified on the command ine, we don't want to download anything at all.
225+
// Therefore we do the cache download here so that lint rules can have access to the cached metadata.
226+
// CONSIDER: Revisit if it's okay to download metadata during analysis? This will be more of a problem
227+
// when we extend the linter rules to include private registry modules.
228+
229+
await publicRegistryModuleMetadataProvider.TryAwaitCache(forceRestore);
225230
}
226231

227232
public override async Task<IDictionary<ArtifactReference, DiagnosticBuilder.DiagnosticBuilderDelegate>> RestoreArtifacts(IEnumerable<OciArtifactReference> references)

0 commit comments

Comments
 (0)