From 5c81dd4de97443d4c8334fd43faaa5e9215b32ec Mon Sep 17 00:00:00 2001 From: David Walker Date: Tue, 7 Dec 2021 23:15:40 -0600 Subject: [PATCH] add .env and .env.user file support and tests --- set-env/Set-Env.ps1 | 37 ++++++++++++++++++++++++++ tests/.env | 2 ++ tests/.env.user | 2 ++ tests/Set-Env.Tests.ps1 | 58 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 tests/.env create mode 100644 tests/.env.user create mode 100644 tests/Set-Env.Tests.ps1 diff --git a/set-env/Set-Env.ps1 b/set-env/Set-Env.ps1 index 779ea87..feacc48 100644 --- a/set-env/Set-Env.ps1 +++ b/set-env/Set-Env.ps1 @@ -67,6 +67,41 @@ function Set-Env { $combined = @($varsToSet) + @($varsToUnset) try { + + $currLocation = "$(Get-Location)" + @((Split-Path $profile -Parent),$PSScriptRoot,($currLocation -ne $PSScriptRoot ? $currLocation : ''),$Set).foreach({ + try { + $p = $_ + if ($p) { + #Write-Verbose "checking:$p\*.env*" + if (Test-Path $p\*.env*) { + Get-ChildItem –Path $p\*.env* | Foreach-Object { + try { + $f = $_ + Write-Verbose "checking:$($f.FullName)" + $content = (Get-Content $f.FullName) + $content | ForEach-Object { + if (-not ($_ -like '#*') -and ($_ -like '*=*')) { + $sp = $_.Split('=') + Write-Verbose "Set-Env $($sp[0])=$($sp[1])" + [System.Environment]::SetEnvironmentVariable($sp[0], $sp[1]) + } + } + } + catch { + Write-Error "ERROR Set-Env $p-$f" -InformationVariable results + } + } + } else { + #Write-Verbose "skipped:$p no *.env* files found" + } + } + } + catch { + Write-Error "ERROR Set-Env $p" -InformationVariable results + } + }) + $combined | % { Set-Item "env:$($_.var)" $_.val } @@ -78,4 +113,6 @@ function Set-Env { Set-Item "env:$($_.var)" $_.original } } + + Write-Verbose "Set-Env:end" } \ No newline at end of file diff --git a/tests/.env b/tests/.env new file mode 100644 index 0000000..c84333f --- /dev/null +++ b/tests/.env @@ -0,0 +1,2 @@ +#comment +set-env-smoke-test-env-set=test \ No newline at end of file diff --git a/tests/.env.user b/tests/.env.user new file mode 100644 index 0000000..0cd6cab --- /dev/null +++ b/tests/.env.user @@ -0,0 +1,2 @@ +#comment +set-env-smoke-test-env-user-set=test \ No newline at end of file diff --git a/tests/Set-Env.Tests.ps1 b/tests/Set-Env.Tests.ps1 new file mode 100644 index 0000000..043a879 --- /dev/null +++ b/tests/Set-Env.Tests.ps1 @@ -0,0 +1,58 @@ +Describe 'Module Tests' { + #$repoPath = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent + #$ModuleName = Split-Path $repoPath -Leaf + #$ModuleScriptName = "$ModuleName.psm1" + #$ModuleScriptPath = "$repoPath\src\$ModuleName\$ModuleScriptName" + + It 'imports successfully' { + $repoPath = Split-Path $PSScriptRoot -Parent + $ModuleName = Split-Path $repoPath -Leaf + #$ModuleScriptPath = "$repoPath\src\$ModuleName\$ModuleName.psm1" + $ModuleScriptPath = "$repoPath\$ModuleName\$ModuleName.psm1" + + Write-Verbose "Import-Module -Name $($ModuleScriptPath)" + { Import-Module -Name $ModuleScriptPath -ErrorAction Stop } | Should -Not -Throw + } + + It 'passes default PSScriptAnalyzer rules' { + $repoPath = Split-Path $PSScriptRoot -Parent + $ModuleName = Split-Path $repoPath -Leaf + $ModuleScriptPath = "$repoPath\$ModuleName\$ModuleName.psm1" + + #fairly certain, this is newly required, used to already be in: + #Install-Module PSScriptAnalyzer -Scope CurrentUser + Invoke-ScriptAnalyzer -Path $ModuleScriptPath | Should -BeNullOrEmpty + } +} + +Describe 'Module Manifest Tests' { + It 'passes Test-ModuleManifest' { + $repoPath = Split-Path $PSScriptRoot -Parent + $ModuleName = Split-Path $repoPath -Leaf + $ModuleManifestName = "$ModuleName.psd1" + $ModuleManifestPath = "$repoPath\$ModuleName\$ModuleManifestName" + + Write-Output $ModuleManifestPath + Test-ModuleManifest -Path $ModuleManifestPath | Should -Not -BeNullOrEmpty + $? | Should -Be $true + } +} + +Describe 'Smoke Tests' { + It 'passes empty params' { + $results = Set-Env -Verbose | Should -Not -BeNullOrEmpty + Write-Verbose "results:$results" + } + It 'passes env set' { + [System.Environment]::SetEnvironmentVariable("set-env-smoke-test-env-set","x") + Set-Env -Verbose | Should -Not -BeNullOrEmpty + $results = (Get-Item 'env:set-env-smoke-test-env-set').value + $results | Should -Be 'test' + } + It 'passes env-user set' { + [System.Environment]::SetEnvironmentVariable("set-env-smoke-test-env-user-set","x") + Set-Env -Verbose | Should -Not -BeNullOrEmpty + $results = (Get-Item 'env:set-env-smoke-test-env-user-set').value + $results | Should -Be 'test' + } +} \ No newline at end of file