Skip to content

Commit de70ee3

Browse files
committed
(chocolatey-archiveGH-427) specs for Get-ChecksumValid
1 parent cd00092 commit de70ee3

1 file changed

Lines changed: 181 additions & 0 deletions

File tree

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
$here = Split-Path -Parent $MyInvocation.MyCommand.Definition
2+
$common = Join-Path (Split-Path -Parent $here) '_Common.ps1'
3+
. $common
4+
5+
Describe "Get-ChecksumValid" {
6+
function setup-testfile {
7+
$filePath = 'some\file.txt'
8+
Setup -File "$filePath" 'yo yo'
9+
10+
Join-Path (Join-Path $env:Temp 'pester') "$filePath"
11+
}
12+
13+
Context "When a good checksum is provided" {
14+
$script:error_message = ""
15+
$filePath = setup-testfile
16+
Mock Start-Process {} #{} -Verifiable {$packageName -eq "chocolateytestpackage" -and $version -eq "0.1"}
17+
18+
try {
19+
Get-ChecksumValid -file "$filePath" -checksum 'E855365208A4CCB9C7FF4B67321DA89B'
20+
} catch {
21+
Write-Host "$_"
22+
$script:error_message = $($_.Exception.Message)
23+
}
24+
25+
It "should not return an error" {
26+
$script:error_message | should BeNullOrEmpty
27+
}
28+
}
29+
30+
Context "When a good checksum and checksum type sha1 is provided" {
31+
$script:error_message = ""
32+
$filePath = setup-testfile
33+
try {
34+
Get-ChecksumValid -file "$filePath" -checksum '0FBD35FFD7EC40CB99D13DB2E759BEAE88512C4C' -checksumType 'sha1'
35+
} catch {
36+
$script:error_message = $($_.Exception.Message)
37+
}
38+
39+
It "should not return an error" {
40+
$script:error_message | should BeNullOrEmpty
41+
}
42+
}
43+
44+
Context "When a good checksum and bad checksum type is provided" {
45+
$script:error_message = ""
46+
$filePath = setup-testfile
47+
try {
48+
Get-ChecksumValid -file "$filePath" -checksum 'E855365208A4CCB9C7FF4B67321DA89B' -checksumType 'wewsha1'
49+
} catch {
50+
$script:error_message = $($_.Exception.Message)
51+
}
52+
53+
It "should default to md5 and validate against that" {}
54+
55+
It "should not return an error" {
56+
$script:error_message | should BeNullOrEmpty
57+
}
58+
}
59+
60+
Context "When a bad checksum is provided" {
61+
$script:error_message = ""
62+
$filePath = setup-testfile
63+
Mock Start-Process {} #{} -Verifiable {$packageName -eq "chocolateytestpackage" -and $version -eq "0.1"}
64+
65+
try {
66+
Get-ChecksumValid -file "$filePath" -checksum '234'
67+
} catch {
68+
$script:error_message = $($_.Exception.Message)
69+
}
70+
71+
It "should error" {
72+
$script:error_message | should not BeNullOrEmpty
73+
}
74+
75+
It "should error with the correct message" {
76+
$script:error_message | should be "Checksum for `'$filePath'` did not meet `'234`' for checksum type `'md5`'."
77+
}
78+
}
79+
80+
Context "When a bad checksum with checksum type sha1 is provided" {
81+
$script:error_message = ""
82+
$filePath = setup-testfile
83+
Mock Start-Process {} #{} -Verifiable {$packageName -eq "chocolateytestpackage" -and $version -eq "0.1"}
84+
85+
try {
86+
Get-ChecksumValid -file "$filePath" -checksum '234' -checksumType 'sha1'
87+
} catch {
88+
$script:error_message = $($_.Exception.Message)
89+
}
90+
91+
It "should error" {
92+
$script:error_message | should not BeNullOrEmpty
93+
}
94+
95+
It "should error with the correct message" {
96+
$script:error_message | should be "Checksum for `'$filePath'` did not meet `'234`' for checksum type `'sha1`'."
97+
}
98+
}
99+
100+
Context "When an invalid checksum type is provided" {
101+
$script:error_message = ""
102+
$filePath = setup-testfile
103+
Mock Start-Process {} #{} -Verifiable {$packageName -eq "chocolateytestpackage" -and $version -eq "0.1"}
104+
105+
try {
106+
Get-ChecksumValid -file "$filePath" -checksum '234' -checksumType 'wewsha1'
107+
} catch {
108+
$script:error_message = $($_.Exception.Message)
109+
}
110+
111+
It "should error" {
112+
$script:error_message | should not BeNullOrEmpty
113+
}
114+
115+
It "should error with message containing checksum type 'md5'" {
116+
$script:error_message | should be "Checksum for `'$filePath'` did not meet `'234`' for checksum type `'md5`'."
117+
}
118+
}
119+
120+
Context "When no checksum is provided" {
121+
$script:error_message = ""
122+
$filePath = setup-testfile
123+
Mock Start-Process {} #{} -Verifiable {$packageName -eq "chocolateytestpackage" -and $version -eq "0.1"}
124+
125+
try {
126+
Get-ChecksumValid -file "$filePath"
127+
} catch {
128+
$script:error_message = $($_.Exception.Message)
129+
}
130+
131+
It "should not error" {
132+
$script:error_message | should BeNullOrEmpty
133+
}
134+
135+
It "should not call checksum to verify" {
136+
Assert-MockCalled Start-Process 0
137+
}
138+
}
139+
140+
Context "When no checksum is provided and the file does not exist" {
141+
$script:error_message = ""
142+
$filePath = setup-testfile
143+
Mock Start-Process {} #{} -Verifiable {$packageName -eq "chocolateytestpackage" -and $version -eq "0.1"}
144+
145+
try {
146+
Remove-Item $filePath -force
147+
Get-ChecksumValid -file "$filePath" -checksum ''
148+
} catch {
149+
$script:error_message = $($_.Exception.Message)
150+
}
151+
152+
It "should not error" {
153+
$script:error_message | should BeNullOrEmpty
154+
}
155+
156+
It "should not call checksum to verify" {
157+
Assert-MockCalled Start-Process 0
158+
}
159+
}
160+
161+
Context "When a checksum is provided but the file doesn't exist" {
162+
$script:error_message = ""
163+
$filePath = setup-testfile
164+
Mock Start-Process {} #{} -Verifiable {$packageName -eq "chocolateytestpackage" -and $version -eq "0.1"}
165+
166+
try {
167+
Remove-Item $filePath -force
168+
Get-ChecksumValid -file "$filePath" -checksum '234'
169+
} catch {
170+
$script:error_message = $($_.Exception.Message)
171+
}
172+
173+
It "should error" {
174+
$script:error_message | should not BeNullOrEmpty
175+
}
176+
177+
It "should error with the correct message" {
178+
$script:error_message | should be "Unable to checksum a file that doesn't exist - Could not find file `'$filePath`'"
179+
}
180+
}
181+
}

0 commit comments

Comments
 (0)