diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..d3710c5 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,25 @@ +--- +kind: pipeline +type: docker +name: Linux_PWSH7_Build + +platform: + os: linux + arch: amd64 + +steps: + - name: Environments + image: mcr.microsoft.com/powershell:latest + commands: + - pwsh -NonInteractive -c "& {Import-Module './tools/DroneIO.psm1' -Verbose; Invoke-ShowEnv -Verbose}" + - pwsh -NonInteractive -c "& {Import-Module './tools/DroneIO.psm1' -Verbose; Invoke-InstallDependencies -Verbose}" + - name: LintTests + image: mcr.microsoft.com/powershell:latest + commands: + - pwsh -NonInteractive -c "& {Import-Module './tools/DroneIO.psm1'; Invoke-InstallDependencies}" + - pwsh -NonInteractive -c "& {Import-Module './tools/DroneIO.psm1'; Invoke-Linter}" + - name: UnitTests + image: mcr.microsoft.com/powershell:latest + commands: + - pwsh -NonInteractive -c "& {Import-Module './tools/DroneIO.psm1'; Invoke-InstallDependencies}" + - pwsh -NonInteractive -c "& {Import-Module './tools/DroneIO.psm1'; Invoke-UnitTests}" diff --git a/tools/DroneIO.psm1 b/tools/DroneIO.psm1 new file mode 100644 index 0000000..21eadf9 --- /dev/null +++ b/tools/DroneIO.psm1 @@ -0,0 +1,89 @@ +$Global:ProgressPreference = 'SilentlyContinue' + +function Invoke-ShowEnv() { + [CmdletBinding()] + param() + + process { + Get-ChildItem -Path 'Env:' | Format-Table | Out-String + } +} + +function Invoke-InstallDependencies() { + [CmdletBinding()] + [OutputType()] + param() + + process { + try { + Install-Module -Name 'PSScriptAnalyzer' -Scope CurrentUser -RequiredVersion '1.19.1' -Force -SkipPublisherCheck -AllowClobber -Verbose:$VerbosePreference -ErrorAction 'Stop' + Install-Module -Name 'Pester' -Scope CurrentUser -RequiredVersion '4.10.1' -Force -SkipPublisherCheck -AllowClobber -Verbose:$VerbosePreference -ErrorAction 'Stop' + Install-Module -Name 'posh-git' -Scope CurrentUser -RequiredVersion '0.7.3' -Force -SkipPublisherCheck -AllowClobber -Verbose:$VerbosePreference -ErrorAction 'Stop' + Install-Module -Name 'PSCoverage' -Scope CurrentUser -Force -SkipPublisherCheck -AllowClobber -RequiredVersion '1.2.108' -Verbose:$VerbosePreference -ErrorAction 'Stop' + } + catch { + $ExceParams = @{ + Exception = [System.Exception]::new( + 'Could not install required build dependencies!', + $PSItem.Exception + ) + ErrorAction = 'Stop' + } + Write-Error @ExceParams + } + + } +} + +function Invoke-Linter () { + [CmdletBinding()] + param() + + process { + $LintRes = Invoke-ScriptAnalyzer -Path './src/' -Recurse + if (-not ($Env:CI_COMMIT_MESSAGE -match 'SkipLint')) { + if ($LintRes ) { + $LintRes | Format-List + Write-Error -Message 'Lint Errors found!' -ErrorAction Stop + } + else { + Write-Host '== No Lint Errors found! ==' + } + } + } +} + +function Invoke-UnitTests { + [CmdletBinding()] + Param() + + process { + + try { + Write-Host '===== Preload internal private functions =====' -ForegroundColor Black -BackgroundColor Yellow + + $Privates = Get-ChildItem -Path (Join-Path -Path $Env:DRONE_WORKSPACE -ChildPath '/src/Private/*') -Include "*.ps1" -Recurse -ErrorAction Stop + foreach ($File in $Privates) { + if (Test-Path -Path $File.FullName) { + . $File.FullName + Write-Verbose -Message ('Private function dot-sourced: {0}' -f $File.FullName) -Verbose + } + else { + Write-Warning -Message ('Could not find file: {0} !' -f $File.FullName) + } + } + } + catch { + $_.Exception.Message | Write-Error + throw 'Could not load required private functions!' + } + + Write-Host '===== Running Pester =====' -ForegroundColor Black -BackgroundColor Yellow + $TestFiles = Get-ChildItem -Path (Join-Path -Path '.' -ChildPath './tests/*.Tests.ps1') -Recurse | Sort-Object -Property Name + $TestResults = Invoke-Pester -Script $TestFiles -ExcludeTag 'Disabled' -PassThru + + if ($TestResults.FailedCount -gt 0) { + throw ('{0} tests failed!' -f $TestResults.FailedCount) + } + } +}