Skip to content

Commit

Permalink
Make DSC resource name check case insentive (#3632)
Browse files Browse the repository at this point in the history
Make resource name check case insensitive.
Add tests

Address #3628
  • Loading branch information
msftrubengu authored and JohnMcPMS committed Sep 19, 2023
1 parent c307ab6 commit 9ecbc52
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ ecfrbrowse
ECustom
EFGH
EFile
efileresource
endregion
ENDSESSION
EPester
Expand Down
17 changes: 17 additions & 0 deletions src/AppInstallerCLIE2ETests/ConfigureCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ public void ConfigServerUnexpectedExit()
FileAssert.DoesNotExist(targetFilePath);
}

/// <summary>
/// Resource name case insensitive test.
/// </summary>
[Test]
public void ResourceCaseInsensitive()
{
TestCommon.EnsureModuleState(Constants.SimpleTestModuleName, present: false);

var result = TestCommon.RunAICLICommand(CommandAndAgreementsAndVerbose, TestCommon.GetTestDataFile("Configuration\\ResourceCaseInsensitive.yml"));
Assert.AreEqual(0, result.ExitCode);

// The configuration creates a file next to itself with the given contents
string targetFilePath = TestCommon.GetTestDataFile("Configuration\\ResourceCaseInsensitive.txt");
FileAssert.Exists(targetFilePath);
Assert.AreEqual("Contents!", System.IO.File.ReadAllText(targetFilePath));
}

private void DeleteTxtFiles()
{
// Delete all .txt files in the test directory; they are placed there by the tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
properties:
configurationVersion: 0.2
resources:
- resource: xE2ETestResource/e2efileresource
directives:
repository: AppInstallerCLIE2ETestsRepo
settings:
Path: ${WinGetConfigRoot}\ResourceCaseInsensitive.txt
Content: Contents!
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ConfigurationUnitAndResource(
ConfigurationUnitInternal configurationUnitInternal,
DscResourceInfoInternal dscResourceInfoInternal)
{
if (configurationUnitInternal.Unit.Type != dscResourceInfoInternal.Name)
if (!configurationUnitInternal.Unit.Type.Equals(dscResourceInfoInternal.Name, StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,72 @@ public void CreateUnitProcessor_ResourceExists()
processorEnvMock.Verify();
}

/// <summary>
/// Test CreateUnitProcessor case insensitive.
/// </summary>
[Fact]
public void CreateUnitProcessor_CaseInsensitive()
{
string resourceName = "name";
string moduleName = "xModuleName";
Version version = new Version("1.0");

var processorEnvMock = new Mock<IProcessorEnvironment>();
processorEnvMock.Setup(
m => m.GetDscResource(It.Is<ConfigurationUnitInternal>(c => c.Unit.Type.Equals("Name", StringComparison.OrdinalIgnoreCase))))
.Returns(new DscResourceInfoInternal("Name", moduleName, version))
.Verifiable();

var configurationSetProcessor = new ConfigurationSetProcessor(
processorEnvMock.Object,
new ConfigurationSet());

var unit = new ConfigurationUnit
{
Type = resourceName,
};
unit.Metadata.Add("module", moduleName);
unit.Metadata.Add("version", version.ToString());

var unitProcessor = configurationSetProcessor.CreateUnitProcessor(unit);
Assert.NotNull(unitProcessor);
Assert.Equal(unit.Type, unitProcessor.Unit.Type);

processorEnvMock.Verify();
}

/// <summary>
/// Test CreateUnitProcessor case insensitive.
/// </summary>
[Fact]
public void CreateUnitProcessor_ResourceNameMismatch()
{
string resourceName = "name";
string moduleName = "xModuleName";
Version version = new Version("1.0");

var processorEnvMock = new Mock<IProcessorEnvironment>();
processorEnvMock.Setup(
m => m.GetDscResource(It.Is<ConfigurationUnitInternal>(c => c.Unit.Type == resourceName)))
.Returns(new DscResourceInfoInternal("OtherName", moduleName, version))
.Verifiable();

var configurationSetProcessor = new ConfigurationSetProcessor(
processorEnvMock.Object,
new ConfigurationSet());

var unit = new ConfigurationUnit
{
Type = resourceName,
};
unit.Metadata.Add("module", moduleName);
unit.Metadata.Add("version", version.ToString());

Assert.Throws<ArgumentException>(() => configurationSetProcessor.CreateUnitProcessor(unit));

processorEnvMock.Verify();
}

/// <summary>
/// Test CreateUnitProcessor with no version directive.
/// </summary>
Expand Down
49 changes: 40 additions & 9 deletions src/PowerShell/tests/Microsoft.WinGet.Configuration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ Describe 'Get configuration' {

Describe 'Invoke-WinGetConfiguration' {

BeforeAll {
BeforeEach {
DeleteConfigTxtFiles
}

Expand Down Expand Up @@ -430,7 +430,6 @@ Describe 'Invoke-WinGetConfiguration' {
}

It 'Piped' {
DeleteConfigTxtFiles
$testFile = GetConfigTestDataFile "Configure_TestRepo.yml"
$result = Get-WinGetConfiguration -File $testFile | Invoke-WinGetConfiguration -AcceptConfigurationAgreements
$result | Should -Not -BeNullOrEmpty
Expand All @@ -445,7 +444,6 @@ Describe 'Invoke-WinGetConfiguration' {
}

It 'Positional' {
DeleteConfigTxtFiles
$testFile = GetConfigTestDataFile "Configure_TestRepo.yml"
$set = Get-WinGetConfiguration $testFile
$set | Should -Not -BeNullOrEmpty
Expand Down Expand Up @@ -498,11 +496,28 @@ Describe 'Invoke-WinGetConfiguration' {
$expectedFile = Join-Path $(GetConfigTestDataPath) "DependentResources_Failure.txt"
Test-Path $expectedFile | Should -Be $false
}

It 'ResourceCaseInsensitive' {
$testFile = GetConfigTestDataFile "ResourceCaseInsensitive.yml"
$set = Get-WinGetConfiguration -File $testFile
$set | Should -Not -BeNullOrEmpty

$result = Invoke-WinGetConfiguration -AcceptConfigurationAgreements -Set $set
$result | Should -Not -BeNullOrEmpty
$result.ResultCode | Should -Be 0
$result.UnitResults.Count | Should -Be 1
$result.UnitResults[0].State | Should -Be "Completed"
$result.UnitResults[0].ResultCode | Should -Be 0

$expectedFile = Join-Path $(GetConfigTestDataPath) "ResourceCaseInsensitive.txt"
Test-Path $expectedFile | Should -Be $true
Get-Content $expectedFile -Raw | Should -Be "Contents!"
}
}

Describe 'Start|Complete-WinGetConfiguration' {

BeforeAll {
BeforeEach {
DeleteConfigTxtFiles
}

Expand All @@ -525,7 +540,6 @@ Describe 'Start|Complete-WinGetConfiguration' {
}

It 'From TestRepo' {
DeleteConfigTxtFiles
$testFile = GetConfigTestDataFile "Configure_TestRepo.yml"
$set = Get-WinGetConfiguration -File $testFile
$set | Should -Not -BeNullOrEmpty
Expand Down Expand Up @@ -680,11 +694,31 @@ Describe 'Start|Complete-WinGetConfiguration' {
$expectedFile = Join-Path $(GetConfigTestDataPath) "DependentResources_Failure.txt"
Test-Path $expectedFile | Should -Be $false
}

It 'ResourceCaseInsensitive' {
$testFile = GetConfigTestDataFile "ResourceCaseInsensitive.yml"
$set = Get-WinGetConfiguration -File $testFile
$set | Should -Not -BeNullOrEmpty

$job = Start-WinGetConfiguration -AcceptConfigurationAgreements -Set $set
$job | Should -Not -BeNullOrEmpty

$result = Complete-WinGetConfiguration -ConfigurationJob $job
$result | Should -Not -BeNullOrEmpty
$result.ResultCode | Should -Be 0
$result.UnitResults.Count | Should -Be 1
$result.UnitResults[0].State | Should -Be "Completed"
$result.UnitResults[0].ResultCode | Should -Be 0

$expectedFile = Join-Path $(GetConfigTestDataPath) "ResourceCaseInsensitive.txt"
Test-Path $expectedFile | Should -Be $true
Get-Content $expectedFile -Raw | Should -Be "Contents!"
}
}

Describe 'Test-WinGetConfiguration' {

BeforeAll {
BeforeEach {
DeleteConfigTxtFiles
}

Expand All @@ -702,7 +736,6 @@ Describe 'Test-WinGetConfiguration' {
}

It 'Positive' {
DeleteConfigTxtFiles
$testFile = GetConfigTestDataFile "Configure_TestRepo.yml"
$set = Get-WinGetConfiguration -File $testFile
$set | Should -Not -BeNullOrEmpty
Expand All @@ -719,7 +752,6 @@ Describe 'Test-WinGetConfiguration' {
}

It 'Piped' {
DeleteConfigTxtFiles
$testFile = GetConfigTestDataFile "Configure_TestRepo.yml"
$result = Get-WinGetConfiguration -File $testFile | Test-WinGetConfiguration -AcceptConfigurationAgreements
$result | Should -Not -BeNullOrEmpty
Expand All @@ -730,7 +762,6 @@ Describe 'Test-WinGetConfiguration' {
}

It 'Positional' {
DeleteConfigTxtFiles
$testFile = GetConfigTestDataFile "Configure_TestRepo.yml"
$set = Get-WinGetConfiguration $testFile
$set | Should -Not -BeNullOrEmpty
Expand Down

0 comments on commit 9ecbc52

Please sign in to comment.