Skip to content

Commit 724541b

Browse files
Refactor ConvertToHumanReadableSize function and improve Get-ModuleMetadata error handling
1 parent 936c9a7 commit 724541b

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

src/Private/ConvertToHumanReadableSize.ps1

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ function ConvertToHumanReadableSize {
22
<#
33
.SYNOPSIS
44
Converts byte size to human-readable format
5-
5+
66
.DESCRIPTION
77
Internal helper function that converts file sizes in bytes to
88
human-readable format (KB, MB, GB, TB).
9-
9+
1010
.PARAMETER Bytes
1111
The size in bytes to convert
12-
12+
1313
.EXAMPLE
1414
ConvertToHumanReadableSize -Bytes 1024
1515
Returns: "1.00 KB"
16-
16+
1717
.EXAMPLE
1818
ConvertToHumanReadableSize -Bytes 1048576
1919
Returns: "1.00 MB"
20-
20+
2121
.OUTPUTS
2222
System.String
23-
23+
2424
.NOTES
2525
Private function - not exported from module
2626
#>
@@ -32,31 +32,31 @@ function ConvertToHumanReadableSize {
3232
[long]
3333
$Bytes
3434
)
35-
35+
3636
# Fail fast: Handle zero bytes
3737
if ($Bytes -eq 0) {
38-
return "0 bytes"
38+
return '0 bytes'
3939
}
40-
40+
4141
# Fail fast: Validate non-negative
4242
if ($Bytes -lt 0) {
4343
throw "Bytes parameter must be non-negative. Received: $Bytes"
4444
}
45-
45+
4646
$sizes = @(
4747
@{ Name = 'TB'; Value = 1TB }
4848
@{ Name = 'GB'; Value = 1GB }
4949
@{ Name = 'MB'; Value = 1MB }
5050
@{ Name = 'KB'; Value = 1KB }
5151
)
52-
52+
5353
foreach ($size in $sizes) {
5454
if ($Bytes -ge $size.Value) {
5555
$value = $Bytes / $size.Value
56-
return "{0:N2} {1}" -f $value, $size.Name
56+
return '{0:N2} {1}' -f $value, $size.Name
5757
}
5858
}
59-
59+
6060
# Less than 1 KB
6161
return "$Bytes bytes"
6262
}

src/Public/Get-ModuleMetadata.Tests.ps1

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ BeforeAll {
77

88
Describe 'Get-ModuleMetadata' {
99
Context 'Parameter Validation' {
10-
It 'Should require Path parameter' {
11-
{ Get-ModuleMetadata } | Should -Throw
12-
}
1310

1411
It 'Should not accept null or empty Path' {
1512
{ Get-ModuleMetadata -Path $null } | Should -Throw
@@ -140,7 +137,7 @@ Describe 'Get-ModuleMetadata' {
140137
It 'Should have correct type name' {
141138
$result = Get-ModuleMetadata -Path 'TestModule.psd1'
142139

143-
$result.PSTypeName | Should -Be 'PSScriptModule.ModuleMetadata'
140+
$result.PSObject.TypeNames[0] | Should -Be 'PSScriptModule.ModuleMetadata'
144141
}
145142
}
146143

@@ -265,8 +262,8 @@ Describe 'Get-ModuleMetadata' {
265262
$verboseOutput = Get-ModuleMetadata -Path 'TestModule.psd1' -Verbose 4>&1
266263

267264
$verboseOutput | Should -Not -BeNullOrEmpty
268-
$verboseOutput | Should -Match 'Starting'
269-
$verboseOutput | Should -Match 'Completed'
265+
$verboseOutput -join ' ' | Should -Match 'Starting'
266+
$verboseOutput -join ' ' | Should -Match 'Completed'
270267
}
271268
}
272269
}

src/Public/Get-ModuleMetadata.ps1

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ function Get-ModuleMetadata {
4747
#>
4848
[CmdletBinding()]
4949
[OutputType([PSCustomObject])]
50+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '', Justification = 'Metadata is conceptually singular')]
51+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseOutputTypeCorrectly', '', Justification = 'Function returns PSCustomObject items from internal List collection')]
5052
param (
5153
[Parameter(
5254
Mandatory,
@@ -76,9 +78,7 @@ function Get-ModuleMetadata {
7678

7779
# Fail fast: Validate path exists
7880
if (-not (Test-Path -Path $itemPath)) {
79-
$errorMessage = "Path does not exist: $itemPath"
80-
Write-Error $errorMessage
81-
throw $errorMessage
81+
throw "Path does not exist: $itemPath"
8282
}
8383

8484
# Resolve to manifest file
@@ -107,7 +107,6 @@ function Get-ModuleMetadata {
107107

108108
# Build result object
109109
$metadata = [PSCustomObject]@{
110-
PSTypeName = 'PSScriptModule.ModuleMetadata'
111110
Name = $manifest.Name
112111
Version = $manifest.Version.ToString()
113112
Author = $manifest.Author
@@ -119,6 +118,7 @@ function Get-ModuleMetadata {
119118
ExportedCmdlets = $manifest.ExportedCmdlets.Keys.Count
120119
Path = $manifestPath
121120
}
121+
$metadata.PSObject.TypeNames.Insert(0, 'PSScriptModule.ModuleMetadata')
122122

123123
# Add size information if requested
124124
if ($IncludeSize) {
@@ -130,10 +130,9 @@ function Get-ModuleMetadata {
130130
$results.Add($metadata)
131131
Write-Verbose "Successfully processed: $($manifest.Name)"
132132
} catch {
133-
$errorMessage = "Failed to process module metadata from '$itemPath': $_"
134-
Write-Verbose $errorMessage
135-
Write-Verbose "Error details: $($_.ScriptStackTrace)"
136-
throw
133+
Write-Verbose "$($MyInvocation.MyCommand) Failed to process '$itemPath': $_"
134+
Write-Verbose "StackTrace: $($_.ScriptStackTrace)"
135+
throw $_
137136
}
138137
}
139138
}

0 commit comments

Comments
 (0)