Skip to content

Commit 3cf8e28

Browse files
authored
Refactor/stream parameter consolidation (#19)
* Pin GitHub Actions to full-length commit SHAs * refactor: consolidate redundant -MemoryStream parameter into -Stream (#17) * chore: bump version to 2.0.2-alpha
1 parent 22df239 commit 3cf8e28

8 files changed

Lines changed: 200 additions & 50 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ jobs:
324324

325325
- name: Download previous module artifact (if not rebuilt)
326326
if: steps.upload-new.outputs.artifact-id == ''
327-
uses: dawidd6/action-download-artifact@v6
327+
uses: dawidd6/action-download-artifact@bf251b5aa9c2f7eeb574a96ee720e24f801b7c11
328328
with:
329329
name: Convert-Module-Universal
330330
path: Artifacts/

.github/workflows/docs.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ jobs:
3030

3131
steps:
3232
- name: Checkout code
33-
uses: actions/checkout@v4
33+
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5
3434

3535
- name: Download module artifact from CI
3636
if: ${{ github.event_name == 'workflow_run' }}
37-
uses: actions/download-artifact@v4
37+
uses: actions/download-artifact@95815c38cf2ff2164869cbab79da8d1f422bc89e
3838
with:
3939
name: Convert-Module-Universal
4040
path: Artifacts/
@@ -51,10 +51,10 @@ jobs:
5151
./build.ps1 -PowerShell -Build
5252
5353
- name: Setup Pages
54-
uses: actions/configure-pages@v5
54+
uses: actions/configure-pages@983d7736d9b0ae728b81ab479565c72886d7745b
5555

5656
- name: Cache PowerShell modules
57-
uses: actions/cache@v4
57+
uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57
5858
with:
5959
path: ~/.local/share/powershell/Modules
6060
key: ${{ runner.os }}-psmodules-${{ hashFiles('install_modules.ps1') }}
@@ -68,7 +68,7 @@ jobs:
6868
run: ./.build/Invoke-DocumentationGeneration.ps1 -Force
6969

7070
- name: Setup Python
71-
uses: actions/setup-python@v5
71+
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065
7272
with:
7373
python-version: '3.11'
7474
cache: 'pip'
@@ -80,7 +80,7 @@ jobs:
8080
run: mkdocs build --strict
8181

8282
- name: Upload artifact
83-
uses: actions/upload-pages-artifact@v3
83+
uses: actions/upload-pages-artifact@56afc609e74202658d3ffba0e8f6dda462b719fa
8484
with:
8585
path: site/
8686

@@ -95,4 +95,4 @@ jobs:
9595
steps:
9696
- name: Deploy to GitHub Pages
9797
id: deployment
98-
uses: actions/deploy-pages@v4
98+
uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e

.github/workflows/publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ jobs:
2727

2828
steps:
2929
- name: Checkout
30-
uses: actions/checkout@v4
30+
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5
3131
with:
3232
sparse-checkout: .build/Invoke-PublishModule.ps1
3333
sparse-checkout-cone-mode: false
3434

3535
- name: Download module artifact
36-
uses: actions/download-artifact@v4
36+
uses: actions/download-artifact@95815c38cf2ff2164869cbab79da8d1f422bc89e
3737
with:
3838
name: ${{ env.MODULE_NAME }}-Module-Universal
3939
path: Artifacts/

src/Convert/Convert.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'Convert.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '2.0.1'
15+
ModuleVersion = '2.0.2'
1616

1717
# Supported PSEditions
1818
CompatiblePSEditions = @(

src/Convert/Public/ConvertFrom-MemoryStreamToString.ps1

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
.DESCRIPTION
66
Converts MemoryStream to a string.
77
8-
.PARAMETER MemoryStream
9-
A System.IO.MemoryStream object for conversion.
10-
118
.PARAMETER Stream
12-
A System.IO.Stream object for conversion.
9+
A System.IO.Stream object for conversion. Accepts any stream type including MemoryStream, FileStream, etc.
1310
1411
.EXAMPLE
1512
$string = 'A string'
@@ -84,16 +81,9 @@ function ConvertFrom-MemoryStreamToString {
8481
Mandatory = $true,
8582
ValueFromPipeline = $true,
8683
ValueFromPipelineByPropertyName = $true,
87-
ParameterSetName = 'MemoryStream')]
88-
[ValidateNotNullOrEmpty()]
89-
[System.IO.MemoryStream[]]
90-
$MemoryStream,
91-
92-
[Parameter(
93-
Mandatory = $true,
94-
ValueFromPipelineByPropertyName = $true,
9584
ParameterSetName = 'Stream')]
9685
[ValidateNotNullOrEmpty()]
86+
[Alias('MemoryStream')]
9787
[System.IO.Stream[]]
9888
$Stream
9989
)
@@ -103,18 +93,9 @@ function ConvertFrom-MemoryStreamToString {
10393
}
10494

10595
process {
106-
switch ($PSCmdlet.ParameterSetName) {
107-
'MemoryStream' {
108-
$inputObject = $MemoryStream
109-
}
110-
'Stream' {
111-
$inputObject = $Stream
112-
}
113-
}
114-
115-
foreach ($object in $inputObject) {
96+
foreach ($object in $Stream) {
11697
try {
117-
if ($PSCmdlet.ParameterSetName -eq 'MemoryStream') {
98+
if ($object.CanSeek) {
11899
$object.Position = 0
119100
}
120101
$reader = [System.IO.StreamReader]::new($object)

src/Convert/Public/ConvertTo-String.ps1

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88
.PARAMETER Base64EncodedString
99
A Base64 Encoded String
1010
11-
.PARAMETER MemoryStream
12-
A MemoryStream object for conversion.
13-
1411
.PARAMETER Stream
15-
A System.IO.Stream object for conversion.
12+
A System.IO.Stream object for conversion. Accepts any stream type including MemoryStream, FileStream, etc.
1613
1714
.PARAMETER Encoding
1815
The encoding to use for conversion.
@@ -105,16 +102,9 @@ function ConvertTo-String {
105102
Mandatory = $true,
106103
ValueFromPipeline = $true,
107104
ValueFromPipelineByPropertyName = $true,
108-
ParameterSetName = 'MemoryStream')]
109-
[ValidateNotNullOrEmpty()]
110-
[System.IO.MemoryStream[]]
111-
$MemoryStream,
112-
113-
[Parameter(
114-
Mandatory = $true,
115-
ValueFromPipelineByPropertyName = $true,
116105
ParameterSetName = 'Stream')]
117106
[ValidateNotNullOrEmpty()]
107+
[Alias('MemoryStream')]
118108
[System.IO.Stream[]]
119109
$Stream,
120110

@@ -158,10 +148,6 @@ function ConvertTo-String {
158148
}
159149
}
160150

161-
'MemoryStream' {
162-
$MemoryStream | ConvertFrom-MemoryStreamToString -ErrorAction $userErrorActionPreference
163-
}
164-
165151
'Stream' {
166152
$Stream | ConvertFrom-MemoryStreamToString -ErrorAction $userErrorActionPreference
167153
}

src/Tests/Unit/ConvertFrom-MemoryStreamToString.Tests.ps1

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,101 @@ $function = $MyInvocation.MyCommand.Name.Split('.')[0]
22

33
Describe -Name $function -Fixture {
44

5+
Context -Name 'Stream input (non-MemoryStream)' -Fixture {
6+
It -Name 'Converts FileStream using -Stream parameter' -Test {
7+
$string = 'ThisIsMyString'
8+
$tempFile = [System.IO.Path]::GetTempFileName()
9+
$fileStream = $null
10+
try {
11+
[System.IO.File]::WriteAllText($tempFile, $string)
12+
$fileStream = [System.IO.File]::OpenRead($tempFile)
13+
14+
$assertion = ConvertFrom-MemoryStreamToString -Stream $fileStream
15+
16+
$assertion | Should -BeExactly $string
17+
} finally {
18+
if ($fileStream) { $fileStream.Dispose() }
19+
if (Test-Path $tempFile) { Remove-Item $tempFile -Force }
20+
}
21+
}
22+
23+
It -Name 'Converts FileStream from Pipeline' -Test {
24+
$string = 'ThisIsMyString'
25+
$tempFile = [System.IO.Path]::GetTempFileName()
26+
$fileStream = $null
27+
try {
28+
[System.IO.File]::WriteAllText($tempFile, $string)
29+
$fileStream = [System.IO.File]::OpenRead($tempFile)
30+
31+
$assertion = $fileStream | ConvertFrom-MemoryStreamToString
32+
33+
$assertion | Should -BeExactly $string
34+
} finally {
35+
if ($fileStream) { $fileStream.Dispose() }
36+
if (Test-Path $tempFile) { Remove-Item $tempFile -Force }
37+
}
38+
}
39+
40+
It -Name 'Converts BufferedStream to string' -Test {
41+
$string = 'ThisIsMyString'
42+
$memStream = [System.IO.MemoryStream]::new()
43+
$writer = [System.IO.StreamWriter]::new($memStream)
44+
$writer.Write($string)
45+
$writer.Flush()
46+
47+
$bufferedStream = [System.IO.BufferedStream]::new($memStream)
48+
49+
$assertion = ConvertFrom-MemoryStreamToString -Stream $bufferedStream
50+
51+
$assertion | Should -BeExactly $string
52+
53+
$bufferedStream.Dispose()
54+
$writer.Dispose()
55+
$memStream.Dispose()
56+
}
57+
58+
It -Name 'Converts array of FileStreams from Pipeline' -Test {
59+
$string = 'ThisIsMyString'
60+
$tempFile1 = [System.IO.Path]::GetTempFileName()
61+
$tempFile2 = [System.IO.Path]::GetTempFileName()
62+
$stream1 = $null
63+
$stream2 = $null
64+
try {
65+
[System.IO.File]::WriteAllText($tempFile1, $string)
66+
[System.IO.File]::WriteAllText($tempFile2, $string)
67+
68+
$stream1 = [System.IO.File]::OpenRead($tempFile1)
69+
$stream2 = [System.IO.File]::OpenRead($tempFile2)
70+
71+
$assertion = @($stream1, $stream2) | ConvertFrom-MemoryStreamToString
72+
73+
$assertion | Should -HaveCount 2
74+
} finally {
75+
if ($stream1) { $stream1.Dispose() }
76+
if ($stream2) { $stream2.Dispose() }
77+
if (Test-Path $tempFile1) { Remove-Item $tempFile1 -Force }
78+
if (Test-Path $tempFile2) { Remove-Item $tempFile2 -Force }
79+
}
80+
}
81+
}
82+
83+
Context -Name 'Alias' -Fixture {
84+
It -Name 'ConvertFrom-StreamToString alias works' -Test {
85+
$string = 'ThisIsMyString'
86+
$stream = [System.IO.MemoryStream]::new()
87+
$writer = [System.IO.StreamWriter]::new($stream)
88+
$writer.Write($string)
89+
$writer.Flush()
90+
91+
$assertion = ConvertFrom-StreamToString -Stream $stream
92+
93+
$assertion | Should -BeExactly $string
94+
95+
$stream.Dispose()
96+
$writer.Dispose()
97+
}
98+
}
99+
5100
Context -Name 'Input/Output' -Fixture {
6101
It -Name "Converts a MemoryStream to a string" -Test {
7102
$string = 'ThisIsMyString'

src/Tests/Unit/ConvertTo-String.Tests.ps1

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,94 @@ Describe -Name $function -Fixture {
5959
}
6060
}
6161

62+
Context -Name 'Stream input' -Fixture {
63+
It -Name 'Converts MemoryStream using -Stream parameter' -Test {
64+
$stream = [System.IO.MemoryStream]::new()
65+
$writer = [System.IO.StreamWriter]::new($stream)
66+
$writer.Write($String)
67+
$writer.Flush()
68+
69+
$assertion = ConvertTo-String -Stream $stream
70+
71+
$assertion | Should -BeExactly $String
72+
73+
$writer.Dispose()
74+
$stream.Dispose()
75+
}
76+
77+
It -Name 'Converts FileStream to string' -Test {
78+
$tempFile = [System.IO.Path]::GetTempFileName()
79+
$fileStream = $null
80+
try {
81+
[System.IO.File]::WriteAllText($tempFile, $String)
82+
$fileStream = [System.IO.File]::OpenRead($tempFile)
83+
84+
$assertion = ConvertTo-String -Stream $fileStream
85+
86+
$assertion | Should -BeExactly $String
87+
} finally {
88+
if ($fileStream) { $fileStream.Dispose() }
89+
if (Test-Path $tempFile) { Remove-Item $tempFile -Force }
90+
}
91+
}
92+
93+
It -Name 'Converts FileStream from Pipeline' -Test {
94+
$tempFile = [System.IO.Path]::GetTempFileName()
95+
$fileStream = $null
96+
try {
97+
[System.IO.File]::WriteAllText($tempFile, $String)
98+
$fileStream = [System.IO.File]::OpenRead($tempFile)
99+
100+
$assertion = $fileStream | ConvertTo-String
101+
102+
$assertion | Should -BeExactly $String
103+
} finally {
104+
if ($fileStream) { $fileStream.Dispose() }
105+
if (Test-Path $tempFile) { Remove-Item $tempFile -Force }
106+
}
107+
}
108+
109+
It -Name 'Converts BufferedStream to string' -Test {
110+
$memStream = [System.IO.MemoryStream]::new()
111+
$writer = [System.IO.StreamWriter]::new($memStream)
112+
$writer.Write($String)
113+
$writer.Flush()
114+
115+
$bufferedStream = [System.IO.BufferedStream]::new($memStream)
116+
117+
$assertion = ConvertTo-String -Stream $bufferedStream
118+
119+
$assertion | Should -BeExactly $String
120+
121+
$bufferedStream.Dispose()
122+
$writer.Dispose()
123+
$memStream.Dispose()
124+
}
125+
126+
It -Name 'Converts array of FileStreams from Pipeline' -Test {
127+
$tempFile1 = [System.IO.Path]::GetTempFileName()
128+
$tempFile2 = [System.IO.Path]::GetTempFileName()
129+
$stream1 = $null
130+
$stream2 = $null
131+
try {
132+
[System.IO.File]::WriteAllText($tempFile1, $String)
133+
[System.IO.File]::WriteAllText($tempFile2, $String)
134+
135+
$stream1 = [System.IO.File]::OpenRead($tempFile1)
136+
$stream2 = [System.IO.File]::OpenRead($tempFile2)
137+
138+
$assertion = @($stream1, $stream2) | ConvertTo-String
139+
140+
$assertion | Should -HaveCount 2
141+
} finally {
142+
if ($stream1) { $stream1.Dispose() }
143+
if ($stream2) { $stream2.Dispose() }
144+
if (Test-Path $tempFile1) { Remove-Item $tempFile1 -Force }
145+
if (Test-Path $tempFile2) { Remove-Item $tempFile2 -Force }
146+
}
147+
}
148+
}
149+
62150
Context -Name 'MemoryStream input' -Fixture {
63151
It -Name 'Converts to base64 correctly' -Test {
64152
$stream = [System.IO.MemoryStream]::new()

0 commit comments

Comments
 (0)