From 931eb1daa42ef6cdb303c683c73b756692ffe662 Mon Sep 17 00:00:00 2001 From: Leonard Jonathan Oh Date: Sat, 7 Aug 2021 07:57:08 +0000 Subject: [PATCH] Fix: Fix bug where non-existing docx file was attempted to be removed Fixes regression in #53 --- ConvertOneNote2MarkDown-v2.Tests.ps1 | 7 +++++-- ConvertOneNote2MarkDown-v2.ps1 | 8 ++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/ConvertOneNote2MarkDown-v2.Tests.ps1 b/ConvertOneNote2MarkDown-v2.Tests.ps1 index 0850426..f31aa61 100644 --- a/ConvertOneNote2MarkDown-v2.Tests.ps1 +++ b/ConvertOneNote2MarkDown-v2.Tests.ps1 @@ -995,8 +995,8 @@ 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 @@ -1004,12 +1004,15 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' { } 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 diff --git a/ConvertOneNote2MarkDown-v2.ps1 b/ConvertOneNote2MarkDown-v2.ps1 index ebdeaf9..33f62ba 100755 --- a/ConvertOneNote2MarkDown-v2.ps1 +++ b/ConvertOneNote2MarkDown-v2.ps1 @@ -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 )" @@ -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 )"