Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 28 additions & 35 deletions Build/Invoke-ModuleTests.ps1
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
$Lines = '-' * 70
$PSVersion = $PSVersionTable.PSVersion

Write-Host $Lines
Write-Host "STATUS: Testing with PowerShell v$($PSVersionTable.PSVersion)"
Write-Host $Lines

# Import the module and add temporary entry to PSModulePath for build/test purposes

Import-Module 'PSKoans'

$Timestamp = Get-Date -Format "yyyyMMdd-hhmmss"
$TestFile = "PS${PSVersion}_${TimeStamp}_PSKoans.TestResults.xml"
$CodeCoverageFile = "PS${PSVersion}_${TimeStamp}_PSKoans.CodeCoverage.xml"

$ModuleFolders = @(
Get-Item -Path "$env:PROJECTROOT/PSKoans"
Get-ChildItem -Path "$env:PROJECTROOT/PSKoans" -Directory -Recurse |
Where-Object { 'Koans' -notin $_.Parent.Name, $_.Parent.Parent.Name }
).FullName -join ';'
$PesterVersion = (Get-Module -Name Pester).Version
$PSVersion = $PSVersionTable.PSVersion

# Tell Azure where the test results & code coverage files will be
Write-Host "##vso[task.setvariable variable=TestResults]$TestFile"
Write-Host "##vso[task.setvariable variable=CodeCoverageFile]$CodeCoverageFile"
Write-Host "##vso[task.setvariable variable=SourceFolders]$ModuleFolders"
Write-Host $Lines
Write-Host "TEST: PowerShell Version: $PSVersion"
Write-Host "TEST: Pester Version: $PesterVersion"
Write-Host $Lines

# Gather test results. Store them in a variable and file
$PesterParams = @{
Path = "$env:PROJECTROOT/Tests"
PassThru = $true
OutputFormat = 'NUnitXml'
OutputFile = "$env:BUILD_ARTIFACTSTAGINGDIRECTORY/$TestFile"
Show = "Header", "Failed", "Summary"
CodeCoverage = (Get-ChildItem -Recurse -Path "$env:PROJECTROOT/PSKoans" -Filter '*.ps*1' -Exclude '*.Koans.ps1').FullName
CodeCoverageOutputFile = "$env:BUILD_ARTIFACTSTAGINGDIRECTORY/$CodeCoverageFile"
try {
# Try/Finally required since -CI will exit with exit code on failure.
Invoke-Pester -Path "$env:PROJECTROOT" -CI -Output Normal
}
$TestResults = Invoke-Pester @PesterParams

# If tests failed, write errors and exit
if ($TestResults.FailedCount -gt 0) {
Write-Error "Failed $($TestResults.FailedCount) tests; build failed!"
exit $TestResults.FailedCount
finally {
$Timestamp = Get-Date -Format "yyyyMMdd-hhmmss"
$TestFile = "PS${PSVersion}_${TimeStamp}_PSKoans.TestResults.xml"
$CodeCoverageFile = "PS${PSVersion}_${TimeStamp}_PSKoans.CodeCoverage.xml"

$ModuleFolders = @(
Get-Item -Path "$env:PROJECTROOT/PSKoans"
Get-ChildItem -Path "$env:PROJECTROOT/PSKoans" -Directory -Recurse |
Where-Object FullName -NotMatch '[\\/]Tests[\\/]|[\\/]PSKoans[\\/]Koans[\\/]'
).FullName -join ';'

# Tell Azure what the test results & code coverage file names will be
Write-Host "##vso[task.setvariable variable=TestResults]$TestFile"
Write-Host "##vso[task.setvariable variable=CodeCoverageFile]$CodeCoverageFile"
Write-Host "##vso[task.setvariable variable=SourceFolders]$ModuleFolders"

# Move files generated from Invoke-Pester to expected location
Move-Item -Path './testResults.xml' -Destination "$env:BUILD_ARTIFACTSTAGINGDIRECTORY/$TestFile"
Move-Item -Path './coverage.xml' -Destination "$env:BUILD_ARTIFACTSTAGINGDIRECTORY/$CodeCoverageFile"
}
18 changes: 14 additions & 4 deletions KoanIndex.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,18 @@ When adding or updating koan topics, please keep this file up to date.

### ActiveDirectory

| Folder | Topic | Position |
| ----------------------- | :------------------------------------------------------------- | :------: |
| Introduction | [AboutFiltering][ActiveDirectory\AboutFiltering] | 101 |
| Folder | Topic | Position |
| ------------ | :----------------------------------------------- | :------: |
| Introduction | [AboutFiltering][ActiveDirectory\AboutFiltering] | 101 |

### Dbatools

> TBD
| Folder | Topic | Position |
| -------- | :-------------------------------------------------------- | :------: |
| dbatools | [AboutDbaDatabase][dbatools\AboutDbaDatabase] | 1001 |
| dbatools | [AboutQueryingDatabases][dbatools\AboutQueryingDatabases] | 1002 |
| dbatools | [AboutNewDatabases][dbatools\AboutNewDatabases] | 1003 |
| dbatools | [AboutBackupDatabases][dbatools\AboutBackupDatabases] | 1004 |

<!-- Links for default koan topics -->

Expand Down Expand Up @@ -120,3 +125,8 @@ When adding or updating koan topics, please keep this file up to date.
<!-- Add links for koans from other modules below this line -->

[ActiveDirectory\AboutFiltering]: PSKoans/Koans/Modules/ActiveDirectory/Introduction/AboutFiltering.Koans.ps1

[dbatools\AboutDbaDatabase]: PSKoans/Koans/Modules/dbatools/AboutDbaDatabase.Koans.ps1
[dbatools\AboutQueryingDatabases]: PSKoans/Koans/Modules/dbatools/AboutQueryingDatabases.Koans.ps1
[dbatools\AboutNewDatabases]: PSKoans/Koans/Modules/dbatools/AboutNewDatabases.Koans.ps1
[dbatools\AboutBackupDatabases]: PSKoans/Koans/Modules/dbatools/AboutBackupDatabases.Koans.ps1
47 changes: 23 additions & 24 deletions PSKoans.ezformat.ps1
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
#Requires -Modules EZOut
# Install-Module EZOut
# or https://github.com/StartAutomating/EZOut

$VerbosePreference = 'Continue'

$ModuleName = $MyInvocation.MyCommand.Name -replace '\.ezformat\.ps1', ''
$ModuleFolder = Get-Item -Path "$PSScriptRoot/PSKoans" | Select-Object -ExpandProperty FullName

Write-Verbose "Building format file for '$ModuleName' in '$ModuleFolder'"

#requires -Module EZOut
# Install-Module EZOut or https://github.com/StartAutomating/EZOut
$myFile = $MyInvocation.MyCommand.ScriptBlock.File
$myModuleName = 'PSKoans'
$myRoot = "$PSScriptRoot"
Push-Location $myRoot
$formatting = @(
# Add your own Write-FormatView here,
# or put them in a Formatting or Views directory
foreach ($potentialDirectory in 'formatting', 'views') {
$path = Join-Path $PSScriptRoot -ChildPath $potentialDirectory
if (Test-Path $path) {
Get-ChildItem -Path $path | ForEach-Object {
Write-Verbose "Processing file '$($_.FullName)'"
Import-FormatView -FilePath $_.FullName
}
}
Join-Path $myRoot -ChildPath $potentialDirectory |
Get-ChildItem -ea ignore |
Import-FormatView -FilePath { $_.Fullname }
}
)

$destinationRoot = "$PSScriptRoot/PSKoans"

if ($formatting) {
$formatFilePath = Join-Path $ModuleFolder -ChildPath "$ModuleName.format.ps1xml"
Write-Verbose "Writing format file to '$formatFilePath'"
$formatting | Out-FormatData -Module $ModuleName | Set-Content $formatFilePath -Encoding UTF8
$myFormatFile = Join-Path $destinationRoot "$myModuleName.format.ps1xml"
$formatting | Out-FormatData -Module $MyModuleName | Set-Content $myFormatFile -Encoding UTF8
}

$types = @(
# Add your own Write-TypeView statements here
# or declare them in the 'Types' directory
Join-Path $myRoot Types |
Get-Item -ea ignore |
Import-TypeView

)

if ($types) {
$TypesFile = Join-Path $PSScriptRoot "$ModuleName.types.ps1xml"
Write-Verbose "Writing types file to '$TypesFile'"
$types | Out-TypeData | Set-Content $TypesFile -Encoding UTF8
$myTypesFile = Join-Path $destinationRoot "$myModuleName.types.ps1xml"
$types | Out-TypeData | Set-Content $myTypesFile -Encoding UTF8
}
Pop-Location
17 changes: 16 additions & 1 deletion PSKoans/Koans/Cmdlets 2/AboutCsvCmdlets.Koans.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ param()
handled directly by PowerShell's CSV cmdlets without some extra work.
#>

Describe '*-Csv Cmdlets' {
Describe 'CSV Cmdlets' {

Context 'Export-Csv' {
<#
Expand Down Expand Up @@ -81,7 +81,21 @@ Describe '*-Csv Cmdlets' {
}

It 'allows you to specify the delimiter' {
$Delimiter = '_'
$Path = 'TestDrive:/DelimitedData.csv'

$Text = @(
'"Number","Square"'
'"__"?"__"'
'"__"?"__"'
'"__"?"__"'
'"__"?"__"'
'"__"?"__"'
)

$Objects | Export-Csv -Path $Path -Delimiter $Delimiter
$FileContents = Get-Content -Path $Path
$Text | Should -BeExactly $FileContents
}
}

Expand Down Expand Up @@ -207,6 +221,7 @@ Describe '*-Csv Cmdlets' {
}

Context 'ConvertFrom-Csv' {

BeforeAll {
$CsvString = @"
"Number","Square"
Expand Down
1 change: 0 additions & 1 deletion PSKoans/Koans/Cmdlets 2/AboutOutCmdlets.Koans.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ Context 'Out-* Cmdlets' {
partially because in older versions of PowerShell it has a different default
encoding than those cmdlets, frequently confusing users.
#>

It 'stores data in files' {
$Path = 'TestDrive:\File.txt'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ param()
'splatted' into the command by specifying the variable name with an @ symbol: @Variable
#>
Describe 'Splatting' {

BeforeAll {
$PSKoansFolder = Get-PSKoanLocation
}
Expand Down Expand Up @@ -74,8 +75,8 @@ Describe 'Splatting' {
}

Get-ChildItem @Parameters |
Select-Object -First 1 |
Should -BeOfType 'System.IO.FileInfo'
Select-Object -First 1 |
Should -BeOfType 'System.IO.FileInfo'
}

It 'can be built from automatic hashtables' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Describe 'System.Text.StringBuilder' {
}

Context 'Constructing the Final String Object' {

BeforeAll {
$SB = [System.Text.StringBuilder]::new("Hello!")
$SB.Append("BYE!")
Expand All @@ -115,9 +116,11 @@ Describe 'System.Text.StringBuilder' {
}

Context 'Other StringBuilder Methods' {

BeforeAll {
$StringBuilder = [System.Text.StringBuilder]::new("TEXT")
}

It 'can be cleared' {
$StringBuilder.Clear()
$StringBuilder.Length | Should -Be 0
Expand All @@ -143,6 +146,7 @@ Describe 'System.Text.StringBuilder' {
}

Context 'StringBuilder Properties' {

BeforeAll {
$StringBuilder = [System.Text.StringBuilder]::new()
$StringBuilder.AppendLine('When you look into the void,')
Expand All @@ -154,12 +158,12 @@ Describe 'System.Text.StringBuilder' {
$PropertyName = '____'

$Properties = $StringBuilder |
Get-Member |
Where-Object MemberType -eq 'Property'
Get-Member |
Where-Object MemberType -EQ 'Property'

$ExpectedPropertyCount = $Properties |
Measure-Object |
Select-Object -ExpandProperty Count
Measure-Object |
Select-Object -ExpandProperty Count

$PropertyCount | Should -Be $ExpectedPropertyCount
$PropertyName | Should -BeIn $Properties.Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Describe 'Arithmetic Operators' {
__ | Should -Be $Array
}
}

Context 'Subtraction' {

It 'works similarly to addition' {
Expand Down Expand Up @@ -148,9 +149,10 @@ Describe 'Arithmetic Operators' {
}

Context 'Modulus' {

# Modulus is a bit of an odd one, but common enough in programming. It performs a
# division, and then returns the integer value of the remainder.
<#
Modulus is a bit of an odd one, but common enough in programming. It performs a
division, and then returns the integer value of the remainder.
#>
It 'is usually used with integers' {
$Remainder = 15 % 7
__ | Should -Be $Remainder
Expand Down
Loading