test/ConvertTo-LF.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
Import-Module -Name $PSScriptRoot\resource\Get-Bytes -Verbose -Force
Import-Module -Name $PSScriptRoot\..\EndOfLine -Verbose -Force

Describe "Test ConvertTo-LF" {
    Context "with UTF-8 CRLF file" {
        InModuleScope EndOfLine {
            $script:SUT = $true
        
            # Using Pester's TestDrive: https://github.com/pester/Pester/wiki/TestDrive
            Copy-Item -Path "resource\index-UTF8-CRLF-NoBOM-NoXtraLine.html" -Destination "TestDrive:\"
        
            It "Should of removed Unicode 13dec chracters" -TestCases @(
                @{  Path                 = "TestDrive:\index-UTF8-CRLF-NoBOM-NoXtraLine.html"; `
                        SkipIgnoreFile   = $true; `
                        ExportReportData = $false; `
                        WhatIf           = $false `
                
                }) {
                Param($Path, $SkipIgnoreFile, $ExportReportData, $WhatIf)

                $Prior = Get-Bytes -Path $Path -Format DecimalOnly | Select-Object -First 10
                ConvertTo-LF -Path $Path -SkipIgnoreFile:$SkipIgnoreFile -ExportReportData:$ExportReportData -WhatIf:$WhatIf
                $Post = Get-Bytes -Path $Path -Format DecimalOnly | Select-Object -First 10

                $Prior[6] | Should -Be 13
                $Prior[7] | Should -Be 10
                $Prior[8] | Should -Be 13
                $Prior[9] | Should -Be 10

                $Post[6] | Should -Be 10
                $Post[7] | Should -Be 10
                $Post[8] | Should -Be 60
                $Post[9] | Should -Be 104

                $Post -notcontains 13 | Should -Be $true
            }
        }
    }
    Context "with UTF-8 CRLF file in 'WhatIf' mode" {
        InModuleScope EndOfLine {
            $script:SUT = $true
        
            # Using Pester's TestDrive: https://github.com/pester/Pester/wiki/TestDrive
            Copy-Item -Path "resource\index-UTF8-CRLF-NoBOM-NoXtraLine.html" -Destination "TestDrive:\"
        
            It "Should of not modified file - WhatIf mode" -TestCases @(
                @{  Path                 = "TestDrive:\index-UTF8-CRLF-NoBOM-NoXtraLine.html"; `
                        SkipIgnoreFile   = $true; `
                        ExportReportData = $false; `
                        WhatIf           = $true `
                
                }) {
                Param($Path, $SkipIgnoreFile, $ExportReportData, $WhatIf)

                $Prior = Get-FileHash -Path $Path | Select-Object -ExpandProperty Hash
                ConvertTo-LF -Path $Path -SkipIgnoreFile:$SkipIgnoreFile -ExportReportData:$ExportReportData -WhatIf:$WhatIf
                $Post = Get-FileHash -Path $Path | Select-Object -ExpandProperty Hash

                $Prior -eq $Post | Should -Be $true
            }
        }
    }
    Context "with UTF-8 CRLF with extra line file" {
        InModuleScope EndOfLine {
            $script:SUT = $true
        
            # Using Pester's TestDrive: https://github.com/pester/Pester/wiki/TestDrive
            Copy-Item -Path "resource\index-UTF8-CRLF-NoBOM-XtraLine.html" -Destination "TestDrive:\"
        
            It "Should of removed Unicode 13dec chracters and preserved extra line at end" -TestCases @(
                @{  Path                 = "TestDrive:\index-UTF8-CRLF-NoBOM-XtraLine.html"; `
                        SkipIgnoreFile   = $true; `
                        ExportReportData = $false; `
                        WhatIf           = $false `
                
                }) {
                Param($Path, $SkipIgnoreFile, $ExportReportData, $WhatIf)

                $Prior = Get-Bytes -Path $Path -Format DecimalOnly | Select-Object -Last 5
                ConvertTo-LF -Path $Path -SkipIgnoreFile:$SkipIgnoreFile -ExportReportData:$ExportReportData -WhatIf:$WhatIf
                $Post = Get-Bytes -Path $Path -Format DecimalOnly | Select-Object -Last 5

                $Prior[0] | Should -Be 62
                $Prior[1] | Should -Be 13
                $Prior[2] | Should -Be 10
                $Prior[3] | Should -Be 13
                $Prior[4] | Should -Be 10

                $Post[0] | Should -Be 109
                $Post[1] | Should -Be 108
                $Post[2] | Should -Be 62
                $Post[3] | Should -Be 10
                $Post[4] | Should -Be 10

                $Post -notcontains 13 | Should -Be $true
            }
        }
    }
    Context "with directory with .gitignore file" {
        InModuleScope EndOfLine {
            $script:SUT = $true
        
            # Using Pester's TestDrive: https://github.com/pester/Pester/wiki/TestDrive
            Copy-Item -Path "resource\.gitignore" -Destination "TestDrive:\"
            # load various files
            Copy-Item -Path "resource\index-UTF8-CRLF-NoBOM-NoXtraLine.html" -Destination "TestDrive:\"
            Copy-Item -Path "resource\index-UTF16BE-CRLF-NoBOM-NoXtraLine.html" -Destination "TestDrive:\"
            Copy-Item -Path "resource\index-UTF16LE-CRLF-NoBOM-NoXtraLine.html" -Destination "TestDrive:\"
        
            It "Should of not modified file - listed in .gitignore file" -TestCases @(
                @{  Path                 = "TestDrive:\index-UTF8-CRLF-NoBOM-NoXtraLine.html"; `
                        SkipIgnoreFile   = $false; `
                        ExportReportData = $false; `
                        WhatIf           = $false `
                
                }) {
                Param($Path, $SkipIgnoreFile, $ExportReportData, $WhatIf)

                $Prior = Get-FileHash -Path $Path | Select-Object -ExpandProperty Hash
                # here we need to specify a directory and not a file. if it was a file,
                # it would negate .gitignore
                ConvertTo-LF -Path $TestDrive -SkipIgnoreFile:$SkipIgnoreFile -ExportReportData:$ExportReportData -WhatIf:$WhatIf
                $Post = Get-FileHash -Path $Path | Select-Object -ExpandProperty Hash

                $Prior -eq $Post | Should -Be $true
            }
        }
    }
    Context "with UTF-8 LF files" {
        InModuleScope EndOfLine {
            $script:SUT = $true
        
            # Using Pester's TestDrive: https://github.com/pester/Pester/wiki/TestDrive
            Copy-Item -Path "resource\index-UTF8-LF-BOM-NoXtraLine.html" -Destination "TestDrive:\"
            Copy-Item -Path "resource\index-UTF8-LF-NoBOM-NoXtraLine.html" -Destination "TestDrive:\"
        
            It "Should of not modified file - same EOL as requested" -TestCases @(
                @{  Path                 = "TestDrive:\index-UTF8-LF-NoBOM-NoXtraLine.html"; `
                        SkipIgnoreFile   = $true; `
                        ExportReportData = $false; `
                        WhatIf           = $false `
                
                },
                @{  Path                 = "TestDrive:\index-UTF8-LF-BOM-NoXtraLine.html"; `
                        SkipIgnoreFile   = $true; `
                        ExportReportData = $false; `
                        WhatIf           = $false `
                
                }) {
                Param($Path, $SkipIgnoreFile, $ExportReportData, $WhatIf)

                $Prior = Get-FileHash -Path $Path | Select-Object -ExpandProperty Hash
                ConvertTo-LF -Path $Path -SkipIgnoreFile:$SkipIgnoreFile -ExportReportData:$ExportReportData -WhatIf:$WhatIf
                $Post = Get-FileHash -Path $Path | Select-Object -ExpandProperty Hash

                $Prior -eq $Post | Should -Be $true
            }
        }
    }
    Context "with UTF-16 files" {
        InModuleScope EndOfLine {
            $script:SUT = $true
        
            # Using Pester's TestDrive: https://github.com/pester/Pester/wiki/TestDrive
            Copy-Item -Path "resource\index-UTF16BE-CRLF-NoBOM-NoXtraLine.html" -Destination "TestDrive:\"
            Copy-Item -Path "resource\index-UTF16LE-CRLF-NoBOM-NoXtraLine.html" -Destination "TestDrive:\"
        
            It "Should of not modified file - not UTF-8 encoded file" -TestCases @(
                @{  Path                 = "TestDrive:\index-UTF16BE-CRLF-NoBOM-NoXtraLine.html"; `
                        SkipIgnoreFile   = $true; `
                        ExportReportData = $false; `
                        WhatIf           = $false `
                
                },
                @{  Path                 = "TestDrive:\index-UTF16LE-CRLF-NoBOM-NoXtraLine.html"; `
                        SkipIgnoreFile   = $true; `
                        ExportReportData = $false; `
                        WhatIf           = $false `
                
                }) {
                Param($Path, $SkipIgnoreFile, $ExportReportData, $WhatIf)

                $Prior = Get-FileHash -Path $Path | Select-Object -ExpandProperty Hash
                ConvertTo-LF -Path $Path -SkipIgnoreFile:$SkipIgnoreFile -ExportReportData:$ExportReportData -WhatIf:$WhatIf
                $Post = Get-FileHash -Path $Path | Select-Object -ExpandProperty Hash

                $Prior -eq $Post | Should -Be $true
            }
        }
    }
}