Skip to content

Commit d91658d

Browse files
Add ConvertToHumanReadableSize function and associated tests; update CI/CD workflow and VSCode settings
1 parent a023095 commit d91658d

12 files changed

+602
-137
lines changed

.github/workflows/powershell-script-module-release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ jobs:
9999
shell: pwsh
100100
env:
101101
PSGALLERY_API_KEY: ${{ secrets.PSGALLERY_API_KEY }}
102+
NUGETORG_API_KEY: ${{ secrets.NUGETORG_API_KEY }}
102103
run: |
103104
Set-StrictMode -Version Latest
104105
[void] (Import-Module InvokeBuild)

.vscode/settings.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
"powershell.codeFormatting.preset": "OTBS",
2727
// Adds a space between a keyword and its associated scriptblock expression.
2828
"powershell.codeFormatting.whitespaceBeforeOpenBrace": true,
29+
// Remove trailing whitespace at the end of lines.
30+
"powershell.codeFormatting.trimTrailingWhitespace": true,
2931
// Adds a space between a keyword (if, elseif, while, switch, etc) and its associated conditional expression.
3032
"powershell.codeFormatting.whitespaceBeforeOpenParen": true,
3133
// Adds spaces before and after an operator ('=', '+', '-', etc.).
@@ -47,20 +49,22 @@
4749
"MD007": {
4850
"indent": 4
4951
},
52+
// MD013 - Line length
5053
"MD013": {
5154
"line_length": 240
5255
},
56+
// MD026 - Trailing punctuation in header
5357
"MD026": {
5458
"punctuation": ".,;:!"
5559
},
56-
"MD029": {
57-
"style": "one"
58-
},
60+
// MD029 - Ordered list item prefix
61+
"MD029": false,
62+
// MD030 - Spaces after list markers
5963
"MD033": false,
64+
// MD034 - Bare URL used
6065
"MD034": false,
61-
"MD038": false,
62-
"MD042": false,
63-
"MD024": false,
66+
//MD045 - Multiple consecutive blank lines
67+
"MD045": false,
6468
"no-hard-tabs": true
6569
},
6670
"[jsonc]": {

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ Comprehensive documentation is available in the [`docs/`](docs/) directory:
180180
- 🛠️ **[Development Guide](docs/development.md)** - Creating functions, building, testing, and generating help
181181
- 🔄 **[CI/CD & Publishing Guide](docs/ci-cd.md)** - Automated pipelines, versioning, and PowerShell Gallery publishing
182182

183-
184-
185183
## 🤝 Contributing
186184

187185
Contributions are welcome! Whether it’s bug fixes, improvements, or ideas for new features, your input helps make this template better for everyone. Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details on:
@@ -199,7 +197,6 @@ If this template saves you time or helps your projects succeed, consider support
199197
- 💬 Provide feedback via issues or discussions
200198
- ❤️ Sponsor ongoing development via GitHub Sponsors
201199

202-
203200
---
204201

205202
Built with ❤️ by [Warehouse Finds](https://github.com/WarehouseFinds)

docs/ci-cd.md

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -559,42 +559,6 @@ git push origin v1.0.0
559559
- Make breaking changes in patch versions
560560
- Publish untested code to PowerShell Gallery
561561

562-
## Advanced Configuration
563-
564-
### Customizing CI Workflow
565-
566-
Edit `.github/workflows/ci.yml`:
567-
568-
```yaml
569-
# Change test coverage threshold
570-
- name: Run Tests
571-
run: |
572-
Invoke-Build Invoke-UnitTests
573-
env:
574-
COVERAGE_THRESHOLD: 80 # Change to your requirement
575-
576-
# Add custom quality gates
577-
- name: Custom Check
578-
run: |
579-
# Your custom validation
580-
```
581-
582-
### Custom GitVersion Configuration
583-
584-
Edit `GitVersion.yml`:
585-
586-
```yaml
587-
# Change version increment behavior
588-
mode: ContinuousDelivery
589-
increment: Inherit
590-
591-
# Customize branch configuration
592-
branches:
593-
main:
594-
tag: ''
595-
increment: Patch
596-
```
597-
598562
## Resources
599563

600564
- 📖 [GitHub Actions Documentation](https://docs.github.com/actions)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
BeforeAll {
2+
. $PSCommandPath.Replace('.Tests.ps1', '.ps1')
3+
}
4+
5+
Describe 'ConvertToHumanReadableSize' {
6+
Context 'Parameter Validation' {
7+
It 'Should require Bytes parameter' {
8+
{ ConvertToHumanReadableSize } | Should -Throw
9+
}
10+
11+
It 'Should not accept negative values' {
12+
{ ConvertToHumanReadableSize -Bytes -1 } | Should -Throw
13+
}
14+
15+
It 'Should accept zero' {
16+
{ ConvertToHumanReadableSize -Bytes 0 } | Should -Not -Throw
17+
}
18+
19+
It 'Should accept maximum long value' {
20+
{ ConvertToHumanReadableSize -Bytes ([long]::MaxValue) } | Should -Not -Throw
21+
}
22+
}
23+
24+
Context 'Size Conversion' {
25+
It 'Should return "0 bytes" for zero input' {
26+
$result = ConvertToHumanReadableSize -Bytes 0
27+
$result | Should -Be '0 bytes'
28+
}
29+
30+
It 'Should format bytes correctly' {
31+
$result = ConvertToHumanReadableSize -Bytes 512
32+
$result | Should -Be '512 bytes'
33+
}
34+
35+
It 'Should convert to KB correctly' {
36+
$result = ConvertToHumanReadableSize -Bytes 1024
37+
$result | Should -Be '1.00 KB'
38+
}
39+
40+
It 'Should convert to MB correctly' {
41+
$result = ConvertToHumanReadableSize -Bytes 1048576
42+
$result | Should -Be '1.00 MB'
43+
}
44+
45+
It 'Should convert to GB correctly' {
46+
$result = ConvertToHumanReadableSize -Bytes 1073741824
47+
$result | Should -Be '1.00 GB'
48+
}
49+
50+
It 'Should convert to TB correctly' {
51+
$result = ConvertToHumanReadableSize -Bytes 1099511627776
52+
$result | Should -Be '1.00 TB'
53+
}
54+
55+
It 'Should format decimal places correctly' {
56+
$result = ConvertToHumanReadableSize -Bytes 1536
57+
$result | Should -Be '1.50 KB'
58+
}
59+
60+
It 'Should round to two decimal places' {
61+
$result = ConvertToHumanReadableSize -Bytes 1555
62+
$result | Should -Match '^\d+\.\d{2} KB$'
63+
}
64+
}
65+
66+
Context 'Largest Unit Selection' {
67+
It 'Should prefer larger units' {
68+
$result = ConvertToHumanReadableSize -Bytes (5GB + 500MB)
69+
$result | Should -Match 'GB'
70+
$result | Should -Not -Match 'MB'
71+
}
72+
73+
It 'Should use bytes for values less than 1KB' {
74+
$result = ConvertToHumanReadableSize -Bytes 1023
75+
$result | Should -Be '1023 bytes'
76+
}
77+
}
78+
79+
Context 'Edge Cases' {
80+
It 'Should handle 1 byte' {
81+
$result = ConvertToHumanReadableSize -Bytes 1
82+
$result | Should -Be '1 bytes'
83+
}
84+
85+
It 'Should handle exactly 1KB boundary' {
86+
$result = ConvertToHumanReadableSize -Bytes 1024
87+
$result | Should -Be '1.00 KB'
88+
}
89+
90+
It 'Should handle exactly 1MB boundary' {
91+
$result = ConvertToHumanReadableSize -Bytes (1024 * 1024)
92+
$result | Should -Be '1.00 MB'
93+
}
94+
95+
It 'Should handle large TB values' {
96+
$result = ConvertToHumanReadableSize -Bytes (10 * 1TB)
97+
$result | Should -Be '10.00 TB'
98+
}
99+
}
100+
101+
Context 'Return Type' {
102+
It 'Should return string type' {
103+
$result = ConvertToHumanReadableSize -Bytes 1024
104+
$result | Should -BeOfType [string]
105+
}
106+
107+
It 'Should not return null' {
108+
$result = ConvertToHumanReadableSize -Bytes 0
109+
$result | Should -Not -BeNullOrEmpty
110+
}
111+
}
112+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function ConvertToHumanReadableSize {
2+
<#
3+
.SYNOPSIS
4+
Converts byte size to human-readable format
5+
6+
.DESCRIPTION
7+
Internal helper function that converts file sizes in bytes to
8+
human-readable format (KB, MB, GB, TB).
9+
10+
.PARAMETER Bytes
11+
The size in bytes to convert
12+
13+
.EXAMPLE
14+
ConvertToHumanReadableSize -Bytes 1024
15+
Returns: "1.00 KB"
16+
17+
.EXAMPLE
18+
ConvertToHumanReadableSize -Bytes 1048576
19+
Returns: "1.00 MB"
20+
21+
.OUTPUTS
22+
System.String
23+
24+
.NOTES
25+
Private function - not exported from module
26+
#>
27+
[CmdletBinding()]
28+
[OutputType([string])]
29+
param (
30+
[Parameter(Mandatory)]
31+
[ValidateRange(0, [long]::MaxValue)]
32+
[long]
33+
$Bytes
34+
)
35+
36+
# Fail fast: Handle zero bytes
37+
if ($Bytes -eq 0) {
38+
return "0 bytes"
39+
}
40+
41+
# Fail fast: Validate non-negative
42+
if ($Bytes -lt 0) {
43+
throw "Bytes parameter must be non-negative. Received: $Bytes"
44+
}
45+
46+
$sizes = @(
47+
@{ Name = 'TB'; Value = 1TB }
48+
@{ Name = 'GB'; Value = 1GB }
49+
@{ Name = 'MB'; Value = 1MB }
50+
@{ Name = 'KB'; Value = 1KB }
51+
)
52+
53+
foreach ($size in $sizes) {
54+
if ($Bytes -ge $size.Value) {
55+
$value = $Bytes / $size.Value
56+
return "{0:N2} {1}" -f $value, $size.Name
57+
}
58+
}
59+
60+
# Less than 1 KB
61+
return "$Bytes bytes"
62+
}

src/Private/GetModuleId.Tests.ps1

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Private/GetModuleId.ps1

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)