EZLog.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
Import-Module -Force (Join-Path $PSScriptRoot EZLog.psd1)

InModuleScope "EZLog" {

    Describe "EZLog Module" {

        Context "WriteEZLog with ',' separator" {
            $logfile = Join-Path $TestDrive pester.log

            It "Creates a log file with a header in it." {
                Write-EZLog -Header -LogFile $logfile -Delimiter ','
                Test-Path $logfile | Should Be $true
            }

            It "Test if the header was written correctly" {
                $r = Select-String -Path $logfile -Pattern '^\+\-*\+$'
                $r.count | Should Be 2            
            }

            It "Test if there's a CR+LF at the end of the 1st header's line" {
               $bytefile = Get-Content -Path $logfile -Encoding Byte
               ($bytefile | ForEach-Object -Begin {$i=0} -Process {if ($_ -eq 10){$bytefile[$i-1]} ; $i++})[0] | Should Be 13
            }

            It "Writes an information into the log file." {
                Write-EZLog -Category INF -Message 'This is an info to be written in the log file'
                Get-Content $logfile | Select-String -Pattern 'INF,' -Quiet | Should Be $true
            }

            It "Writes a warning into the log file." {
                Write-EZLog -Category WAR -Message 'This is a warning to be written in the log file'
                Get-Content $logfile | Select-String -Pattern 'WAR,' -Quiet | Should Be $true
            }
            
            It "Writes an error into the log file." {
                Write-EZLog -Category ERR -Message 'This is an error to be written in the log file'
                Get-Content $logfile | Select-String -Pattern 'ERR,' -Quiet | Should Be $true
            }

            It "Writes the footer into the log file." {
                Write-EZLog -Footer
                Get-Content $logfile -Tail 1 | Should BeLike '+-*-+'
            }
            Copy-Item -Path $logfile -Destination $PSScriptRoot
        } # Context "Using separator ','"

        Context "WriteEZLog with ';' separator" {
            $logfile = Join-Path $TestDrive pester.log

            It "Creates a log file with a header in it." {
                Write-EZLog -Header -LogFile $logfile -Delimiter ';'
                Test-Path $logfile | Should Be $true
            }

            It "Test if the header was written correctly" {
                $r = Select-String -Path $logfile -Pattern '^\+\-*\+$'
                $r.count | Should Be 2            
            }

            It "Writes an information into the log file." {
                Write-EZLog -Category INF -Message 'This is an info to be written in the log file'
                Get-Content $logfile | Select-String -Pattern 'INF;' -Quiet | Should Be $true
            }

            It "Writes a warning into the log file." {
                Write-EZLog -Category WAR -Message 'This is a warning to be written in the log file'
                Get-Content $logfile | Select-String -Pattern 'WAR;' -Quiet | Should Be $true
            }
            
            It "Writes an error into the log file." {
                Write-EZLog -Category ERR -Message 'This is an error to be written in the log file'
                Get-Content $logfile | Select-String -Pattern 'ERR;' -Quiet | Should Be $true
            }

            It "Writes the footer into the log file." {
                Write-EZLog -Footer
                Get-Content $logfile -Tail 1 | Should BeLike '+-*-+'
            }
        } # Context "Using separator ';'"


        Context "Write-EZLog with 'TAB' separator" {
            $logfile = Join-Path $TestDrive pester.log

            It "Creates a log file with a header in it." {
                Write-EZLog -Header -LogFile $logfile -Delimiter "`t"
                Test-Path $logfile | Should Be $true
            }

            It "Test if the header was written correctly" {
                $r = Select-String -Path $logfile -Pattern '^\+\-*\+$'
                $r.count | Should Be 2            
            }

            It "Writes an information into the log file." {
                Write-EZLog -Category INF -Message 'This is an info to be written in the log file'
                Get-Content $logfile | Select-String -Pattern "INF`t" -Quiet | Should Be $true
            }

            It "Writes a warning into the log file." {
                Write-EZLog -Category WAR -Message 'This is a warning to be written in the log file'
                Get-Content $logfile | Select-String -Pattern "WAR`t" -Quiet | Should Be $true
            }
            
            It "Writes an error into the log file." {
                Write-EZLog -Category ERR -Message 'This is an error to be written in the log file'
                Get-Content $logfile | Select-String -Pattern "ERR`t" -Quiet | Should Be $true
            }

            It "Writes the footer into the log file." {
                Write-EZLog -Footer
                Get-Content $logfile -Tail 1 | Should BeLike '+-*-+'
            }

        } # Context "Using separator 'TAB'"

        Context "ConvertFrom-EZLog" {
            $logfile = Join-Path $TestDrive pester.log

            Write-EZLog -Header -LogFile $logfile -Delimiter ','
            Write-EZLog -Category INF -Message 'This is an info to be written in the log file'
            Write-EZLog -Category WAR -Message 'This is a warning to be written in the log file'
            Write-EZLog -Category ERR -Message 'This is an error to be written in the log file'
            Write-EZLog -Footer

            $objLog = ConvertFrom-EZlog -FilePath $logFile

            It "It converts the log to a Hashtable" {
                $objLog | Should BeOfType [System.Collections.Hashtable]
            }

            It "It exists 3 properties in the Hashtable" {
                $objLog.keys.count | Should Be 3
            }

            $objLog.Header.psbase.keys | foreach { 
                It "Header : property '$_' is not null " {
                    $objLog.Header.$_ | Should Not BeNullOrEmpty
                }
            }

            $objLog.Footer.psbase.keys | foreach { 
                It "Footer : property '$_' is not null " {
                    $objLog.Footer.$_ | Should Not BeNullOrEmpty
                }
            }
        } # Context ConvertFrom-EZLog
    } # Describe
}