Skip to content

Commit

Permalink
appveyor and pester
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Butler committed Jun 1, 2018
1 parent 5d3cd5a commit 4cf4cdf
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 0 deletions.
1 change: 1 addition & 0 deletions .GITIGNORE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
25 changes: 25 additions & 0 deletions AppVeyor/builddocs.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Import-Module -Name "$env:APPVEYOR_BUILD_FOLDER\CTXXD-Replicate" -Force -Verbose
$verbs = (Get-Command -Module CTXXD-Replicate).Verb | Select-Object -Unique

foreach ($verb in $verbs)
{
$data = @()
$data += "$verb Commands"
$data += '========================='
$data += ''
$data += "This page contains details on **$verb** commands."
$data += ''
foreach ($help in (Get-Command -Module CTXXD-Replicate | Where-Object -FilterScript {
$_.name -like "$verb-*"
}))
{
$data += $help.Name
$data += '-------------------------'
$data += ''
$data += Get-Help -Name $help.name -Detailed
$data += ''
}

$data | Out-File -FilePath "$env:APPVEYOR_BUILD_FOLDER\docs\cmd_$($verb.ToLower()).rst" -Encoding utf8
Write-Output " cmd_$($verb.ToLower())"
}
75 changes: 75 additions & 0 deletions AppVeyor/deploy.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
[CmdletBinding()]
param(

)
# Line break for readability in AppVeyor console
Write-Host -Object ''
Import-Module posh-git -Force

if ($env:APPVEYOR_REPO_BRANCH -ne 'master')
{
Write-Warning -Message "Skipping version increment and publish for branch $env:APPVEYOR_REPO_BRANCH"
}
elseif ($env:APPVEYOR_PULL_REQUEST_NUMBER -gt 0)
{
Write-Warning -Message "Skipping version increment and publish for pull request #$env:APPVEYOR_PULL_REQUEST_NUMBER"
}
else
{
Try {
$updates = Get-ChildItem $env:APPVEYOR_BUILD_FOLDER -Filter "*.psd1" -Recurse

$pubme = $false
if($updates.count -gt 0)
{
Write-Verbose $updates
foreach ($update in $updates)
{
$localver = Test-ModuleManifest $update.fullname
$psgallerver = Find-Module $localver.name -Repository PSgallery
if ($psgallerver.version -le $localver.version)
{
Write-Verbose "Updating version and publishing to PSgallery"
$fileVersion = $localver.Version
$newVersion = "{0}.{1}.{2}" -f $fileVersion.Major, $fileVersion.Minor, ($fileVersion.Build + 1)
$funcs = Get-ChildItem -path $env:APPVEYOR_BUILD_FOLDER\ctxal-sdk\Public|select-object basename|sort-object basename
Update-ModuleManifest -Path $update.fullname -ModuleVersion $newVersion -FunctionsToExport $funcs.basename
Publish-Module -Path $update.Directoryname -NuGetApiKey $env:PSGKey
$pubme = $true
}
else
{
Write-Warning "$($localver.name) not found on PSgallery or PSgallery version higher"
}
}
}
else {
Write-Warning "Nothing to update"
}
}
catch {
Write-Warning "Version update failed"
throw $_
}

Try
{
if($pubme)
{
git checkout master
git add --all
git commit -m "PSGallery Version Update to $newVersion"
git push origin master
Write-Verbose "Repo has been pushed to github"
}
else {
Write-Verbose "Nothing to push"
}
}
Catch
{
Write-Warning "Github push failed"
throw $_
}

}
82 changes: 82 additions & 0 deletions Tests/Main.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
Function Test-Encoding
{
[CmdletBinding()]
Param (
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
[string]$Path
)
$contents = new-object byte[] 3
$stream = [System.IO.File]::OpenRead($path)
$stream.Read($contents, 0, 3)|out-null
$stream.Close()
($contents[0] -eq 0x66 -and $contents[1] -eq 0x75 -and $contents[2] -eq 0x6E) -or ($contents[0] -eq 0x20 -and $contents[1] -eq 0x66 -and $contents[2] -eq 0x75) -or ($contents[0] -eq 0x24 -and $contents[1] -eq 0x50 -and $contents[2] -eq 0x75)

}

$projectRoot = $env:APPVEYOR_BUILD_FOLDER


Describe "General project validation" {

$scripts = Get-ChildItem "$projectRoot\ctxal-sdk\" -Recurse -Include *.ps1,*.psm1

# TestCases are splatted to the script so we need hashtables
$testCase = $scripts | Foreach-Object{@{file=$_}}
It "Script <file> should be valid powershell" -TestCases $testCase {
param($file)

$file.fullname | Should Exist

$contents = Get-Content -Path $file.fullname -ErrorAction Stop
$errors = $null
$null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors)
$errors.Count | Should Be 0
}

$testCase = $scripts | Foreach-Object{@{file=$_}}
It "Script <file> should be UTF-8" -TestCases $testCase {
param($file)
Test-Encoding -Path $file.fullname|should -Be 'TRUE'
}

$scriptAnalyzerRules = Get-ScriptAnalyzerRule
It "<file> should pass ScriptAnalyzer" -TestCases $testCase {
param($file)
$analysis = Invoke-ScriptAnalyzer -Path $file.fullname -Severity @('Warning','Error')

forEach ($rule in $scriptAnalyzerRules) {
if ($analysis.RuleName -contains $rule) {
$analysis |
Where-Object RuleName -EQ $rule -outvariable failures |
Out-Default
$failures.Count | Should Be 0
}

}
}

}

Describe "Function validation" {

$scripts = Get-ChildItem "$projectRoot\ctxal-sdk\" -Recurse -Include *.ps1
$testCase = $scripts | Foreach-Object{@{file=$_}}
It "Script <file> should only contain one function" -TestCases $testCase {
param($file)
$file.fullname | Should Exist
$contents = Get-Content -Path $file.fullname -ErrorAction Stop
$describes = [Management.Automation.Language.Parser]::ParseInput($contents, [ref]$null, [ref]$null)
$test = $describes.FindAll({$args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]}, $true)
$test.Count | Should Be 1
}

It "<file> should match function name and contain -AL" -TestCases $testCase {
param($file)
$file.fullname | Should Exist
$contents = Get-Content -Path $file.fullname -ErrorAction Stop
$describes = [Management.Automation.Language.Parser]::ParseInput($contents, [ref]$null, [ref]$null)
$test = $describes.FindAll({$args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]}, $true)
$test[0].name | Should Be $file.basename
$test[0].name | Should BeLike "*-AL*"
}
}
12 changes: 12 additions & 0 deletions Tests/ctxal-sdk.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$manifest = "$env:APPVEYOR_BUILD_FOLDER\ctxal-sdk\ctxal-sdk.psd1"
$module = "$env:APPVEYOR_BUILD_FOLDER\ctxal-sdk\ctxal-sdk.psm1"

Describe 'Module Metadata Validation' {
it 'Script fileinfo should be ok' {
{Test-ModuleManifest $manifest -ErrorAction Stop} | Should Not Throw
}

it 'Import module should be ok'{
{Import-Module $module -Force -ErrorAction Stop} | Should Not Throw
}
}
49 changes: 49 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
version: 1.0.{build}
os: WMF 5
skip_commits:
files:
- README.md
message: /updated readme.*|update readme.*s|update docs.*|PSGallery Version.*|update appveyor.*/
only_commits:
files:
- ctxal-sdk/
pull_requests:
do_not_increment_build_number: true
build_script:
- ps: |
Install-PackageProvider -Name NuGet -Force | Out-Null
find-module -Repository PSGallery -Name PowerShellGet | Out-Null
Install-Module -Name PowerShellGet -Force -Repository PSGallery | Out-Null
find-module -Repository PSGallery -Name PSScriptAnalyzer | Out-Null
Install-Module -Name PSScriptAnalyzer -Force -Repository PSGallery | Out-Null
find-module -Repository PSGallery -Name Pester | Out-Null
Install-Module -Name Pester -Force -Repository PSGallery | Out-Null
find-module -Repository PSGallery -Name PSDeploy | Out-Null
Install-Module -Name PSDeploy -Force -Repository PSGallery | Out-Null
find-module -Repository PSGallery -Name posh-git | Out-Null
Install-Module -Name posh-git -Force -Repository PSGallery | Out-Null
$env:Path += ";$env:ProgramFiles\Git\cmd"
test_script:
- ps: |
write-verbose "Running Pester..."
$pest = Invoke-Pester -passthru -verbose
if ($pest.FailedCount -gt 0) {
throw "$($pest.FailedCount) tests failed."
}
deploy_script:
- git config --global credential.helper store
- ps: Add-Content "$env:USERPROFILE\.git-credentials" "https://$($env:github):x-oauth-basic@github.com`n"
- git config --global user.email "ryan@ryancbutler.com"
- git config --global user.name "Ryan Butler"
- git config --global core.autocrlf false
- git config --global core.safecrlf false
- ps: |
write-verbose "Building Docs..."
. .\AppVeyor\builddocs.ps1 -verbose
Write-Verbose "Deploying..."
. .\AppVeyor\deploy.ps1 -verbose
environment:
PSGKey:
secure: XbgpBX+B+lBgfWt4379Ez1823W4nLM2zPlOWWvw8zGrvKmZD7/uIlpLYrW4zpjMb
github:
secure: qJwCE47jamnSv6Au8wD9CcJmovghdkcm/qoX0IjWa3SDGaeZkGQlQMUlfW9RlsHe

0 comments on commit 4cf4cdf

Please sign in to comment.