Skip to content

Commit 6bb4ef4

Browse files
Merge pull request #64 from theohbrothers/fix/fix-create-option-to-work-correctly-when-optional-attributes-are-omitted
Fix: Fix `create` option to work correctly when optional attributes are omitted
2 parents 39a72cc + e3145af commit 6bb4ef4

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/Log-Rotate/Log-Rotate.Integration.Tests.ps1

+26-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,32 @@ Describe 'Log-Rotate' -Tag 'Integration' {
298298
$logFileHash.Hash | Should -Not -Be $rotatedLogFileHash.Hash
299299
}
300300

301-
It "Option 'create': rotates a log file and immediately creates a new original file" {
301+
It "Option 'create' (without attributes): rotates a log file and immediately creates a new original file" {
302+
$configFileContent = @"
303+
"$logFile" {
304+
create
305+
}
306+
"@
307+
Init
308+
309+
Log-Rotate -config $configFile -State $stateFile -ErrorAction $eaPreference #-Verbose
310+
311+
# Assert that the log file should remain
312+
$logItem = Get-Item $logFile -ErrorAction SilentlyContinue
313+
$logItem | Should -BeOfType [System.IO.FileSystemInfo]
314+
$logItem.Name | Should -Be "$( Split-Path $logFile -Leaf )"
315+
$logItem.Length | Should -Be 0
316+
317+
# Assert that the rotated log file should be there
318+
$rotatedLogItems = @( Get-Item $logDir/* )
319+
$rotatedLogItems.Count | Should -Be 2
320+
$rotatedLogItems[1] | Should -BeOfType [System.IO.FileSystemInfo]
321+
322+
# Assert that the rotated log file should be named
323+
$rotatedLogItems[1].Name | Should -Be "$( Split-Path $logFile -Leaf ).1"
324+
}
325+
326+
It "Option 'create' (with mode, owner, and group attributes): rotates a log file and immediately creates a new original file" {
302327
$configFileContent = @"
303328
"$logFile" {
304329
create 700 1000 1000

src/Log-Rotate/private/rotate/Process-Local-Block.ps1

+9-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,15 @@ function Process-Local-Block {
9191
$copy
9292
}
9393
$options['copytruncate'] = if ($nocopytruncate) { $false } else { $copytruncate }
94-
$options['create'] = if ($nocreate) { '' } else { $create }
94+
95+
$options['create'] = if ($nocreate) { '' } else {
96+
# 'create' option's attributes are optional, in which case its value is an empty string
97+
if ($PSBoundParameters.ContainsKey('create') -and $PSBoundParameters['create'] -eq '') {
98+
' ' # Set to a non-empty string
99+
}else {
100+
$create
101+
}
102+
}
95103
$options['delaycompress'] = if ($nodelaycompress) { $false } else { $delaycompress }
96104
$options['dateext'] = if ($nodateext) { $false } else { $dateext }
97105
$options['mail'] = if ($nomail) { $false } else { $mail }

0 commit comments

Comments
 (0)