Skip to content

Commit

Permalink
Convert-PesterSyntax: Enable save to file (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju authored Jul 11, 2024
1 parent 06cff82 commit 3891393
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/code-analysis-built-module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ jobs:
with:
fetch-depth: 0
- name: Install GitVersion
shell: powershell
shell: pwsh
run: |
dotnet tool install --global GitVersion.Tool
- name: Run GitVersion
shell: powershell
shell: pwsh
run: |
dotnet-gitversion | ConvertFrom-Json
- name: Build Module
shell: powershell
shell: pwsh
run: |
Write-Information -MessageData 'Module is being built.' -InformationAction 'Continue'
.\build.ps1 -ResolveDependency -Tasks 'build'
- name: Run PSScriptAnalyzer
shell: powershell
shell: pwsh
run: |
Write-Information -MessageData 'Prepare the test pipeline.' -InformationAction 'Continue'
.\build.ps1 -Tasks 'noop'
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/code-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ jobs:
with:
fetch-depth: 0
- name: Install GitVersion
shell: powershell
shell: pwsh
run: |
dotnet tool install --global GitVersion.Tool
- name: Run GitVersion
shell: powershell
shell: pwsh
run: |
dotnet-gitversion | ConvertFrom-Json
- name: Build Module
shell: powershell
shell: pwsh
run: |
Write-Information -MessageData 'Module is being built so that examples can be scanned.' -InformationAction 'Continue'
.\build.ps1 -ResolveDependency -Tasks 'build'
- name: Run PSScriptAnalyzer
shell: powershell
shell: pwsh
run: |
Write-Information -MessageData 'Prepare the test pipeline.' -InformationAction 'Continue'
.\build.ps1 -Tasks 'noop'
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Public commands:
- `Convert-PesterSyntx`
- `Convert-PesterSyntax`
- GitHub templates.
13 changes: 10 additions & 3 deletions source/Public/Convert-PesterSyntax.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ function Convert-PesterSyntax

foreach ($commandAst in $shouldCommandAst)
{
$apply = $true

# Get start and end offsets of commandAst.Extent
$startOffset = $commandAst.Extent.StartOffset
$endOffset = $commandAst.Extent.EndOffset
Expand Down Expand Up @@ -268,6 +270,8 @@ function Convert-PesterSyntax
default
{
Write-Warning -Message ('Unsupported command operator ''{0}'' in extent: `{1}`' -f $operatorName, $commandAst.Extent.Text)

$apply = $false
}
}
}
Expand All @@ -276,8 +280,11 @@ function Convert-PesterSyntax
Write-Warning -Message ('Did not found any of the supported command operators in extent: `{0}`' -f $commandAst.Extent.Text)
}

# Replace the portion of the script.
$convertedScriptText = $convertedScriptText.Remove($startOffset, $endOffset - $startOffset).Insert($startOffset, $newExtentText)
if ($apply)
{
# Replace the portion of the script.
$convertedScriptText = $convertedScriptText.Remove($startOffset, $endOffset - $startOffset).Insert($startOffset, $newExtentText)
}
}

Write-Progress -Id 2 -ParentId 1 -Activity 'Converting Should command syntax' -Status 'Completed' -PercentComplete 100 -Completed
Expand All @@ -293,7 +300,7 @@ function Convert-PesterSyntax
}
else
{
#$filePath | Set-Content -Value $convertedScriptText
Set-Content -Path $filePath -Value $convertedScriptText -NoNewLine -ErrorAction 'Stop'
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/Public/Convert-PesterSyntax.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -789,5 +789,37 @@ Describe 'Convert-PesterSyntax' {
$result | Should-BeString -CaseSensitive -Expected $mockAstExtentText -TrimWhitespace
}
}

Context 'When saving the converted script back to the file' {
BeforeAll {
$mockAstExtentText = {
Describe 'Should -Be' {
It 'Should -Be' {
Should -Be 1
}
}
}.Ast.GetScriptBlock().ToString()

$mockScriptFilePath = Join-Path -Path $TestDrive -ChildPath 'Mock.Tests.ps1'

Set-Content -Path $mockScriptFilePath -Value $mockAstExtentText -Encoding 'utf8'

Mock -CommandName Write-Warning
}

It 'Should not throw an exception' {
$mockExpectedConvertedScript = {
Describe 'Should -Be' {
It 'Should -Be' {
Should-Be 1
}
}
}.Ast.GetScriptBlock().ToString()

Convert-PesterSyntax -Path $mockScriptFilePath -Force

Get-Content -Raw -Path $mockScriptFilePath | Should-BeString -CaseSensitive -Expected $mockExpectedConvertedScript -TrimWhitespace
}
}
}
}

0 comments on commit 3891393

Please sign in to comment.