Skip to content

Commit

Permalink
Merge pull request #64 from theohbrothers/fix/fix-create-option-to-wo…
Browse files Browse the repository at this point in the history
…rk-correctly-when-optional-attributes-are-omitted

Fix: Fix `create` option to work correctly when optional attributes are omitted
  • Loading branch information
leojonathanoh authored Oct 24, 2023
2 parents 39a72cc + e3145af commit 6bb4ef4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/Log-Rotate/Log-Rotate.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,32 @@ Describe 'Log-Rotate' -Tag 'Integration' {
$logFileHash.Hash | Should -Not -Be $rotatedLogFileHash.Hash
}

It "Option 'create': rotates a log file and immediately creates a new original file" {
It "Option 'create' (without attributes): rotates a log file and immediately creates a new original file" {
$configFileContent = @"
"$logFile" {
create
}
"@
Init

Log-Rotate -config $configFile -State $stateFile -ErrorAction $eaPreference #-Verbose

# Assert that the log file should remain
$logItem = Get-Item $logFile -ErrorAction SilentlyContinue
$logItem | Should -BeOfType [System.IO.FileSystemInfo]
$logItem.Name | Should -Be "$( Split-Path $logFile -Leaf )"
$logItem.Length | Should -Be 0

# Assert that the rotated log file should be there
$rotatedLogItems = @( Get-Item $logDir/* )
$rotatedLogItems.Count | Should -Be 2
$rotatedLogItems[1] | Should -BeOfType [System.IO.FileSystemInfo]

# Assert that the rotated log file should be named
$rotatedLogItems[1].Name | Should -Be "$( Split-Path $logFile -Leaf ).1"
}

It "Option 'create' (with mode, owner, and group attributes): rotates a log file and immediately creates a new original file" {
$configFileContent = @"
"$logFile" {
create 700 1000 1000
Expand Down
10 changes: 9 additions & 1 deletion src/Log-Rotate/private/rotate/Process-Local-Block.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,15 @@ function Process-Local-Block {
$copy
}
$options['copytruncate'] = if ($nocopytruncate) { $false } else { $copytruncate }
$options['create'] = if ($nocreate) { '' } else { $create }

$options['create'] = if ($nocreate) { '' } else {
# 'create' option's attributes are optional, in which case its value is an empty string
if ($PSBoundParameters.ContainsKey('create') -and $PSBoundParameters['create'] -eq '') {
' ' # Set to a non-empty string
}else {
$create
}
}
$options['delaycompress'] = if ($nodelaycompress) { $false } else { $delaycompress }
$options['dateext'] = if ($nodateext) { $false } else { $dateext }
$options['mail'] = if ($nomail) { $false } else { $mail }
Expand Down

0 comments on commit 6bb4ef4

Please sign in to comment.