diff --git a/test/powershell/Common/Meta.Tests.ps1 b/test/powershell/Common/Meta.Tests.ps1 new file mode 100644 index 00000000000..02672d6e6b2 --- /dev/null +++ b/test/powershell/Common/Meta.Tests.ps1 @@ -0,0 +1,117 @@ +using namespace System.Text.RegularExpressions + +# We run the Meta tests only on AppVeyor CI and locally +if ($env:TRAVIS) { + $skipTests = $true +} + +# Get a root repository folder +try { + if ($env:APPVEYOR) { + $rootPath = $env:APPVEYOR_BUILD_FOLDER + } else { + # Local test + $rootPath = Join-Path $PSScriptRoot $(git rev-parse --show-cdup) + } +} catch { + # Skip tests if no GIT + $skipTests = $true +} + +if (!$skipTests) { + + # We check only files with following extensions + $extensions = "*.ps1", "*.psm1", "*.cs", ".resx" + + # Output warnings with failed files + $IsWarning = $true + + $AllFiles = $false + + if ($AllFiles) { + # We check all files only on demand + $checkPaths = Get-ChildItem -Path $rootPath -Include $extensions -Recurse | Select-Object -ExpandProperty FullName + } else { + # On CI we check only files changed in PR + $checkPaths = @(git diff --name-only origin/master..) -match $extensionRegex | ForEach-Object { Resolve-Path (Join-Path $rootPath $_) } + } + + $WrongEncodedFiles = $TabsInFiles = $EmptyFiles = $NoNewlineFiles = $NoHttpsFiles = @() + $tabsRegEx = [Regex]::new('(?m)^\s*[\t]+\s*\w*|[ \t]+\r?$', [RegexOptions]::Multiline+[RegexOptions]::Compiled) + + # The regex catch itself so mask it + $strHttp = '(?m)HelpUri[ \r\n]*?=[ \r\n]*?"http' + $strHttp += '://.*?"' + $HttpRegEx = [Regex]::new($strHttp, [RegexOptions]::Multiline+[RegexOptions]::Compiled) + + foreach ($file in $checkPaths) { + + Write-Host "Check file: $file" + $text = [System.IO.File]::ReadAllText($file) + if ($text -eq "") { + $EmptyFiles += ,$file + continue + } + + if ($tabsRegEx.Match($text).Success) { + $TabsInFiles += ,$file + } + + if ($HttpRegEx.Match($text).Success) { + $NoHttpsFiles += ,$file + } + + if ($text[-1] -ne "`n") { + $NoNewlineFiles += ,$file + } + + } +} + +Describe 'Common Tests - File Formatting' -Tags "CI" { + BeforeAll { + $defaultParamValues = $PSDefaultParameterValues.Clone() + $PSDefaultParameterValues["It:Skip"] = $skipTests + } + + AfterAll { + $global:PSDefaultParameterValues = $defaultParamValues + + if ($IsWarning) { + if ($WrongEncodedFiles.Count -gt 0) { + Write-Warning "Wrong Encoded Files: $($WrongEncodedFiles -join [Environment]::Newline)" + } + if ($TabsInFiles.Count -gt 0) { + Write-Warning "Files with leading tabs and trailing spaces and tabs: $($TabsInFiles -join [Environment]::Newline)" + } + if ($EmptyFiles.Count -gt 0) { + Write-Warning "Empty Files: $($EmptyFiles -join [Environment]::Newline)" + } + if ($NoNewlineFiles.Count -gt 0) { + Write-Warning "Files without Newline in the End: $($NoNewlineFiles -join [Environment]::Newline)" + } + if ($NoHttpsFiles.Count -gt 0) { + Write-Warning "Files with HTTP links: $($NoHttpsFiles -join [Environment]::Newline)" + } + } + } + + It "Should not contain any files with non-Unicode file encoding" -Pending:$true <#-Skip:$skipTests#> { + $WrongEncodedFiles.Count | Should Be 0 + } + + It 'Should not contain any files with leading tab characters and trailing spaces and tab characters' { + $TabsInFiles.Count | Should Be 0 + } + + It 'Should not contain empty files' { + $EmptyFiles.Count | Should Be 0 + } + + It 'Should not contain files without a newline at the file end' { + $NoNewlineFiles.Count | Should Be 0 + } + It 'Should not contain files with HTTP links' { + $NoHttpsFiles.Count | Should Be 0 + } +} diff --git a/test/powershell/Common/MetaTest-Empty.cs b/test/powershell/Common/MetaTest-Empty.cs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/powershell/Common/MetaTest-Good.cs b/test/powershell/Common/MetaTest-Good.cs new file mode 100644 index 00000000000..5054d6e8a50 --- /dev/null +++ b/test/powershell/Common/MetaTest-Good.cs @@ -0,0 +1,3 @@ +T e x t 1 +T e x t 2 +T e x t 3 diff --git a/test/powershell/Common/MetaTest-HttpInHelpURI.cs b/test/powershell/Common/MetaTest-HttpInHelpURI.cs new file mode 100644 index 00000000000..a03b5595edd --- /dev/null +++ b/test/powershell/Common/MetaTest-HttpInHelpURI.cs @@ -0,0 +1,6 @@ + [Cmdlet(VerbsCommon.Get, + GetCimAssociatedInstanceCommand.Noun, + DefaultParameterSetName = CimBaseCommand.ComputerSetName, + HelpUri = + "http://go.microsoft.com/fwlink/?LinkId=227958")] + [OutputType(typeof(CimInstance))] diff --git a/test/powershell/Common/MetaTest-HttpInResx.cs b/test/powershell/Common/MetaTest-HttpInResx.cs new file mode 100644 index 00000000000..036ec0982aa --- /dev/null +++ b/test/powershell/Common/MetaTest-HttpInResx.cs @@ -0,0 +1 @@ + diff --git a/test/powershell/Common/MetaTest-LeadingTabs.cs b/test/powershell/Common/MetaTest-LeadingTabs.cs new file mode 100644 index 00000000000..694119e92d4 --- /dev/null +++ b/test/powershell/Common/MetaTest-LeadingTabs.cs @@ -0,0 +1,3 @@ +T e x t 1 + T e x t 2 +T e x t 3 diff --git a/test/powershell/Common/MetaTest-NoNewline.cs b/test/powershell/Common/MetaTest-NoNewline.cs new file mode 100644 index 00000000000..d8e28c7dfab --- /dev/null +++ b/test/powershell/Common/MetaTest-NoNewline.cs @@ -0,0 +1,3 @@ +T e x t 1 +T e x t 2 +T e x t 3 \ No newline at end of file diff --git a/test/powershell/Common/MetaTest-TailingTabs.cs b/test/powershell/Common/MetaTest-TailingTabs.cs new file mode 100644 index 00000000000..f32efa8a3bd --- /dev/null +++ b/test/powershell/Common/MetaTest-TailingTabs.cs @@ -0,0 +1,3 @@ +T e x t 1 +T e x t 2 +T e x t 3