Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ALZ/ALZ.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
'Get-ALZGithubRelease'
'New-ALZEnvironment'
'Test-ALZRequirement'
'Edit-LineEnding'
)

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand Down
41 changes: 41 additions & 0 deletions src/ALZ/Public/Edit-LineEnding.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
enum LineEndingTypes {
Darwin
Unix
Win
}

function Edit-LineEnding {
[CmdletBinding(SupportsShouldProcess = $true)]
[OutputType([String[]])]
param (
[Parameter(ValueFromPipeline = $true)]
[String[]]$InputText,
[Parameter()][LineEndingTypes]$LineEnding = "Unix"
)

Begin {

Switch ("$LineEnding".ToLower()) {
"darwin" { $eol = "`r" }
"unix" { $eol = "`n" }
"win" { $eol = "`r`n" }
}

}

Process {

[String[]]$outputText += $InputText |
ForEach-Object { $_ -replace "`r`n", "`n" } |
ForEach-Object { $_ -replace "`r", "`n" } |
ForEach-Object { $_ -replace "`n", "$eol" }

}

End {

return $outputText

}

}
89 changes: 89 additions & 0 deletions src/Tests/Unit/Public/Edit-LineEnding.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#-------------------------------------------------------------------------
Set-Location -Path $PSScriptRoot
#-------------------------------------------------------------------------
$ModuleName = 'ALZ'
$PathToManifest = [System.IO.Path]::Combine('..', '..', '..', $ModuleName, "$ModuleName.psd1")
#-------------------------------------------------------------------------
if (Get-Module -Name $ModuleName -ErrorAction 'SilentlyContinue') {
#if the module is already in memory, remove it
Remove-Module -Name $ModuleName -Force
}
Import-Module $PathToManifest -Force
#-------------------------------------------------------------------------

InModuleScope 'ALZ' {
Describe "Edit-LineEnding" {
BeforeAll {
$WarningPreference = 'SilentlyContinue'
$ErrorActionPreference = 'SilentlyContinue'
}
Context "When converting to Unix line endings" {
It "Converts Windows line endings to Unix" {
$inputText = "Hello`r`nWorld`r`n"
$expectedOutput = "Hello`nWorld`n"
$result = Edit-LineEnding -InputText $inputText -LineEnding Unix
$result | Should -Be $expectedOutput
}

It "Converts mixed line endings to Unix" {
$inputText = "Hello`rWorld`n"
$expectedOutput = "Hello`nWorld`n"
$result = Edit-LineEnding -InputText $inputText -LineEnding Unix
$result | Should -Be $expectedOutput
}

It "Does not modify Unix line endings" {
$inputText = "Hello`nWorld`n"
$expectedOutput = "Hello`nWorld`n"
$result = Edit-LineEnding -InputText $inputText -LineEnding Unix
$result | Should -Be $expectedOutput
}
}

Context "When converting to Windows line endings" {
It "Converts Unix line endings to Windows" {
$inputText = "Hello`nWorld`n"
$expectedOutput = "Hello`r`nWorld`r`n"
$result = Edit-LineEnding -InputText $inputText -LineEnding Win
$result | Should -Be $expectedOutput
}

It "Converts mixed line endings to Windows" {
$inputText = "Hello`rWorld`n"
$expectedOutput = "Hello`r`nWorld`r`n"
$result = Edit-LineEnding -InputText $inputText -LineEnding Win
$result | Should -Be $expectedOutput
}

It "Does not modify Windows line endings" {
$inputText = "Hello`r`nWorld`r`n"
$expectedOutput = "Hello`r`nWorld`r`n"
$result = Edit-LineEnding -InputText $inputText -LineEnding Win
$result | Should -Be $expectedOutput
}
}

Context "When converting to Darwin line endings" {
It "Converts Unix line endings to Darwin" {
$inputText = "Hello`nWorld`n"
$expectedOutput = "Hello`rWorld`r"
$result = Edit-LineEnding -InputText $inputText -LineEnding Darwin
$result | Should -Be $expectedOutput
}

It "Converts mixed line endings to Darwin" {
$inputText = "Hello`rWorld`n"
$expectedOutput = "Hello`rWorld`r"
$result = Edit-LineEnding -InputText $inputText -LineEnding Darwin
$result | Should -Be $expectedOutput
}

It "Does not modify Darwin line endings" {
$inputText = "Hello`rWorld`r"
$expectedOutput = "Hello`rWorld`r"
$result = Edit-LineEnding -InputText $inputText -LineEnding Darwin
$result | Should -Be $expectedOutput
}
}
}
}