Skip to content

Commit

Permalink
Merge pull request #58 from theohbrothers/fix/fix-bug-where-non-exist…
Browse files Browse the repository at this point in the history
…ing-docx-file-was-attempted-to-be-removed

Fix: Fix bug where non-existing docx file was attempted to be removed
  • Loading branch information
leojonathanoh committed Aug 7, 2021
2 parents 51825f7 + 931eb1d commit 794162d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 5 additions & 2 deletions ConvertOneNote2MarkDown-v2.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -995,21 +995,24 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' {
Assert-MockCalled -CommandName New-Item -ParameterFilter { $Path -and $Force } -Times 4
}

It "Halts converting if creation of any directory 6fails" {
Mock Remove-Item -ParameterFilter { $Path -and $Force } { throw }
It "Halts converting if creation of any directory fails" {
Mock New-Item -ParameterFilter { $ItemType -eq 'Directory' -and $Force } { throw }

$err = Convert-OneNotePage @params 6>$null 2>&1

$err.Exception.Message | Select-Object -First 1 | Should -match 'Failed to convert page'
}

It "Removes existing docx by default" {
Mock Test-Path { $true }

Convert-OneNotePage @params 6>$null

Assert-MockCalled -CommandName Remove-Item -ParameterFilter { $Path -and $Force } -Times 2
}

It "Halts converting if removal of existing docx fails" {
Mock Test-Path { $true }
Mock Remove-Item -ParameterFilter { $Path -and $Force } { throw }

$err = Convert-OneNotePage @params 6>$null 2>&1
Expand Down
8 changes: 6 additions & 2 deletions ConvertOneNote2MarkDown-v2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,9 @@ Function Convert-OneNotePage {
try {
"Removing existing docx file: $( $pageCfg['fullexportpath'] )" | Write-Verbose
if ($config['dryRun']['value'] -eq 1) {
Remove-Item -path $pageCfg['fullexportpath'] -Force -ErrorAction Stop
if (Test-Path $pageCfg['fullexportpath']) {
Remove-Item -path $pageCfg['fullexportpath'] -Force -ErrorAction Stop
}
}
}catch {
throw "Error removing intermediary docx file $( $pageCfg['fullexportpath'] ): $( $_.Exception.Message )"
Expand Down Expand Up @@ -864,7 +866,9 @@ Function Convert-OneNotePage {
try {
"Removing existing docx file: $( $pageCfg['fullexportpath'] )" | Write-Verbose
if ($config['dryRun']['value'] -eq 1) {
Remove-Item -path $pageCfg['fullexportpath'] -Force -ErrorAction Stop
if (Test-Path $pageCfg['fullexportpath']) {
Remove-Item -path $pageCfg['fullexportpath'] -Force -ErrorAction Stop
}
}
}catch {
Write-Error "Error removing intermediary docx file $( $pageCfg['fullexportpath'] ): $( $_.Exception.Message )"
Expand Down

0 comments on commit 794162d

Please sign in to comment.