@@ -7,6 +7,16 @@ if (-not (Get-Module PSKoans)) {
77# endregion
88
99Describe ' Show-Karma' {
10+ BeforeAll {
11+ $StartingLocation = Get-PSKoanLocation
12+ Set-PSKoanLocation - Path " $TestDrive /Koans"
13+
14+ Reset-PSKoan - Confirm:$false
15+ }
16+
17+ AfterAll {
18+ Set-PSKoanLocation - Path $StartingLocation
19+ }
1020
1121 InModuleScope ' PSKoans' {
1222
@@ -23,9 +33,10 @@ Describe 'Show-Karma' {
2333 Expectation = ' ExpectedTest'
2434 It = ' TestIt'
2535 CurrentTopic = [PSCustomObject ]@ {
26- Name = ' TestTopic"'
27- Completed = 0
28- Total = 4
36+ Name = ' TestTopic"'
37+ Completed = 0
38+ Total = 4
39+ CurrentLine = 1
2940 }
3041 }
3142 }
@@ -77,9 +88,10 @@ Describe 'Show-Karma' {
7788 Expectation = ' ExpectedTest'
7889 It = ' TestIt'
7990 CurrentTopic = [PSCustomObject ]@ {
80- Name = ' TestTopic"'
81- Completed = 0
82- Total = 4
91+ Name = ' TestTopic"'
92+ Completed = 0
93+ Total = 4
94+ CurrentLine = 1
8395 }
8496 }
8597 }
@@ -109,6 +121,9 @@ Describe 'Show-Karma' {
109121 Mock Get-PSKoan - ModuleName ' PSKoans' { }
110122 Mock Update-PSKoan - ModuleName ' PSKoans' { throw ' Prevent recursion' }
111123 Mock Write-Warning
124+ Mock Test-Path { $false }
125+ Mock Invoke-Item
126+ Mock New-Item
112127 }
113128
114129 It ' should attempt to populate koans and then recurse to reassess' {
@@ -126,6 +141,22 @@ Describe 'Show-Karma' {
126141 It ' throws an error if a Topic is specified that matches nothing' {
127142 { Show-Karma - Topic ' AboutAbsolutelyNothing' } | Should - Throw - ErrorId ' PSKoans.TopicNotFound'
128143 }
144+
145+ It ' should create PSKoans directory with -Library' {
146+ { Show-Karma - Library } | Should - Throw - ExpectedMessage ' Prevent recursion'
147+
148+ Assert-MockCalled Test-Path - Times 1
149+ Assert-MockCalled Update-PSKoan - Times 1
150+ Assert-MockCalled New-Item - Times 1
151+ }
152+
153+ It ' should create PSKoans directory with -Contemplate' {
154+ { Show-Karma - Contemplate } | Should - Throw - ExpectedMessage ' Prevent recursion'
155+
156+ Assert-MockCalled Test-Path - Times 1
157+ Assert-MockCalled Update-PSKoan - Times 1
158+ Assert-MockCalled New-Item - Times 1
159+ }
129160 }
130161
131162 Context ' With -ListTopics Parameter' {
@@ -152,9 +183,10 @@ Describe 'Show-Karma' {
152183 Expectation = ' ExpectedTest'
153184 It = ' TestIt'
154185 CurrentTopic = [PSCustomObject ]@ {
155- Name = ' TestTopic"'
156- Completed = 0
157- Total = 4
186+ Name = ' TestTopic"'
187+ Completed = 0
188+ Total = 4
189+ CurrentLine = 1
158190 }
159191 RequestedTopic = $Topic
160192 }
@@ -186,73 +218,131 @@ Describe 'Show-Karma' {
186218 }
187219 }
188220
189- Context ' With -Meditate Switch' {
221+ Context ' With -Contemplate Switch' {
222+ BeforeAll {
223+ $TestFile = New-TemporaryFile
190224
191- Context ' With "code" Set as the Editor' {
192- BeforeAll {
193- Mock Get-Command { $true }
194- Mock Start-Process {
195- @ { Editor = $FilePath ; Path = $ArgumentList }
225+ Mock Invoke-Item { $Path }
226+ Mock Get-Command { $true } - ParameterFilter { $Name -ne " missing_editor" }
227+ Mock Get-Command { $false } - ParameterFilter { $Name -eq " missing_editor" }
228+ Mock Start-Process {
229+ @ { Editor = $FilePath ; Arguments = $ArgumentList }
230+ }
231+ Mock Get-Karma - ModuleName ' PSKoans' {
232+ [PSCustomObject ]@ {
233+ PSTypeName = ' PSKoans.Result'
234+ Meditation = ' TestMeditation'
235+ KoansPassed = 0
236+ TotalKoans = 400
237+ Describe = ' TestDescribe'
238+ Expectation = ' ExpectedTest'
239+ It = ' TestIt'
240+ CurrentTopic = [PSCustomObject ]@ {
241+ Name = ' TestTopic"'
242+ Completed = 0
243+ Total = 4
244+ CurrentLine = 1
245+ }
196246 }
197- Set-PSKoanSetting - Name Editor - Value ' code'
198-
199- $Result = Show-Karma - Contemplate
200247 }
248+ Mock Get-PSKoan {
249+ [PSCustomObject ]@ { Path = $TestFile.FullName }
250+ }
251+ }
201252
202- It ' should start VS Code with Start-Process' {
203- $Result.Editor | Should - Be ' code'
253+ AfterAll {
254+ $TestFile | Remove-Item
255+ }
204256
205- Assert-MockCalled Get-Command - Times 1
206- Assert-MockCalled Start-Process - Times 1
207- }
257+ It ' invokes VS Code with "code" set as Editor with proper arguments ' {
258+ Set-PSKoanSetting - Name Editor - Value ' code '
259+ $Result = Show-Karma - Contemplate
208260
209- It ' should pass a resolved path' {
210- # Resolve-Path doesn't like embedded quotes
211- $Path = $Result.Path -replace ' "'
212- $Path | Should - BeExactly (Resolve-Path - Path $Path ).Path
213- }
261+ $Result.Editor | Should - BeExactly ' code'
262+ $Result.Arguments [0 ] | Should - BeExactly ' --goto'
263+ $Result.Arguments [1 ] | Should - MatchExactly ' "[^"]+":\d+'
264+ $Result.Arguments [2 ] | Should - BeExactly ' --reuse-window'
214265
215- It ' should enclose the path in quotes' {
216- $Result.Path | Should - MatchExactly ' "[^"]+"'
217- }
266+ # Resolve-Path doesn't like embedded quotes
267+ $Path = ($Result.Arguments [1 ] -split ' (?<="):' )[0 ] -replace ' "'
268+ $Path | Should - BeExactly (Resolve-Path - Path $Path ).Path
269+
270+ Assert-MockCalled Get-Command - Times 1
271+ Assert-MockCalled Start-Process - Times 1
218272 }
219273
220- Context ' With Editor Not Found' {
221- BeforeAll {
222- Mock Get-Command { $false }
223- Mock Invoke-Item
224- Set-PSKoanSetting - Name Editor - Value " ascsadsa"
225- }
274+ It ' invokes the set editor with unknown editor chosen' {
275+ Set-PSKoanSetting - Name Editor - Value ' vim'
226276
227- It ' should not produce output ' {
228- Show-Karma - Meditate | Should - BeNullOrEmpty
229- }
277+ $Result = Show-Karma - Contemplate
278+ $Result .Editor | Should - BeExactly ' vim '
279+ $Result .Arguments | Should - MatchExactly ' "[^"]+" '
230280
231- It ' should open the koans directory with Invoke-Item' {
232- Assert-MockCalled Get-Command - Times 1 - ParameterFilter { $Name -eq " ascsadsa" }
233- Assert-MockCalled Invoke-Item - Times 1
234- }
281+ # Resolve-Path doesn't like embedded quotes
282+ $Path = $Result.Arguments -replace ' "'
283+ $Path | Should - BeExactly (Resolve-Path - Path $Path ).Path
284+
285+ Assert-MockCalled Get-Command - Times 1
286+ Assert-MockCalled Start-Process - Times 1
235287 }
236288
237- Context ' With Nonexistent KoanLocation' {
238- BeforeAll {
239- Mock Test-Path { $false }
240- Mock Update-PSKoan
241- Mock Get-Command { $false }
242- Mock Invoke-Item
243- Mock New-Item
244- }
289+ It ' opens the file directly when selected editor is unavailable' {
290+ Set-PSKoanSetting - Name Editor - Value " missing_editor"
291+
292+ Show-Karma - Contemplate | Should - BeExactly $TestFile.FullName
245293
246- It ' should create PSKoans directory' {
247- Show-Karma - Meditate
294+ Assert-MockCalled Get-Command - Times 1 - ParameterFilter { $Name -eq " missing_editor" }
295+ Assert-MockCalled Invoke-Item - Times 1
296+ }
297+ }
248298
249- Assert-MockCalled Test-Path - Times 1
250- Assert-MockCalled Update-PSKoan - Times 1
251- Assert-MockCalled Get-Command - Times 1
252- Assert-MockCalled New-Item - Times 1
253- Assert-MockCalled Invoke-Item - Times 1
299+ Context ' With -Library Switch' {
300+ BeforeAll {
301+ Mock Get-Command { $true } - ParameterFilter { $Name -ne " missing_editor" }
302+ Mock Get-Command { $false } - ParameterFilter { $Name -eq " missing_editor" }
303+ Mock Start-Process {
304+ @ { Editor = $FilePath ; Arguments = $ArgumentList }
254305 }
306+ Mock Invoke-Item { $Path }
307+ }
308+
309+ It ' invokes VS Code with "code" set as Editor with proper arguments' {
310+ Set-PSKoanSetting - Name Editor - Value ' code'
311+ $Result = Show-Karma - Library
312+
313+ $Result.Editor | Should - BeExactly ' code'
314+
315+ # Resolve-Path doesn't like embedded quotes
316+ $Path = $Result.Arguments -replace ' "'
317+ $Path | Should - BeExactly (Resolve-Path - Path $Path ).Path
318+
319+ Assert-MockCalled Get-Command - Times 1
320+ Assert-MockCalled Start-Process - Times 1
321+ }
322+
323+ It ' invokes the set editor with unknown editor chosen' {
324+ Set-PSKoanSetting - Name Editor - Value ' vim'
325+
326+ $Result = Show-Karma - Library
327+ $Result.Editor | Should - BeExactly ' vim'
328+
329+ # Resolve-Path doesn't like embedded quotes
330+ $Path = $Result.Arguments -replace ' "'
331+ $Path | Should - BeExactly (Resolve-Path - Path $Path ).Path
332+
333+ Assert-MockCalled Get-Command - Times 1
334+ Assert-MockCalled Start-Process - Times 1
335+ }
336+
337+ It ' opens the file directly when selected editor is unavailable' {
338+ Set-PSKoanSetting - Name Editor - Value " missing_editor"
339+
340+ Show-Karma - Library | Should - BeExactly (Get-PSKoanLocation )
341+
342+ Assert-MockCalled Get-Command - Times 1 - ParameterFilter { $Name -eq " missing_editor" }
343+ Assert-MockCalled Invoke-Item - Times 1
255344 }
256345 }
346+
257347 }
258348}
0 commit comments