-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExport-DevSetupEnv.Tests.ps1
More file actions
111 lines (105 loc) · 6.72 KB
/
Export-DevSetupEnv.Tests.ps1
File metadata and controls
111 lines (105 loc) · 6.72 KB
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
BeforeAll {
. (Join-Path $PSScriptRoot "Export-DevSetupEnv.ps1")
. (Join-Path $PSScriptRoot "..\..\..\DevSetup\Private\Utils\Get-DevSetupEnvPath.ps1")
. (Join-Path $PSScriptRoot "..\..\..\DevSetup\Private\Utils\Get-DevSetupLocalEnvPath.ps1")
. (Join-Path $PSScriptRoot "..\..\..\DevSetup\Private\Utils\Get-DevSetupCommunityEnvPath.ps1")
. (Join-Path $PSScriptRoot "..\..\..\DevSetup\Private\Utils\Write-NewConfig.ps1")
. (Join-Path $PSScriptRoot "..\..\..\DevSetup\Private\Utils\Write-StatusMessage.ps1")
if ($PSVersionTable.PSVersion.Major -eq 5) {
Mock Get-DevSetupEnvPath { "$TestDrive\DevSetup\DevSetupEnvs" }
Mock Get-DevSetupLocalEnvPath { "$TestDrive\DevSetup\DevSetupEnvs\local" }
Mock Get-DevSetupCommunityEnvPath { "$TestDrive\DevSetup\DevSetupEnvs\community" }
} elseif ($PSVersionTable.PSVersion.Major -ge 6) {
if ($IsWindows) {
Mock Get-DevSetupEnvPath { "$TestDrive\DevSetup\DevSetupEnvs" }
Mock Get-DevSetupLocalEnvPath { "$TestDrive\DevSetup\DevSetupEnvs\local" }
Mock Get-DevSetupCommunityEnvPath { "$TestDrive\DevSetup\DevSetupEnvs\community" }
}
if ($IsLinux) {
Mock Get-DevSetupEnvPath { "$TestDrive/home/testuser/DevSetup/DevSetupEnvs" }
Mock Get-DevSetupLocalEnvPath { "$TestDrive/home/testuser/DevSetup/DevSetupEnvs/local" }
Mock Get-DevSetupCommunityEnvPath { "$TestDrive/home/testuser/DevSetup/DevSetupEnvs/community" }
}
if ($IsMacOS) {
Mock Get-DevSetupEnvPath { "$TestDrive/Users/TestUser/DevSetup/DevSetupEnvs" }
Mock Get-DevSetupLocalEnvPath { "$TestDrive/Users/TestUser/DevSetup/DevSetupEnvs/local" }
Mock Get-DevSetupCommunityEnvPath { "$TestDrive/Users/TestUser/DevSetup/DevSetupEnvs/community" }
}
}
Mock Write-NewConfig { param($OutFile) $OutFile }
Mock Write-Host { }
Mock Write-Error { }
Mock Write-StatusMessage { }
}
Describe "Export-DevSetupEnv" {
Context "When called with a valid name" {
It "Should create the config file and return its path" {
$result = Export-DevSetupEnv -Name "MyEnv"
if ($PSVersionTable.PSVersion.Major -eq 5 -or ($PSVersionTable.PSVersion.Major -ge 6 -and $IsWindows)) {
$expectedPath = "$TestDrive\DevSetup\DevSetupEnvs\local\MyEnv.devsetup"
} elseif ($PSVersionTable.PSVersion.Major -ge 6 -and $IsLinux) {
$expectedPath = "$TestDrive/home/testuser/DevSetup/DevSetupEnvs/local/MyEnv.devsetup"
} elseif ($PSVersionTable.PSVersion.Major -ge 6 -and $IsMacOS) {
$expectedPath = "$TestDrive/Users/TestUser/DevSetup/DevSetupEnvs/local/MyEnv.devsetup"
}
$result | Should -Be $expectedPath
Assert-MockCalled Write-NewConfig -Exactly 1 -Scope It -ParameterFilter { $OutFile -eq $expectedPath }
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter { $Message -match "exported to" -and $ForegroundColor -eq "Green" }
}
}
Context "When called with a valid path" {
It "Should create the config file and return its path" {
if ($PSVersionTable.PSVersion.Major -eq 5 -or ($PSVersionTable.PSVersion.Major -ge 6 -and $IsWindows)) {
$result = Export-DevSetupEnv -Path "$TestDrive\MyCustomPath\MyEnv.devsetup"
$expectedPath = "$TestDrive\MyCustomPath\MyEnv.devsetup"
} elseif ($PSVersionTable.PSVersion.Major -ge 6 -and $IsLinux) {
$result = Export-DevSetupEnv -Path "$TestDrive/MyCustomPath/MyEnv.devsetup"
$expectedPath = "$TestDrive/MyCustomPath/MyEnv.devsetup"
} elseif ($PSVersionTable.PSVersion.Major -ge 6 -and $IsMacOS) {
$result = Export-DevSetupEnv -Path "$TestDrive/MyCustomPath/MyEnv.devsetup"
$expectedPath = "$TestDrive/MyCustomPath/MyEnv.devsetup"
}
$result | Should -Be $expectedPath
Assert-MockCalled Write-NewConfig -Exactly 1 -Scope It -ParameterFilter { $OutFile -eq $expectedPath }
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter { $Message -match "exported to" -and $ForegroundColor -eq "Green" }
}
}
Context "When called with a name that needs sanitization" {
It "Should sanitize the name and warn" {
$result = Export-DevSetupEnv -Name "Data Science Environment!"
if ($PSVersionTable.PSVersion.Major -eq 5 -or ($PSVersionTable.PSVersion.Major -ge 6 -and $IsWindows)) {
$expectedPath = "$TestDrive\DevSetup\DevSetupEnvs\local\DataScienceEnvironment.devsetup"
} elseif ($PSVersionTable.PSVersion.Major -ge 6 -and $IsLinux) {
$expectedPath = "$TestDrive/home/testuser/DevSetup/DevSetupEnvs/local/DataScienceEnvironment.devsetup"
} elseif ($PSVersionTable.PSVersion.Major -ge 6 -and $IsMacOS) {
$expectedPath = "$TestDrive/Users/TestUser/DevSetup/DevSetupEnvs/local/DataScienceEnvironment.devsetup"
}
$result | Should -Be $expectedPath
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter { $Message -match "sanitized" -and $ForegroundColor -eq "Yellow" }
}
}
Context "When called with a path that needs sanitization" {
It "Should sanitize the path and warn" {
if ($PSVersionTable.PSVersion.Major -eq 5 -or ($PSVersionTable.PSVersion.Major -ge 6 -and $IsWindows)) {
$result = Export-DevSetupEnv -Path "$TestDrive\MyCustomPath\MyEnv!.devsetup"
$expectedPath = "$TestDrive\MyCustomPath\MyEnv.devsetup"
} elseif ($PSVersionTable.PSVersion.Major -ge 6 -and $IsLinux) {
$result = Export-DevSetupEnv -Path "$TestDrive/MyCustomPath/MyEnv!.devsetup"
$expectedPath = "$TestDrive/MyCustomPath/MyEnv.devsetup"
} elseif ($PSVersionTable.PSVersion.Major -ge 6 -and $IsMacOS) {
$result = Export-DevSetupEnv -Path "$TestDrive/MyCustomPath/MyEnv!.devsetup"
$expectedPath = "$TestDrive/MyCustomPath/MyEnv.devsetup"
}
$result | Should -Be $expectedPath
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter { $Message -match "sanitized" -and $ForegroundColor -eq "Yellow" }
}
}
Context "When Write-NewConfig fails" {
It "Should write error and return null" {
Mock Write-NewConfig { param($OutFile) $null }
$result = Export-DevSetupEnv -Name "FailEnv"
$result | Should -Be $null
Assert-MockCalled Write-StatusMessage -Exactly 1 -Scope It -ParameterFilter { $Message -match "Failed to create configuration file" -and $Verbosity -eq "Error" }
}
}
}