Skip to content

Commit

Permalink
Changes to Meta.Tests.ps1
Browse files Browse the repository at this point in the history
- Adding opt-in common test for spellchecking markdown files. Opt-in by
  adding "Common Tests - Spellcheck Markdown Files" in the file
  .MetaTestOptIn.json (issue PowerShell#211).
  • Loading branch information
johlju committed Aug 13, 2018
1 parent 8e28363 commit 4e2b623
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 54 deletions.
220 changes: 166 additions & 54 deletions Meta.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -864,68 +864,70 @@ Describe 'Common Tests - Validate Markdown Files' -Tag 'Markdown' {
}
}

if (Test-Path -Path (Join-Path -Path $repoRootPath -ChildPath '.markdownlint.json'))
{
Write-Verbose -Message ('Using markdownlint settings file from repository folder ''{0}''.' -f $repoRootPath) -Verbose
$markdownlintSettingsFilePath = Join-Path -Path $repoRootPath -ChildPath '.markdownlint.json'
}
else
{
Write-Verbose -Message 'Using markdownlint settings file from DscResource.Test repository.' -Verbose
$markdownlintSettingsFilePath = $null
}

It "Should not have errors in any markdown files" {

$mdErrors = 0
try
Context 'When there are markdown files' {
if (Test-Path -Path (Join-Path -Path $repoRootPath -ChildPath '.markdownlint.json'))
{
Write-Verbose -Message ('Using markdownlint settings file from repository folder ''{0}''.' -f $repoRootPath) -Verbose
$markdownlintSettingsFilePath = Join-Path -Path $repoRootPath -ChildPath '.markdownlint.json'
}
else
{
Write-Verbose -Message 'Using markdownlint settings file from DscResource.Test repository.' -Verbose
$markdownlintSettingsFilePath = $null
}

$gulpArgumentList = @(
'test-mdsyntax',
'--silent',
'--rootpath',
$repoRootPath,
'--dscresourcespath',
$dscResourcesFolderFilePath
)
It "Should not have errors in any markdown files" {

if ($markdownlintSettingsFilePath)
$mdErrors = 0
try
{
$gulpArgumentList += @(
'--settingspath',
$markdownlintSettingsFilePath

$gulpArgumentList = @(
'test-mdsyntax',
'--silent',
'--rootpath',
$repoRootPath,
'--dscresourcespath',
$dscResourcesFolderFilePath
)
}

Start-Process -FilePath "gulp" -ArgumentList $gulpArgumentList `
-Wait -WorkingDirectory $PSScriptRoot -PassThru -NoNewWindow
Start-Sleep -Seconds 3
$mdIssuesPath = Join-Path -Path $PSScriptRoot -ChildPath "markdownissues.txt"
if ($markdownlintSettingsFilePath)
{
$gulpArgumentList += @(
'--settingspath',
$markdownlintSettingsFilePath
)
}

if ((Test-Path -Path $mdIssuesPath) -eq $true)
{
Get-Content -Path $mdIssuesPath | ForEach-Object -Process {
if ([string]::IsNullOrEmpty($_) -eq $false)
{
Write-Warning -Message $_
$mdErrors ++
Start-Process -FilePath 'gulp' -ArgumentList $gulpArgumentList `
-Wait -WorkingDirectory $PSScriptRoot -PassThru -NoNewWindow
Start-Sleep -Seconds 3
$mdIssuesPath = Join-Path -Path $PSScriptRoot -ChildPath 'markdownissues.txt'

if ((Test-Path -Path $mdIssuesPath) -eq $true)
{
Get-Content -Path $mdIssuesPath | ForEach-Object -Process {
if ([string]::IsNullOrEmpty($_) -eq $false)
{
Write-Warning -Message $_
$mdErrors ++
}
}
}
Remove-Item -Path $mdIssuesPath -Force -ErrorAction SilentlyContinue
}
catch [System.Exception]
{
Write-Warning -Message ('Unable to run gulp to test markdown files. Please ' + `
'be sure that you have installed nodejs and have ' + `
'run ''npm install -g gulp'' in order to have this ' + `
'test execute.')
}
Remove-Item -Path $mdIssuesPath -Force -ErrorAction SilentlyContinue
}
catch [System.Exception]
{
Write-Warning -Message ("Unable to run gulp to test markdown files. Please " + `
"be sure that you have installed nodejs and have " + `
"run 'npm install -g gulp' in order to have this " + `
"text execute.")
}

if ($optIn)
{
$mdErrors | Should -Be 0
if ($optIn)
{
$mdErrors | Should -Be 0
}
}
}

Expand Down Expand Up @@ -993,8 +995,118 @@ Describe 'Common Tests - Validate Markdown Files' -Tag 'Markdown' {
}
else
{
Write-Warning -Message ("Unable to run gulp to test markdown files. Please " + `
"be sure that you have installed nodejs and npm in order " + `
"to have this text execute.")
Write-Warning -Message ('Unable to run gulp to test markdown files. Please ' + `
'be sure that you have installed nodejs and npm in order ' + `
'to have this text execute.')
}
}

Describe 'Common Tests - Spellcheck Files' -Tag 'Spellcheck' {
BeforeAll {
$npmParametersForStartProcess = @{
FilePath = 'npm'
ArgumentList = ''
WorkingDirectory = $PSScriptRoot
Wait = $true
WindowStyle = 'Hidden'
}
}

if ((Get-Command -Name 'npm' -ErrorAction SilentlyContinue))
{
$skipDependency = $false
}
else
{
Write-Warning -Message ('Unable to run cSpell to spellcheck markdown files. Please ' + `
'be sure that you have installed nodejs and npm in order ' + `
'to have this text execute.')

$skipDependency = $true
}

Context 'When installing spellcheck dependencies' {
It 'Should not throw an error when installing package cSpell in global scope' -Skip:$skipDependency {
{
# More information about cSpell: https://www.npmjs.com/package/cspell
$npmParametersForStartProcess['ArgumentList'] = 'install -g cspell'
Start-Process @npmParametersForStartProcess
} | Should -Not -Throw
}
}

# If npm wasn't installed then we can't run this test.
$optIn = -not $skipDependency -and (Get-OptInStatus -OptIns $optIns -Name 'Common Tests - Spellcheck Markdown Files')

Context 'When there are markdown files' {
$errorFileName = 'SpellingErrors.txt'

It 'Should not have spelling errors in any markdown files' -Skip:(!$optIn) {
$spellcheckSettingsFilePath = Join-Path -Path $repoRootPath -ChildPath '.vscode\cSpell.json'
if (Test-Path -Path $spellcheckSettingsFilePath)
{
Write-Info -Message ('Using spellcheck settings file ''{0}''.' -f $spellcheckSettingsFilePath)
}
else
{
$spellcheckSettingsFilePath = $null
}

$cSpellArgumentList = @(
'"**/*.md"',
'--no-color'
)

if ($spellcheckSettingsFilePath)
{
$cSpellArgumentList += @(
'--config',
$spellcheckSettingsFilePath
)
}

# This must be last, we send output to the error file.
$cSpellArgumentList += @(
('>{0}' -f $errorFileName)
)

$startProcessParameters = @{
FilePath = 'cspell'
ArgumentList = $cSpellArgumentList
Wait = $true
PassThru = $true
NoNewWindow = $true
}

$process = Start-Process @startProcessParameters
$process.ExitCode | Should -Be 0
} -ErrorVariable itBlockError

# If the It-block did not pass the test, output the spelling errors.
if ($itBlockError.Count -ne 0)
{
$misspelledErrors = Get-Content -Path $errorFileName

foreach ($misspelledError in $misspelledErrors)
{
$message = '{0}' -f $misspelledError
Write-Host -BackgroundColor Yellow -ForegroundColor Black -Object $message
}
}

# Make sure we always remove the file if it exist.
if (Test-Path $errorFileName)
{
Remove-Item -Path $errorFileName -Force
}
}

Context 'When uninstalling spellcheck dependencies' {
It 'Should not throw an error when uninstalling package cSpell in global scope' -Skip:$skipDependency {
{
$npmParametersForStartProcess['ArgumentList'] = 'uninstall -g cspell'
Start-Process @npmParametersForStartProcess
} | Should -Not -Throw
}
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ The following opt-in flags are available:
new error-level script analyzer rules are violated.
- **Common Tests - Custom Script Analyzer Rules**: fail tests if any
custom script analyzer rules are violated.
* **Common Tests - Spellcheck Markdown Files**: fail test if there are any
spelling errors in the markdown files. There is the possibility to add
or override words in the `.vscode\cSpell.json` file.

### Using AppVeyor.psm1 with the default shared model

Expand Down Expand Up @@ -1024,6 +1027,9 @@ Contributors that add or change an example to be published must make sure that
this repository will never have examples.
- Added Rule Name to PS Script Analyzer custom rules
- Added PsScript Analyzer Rule Name to Write-Warning output in meta.tests
* Adding opt-in common test for spellchecking markdown files. Opt-in by
adding "Common Tests - Spellcheck Markdown Files" in the file
.MetaTestOptIn.json ([issue #211](https://github.com/PowerShell/DscResource.Tests/issues/211)).

### 0.2.0.0

Expand Down

0 comments on commit 4e2b623

Please sign in to comment.