Skip to content
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
42 changes: 42 additions & 0 deletions ConvertOneNote2MarkDown-v2.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,48 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' {
$err | Should -Not -Be $null
}

It "Removes the docx directory if it is empty after removing the docx file" {
Mock Get-ChildItem -ParameterFilter { $LiteralPath } {}
$docxDirectory = Split-Path $params['ConversionConfig']['docxExportFilePath'] -Parent

Convert-OneNotePage @params 6>$null

Assert-MockCalled -CommandName Remove-Item -ParameterFilter { $LiteralPath -eq $docxDirectory -and $Force } -Times 1 -Scope It
}

It "Keeps the docx directory if it is not empty" {
Mock Get-ChildItem -ParameterFilter { $LiteralPath } {
[pscustomobject]@{
Name = 'some-other.docx'
}
}
$docxDirectory = Split-Path $params['ConversionConfig']['docxExportFilePath'] -Parent

Convert-OneNotePage @params 6>$null

Assert-MockCalled -CommandName Remove-Item -ParameterFilter { $LiteralPath -eq $docxDirectory } -Times 0 -Scope It
}

It "Keeps the docx directory if keeping docx files" {
Mock Get-ChildItem -ParameterFilter { $LiteralPath } {}
$docxDirectory = Split-Path $params['ConversionConfig']['docxExportFilePath'] -Parent
$params['Config']['keepdocx']['value'] = 2

Convert-OneNotePage @params 6>$null

Assert-MockCalled -CommandName Remove-Item -ParameterFilter { $LiteralPath -eq $docxDirectory } -Times 0 -Scope It
}

It "Does not halt conversion if removing the empty docx directory fails" {
Mock Get-ChildItem -ParameterFilter { $LiteralPath } {}
Mock Remove-Item -ParameterFilter { $LiteralPath -and $Force } { throw "i failed" }

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

$err.Exception.Message | Select-Object -First 1 | Should -match 'Failed to remove empty docx directory'
Assert-MockCalled -CommandName Copy-Item -Times 1 -Scope It
}

It "Saves page attachment(s)" {
Convert-OneNotePage @params 6>$null

Expand Down
12 changes: 12 additions & 0 deletions ConvertOneNote2MarkDown-v2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,18 @@ Function Convert-OneNotePage {
}catch {
Write-Error "Failed to remove existing docx file $( $pageCfg['docxExportFilePath'] ). Exception: $( $_.Exception.Message )" -ErrorAction Continue
}
# Remove the docx directory if it is empty, so that it is not left behind after the conversion. It is created again for the next page
$docxDirectory = Split-Path $pageCfg['docxExportFilePath'] -Parent
try {
if (!$config['dryRun']['value']) {
if (@( Get-ChildItem -LiteralPath $docxDirectory -Force -ErrorAction Stop ).Count -eq 0) {
"Removing empty docx directory: $( $docxDirectory )" | Write-Verbose
Remove-Item -LiteralPath $docxDirectory -Force -ErrorAction Stop
}
}
}catch {
Write-Error "Failed to remove empty docx directory $( $docxDirectory ). Exception: $( $_.Exception.Message )" -ErrorAction Continue
}
}

# Save any attachments
Expand Down