Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
485 changes: 485 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion DevSetup/DevSetup.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'DevSetup.psm1'

# Version number of this module.
ModuleVersion = '1.0.7'
ModuleVersion = '1.0.8'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
22 changes: 21 additions & 1 deletion DevSetup/Private/Commands/Export-DevSetupEnv.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
BeforeAll {
. $PSScriptRoot\Export-DevSetupEnv.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Get-DevSetupEnvPath.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Get-DevSetupLocalEnvPath.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Get-DevSetupCommunityEnvPath.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Write-NewConfig.ps1
Mock Get-DevSetupEnvPath { "TestDrive:\DevSetupEnvs" }
Mock Get-DevSetupLocalEnvPath { "TestDrive:\DevSetupEnvs\local"}
Mock Get-DevSetupCommunityEnvPath { "TestDrive:\DevSetupEnvs\community"}
Mock Write-NewConfig { param($OutFile) $OutFile }
Mock Write-Host { }
Mock Write-Error { }
Expand All @@ -20,6 +23,15 @@ Describe "Export-DevSetupEnv" {
}
}

Context "When called with a valid path" {
It "Should create the config file and return its path" {
$result = Export-DevSetupEnv -Path "TestDrive:\MyCustomPath\MyEnv.devsetup"
$result | Should -Be "TestDrive:\MyCustomPath\MyEnv.devsetup"
Assert-MockCalled Write-NewConfig -Exactly 1 -Scope It -ParameterFilter { $OutFile -eq "TestDrive:\MyCustomPath\MyEnv.devsetup" }
Assert-MockCalled Write-Host -Scope It -ParameterFilter { $Object -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!"
Expand All @@ -28,6 +40,14 @@ Describe "Export-DevSetupEnv" {
}
}

Context "When called with a path that needs sanitization" {
It "Should sanitize the path and warn" {
$result = Export-DevSetupEnv -Path "TestDrive:\MyCustomPath\MyEnv!.devsetup"
$result | Should -Be "TestDrive:\MyCustomPath\MyEnv.devsetup"
Assert-MockCalled Write-Host -Scope It -ParameterFilter { $Object -match "sanitized" -and $ForegroundColor -eq "Yellow" }
}
}

Context "When Write-NewConfig fails" {
It "Should write error and return null" {
Mock Write-NewConfig { param($OutFile) $null }
Expand All @@ -36,4 +56,4 @@ Describe "Export-DevSetupEnv" {
Assert-MockCalled Write-Error -Exactly 1 -Scope It -ParameterFilter { $Message -match "Failed to create configuration file" }
}
}
}
}
54 changes: 47 additions & 7 deletions DevSetup/Private/Commands/Export-DevSetupEnv.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,56 @@
#>

Function Export-DevSetupEnv {
[CmdletBinding()]
Param(
[string]$Name
[Parameter(Mandatory=$true, ParameterSetName="Export")]
[string]$Name,
[Parameter(Mandatory=$true, ParameterSetName="ExportPath")]
[string]$Path
)
# Sanitize EnvName to only contain alphanumeric characters, hyphens, and periods
$sanitizedEnvName = $Name -replace '[^a-zA-Z0-9\-\.]', ''
if ($sanitizedEnvName -ne $Name) {
Write-Host "EnvName sanitized from '$Name' to '$sanitizedEnvName' (removed non-alphanumeric characters)" -ForegroundColor Yellow
$OutFile = $null
if($PSBoundParameters.ContainsKey('Name')) {
$Provider = "local"

if($Name -like "*:*") {
$Parts = $Name.Split(":")
$Name = $Parts[1];
$Provider = $Parts[0]
}
$SanitizedEnvName = $Name -replace '[^a-zA-Z0-9\-\.]', ''
if ($SanitizedEnvName -ne $Name) {
Write-Host "EnvName sanitized from '$Name' to '$SanitizedEnvName' (removed non-alphanumeric characters)" -ForegroundColor Yellow
}

$BasePath = Join-Path -Path (Get-DevSetupEnvPath) -ChildPath $Provider
if(-not (Test-Path -Path $BasePath)) {
New-Item -Path $BasePath -ItemType Directory -Force | Out-Null
}

$OutFile = Join-Path -Path $BasePath -ChildPath "$SanitizedEnvName.devsetup"
} elseif($PSBoundParameters.ContainsKey('Path')) {
$BasePath = Split-Path -Path $Path -Parent
if(-not (Test-Path -Path $BasePath)) {
New-Item -Path $BasePath -ItemType Directory -Force | Out-Null
}
$Name = Split-Path -Path $Path -Leaf
if($Name -notlike "*.devsetup") {
$Name = "$Name.devsetup"
}

$SanitizedEnvName = $Name -replace '[^a-zA-Z0-9\-\.]', ''

if ($SanitizedEnvName -ne $Name) {
Write-Host "EnvName sanitized from '$Name' to '$SanitizedEnvName' (removed non-alphanumeric characters)" -ForegroundColor Yellow
}
$OutFile = Join-Path -Path $BasePath -ChildPath $SanitizedEnvName
}
$Name = $sanitizedEnvName
$OutFile = Join-Path -Path (Get-DevSetupLocalEnvPath) -ChildPath "$Name.devsetup"

if(-not $OutFile) {
Write-Error "Failed to determine output file path"
return $null
}

$config = Write-NewConfig -OutFile $OutFile
if (-not $config) {
Write-Error "Failed to create configuration file"
Expand Down
3 changes: 3 additions & 0 deletions DevSetup/Private/Commands/Initialize-DevSetup.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
BeforeAll {
. $PSScriptRoot\Initialize-DevSetup.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Get-DevSetupPath.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Get-DevSetupEnvPath.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Get-DevSetupLocalEnvPath.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Get-DevSetupCommunityEnvPath.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Providers\Core\Install-CoreDependencies.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Initialize-DevSetupEnvs.ps1
Mock Write-Host { }
Expand Down
102 changes: 87 additions & 15 deletions DevSetup/Private/Commands/Show-DevSetupEnvList.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ BeforeAll {
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Get-DevSetupPath.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Test-OperatingSystem.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Format-PrettyTable.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Write-StatusMessage.ps1
Mock Get-DevSetupPath { "C:\DevSetup" }
Mock Optimize-DevSetupEnvs { }
Mock Write-Host { }
Expand All @@ -15,13 +16,14 @@ BeforeAll {
Mock Get-Content { '[{"name":"EnvWin","version":"1.0","platform":"windows","file":"envwin.yaml"},{"name":"EnvLinux","version":"2.0","platform":"linux","file":"envlinux.yaml"},{"name":"EnvMac","version":"3.0","platform":"macos","file":"envmac.yaml"},{"name":"EnvCross","version":"4.0","platform":"cross-platform","file":"envcross.yaml"},{"name":"EnvUnspec","version":"5.0","file":"envunspec.yaml"}]' }
Mock ConvertFrom-Json {
@(
@{ name = "EnvWin"; version = "1.0"; platform = "windows"; file = "envwin.yaml" },
@{ name = "EnvLinux"; version = "2.0"; platform = "linux"; file = "envlinux.yaml" },
@{ name = "EnvMac"; version = "3.0"; platform = "macos"; file = "envmac.yaml" },
@{ name = "EnvCross"; version = "4.0"; platform = "cross-platform"; file = "envcross.yaml" },
@{ name = "EnvUnspec"; version = "5.0"; file = "envunspec.yaml" }
@{ name = "EnvWin"; version = "1.0"; platform = "windows"; provider = "local"; file = "envwin.yaml" },
@{ name = "EnvLinux"; version = "2.0"; platform = "linux"; provider = "community"; file = "envlinux.yaml" },
@{ name = "EnvMac"; version = "3.0"; platform = "macos"; provider = "other"; file = "envmac.yaml" },
@{ name = "EnvCross"; version = "4.0"; platform = "cross-platform"; provider = "local"; file = "envcross.yaml" },
@{ name = "EnvUnspec"; version = "5.0"; provider = "other2"; file = "envunspec.yaml" }
)
}
Mock Write-StatusMessage { Param($Message) }
}

Describe "Show-DevSetupEnvList" {
Expand Down Expand Up @@ -49,8 +51,8 @@ Describe "Show-DevSetupEnvList" {
Mock Format-PrettyTable { }
Mock Test-OperatingSystem { param($Windows, $Linux, $MacOS) if ($Windows) { $true } else { $false } }
Show-DevSetupEnvList -Platform "current"
Assert-MockCalled Write-Host -Scope It -ParameterFilter { $Object -match "Filtering for current platform: windows" }
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter { $Message -match "Filtering for platform: windows" }
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It -ParameterFilter { $Rows.Count -eq 1 }
}
}

Expand All @@ -59,8 +61,8 @@ Describe "Show-DevSetupEnvList" {
Mock Format-PrettyTable { }
Mock Test-OperatingSystem { param($Windows, $Linux, $MacOS) if ($Linux) { $true } else { $false } }
Show-DevSetupEnvList -Platform "current"
Assert-MockCalled Write-Host -Scope It -ParameterFilter { $Object -match "Filtering for current platform: linux" }
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter { $Message -match "Filtering for platform: linux" }
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It -ParameterFilter { $Rows.Count -eq 1 }
}
}

Expand All @@ -69,28 +71,98 @@ Describe "Show-DevSetupEnvList" {
Mock Format-PrettyTable { }
Mock Test-OperatingSystem { param($Windows, $Linux, $MacOS) if ($MacOS) { $true } else { $false } }
Show-DevSetupEnvList -Platform "current"
Assert-MockCalled Write-Host -Scope It -ParameterFilter { $Object -match "Filtering for current platform: macos" }
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter { $Message -match "Filtering for platform: macos" }
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It -ParameterFilter { $Rows.Count -eq 1 }
}
}

Context "When filtering for all platforms" {
It "Should show all environments without filtering" {
Mock Format-PrettyTable { }
Show-DevSetupEnvList -Platform "all"
Assert-MockCalled Write-Host -Scope It -ParameterFilter { $Object -match "Showing all environments regardless of platform" }
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter { $Message -match "Filtering for platform: all" }
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It -ParameterFilter { $Rows.Count -eq 5 }
}
}

Context "When filtering for specific platform (windows)" {
It "Should filter and display only windows-compatible environments" {
Mock Format-PrettyTable { }
Show-DevSetupEnvList -Platform "windows"
Assert-MockCalled Write-Host -Scope It -ParameterFilter { $Object -match "Filtering for platform: windows" }
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter { $Message -match "Filtering for platform: windows" }
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It -ParameterFilter { $Rows.Count -eq 1 }
}
}

Context "When filtering for specific platform (windows) and provider (local)" {
It "Should filter and display only windows-compatible environments from local provider" {
Mock Format-PrettyTable { }
Show-DevSetupEnvList -Platform "windows" -Provider "local"
$script:count = 0;
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter {
if($script:count -eq 0) { $Message -match "Filtering for platform: windows" }
if($script:count -eq 1) { $Message -match ", provider: local" }
$script:count++;
} -Times 2
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It -ParameterFilter { $Rows.Count -eq 1 }
}
}

Context "When filtering for specific platform (windows) and provider (community)" {
It "Should filter and display only windows-compatible environments from community provider" {
Mock Format-PrettyTable { }
Show-DevSetupEnvList -Platform "windows" -Provider "community"
$script:count = 0;
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter {
if($script:count -eq 0) { $Message -match "Filtering for platform: windows" }
if($script:count -eq 1) { $Message -match ", provider: community" }
$script:count++;
} -Times 2
Assert-MockCalled Format-PrettyTable -Exactly 0 -Scope It -ParameterFilter { $Rows.Count -eq 0 }
}
}

Context "When filtering for specific platform (linux) and provider (community)" {
It "Should filter and display only linux-compatible environments from community provider" {
Mock Format-PrettyTable { }
Show-DevSetupEnvList -Platform "linux" -Provider "community"
$script:count = 0;
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter {
if($script:count -eq 0) { $Message -match "Filtering for platform: linux" }
if($script:count -eq 1) { $Message -match ", provider: community" }
$script:count++;
} -Times 2
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It -ParameterFilter { $Rows.Count -eq 1 }
}
}

Context "When filtering for specific platform (macos) and provider (other)" {
It "Should filter and display only macos-compatible environments from other provider" {
Mock Format-PrettyTable { }
Show-DevSetupEnvList -Platform "macos" -Provider "other"
$script:count = 0;
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter {
if($script:count -eq 0) { $Message -match "Filtering for platform: macos" }
if($script:count -eq 1) { $Message -match ", provider: other" }
$script:count++;
} -Times 2
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It -ParameterFilter { $Rows.Count -eq 1 }
}
}

Context "When filtering for specific platform (all) and provider (local)" {
It "Should filter and display all environment from local provider" {
Mock Format-PrettyTable { }
Show-DevSetupEnvList -Platform "all" -Provider "local"
$script:count = 0;
Assert-MockCalled Write-StatusMessage -Scope It -ParameterFilter {
if($script:count -eq 0) { $Message -match "Filtering for platform: all" }
if($script:count -eq 1) { $Message -match ", provider: local" }
$script:count++;
} -Times 2
Assert-MockCalled Format-PrettyTable -Exactly 1 -Scope It -ParameterFilter { $Rows.Count -eq 2 }
}
}

Context "When no environments are found for a platform" {
It "Should display guidance message" {
Expand Down
38 changes: 23 additions & 15 deletions DevSetup/Private/Commands/Show-DevSetupEnvList.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ Function Show-DevSetupEnvList {
Param (
[Parameter(Mandatory=$false, Position=0)]
[ValidateSet("current", "all", "windows", "linux", "macos")]
[string]$Platform = "current" # Default to current platform
[string]$Platform = "current", # Default to current platform
[Parameter(Mandatory=$false)]
[string]$Provider,
[Parameter(Mandatory=$false)]
[switch]$Installed
)


Expand All @@ -88,13 +92,18 @@ Function Show-DevSetupEnvList {
} else {
$platformFilter = "windows"
}
Write-Host "Filtering for current platform: $platformFilter" -ForegroundColor Gray
} elseif ($platformFilter -eq "all") {
Write-Host "Showing all environments regardless of platform" -ForegroundColor Gray
} else {
Write-Host "Filtering for platform: $platformFilter" -ForegroundColor Gray
}
Write-StatusMessage "Filtering for platform: $platformFilter" -ForegroundColor Gray -NoNewLine

if($Provider) {
Write-StatusMessage ", provider: $Provider" -ForegroundColor Gray -NoNewLine
}

if($Installed) {
Write-StatusMessage ", installed only" -ForegroundColor Gray -NoNewLine
}
Write-Host ""

# Get the environments.json file path
$devSetupPath = Get-DevSetupPath
$environmentsJsonPath = Join-Path -Path $devSetupPath -ChildPath "environments.json"
Expand All @@ -114,17 +123,16 @@ Function Show-DevSetupEnvList {
}
}

if ($Provider) {
$environments = $environments | Where-Object { $_.provider -and ($_.provider.ToLower() -eq $Provider.ToLower()) }
}
#if ($Installed) {
# $environments = $environments | Where-Object { Test-DevSetupEnvInstalled -Name $_.name -Provider $_.provider }
#}

# Filter environments by platform
if ($platformFilter -ne "all") {
$filteredEnvironments = @()
foreach ($env in $environments) {
$envPlatform = if ($env.platform) { $env.platform.ToLower() } else { "" }
# Match exact platform or cross-platform environments
if ($envPlatform -eq $platformFilter) {
$filteredEnvironments += $env
}
}
$environments = $filteredEnvironments
$environments = $environments | Where-Object { ($_.platform -and ($_.platform.ToLower() -eq $platformFilter)) }
}

if ($environments.Count -eq 0) {
Expand Down
2 changes: 2 additions & 0 deletions DevSetup/Private/Commands/Uninstall-DevSetupEnv.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ BeforeAll {
. $PSScriptRoot\Uninstall-DevSetupEnv.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Read-ConfigurationFile.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Get-DevSetupEnvPath.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Utils\Test-OperatingSystem.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Providers\Scoop\Uninstall-ScoopComponents.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Providers\Chocolatey\Uninstall-ChocolateyPackages.ps1
. $PSScriptRoot\..\..\..\DevSetup\Private\Providers\PowerShell\Uninstall-PowershellModules.ps1
Expand All @@ -11,6 +12,7 @@ BeforeAll {
Mock Uninstall-PowershellModules { $true }
Mock Uninstall-ChocolateyPackages { $true }
Mock Uninstall-ScoopComponents { $true }
Mock Test-OperatingSystem { $true }
Mock Write-Host { }
Mock Write-Error { }
}
Expand Down
Loading