From 1000e1c38c77bec93a7ea3e7f6c23f0c63d38ee2 Mon Sep 17 00:00:00 2001 From: WhiteTomX <38078578+WhiteTomX@users.noreply.github.com> Date: Mon, 2 May 2022 22:14:50 +0200 Subject: [PATCH] Fix global Terraform (#19) * add test for path reload * add test for fallback * fallback to terraform from path * reload Path * add changelog * upgrade module version --- CHANGELOG.md | 4 ++++ WTerraform/Public/Invoke-WTerraform.ps1 | 23 +++++++++++++-------- WTerraform/Public/Set-WTerraformVersion.ps1 | 1 + WTerraform/WTerraform.psd1 | 2 +- tests/Invoke-WTerraform.Tests.ps1 | 19 ++++++++++++++++- tests/Set-WTerraformVersion.Tests.ps1 | 3 +++ 6 files changed, 41 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f594c89..d6f105f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.1.1] + +Fix global terraform version by fallback to `terraform.exe` from path and reload path. + ## [0.1.0] Added option to set a global terraform version by adding to user's path. This should work with external applications like VSCode. diff --git a/WTerraform/Public/Invoke-WTerraform.ps1 b/WTerraform/Public/Invoke-WTerraform.ps1 index 7fa7bfe..a239607 100644 --- a/WTerraform/Public/Invoke-WTerraform.ps1 +++ b/WTerraform/Public/Invoke-WTerraform.ps1 @@ -9,17 +9,22 @@ Invoke-WTerraform -version Throws error if no Terraform Version was specified earlier. Otherwise runs command 'terraform -version' #> function Invoke-WTerraform { - $version = Get-WTerraformVersion - if ($version) { - $path = Join-Path -Path $env:LOCALAPPDATA -ChildPath "WTerraform" | Join-Path -ChildPath $version | Join-Path -ChildPath "terraform.exe" - if (Test-Path -LiteralPath $path) { - & $path $args - } else { - #TODO: Redownload terraform - throw "$version is not found" + $pwdVersion = Get-WTerraformVersion + $wTerraformPath = Join-Path -Path $env:LOCALAPPDATA -ChildPath "WTerraform" + if ($pwdVersion) { + $path = Join-Path -Path $wTerraformPath -ChildPath $pwdVersion | Join-Path -ChildPath "terraform.exe" + } elseif ($terraformCommand = Get-Command -Name "terraform.exe" -CommandType Application -ErrorAction SilentlyContinue) { + $path = $terraformCommand.Source + if (-not $path.StartsWith($wTerraformPath)) { + Write-Warning -Message "Using terraform in $path. This is not installed by WTerraform!" } - } else { throw "No Version for $pwd specified. Please Run Set-WTerraformVersion." } + if (Test-Path -LiteralPath $path) { + & $path $args + } else { + #TODO: Redownload terraform + throw "$pwdVersion is not found" + } } diff --git a/WTerraform/Public/Set-WTerraformVersion.ps1 b/WTerraform/Public/Set-WTerraformVersion.ps1 index c91cd05..8efbf9f 100644 --- a/WTerraform/Public/Set-WTerraformVersion.ps1 +++ b/WTerraform/Public/Set-WTerraformVersion.ps1 @@ -44,6 +44,7 @@ function Set-WTerraformVersion { } $path = $path + ";" + $versionPath [System.Environment]::SetEnvironmentVariable("Path", $path, [System.EnvironmentVariableTarget]::User) + $env:Path = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine) + ";"+ [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User) } else { $versionMap = Get-WTerraformVersionMap $oldVersion = Get-WTerraformversion diff --git a/WTerraform/WTerraform.psd1 b/WTerraform/WTerraform.psd1 index e0f9c85..12ebbe7 100644 --- a/WTerraform/WTerraform.psd1 +++ b/WTerraform/WTerraform.psd1 @@ -11,7 +11,7 @@ RootModule = 'WTerraform.psm1' # Version number of this module. - ModuleVersion = '0.1.0' + ModuleVersion = '0.1.1' # Supported PSEditions #CompatiblePSEditions = @("Desktop") diff --git a/tests/Invoke-WTerraform.Tests.ps1 b/tests/Invoke-WTerraform.Tests.ps1 index 56ac132..badf016 100644 --- a/tests/Invoke-WTerraform.Tests.ps1 +++ b/tests/Invoke-WTerraform.Tests.ps1 @@ -23,7 +23,7 @@ Describe "Invoke-WTerraform" { Move-Item -LiteralPath $tempcachePath -Destination $cachePath } } - Context "Configured Folders" { + Context "Version for current Folder configured" { BeforeAll { $path = "TestDrive:\1" New-Item -Path $path -ItemType Directory @@ -49,4 +49,21 @@ Describe "Invoke-WTerraform" { Pop-Location -ErrorAction SilentlyContinue } } + + Context "Global Version configured" { + BeforeAll { + Set-WTerraformVersion -Version "0.14.0" -Global + $path = "TestDrive:\global" + New-Item -Path $path -ItemType Directory + Push-Location -Path $path + } + It "Should fallback to global version" { + $actualVersion = Invoke-WTerraform -version + $actualVersion[0] | Should -Be "Terraform v0.14.0" + } + AfterAll { + Pop-Location + Pop-Location -ErrorAction SilentlyContinue + } + } } diff --git a/tests/Set-WTerraformVersion.Tests.ps1 b/tests/Set-WTerraformVersion.Tests.ps1 index 9b6e78c..1826e9b 100644 --- a/tests/Set-WTerraformVersion.Tests.ps1 +++ b/tests/Set-WTerraformVersion.Tests.ps1 @@ -87,5 +87,8 @@ Describe "Set-WTerraformVersion global" { It "Should remove old Version Folder from Path" { [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User) | Should -Not -BeLike "*$($versionPath)*" } + It "Should reload Path" { + $env:Path | Should -BeLike "*$([System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User))" + } } }