DscWorkshop icon indicating copy to clipboard operation
DscWorkshop copied to clipboard

Add a Yaml validation in the Build script

Open ykuijs opened this issue 5 years ago • 4 comments

When you make an error in a Yaml file, the MOF compilation process fails without a nice error message. Would be great to add a Yaml validation step in the pre-Build steps.

ykuijs avatar Oct 08 '20 13:10 ykuijs

I used to have this one: https://github.com/gaelcolas/DscInfraSample/blob/master/Tests/01%20ConfigData/ConfigData.Tests.ps1#L19

Is it not in this repo @raandree ?

gaelcolas avatar Oct 08 '20 13:10 gaelcolas

I can see the same line in this repo as well: https://github.com/dsccommunity/DscWorkshop/blob/09257aa9e9e8e0e75e24b11bd87b044fe66cc7a8/DSC/Tests/ConfigData/ConfigData.Tests.ps1#L19

But I just made a mistake in a Yaml file and this didn't fire......

ykuijs avatar Oct 08 '20 14:10 ykuijs

Have been reviewing the ConfigData.Tests file. I noticed that this file checks the Datum.yml file, all Node yml files and the Role yml files. All other Yml files, like location, environment and any other custom layers aren't checked.

I have fixed this issue by adding this test to the file:

$allDefinitions = Get-ChildItem $here\..\..\DscConfigData -Recurse -Include *.yml
Describe 'Validate All Definition Files' -Tag Integration {
    $allDefinitions.ForEach{
        # A Node cannot be empty
        $content = Get-Content -Path $_ -Raw
        $fileName = $_.Name

        It "'$fileName' is a valid yaml" {
            { $content | ConvertFrom-Yaml } | Should -Not -Throw
        }
    }
}

ykuijs avatar Oct 09 '20 06:10 ykuijs

Thanks, @ykuijs, I have added the test.

I am wondering that you did run into this error. If there is a syntax error in one of the yaml files, 'Get-FilteredConfigurationData' should throw an error, one that is not very intuitive. I have added this additionally to https://github.com/gaelcolas/DscInfraSample/blob/master/Tests/01%20ConfigData/ConfigData.Tests.ps1:

$configurationData = try {
    Get-FilteredConfigurationData -Datum $datum -Filter $filter
}
catch { 
    Write-Error "'Get-FilteredConfigurationData' did not return any data. Please check if all YAML files are valid and don't have syntax errors"
}

raandree avatar Oct 10 '20 16:10 raandree