Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Modules/DSCParser/DSCParser.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'DSCParser.psm1'

# Version number of this module.
ModuleVersion = '3.1.0.6'
ModuleVersion = '3.1.0.7'

# ID used to uniquely identify this module
GUID = 'e168239a-233d-468d-9025-d6dfc0e4e2b6'
Expand Down Expand Up @@ -110,7 +110,18 @@
IconUri = 'https://github.com/Microsoft365DSC/DSCParser/blob/master/Images/DSCParser.png?raw=true'

# ReleaseNotes of this module
ReleaseNotes = '* Fixed an issue reading the Node block of a configuration parsed with registered keywords.'
ReleaseNotes = '* Fixed warning line numbers for configurations parsed with registered keywords.
* Fixed an exception reading arrays that mix values and CIM instances.
* Fixed resources lost from configurations without a Node block.
* Missing mandatory properties are now warnings naming resource and instance.
* Every Node block is now read, including blocks inside if or foreach.
* A warning now reports Node blocks with different node names.
* Resources placed beside a Node block are now read.
* Constant string and number concatenations are now converted to their value.
* Comma-separated lists without @() are now converted to arrays.
* Commands inside arrays are kept as text with a warning.
* Fixed reading a CIM property whose value starts on the next line.
* Keywords registered from a schema cache now keep empty ValueMap entries.'
} # End of PSData hashtable

} # End of PrivateData hashtable
Expand Down
761 changes: 567 additions & 194 deletions src/DSCParser.CSharp/DscParser.cs

Large diffs are not rendered by default.

36 changes: 35 additions & 1 deletion src/DSCParser.PSDSC/DscSchemaCacheKeywords.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Language;

Expand All @@ -13,6 +14,8 @@ namespace DSCParser.PSDSC
/// </summary>
internal static class DscSchemaCacheKeywords
{
public const string MissingMandatoryPropertyErrorId = "MissingValueForMandatoryProperty";

public static DynamicKeyword Build(object entry)
{
if (entry is null)
Expand Down Expand Up @@ -48,6 +51,15 @@ public static DynamicKeyword Build(object entry)
keyword.Properties.Add(property.Key, BuildProperty(property.Value));
}

List<string> mandatoryProperties = keyword.Properties
.Where(property => property.Value.Mandatory)
.Select(property => property.Key)
.ToList();
if (mandatoryProperties.Count > 0)
{
keyword.SemanticCheck = statement => CheckMandatoryProperties(statement, mandatoryProperties);
}

return keyword;
}

Expand Down Expand Up @@ -80,7 +92,7 @@ private static DynamicKeywordProperty BuildProperty(object source)
foreach (object pair in GetSequence(GetValue(source, "valueMap")))
{
string? key = GetString(pair, "key");
if (!string.IsNullOrEmpty(key))
if (key is not null)
{
property.ValueMap[key] = GetString(pair, "value");
}
Expand All @@ -89,6 +101,28 @@ private static DynamicKeywordProperty BuildProperty(object source)
return property;
}

private static ParseError[]? CheckMandatoryProperties(DynamicKeywordStatementAst statement, List<string> mandatoryProperties)
{
if (statement.CommandElements.OfType<HashtableAst>().FirstOrDefault() is not HashtableAst body)
{
return null;
}

HashSet<string> present = new(
body.KeyValuePairs.Select(pair => pair.Item1).OfType<StringConstantExpressionAst>().Select(key => key.Value),
StringComparer.OrdinalIgnoreCase);

ParseError[] errors = mandatoryProperties
.Where(name => !present.Contains(name))
.Select(name => new ParseError(
statement.CommandElements[0].Extent,
MissingMandatoryPropertyErrorId,
$"Resource '{statement.CommandElements[0].Extent.Text}' requires a value for the mandatory property '{name}'."))
.ToArray();

return errors.Length == 0 ? null : errors;
}

private static TEnum ParseEnum<TEnum>(string? value, TEnum fallback)
where TEnum : struct
{
Expand Down
16 changes: 10 additions & 6 deletions src/DSCParser.Tests/DscParserPrivateMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,11 @@ public void ProcessExpressionAst_WithArray_ShouldReturnList()
[Fact]
public void ProcessExpressionAst_WithUnknownExpression_ShouldReturnItsText()
{
var expr = FindAst<BinaryExpressionAst>("1 + 2");
var expr = FindAst<BinaryExpressionAst>("1 - 2");

var result = _processExpressionAst.Invoke(null, [expr, true]);

Assert.Equal("1 + 2", result);
Assert.Equal("1 - 2", result);
}

#endregion
Expand Down Expand Up @@ -515,13 +515,17 @@ public void ProcessMemberExpressionAst_ShouldReturnMemberText()
#region ProcessArrayExpressionAst

[Fact]
public void ProcessArrayExpressionAst_WithCommandElement_ShouldProcessItAsComplexItem()
public void ProcessArrayExpressionAst_WithCommandElement_ShouldKeepItsTextAndWarn()
{
var array = FindAst<ArrayExpressionAst>("@( Get-ChildItem )");
var array = FindAst<ArrayExpressionAst>("@( Get-Item $path )");

var result = (List<object>)_processArrayExpressionAst.Invoke(null, [array, true])!;
var warnings = CaptureWarnings(() =>
{
var result = (List<object>)_processArrayExpressionAst.Invoke(null, [array, true])!;
Assert.Equal("Get-Item $path", Assert.Single(result));
});

Assert.Single(result);
Assert.Contains("'Get-Item $path' (line 1) is not a constant expression", Assert.Single(warnings));
}

[Fact]
Expand Down
Loading