-
Notifications
You must be signed in to change notification settings - Fork 831
Expand file tree
/
Copy pathBicepConfigurationManagerTests.cs
More file actions
752 lines (630 loc) · 35.7 KB
/
Copy pathBicepConfigurationManagerTests.cs
File metadata and controls
752 lines (630 loc) · 35.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Bicep.Core.Configuration;
using Bicep.Core.Diagnostics;
using Bicep.IO.Abstraction;
using Bicep.Testing;
using Bicep.Testing.IO;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Bicep.Core.UnitTests.Configuration
{
[TestClass]
public class BicepConfigurationManagerTests
{
// ── Helpers ──────────────────────────────────────────────────────────
private static IBicepConfigurationChain GetChain(TestFileSet fileSet, string sourceFile = "main.bicep")
{
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
return sut.GetConfigurationChain(fileSet.GetUri(sourceFile));
}
private static IDiagnostic GetSingleDiagnostic(IBicepConfigurationChain chain)
{
var diagnostics = chain.GetEffectiveConfiguration().GetDiagnostics().ToList();
diagnostics.Should().HaveCount(1);
return diagnostics[0];
}
// ── No config file ────────────────────────────────────────────────────
[TestMethod]
public void GetConfigurationChain_NoConfigFile_ReturnsBuiltInChain()
{
// Arrange — source file with no bicepconfig.json anywhere nearby.
var fileSet = InMemoryTestFileSet.Create(("main.bicep", ""));
// Act.
var chain = GetChain(fileSet);
// Assert — effective config is the built-in.
chain.GetEffectiveConfiguration().IsBuiltIn.Should().BeTrue();
chain.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
}
// ── Single config, no extends ─────────────────────────────────────────
[TestMethod]
public void GetConfigurationChain_SingleConfigNoExtends_ReturnsConfigWithNoErrors()
{
// Arrange.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "experimentalFeaturesWarning": true }"""));
// Act.
var chain = GetChain(fileSet);
// Assert.
chain.GetEffectiveConfiguration().IsBuiltIn.Should().BeFalse();
chain.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeTrue();
chain.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
}
[DataTestMethod]
[DataRow("AzureBleuCloud")]
[DataRow("AzureUSGovernment")]
public void GetConfigurationChain_CanonicalCloudProfileOverride_IsTrusted(string profileName)
{
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", $$"""
{
"cloud": {
"currentProfile": "{{profileName}}"
}
}
"""));
GetChain(fileSet).GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
}
[TestMethod]
public void GetConfigurationChain_CustomCloudProfile_ReportsUntrustedProfile()
{
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """
{
"cloud": {
"currentProfile": "Custom",
"profiles": {
"Custom": {
"resourceManagerEndpoint": "https://management.example.invalid",
"activeDirectoryAuthority": "https://login.example.invalid"
}
}
}
}
"""));
GetChain(fileSet).GetEffectiveConfiguration().GetDiagnostics()
.Should().ContainSingle(diagnostic => diagnostic.Code == "BCP456");
}
[TestMethod]
public void GetConfigurationChain_RepositoryEnvironmentCredential_DoesNotRequireSeparateTrust()
{
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "cloud": { "credentialPrecedence": ["Environment"] } }"""));
GetChain(fileSet).GetEffectiveConfiguration().GetDiagnostics()
.Should().BeEmpty();
}
[TestMethod]
public void GetConfigurationChain_ExactExternalTrustApprovesCustomProfile()
{
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """
{
"cloud": {
"currentProfile": "Custom",
"profiles": {
"Custom": {
"resourceManagerEndpoint": "https://management.example.invalid",
"activeDirectoryAuthority": "https://login.example.invalid"
}
},
"credentialPrecedence": ["Environment"]
}
}
"""));
var trustPolicy = CloudConfigurationTrustPolicy.FromEnvironmentValue("""
[{
"resourceManagerEndpoint": "https://management.example.invalid",
"activeDirectoryAuthority": "https://login.example.invalid"
}]
""");
var sut = new BicepConfigurationManager(fileSet.FileExplorer, trustPolicy);
sut.GetConfigurationChain(fileSet.GetUri("main.bicep"))
.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
}
[TestMethod]
public void GetConfigurationChain_MalformedExternalTrust_DoesNotBlockBuiltInCloud()
{
var fileSet = InMemoryTestFileSet.Create(("main.bicep", ""));
var sut = new BicepConfigurationManager(
fileSet.FileExplorer,
CloudConfigurationTrustPolicy.FromEnvironmentValue("*"));
sut.GetConfigurationChain(fileSet.GetUri("main.bicep"))
.GetEffectiveConfiguration().GetDiagnostics()
.Should().BeEmpty();
}
// ── Simple two-level extends ──────────────────────────────────────────
[TestMethod]
public void GetConfigurationChain_LeafExtendsBase_LeafWinsOnConflict()
{
// Arrange — leaf overrides experimentalFeaturesWarning from base.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """
{
"extends": "./base/bicepconfig.base.json",
"experimentalFeaturesWarning": true
}
"""),
("base/bicepconfig.base.json", """
{
"experimentalFeaturesWarning": false
}
"""));
// Act.
var chain = GetChain(fileSet);
// Assert — leaf value (true) wins over base value (false).
chain.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeTrue();
chain.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
}
[TestMethod]
public void GetConfigurationChain_LeafExtendsBase_InheritsBaseValuesNotOverridden()
{
// Arrange — leaf does not set cacheRootDirectory; base does.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """
{
"extends": "./base/bicepconfig.base.json",
"experimentalFeaturesWarning": true
}
"""),
("base/bicepconfig.base.json", """
{
"cacheRootDirectory": "/tmp/cache"
}
"""));
// Act.
var chain = GetChain(fileSet);
// Assert — base value is inherited.
chain.GetEffectiveConfiguration().CacheRootDirectory.Should().Be("/tmp/cache");
chain.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
}
// ── Full config merge — multiple sections ─────────────────────────────
[TestMethod]
public void GetConfigurationChain_FullConfigMerge_LeafWinsAndBaseInherited()
{
// Arrange:
// Leaf sets: cacheRootDirectory (overrides base), experimentalFeaturesWarning (overrides base),
// formatting.indentSize (overrides base)
// Base sets: cacheRootDirectory (overridden by leaf), experimentalFeaturesWarning (overridden by leaf),
// formatting.indentSize (overridden by leaf), formatting.width (inherited — leaf doesn't set it)
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """
{
"extends": "./base/bicepconfig.base.json",
"cacheRootDirectory": "/leaf/cache",
"experimentalFeaturesWarning": true,
"formatting": {
"indentSize": 4
}
}
"""),
("base/bicepconfig.base.json", """
{
"cacheRootDirectory": "/base/cache",
"experimentalFeaturesWarning": false,
"formatting": {
"indentSize": 8,
"width": 80
}
}
"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
// Act — load chain once.
var chain1 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
var config = chain1.GetEffectiveConfiguration();
// Assert — chain structure: leaf + base = 2 layers, no diagnostics.
chain1.LayerCount.Should().Be(2);
config.GetDiagnostics().Should().BeEmpty();
// Leaf wins on conflict.
config.CacheRootDirectory.Should().Be("/leaf/cache");
config.ExperimentalFeaturesWarning.Should().BeTrue();
config.Formatting.Data.IndentSize.Should().Be(4);
// Base value inherited — leaf didn't set formatting.width.
config.Formatting.Data.Width.Should().Be(80);
// Assert — second call returns same cached instance with same chain structure.
var chain2 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
chain2.Should().BeSameAs(chain1);
chain2.LayerCount.Should().Be(2);
// Assert — after base file changes, chain is invalidated but structure and values are still correct.
sut.PurgeCacheForAffectedChains(fileSet.GetUri("base/bicepconfig.base.json"));
var chain3 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
chain3.Should().NotBeSameAs(chain1);
// Rebuilt chain must have same structure and correct values.
chain3.LayerCount.Should().Be(2);
var configAfterRebuild = chain3.GetEffectiveConfiguration();
configAfterRebuild.GetDiagnostics().Should().BeEmpty();
configAfterRebuild.CacheRootDirectory.Should().Be("/leaf/cache");
configAfterRebuild.ExperimentalFeaturesWarning.Should().BeTrue();
configAfterRebuild.Formatting.Data.IndentSize.Should().Be(4);
configAfterRebuild.Formatting.Data.Width.Should().Be(80);
}
// ── Absolute path rejected ────────────────────────────────────────────
[TestMethod]
public void GetConfigurationChain_ExtendsAbsolutePath_EmitsBCP453()
{
// Arrange.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "extends": "/absolute/path/bicepconfig.json" }"""));
// Act.
var chain = GetChain(fileSet);
var diagnostic = GetSingleDiagnostic(chain);
// Assert.
diagnostic.Code.Should().Be("BCP453");
diagnostic.Level.Should().Be(DiagnosticLevel.Error);
}
// ── Cycle detection ───────────────────────────────────────────────────
[TestMethod]
public void GetConfigurationChain_DirectCycle_EmitsBCP454()
{
// Arrange — A extends B, B extends A.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "extends": "./other/bicepconfig.json" }"""),
("other/bicepconfig.json", """{ "extends": "../bicepconfig.json" }"""));
// Act.
var chain = GetChain(fileSet);
var diagnostic = GetSingleDiagnostic(chain);
// Assert.
diagnostic.Code.Should().Be("BCP454");
diagnostic.Level.Should().Be(DiagnosticLevel.Error);
}
// ── File not found ────────────────────────────────────────────────────
[TestMethod]
public void GetConfigurationChain_ExtendsFileMissing_EmitsUnloadableDiagnostic()
{
// Arrange — extends points to a file that doesn't exist.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "extends": "./nonexistent/bicepconfig.json" }"""));
// Act.
var chain = GetChain(fileSet);
var diagnostic = GetSingleDiagnostic(chain);
// Assert.
diagnostic.Code.Should().Be("BCP272");
diagnostic.Level.Should().Be(DiagnosticLevel.Error);
diagnostic.Message.Should().Contain("File not found");
}
// ── Invalid JSON ──────────────────────────────────────────────────────
[TestMethod]
public void GetConfigurationChain_InvalidJson_EmitsUnparsableDiagnostic()
{
// Arrange.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", "not json at all"));
// Act.
var chain = GetChain(fileSet);
var diagnostic = GetSingleDiagnostic(chain);
// Assert.
diagnostic.Code.Should().Be("BCP271");
diagnostic.Level.Should().Be(DiagnosticLevel.Error);
}
// ── Depth limit ───────────────────────────────────────────────────────
[TestMethod]
public void GetConfigurationChain_ChainExceedsMaxDepth_EmitsBCP455()
{
// Arrange — build a flat chain of 65 files all in the same directory.
// Each extends the next: bicepconfig.json -> bicepconfig1.json -> ... -> bicepconfig64.json
const int depth = 65;
var files = new List<(string, string)> { ("main.bicep", "") };
for (int i = 0; i < depth; i++)
{
var path = i == 0 ? "bicepconfig.json" : $"bicepconfig{i}.json";
var extendsLine = i < depth - 1
? $""" "extends": "./bicepconfig{i + 1}.json" """
: "";
files.Add((path, $$"""{ {{extendsLine}} }"""));
}
var fileSet = InMemoryTestFileSet.Create([.. files]);
// Act.
var chain = GetChain(fileSet);
var diagnostic = GetSingleDiagnostic(chain);
// Assert.
diagnostic.Code.Should().Be("BCP455");
diagnostic.Level.Should().Be(DiagnosticLevel.Error);
}
// ── Non-file URI ──────────────────────────────────────────────────────
[TestMethod]
public void GetConfigurationChain_NonFileUri_ReturnsBuiltInChain()
{
// Arrange — source file is a remote URI (e.g. from a registry).
var fileSet = InMemoryTestFileSet.Create();
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
var remoteUri = new IOUri(new IOUriScheme("https"), "management.azure.com", "/bicep/main.bicep");
// Act.
var chain = sut.GetConfigurationChain(remoteUri);
// Assert.
chain.GetEffectiveConfiguration().IsBuiltIn.Should().BeTrue();
chain.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
}
// ── Caching ───────────────────────────────────────────────────────────
[TestMethod]
public void GetConfigurationChain_CalledTwiceForSameSource_ReturnsSameChainInstance()
{
// Arrange.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "experimentalFeaturesWarning": true }"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
// Act.
var chain1 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
var chain2 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
// Assert — same instance returned from cache.
chain1.Should().BeSameAs(chain2);
}
[TestMethod]
public void GetConfigurationChain_AfterPurgeCache_ReturnsNewInstance()
{
// Arrange.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "experimentalFeaturesWarning": true }"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
// Act.
var chain1 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
sut.PurgeCache();
var chain2 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
// Assert — different instance after purge.
chain1.Should().NotBeSameAs(chain2);
}
// ── Real content change after purge ───────────────────────────────────
[TestMethod]
public void PurgeCacheForAffectedChains_AfterBaseFileContentChanges_PicksUpNewValues()
{
// Arrange — base starts with experimentalFeaturesWarning: false.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "extends": "./base/bicepconfig.base.json" }"""),
("base/bicepconfig.base.json", """{ "experimentalFeaturesWarning": false }"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
// Load chain — should reflect base value (false).
var chain1 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
chain1.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeFalse();
chain1.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
// Simulate base file being updated.
fileSet.AddFile("base/bicepconfig.base.json", """{ "experimentalFeaturesWarning": true }""");
// Purge the chain cache for the changed file.
sut.PurgeCacheForAffectedChains(fileSet.GetUri("base/bicepconfig.base.json"));
// Reload — must pick up the NEW value from the updated base file.
var chain2 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
chain2.Should().NotBeSameAs(chain1);
chain2.LayerCount.Should().Be(2);
chain2.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeTrue();
chain2.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
}
[TestMethod]
public void GetDependenciesForLeaf_SingleConfig_TracksSelf()
{
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "experimentalFeaturesWarning": true }"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
var deps = sut.GetDependenciesForLeaf(fileSet.GetUri("bicepconfig.json"));
deps.Should().ContainSingle().Which.Should().Be(fileSet.GetUri("bicepconfig.json"));
}
[TestMethod]
public void GetDependenciesForLeaf_LeafExtendsBase_TracksBothFiles()
{
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "extends": "./base/bicepconfig.base.json" }"""),
("base/bicepconfig.base.json", """{ "experimentalFeaturesWarning": true }"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
var deps = sut.GetDependenciesForLeaf(fileSet.GetUri("bicepconfig.json"));
deps.Should().HaveCount(2);
deps.Should().Contain(fileSet.GetUri("bicepconfig.json"));
deps.Should().Contain(fileSet.GetUri("base/bicepconfig.base.json"));
}
[TestMethod]
public void GetDependenciesForLeaf_DeepChain_TracksAllFiles()
{
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "extends": "./b/bicepconfig.b.json" }"""),
("b/bicepconfig.b.json", """{ "extends": "../c/bicepconfig.c.json" }"""),
("c/bicepconfig.c.json", """{ "experimentalFeaturesWarning": true }"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
var deps = sut.GetDependenciesForLeaf(fileSet.GetUri("bicepconfig.json"));
deps.Should().HaveCount(3);
deps.Should().Contain(fileSet.GetUri("bicepconfig.json"));
deps.Should().Contain(fileSet.GetUri("b/bicepconfig.b.json"));
deps.Should().Contain(fileSet.GetUri("c/bicepconfig.c.json"));
}
// ── PurgeCacheForAffectedChains ───────────────────────────────────────
[TestMethod]
public void PurgeCacheForAffectedChains_BaseFileChanges_InvalidatesAffectedChain()
{
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "extends": "./base/bicepconfig.base.json" }"""),
("base/bicepconfig.base.json", """{ "experimentalFeaturesWarning": true }"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
var chain1 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
// Verify chain1 has correct content before purge.
chain1.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeTrue();
chain1.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
sut.PurgeCacheForAffectedChains(fileSet.GetUri("base/bicepconfig.base.json"));
var chain2 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
// Chain was rebuilt (different instance) but content is still correct.
chain1.Should().NotBeSameAs(chain2);
chain2.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeTrue();
chain2.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
}
[TestMethod]
public void PurgeCacheForAffectedChains_UnrelatedFileChanges_DoesNotInvalidateChain()
{
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "experimentalFeaturesWarning": true }"""),
("other/other.bicep", ""),
("other/bicepconfig.json", """{ "experimentalFeaturesWarning": false }"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
var chain1 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
// Verify chain1 has correct content before purge.
chain1.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeTrue();
sut.PurgeCacheForAffectedChains(fileSet.GetUri("other/bicepconfig.json"));
var chain2 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
// Unrelated change — chain must NOT be invalidated (same instance, same values).
chain1.Should().BeSameAs(chain2);
chain2.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeTrue();
chain2.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
}
[TestMethod]
public void PurgeCacheForAffectedChains_LeafFileChanges_InvalidatesItsOwnChain()
{
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "experimentalFeaturesWarning": true }"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
var chain1 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
// Verify chain1 has correct content before purge.
chain1.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeTrue();
sut.PurgeCacheForAffectedChains(fileSet.GetUri("bicepconfig.json"));
var chain2 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
// Chain was rebuilt — different instance, but correct content.
chain1.Should().NotBeSameAs(chain2);
chain2.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeTrue();
chain2.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
}
[TestMethod]
public void PurgeCacheForAffectedChains_DeepChainBaseChanges_InvalidatesEntireChain()
{
// A extends B extends C — deepest file C changes.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "extends": "./b/bicepconfig.b.json" }"""),
("b/bicepconfig.b.json", """{ "extends": "../c/bicepconfig.c.json" }"""),
("c/bicepconfig.c.json", """{ "experimentalFeaturesWarning": true }"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
var chain1 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
// Verify chain1 has correct content — value inherited from deepest C.
chain1.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeTrue();
sut.PurgeCacheForAffectedChains(fileSet.GetUri("c/bicepconfig.c.json"));
var chain2 = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
// Chain was rebuilt — value from C still flows through correctly.
chain1.Should().NotBeSameAs(chain2);
chain2.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeTrue();
chain2.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
}
[TestMethod]
public void PurgeCacheForAffectedChains_SharedBase_InvalidatesOnlyAffectedLeaf()
{
// main extends shared; other does not — only main's chain should be purged.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "extends": "./shared/bicepconfig.shared.json" }"""),
("other/other.bicep", ""),
("other/bicepconfig.json", """{ "experimentalFeaturesWarning": false }"""),
("shared/bicepconfig.shared.json", """{ "experimentalFeaturesWarning": true }"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
var chainMain = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
var chainOther = sut.GetConfigurationChain(fileSet.GetUri("other/other.bicep"));
// Verify initial content.
chainMain.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeTrue(); // from shared
chainOther.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeFalse(); // its own value
sut.PurgeCacheForAffectedChains(fileSet.GetUri("shared/bicepconfig.shared.json"));
var chainMainAfter = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
var chainOtherAfter = sut.GetConfigurationChain(fileSet.GetUri("other/other.bicep"));
// main's chain was rebuilt — different instance, correct content.
chainMain.Should().NotBeSameAs(chainMainAfter);
chainMainAfter.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeTrue();
chainMainAfter.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
// other's chain was untouched — same instance, correct content.
chainOther.Should().BeSameAs(chainOtherAfter);
chainOtherAfter.GetEffectiveConfiguration().ExperimentalFeaturesWarning.Should().BeFalse();
chainOtherAfter.GetEffectiveConfiguration().GetDiagnostics().Should().BeEmpty();
}
// ── moduleAliasesMock relative path provenance ────────────────────────
[TestMethod]
public void GetConfigurationChain_AliasInLeaf_DeclaringConfigUriIsLeaf()
{
// Alias declared in the leaf — DeclaringConfigUri should be the leaf URI.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """
{
"moduleAliasesMock": {
"br": {
"shared": { "mapToFilePath": "../modules" }
}
}
}
"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
var chain = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
var alias = chain.GetEffectiveConfiguration().ModuleAliasesMock
.TryGetOciArtifactModuleAliasMock("shared");
alias.IsSuccess(out var mockAlias, out _).Should().BeTrue();
mockAlias!.DeclaringConfigUri.Should().Be(fileSet.GetUri("bicepconfig.json"));
}
[TestMethod]
public void GetConfigurationChain_AliasInBase_DeclaringConfigUriIsBase()
{
// Alias declared only in the base — DeclaringConfigUri should be the base URI, not the leaf.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """{ "extends": "./base/bicepconfig.base.json" }"""),
("base/bicepconfig.base.json", """
{
"moduleAliasesMock": {
"br": {
"shared": { "mapToFilePath": "../modules" }
}
}
}
"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
var chain = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
var alias = chain.GetEffectiveConfiguration().ModuleAliasesMock
.TryGetOciArtifactModuleAliasMock("shared");
alias.IsSuccess(out var mockAlias, out _).Should().BeTrue();
// Must be the base URI — leaf doesn't declare this alias.
mockAlias!.DeclaringConfigUri.Should().Be(fileSet.GetUri("base/bicepconfig.base.json"));
}
[TestMethod]
public void GetConfigurationChain_AliasOverriddenInLeaf_DeclaringConfigUriIsLeaf()
{
// Both leaf and base declare the same alias — leaf wins, so DeclaringConfigUri is the leaf.
var fileSet = InMemoryTestFileSet.Create(
("main.bicep", ""),
("bicepconfig.json", """
{
"extends": "./base/bicepconfig.base.json",
"moduleAliasesMock": {
"br": {
"shared": { "mapToFilePath": "./leaf-modules" }
}
}
}
"""),
("base/bicepconfig.base.json", """
{
"moduleAliasesMock": {
"br": {
"shared": { "mapToFilePath": "./base-modules" }
}
}
}
"""));
var sut = new BicepConfigurationManager(fileSet.FileExplorer, BicepTestConstants.TestCloudConfigurationTrustPolicy);
var chain = sut.GetConfigurationChain(fileSet.GetUri("main.bicep"));
var alias = chain.GetEffectiveConfiguration().ModuleAliasesMock
.TryGetOciArtifactModuleAliasMock("shared");
alias.IsSuccess(out var mockAlias, out _).Should().BeTrue();
// Leaf overrides the alias — declaring URI must be the leaf.
mockAlias!.DeclaringConfigUri.Should().Be(fileSet.GetUri("bicepconfig.json"));
mockAlias.MapToFilePath.Should().Be("./leaf-modules");
}
}
}