Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds draft caching mechanism #461

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions PSKoans/Init/InitializeCache.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if (-not (Test-Path -Path $Script:CachePath)) {
New-Item -Path $Script:CachePath -ItemType Directory > $null
}

$Script:KoanResultCache = @{}

foreach ($cacheItem in Get-ChildItem $Script:CachePath -Filter *.xml) {
$Script:KoanResultCache[$cacheItem.BaseName] = Import-Clixml -Path $cacheItem.FullName
}
1 change: 1 addition & 0 deletions PSKoans/PSKoans.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

$script:ModuleRoot = $PSScriptRoot
$script:ConfigPath = '~/.config/PSKoans/config.json'
$script:CachePath = '~/.config/PSKoans/cache'
$script:DefaultSettings = @{
KoanLocation = Resolve-Path -Path '~' | Join-Path -ChildPath 'PSKoans'
Editor = 'code'
Expand Down
46 changes: 46 additions & 0 deletions PSKoans/Private/Add-KoanCachedResult.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function Add-KoanCachedResult {
<#
.SYNOPSIS
Add an entry to the koan cache.

.DESCRIPTION
Cache the results of running tests for this koan. This avoids the overhead of repeatedly running pester on unchanged content.

.PARAMETER Path
The path to the koans test file.

.PARAMETER PesterTests
The result of running tests against the file created by the Invoke-Koan command.
#>

[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$Path,

[Parameter(Mandatory)]
[object]
$Result
)

$currentHash = (Get-FileHash -Path $Path).Hash
$cacheEntryName = [System.IO.Path]::GetFileNameWithoutExtension($Path)

if ($Script:KoanResultCache.Contains($cacheEntryName) -and $currentHash -eq $Script:KoanResultCache[$cacheEntryName]['Hash']) {
# No work to do for this file. Return immediately.
return
}

$cacheItemPath = Join-Path -Path $Script:CachePath -ChildPath (
'{0}.xml' -f [System.IO.Path]::GetFileNameWithoutExtension($Path)
)

$cacheEntry = @{
Hash = $currentHash
Result = $Result
}
$cacheEntry | Export-CliXml -Path $cacheItemPath -Depth 5

$Script:KoanResultCache[$cacheEntryName] = $cacheEntry
}
30 changes: 30 additions & 0 deletions PSKoans/Private/Get-KoanCachedResult.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function Get-KoanCachedResult {
<#
.SYNOPSIS
Add an entry to the koan cache.

.DESCRIPTION
Cache the results of running tests for this koan. This avoids the overhead of repeatedly running pester on unchanged content.

.PARAMETER Path
The path to the koans test file.
#>

[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$Path
)

$cacheEntryName = [System.IO.Path]::GetFileNameWithoutExtension($Path)

if ($Script:KoanResultCache.ContainsKey($cacheEntryName)) {
$currentHash = (Get-FileHash -Path $Path).Hash
$cacheEntry = $Script:KoanResultCache[$cacheEntryName]

if ($currentHash -eq $cacheEntry['Hash']) {
$cacheEntry['Result']
}
}
}
9 changes: 9 additions & 0 deletions PSKoans/Private/Invoke-Koan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
}
}
end {
$CachedResult = Get-KoanCachedResult -Path $ParameterSplat['Script']
if ($CachedResult) {
return $CachedResult
}

try {
$Requirements = [System.Management.Automation.Language.Parser]::ParseFile(
$ParameterSplat.Script,
Expand Down Expand Up @@ -73,6 +78,7 @@

$Status = $ps.BeginInvoke()

# Wait for the runspace in PS to support use of Control+C
do { Start-Sleep -Milliseconds 100 } until ($Status.IsCompleted)

$Result = $ps.EndInvoke($Status)
Expand All @@ -82,6 +88,9 @@
foreach ($errorItem in $ps.Streams.Error) {
$PSCmdlet.WriteError($errorItem)
}
} else {
# Never cache when errors are raised during the pester run
Add-KoanCachedResult -Path $ParameterSplat['Script'] -Result $Result
}

$Result
Expand Down
2 changes: 1 addition & 1 deletion PSKoans/Private/Measure-Koan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
) -join [System.IO.Path]::PathSeparator
}
process {
Write-Verbose "Discovering koans in [$($KoanInfo.Name -join '], [')]"
Write-Verbose "Discovering koans in [$($KoanInfo.Path -join '], [')]"

$Result = & (Get-Module Pester) {
[CmdletBinding()]
Expand Down
7 changes: 6 additions & 1 deletion PSKoans/Private/New-KoanRunspace.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ function New-KoanRunspace {

try {
$script = {
param( $PSKoansPath )
param(
$PesterPath,
$PSKoansPath
)

Get-Module $PesterPath -ListAvailable | Import-Module
Get-Module $PSKoansPath -ListAvailable | Import-Module
}

$ps.AddScript($script) > $null
$ps.AddParameter('PesterPath', (Get-Module Pester).ModuleBase) > $null
$ps.AddParameter('PSKoansPath', $MyInvocation.MyCommand.Module.ModuleBase) > $null
$ps.Invoke() > $null

Expand Down
6 changes: 5 additions & 1 deletion Start-DebugSession.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ $env:PSModulePath = $(
) -join [System.IO.Path]::PathSeparator

& $PSScriptRoot/PSKoans.ezformat.ps1

$pester = Get-Module pester -ListAvailable | Where-Object Version -eq '5.0.2'

Import-Module (Join-Path -Path $pester.ModuleBase -ChildPath 'pester.psd1')
Import-Module $PSScriptRoot/PSKoans

### Enter code to test below
### Enter code to test here
38 changes: 38 additions & 0 deletions Tests/Functions/Private/Add-KoanCachedResult.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#Requires -Modules PSKoans

Describe 'Add-KoanCachedResult' {
BeforeAll {
Mock 'Get-FileHash' {
[PSCustomObject]@{
Hash = 'abcdef'
}
}
Mock 'Export-CliXml'
}

It 'adds an entry to the cache when the entry does not exist' {
InModuleScope -ModuleName 'PSKoans' {
$Script:KoanResultCache = @{}

Add-KoanCachedResult -Path 'AboutAssertions.Koans.ps1' -Result 'Result'
}

Should -Invoke 'Get-FileHash'
Should -Invoke 'Export-CliXml'
}

It 'does nothing when the cache entry already exists and hashes match' {
InModuleScope -ModuleName 'PSKoans' {
$Script:KoanResultCache = @{
'AboutAssertions.Koans' = @{
Hash = 'abcdef'
}
}

Add-KoanCachedResult -Path 'AboutAssertions.Koans.ps1' -Result 'Result'
}

Should -Invoke 'Get-FileHash'
Should -Invoke 'Export-CliXml' -Times 0
}
}
45 changes: 45 additions & 0 deletions Tests/Functions/Private/Get-KoanCachedResult.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#Requires -Modules PSKoans

Describe 'Get-KoanCachedResult' {
BeforeAll {
Mock 'Get-FileHash' {
[PSCustomObject]@{
Hash = 'abcdef'
}
}
}

It 'gets an entry when hashes match' {
InModuleScope -ModuleName 'PSKoans' {
$Script:KoanResultCache = @{
'AboutAssertions.Koans' = @{
Hash = 'abcdef'
Result = 'Result'
}
}

Get-KoanCachedResult -Path 'AboutAssertions.Koans.ps1'
} | Should -Be 'Result'
}

It 'returns nothing when hashes do not match' {
InModuleScope -ModuleName 'PSKoans' {
$Script:KoanResultCache = @{
'AboutAssertions.Koans' = @{
Hash = 'defabc'
Result = 'Result'
}
}

Get-KoanCachedResult -Path 'AboutAssertions.Koans.ps1'
} | Should -BeNullOrEmpty
}

It 'returns nothing when the item is not in the cache' {
InModuleScope -ModuleName 'PSKoans' {
$Script:KoanResultCache = @{}

Get-KoanCachedResult -Path 'AboutAssertions.Koans.ps1'
} | Should -BeNullOrEmpty
}
}
33 changes: 33 additions & 0 deletions Tests/Functions/Private/Invoke-Koan.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Describe 'Invoke-Koan' {

BeforeAll {
Mock Add-KoanCachedResult
Mock Get-KoanCachedResult

$testFile = @{ Script = "$PSScriptRoot/ControlTests/Invoke-Koan.Control_Tests.ps1" }
}

Expand Down Expand Up @@ -43,4 +46,34 @@ Describe 'Invoke-Koan' {
ForEach-Object -MemberName GetType |
Should -Be @([Exception], [NotImplementedException])
}

Context 'item exists in cache' {
BeforeAll {
Mock Get-KoanCachedResult {
'CachedResult'
}
}

It 'returns a cached results if a cache entry exists' {
InModuleScope 'PSKoans' -Parameters $testFile {
param($Script)
Invoke-Koan @{ Script = $Script; PassThru = $true }
} | Should -Be 'CachedResult'

Should -Invoke 'Get-KoanCachedResult'
Should -Invoke 'Add-KoanCachedResult' -Times 0
}
}

Context 'item does not exist in cache' {
It 'adds an item to the cache when the pester run is successful' {
InModuleScope 'PSKoans' -Parameters $testFile {
param($Script)
Invoke-Koan @{ Script = $Script; PassThru = $true }
}

Should -Invoke 'Get-KoanCachedResult'
Should -Invoke 'Add-KoanCachedResult'
}
}
}