Skip to content

Commit

Permalink
tox.ini: Create
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb committed Jun 20, 2019
1 parent 845f613 commit 0da8306
Show file tree
Hide file tree
Showing 104 changed files with 6,704 additions and 11,165 deletions.
53 changes: 53 additions & 0 deletions .ci/AppVeyorPSLint.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Import-Module -Name 'PsScriptAnalyzer' -Force
$base = $global:PSScriptRoot

. "$base\Stop-Function.ps1"
. "$base\Test-FunctionInterrupt.ps1"
. "$base\Invoke-DbatoolsFormatter.ps1"

$ScriptAnalyzerResult = Invoke-ScriptAnalyzer -Setting $base\PSScriptAnalyzerSettings.psd1 -Path $pwd -Recurse

if ($ScriptAnalyzerResult) {
$ScriptAnalyzerResultString = $ScriptAnalyzerResult |
Out-String

Write-Warning $ScriptAnalyzerResultString
}


if ($env:APPVEYOR) {
Import-Module "$base\Export-NUnitXml.psm1"
Export-NUnitXml -ScriptAnalyzerResult $ScriptAnalyzerResult -Path ".\ScriptAnalyzerResult.xml"
(New-Object 'System.Net.WebClient').UploadFile(
"https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)",
(Resolve-Path .\ScriptAnalyzerResult.xml)
)
}

if ($ScriptAnalyzerResult) {
# Failing the build
Throw 'Build failed because there was one or more PSScriptAnalyzer violation. See test results for more information.'
}

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False

Get-ChildItem -Recurse -Force '*.ps*' | ForEach-Object {
# Ignore .git, virtualenv activate.ps1, copies, and templates
if (!($_.Name -eq 'Export-NUnitXml.psm1' -or
$_.FullName.Contains('.git') -or
$_.Name -eq 'activate.ps1' -or
$_.Name -eq 'install.ps1' -or
$_.Name -eq 'Invoke-DbatoolsFormatter.ps1' -or
$_.Name -eq 'Stop-Function.ps1' -or
$_.Name -eq 'Test-FunctionInterrupt.ps1' -or
$_.Name -eq 'constants.ps1.jj2')) {
Invoke-DbatoolsFormatter $_
$content = Get-Content -Raw $_
if (!($content.EndsWith("`n"))) {
$content = ($content + "`n")
[System.IO.File]::WriteAllText($_, $content, $Utf8NoBomEncoding)
}
}
}

git diff --exit-code
107 changes: 107 additions & 0 deletions .ci/Export-NUnitXml.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
Function Export-NUnitXml {
<#
.SYNOPSIS
Takes results from PSScriptAnalyzer and exports them as a Pester test results file (NUnitXml format).
.DESCRIPTION
Takes results from PSScriptAnalyzer and exports them as a Pester test results file (NUnit XML schema).
Because the generated file in NUnit-compatible, it can be consumed and published by most continuous integration tools.
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory, Position=0)]
[AllowNull()]
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]]$ScriptAnalyzerResult,

[Parameter(Mandatory, Position=1)]
[string]$Path
)

$TotalNumber = If ($ScriptAnalyzerResult) { $ScriptAnalyzerResult.Count -as [string] } Else { '1' }
$FailedNumber = If ($ScriptAnalyzerResult) { $ScriptAnalyzerResult.Count -as [string] } Else { '0' }
$Now = Get-Date
$FormattedDate = Get-Date $Now -Format 'yyyy-MM-dd'
$FormattedTime = Get-Date $Now -Format 'T'
$User = $env:USERNAME
$MachineName = $env:COMPUTERNAME
$Cwd = $pwd.Path
$UserDomain = $env:USERDOMAIN
$OS = Get-CimInstance -ClassName Win32_OperatingSystem
$Platform = $OS.Caption
$OSVersion = $OS.Version
$ClrVersion = $PSVersionTable.CLRVersion.ToString()
$CurrentCulture = (Get-Culture).Name
$UICulture = (Get-UICulture).Name

Switch ($ScriptAnalyzerResult) {
$Null { $TestResult = 'Success'; $TestSuccess = 'True'; Break}
Default { $TestResult = 'Failure'; $TestSuccess = 'False'}
}

$Header = @"
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<test-results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="nunit_schema_2.5.xsd" name="PSScriptAnalyzer" total="$TotalNumber" errors="0" failures="$FailedNumber" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="$FormattedDate" time="$FormattedTime">
<environment user="$User" machine-name="$MachineName" cwd="$Cwd" user-domain="$UserDomain" platform="$Platform" nunit-version="2.5.8.0" os-version="$OSVersion" clr-version="$ClrVersion" />
<culture-info current-culture="$CurrentCulture" current-uiculture="$UICulture" />
<test-suite type="Powershell" name="PSScriptAnalyzer" executed="True" result="$TestResult" success="$TestSuccess" time="0.0" asserts="0">
<results>
<test-suite type="TestFixture" name="PSScriptAnalyzer" executed="True" result="$TestResult" success="$TestSuccess" time="0.0" asserts="0" description="PSScriptAnalyzer">
<results>`n
"@

$Footer = @"
</results>
</test-suite>
</results>
</test-suite>
</test-results>
"@

If ( -not($ScriptAnalyzerResult) ) {

$TestDescription = 'All PowerShell files pass the specified PSScriptAnalyzer rules'
$TestName = "PSScriptAnalyzer.{0}" -f $TestDescription

$Body = @"
<test-case description="$TestDescription" name="$TestName" time="0.0" asserts="0" success="True" result="Success" executed="True" />`n
"@
}
Else { # $ScriptAnalyzerResult is not null
$Body = [string]::Empty
Foreach ( $Result in $ScriptAnalyzerResult ) {

$TestDescription = "Rule name : $($Result.RuleName)"
$TestName = "PSScriptAnalyzer.{0} - {1} - Line {2}" -f $TestDescription, $($Result.ScriptName), $($Result.Line.ToString())

# Need to Escape these otherwise we can end up with an invalid XML if the Stacktrace has non XML friendly chars like &, etc
$Line = [System.Security.SecurityElement]::Escape($Result.Line)
$ScriptPath = [System.Security.SecurityElement]::Escape($Result.ScriptPath)
$Text = [System.Security.SecurityElement]::Escape($Result.Extent.Text)
$Severity = [System.Security.SecurityElement]::Escape($Result.Severity)

$TestCase = @"
<test-case description="$TestDescription" name="$TestName" time="0.0" asserts="0" success="False" result="Failure" executed="True">
<failure>
<message>$($Result.Message)</message>
<stack-trace>at line: $($Line) in $($ScriptPath)
$($Line): $($Text)
Rule severity : $($Severity)
</stack-trace>
</failure>
</test-case>`n
"@

$Body += $TestCase
}
}
$OutputXml = $Header + $Body + $Footer

# Checking our output is a well formed XML document
Try {
$XmlCheck = [xml]$OutputXml
}
Catch {
Throw "There was an problem when attempting to cast the output to XML : $($_.Exception.Message)"
}
$OutputXml | Out-File -FilePath $Path -Encoding utf8 -Force
}
81 changes: 81 additions & 0 deletions .ci/FudgeAppVeyor.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
. $env:FudgeCI/FudgeGenerateFake.ps1
. $env:FudgeCI/PrepareAVVM.ps1

Set-StrictMode -Version latest

function Fix-MinGW {
# TODO: Handle versions other than 8.1.0
Move-Item C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64 C:\MinGW81-x64
}

function Choose-Preinstalled-Products {
param(
[array]
$Packages
)

foreach ($pkg in $Packages) {
try {
$product = $pkg.AppVeyor
} catch {
continue
}

$version = $pkg.Version

$version_parts = ($version.Split('.'))

if ($product -eq 'jdk') {
# 8 -> 1.8.0
$version = "1." + $version_parts[0] + ".0"
} elseif ($product -eq 'MinGW') {
Fix-MinGW
} elseif ($product -eq 'miniconda') {
# TODO improve translation of real miniconda versions
# into AppVeyor versions which are the python version
if ($version -eq '4.5.12') {
$version = '3.7'
}

if ($version[0] -eq '2') {
Fix-Miniconda27
}
}

# Allow the installed version of python to be over
if ($product -eq 'python') {
if ($env:PYTHON_VERSION) {
$version = $env:PYTHON_VERSION
}
}

Add-Product $product $version $env:PLATFORM
if (Test-Path "C:\avvm\$product\$version\$env:PLATFORM") {
Install-Product $product $version $env:PLATFORM
} elseif (Test-Path "C:\avvm\$product\$version") {
if ($env:PLATFORM -eq 'x86') {
$platform = 'x64'
} else {
$platform = 'x86'
}
Install-Product $product $version $platform
}
}
}

function Fix-AppVeyor {
# Special case, so the generated file isnt in mobans repository assets
if (Test-Path assets/fudge) {
$config = Get-FudgefileContent .ci/Fudgefile.appveyor
} else {
$config = Get-FudgefileContent $env:FudgeCI/Fudgefile.appveyor
}

PackFakeNupkgs $config.packages

SetDefaultVersions

Choose-Preinstalled-Products $config.packages
}

Export-ModuleMember -Function Fix-AppVeyor
72 changes: 72 additions & 0 deletions .ci/FudgeGenerateFake.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
Set-StrictMode -Version latest

$template = @'
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>{name}</id>
<version>{version}</version>
<title>{name} {version}</title>
<authors>AppVeyor</authors>
<description>Fake generated {name} package to fulfil dependencies.</description>
<dependencies>
<dependency id="chocolatey"/>
</dependencies>
</metadata>
</package>
'@

function GenerateFakeNuspec {
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
$name,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]
$version
)

$content = $template -replace '{name}', $name
$content = $content -replace '{version}', $version

$nuspec = ($env:FudgeCI + '\nuspecs\' + $name + '.nuspec')

Set-Content $nuspec $content

Write-Output "Created $nuspec"
}

function GenerateFakeNuspecs {
param(
[array]
$Packages
)

foreach ($pkg in $Packages) {
GenerateFakeNuspec $pkg.Name $pkg.version
}
}

function PackFakeNupkgs {
param(
[array]
$Packages
)

mkdir -Force $env:FudgeCI\nuspecs\ > $null

GenerateFakeNuspecs $Packages

# This should work, but is failing
# fudge pack -FudgefilePath .ci/Fudgefile.appveyor
foreach ($pkg in $Packages) {
$filename = ($pkg.Name + '.nuspec')
choco pack "$env:FudgeCI\nuspecs\$filename" > $null
}
mv *.nupkg $env:FudgeCI\nuspecs\

Write-Output 'Packed!'
}
54 changes: 54 additions & 0 deletions .ci/FudgePostInstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
. $env:ChocolateyInstall\helpers\functions\Write-FunctionCallLogMessage.ps1
. $env:ChocolateyInstall\helpers\functions\Get-EnvironmentVariable.ps1
. $env:ChocolateyInstall\helpers\functions\Get-EnvironmentVariableNames.ps1
. $env:ChocolateyInstall\helpers\functions\Start-ChocolateyProcessAsAdmin.ps1
. $env:ChocolateyInstall\helpers\functions\Set-EnvironmentVariable.ps1
. $env:ChocolateyInstall\helpers\functions\Set-PowerShellExitCode.ps1
. $env:ChocolateyInstall\helpers\functions\Update-SessionEnvironment.ps1
. $env:ChocolateyInstall\helpers\functions\Write-FunctionCallLogMessage.ps1
. $env:ChocolateyInstall\helpers\functions\Install-ChocolateyPath.ps1

Set-StrictMode -Version latest

$deps_base = $env:FudgeCI

function Run-PostInstall {
choco list --local-only

Update-SessionEnvironment

Write-Host "PATH = $env:PATH"

$config = Get-FudgefileContent Fudgefile

foreach ($pkg in $config.Packages) {
$name = $pkg.Name

$glob = "$deps_base/deps.$name.ps1"

if (Test-Path $glob) {
Write-Host "Running post-install for $name"

. $glob
Do-PostInstall
}
}

Update-SessionEnvironment

Write-Host "PATH = $env:PATH"

foreach ($pkg in $config.Packages) {
$name = $pkg.Name

$glob = "$deps_base/deps.$name-packages.ps1"
if (Test-Path $glob) {
Write-Host "Running $name package installation"

. $glob
Do-Install-Packages
}
}
}

Export-ModuleMember -Function Run-PostInstall
Loading

0 comments on commit 0da8306

Please sign in to comment.