Public/Invoke-Terrafun.Tests.ps1

Import-Module -Force "./Terrafun/Terrafun.psd1"

BeforeAll {
}

Describe 'Invoke-Terrafun' -Tag "Acceptance" {

    InModuleScope "Terrafun" {

        BeforeEach {
            Push-Location
            Set-Location -Path "TestDrive:\"
        }

        AfterEach {
            Pop-Location
        }

        It 'Defaults to the latest version' {
            $UserProfile = (Get-Item ~).FullName
            $ConfigPath = Join-Path -path ~ -ChildPath ".terrafun/config.json"
            Remove-item $ConfigPath -ErrorAction SilentlyContinue -Force
            $Output = Invoke-Terrafun version
            $Output | Should -Not -BeLike "*Your version of Terraform is out of date !*"
        }

        It 'Lists the available versions' {
            $UserProfile = (Get-Item ~).FullName
            $ConfigPath = Join-Path -path ~ -ChildPath ".terrafun/config.json"
            Remove-item $ConfigPath -ErrorAction SilentlyContinue -Force
            $Output = Invoke-Terrafun version list
            $Output.count | Should -BeGreaterThan 100
        }

        It 'Sets the correct version inside the current directory' {
            Invoke-Terrafun version 1.0.0
            Invoke-Terrafun version global 1.0.1
            $Content = Get-Content ".terraformversion"
            $Output = Invoke-Terrafun version | Out-String

            $Content | Should -Be "1.0.0"
            $Output | Should -BeLike "*Terraform v1.0.0*"
        }

        It 'Sets the correct version in the user profile' {
            Remove-Item ".terraformversion" -Force -ErrorAction SilentlyContinue
            Invoke-Terrafun version global 1.0.1
            $Output = Invoke-Terrafun version | Out-String

            $Output | Should -BeLike "*Terraform v1.0.1*"
        }

        It 'Uses the version set in the current directory when a user profile is present' {
            Invoke-Terrafun version 1.0.0
            Invoke-Terrafun version global 1.0.1
            $Content = Get-Content ".terraformversion"
            $Output = Invoke-Terrafun version | Out-String

            $Content | Should -Be "1.0.0"
            $Output | Should -BeLike "*Terraform v1.0.0*"
        }


        It 'Executes the "terraform version" command' {
            Invoke-Terrafun version 1.0.0
            $Output = Invoke-Terrafun version | Out-String

            $Output | Should -BeLike "*Terraform v1.0.0*"
        }

        It 'Successfully executes the "terraform init" command' {
            $Content = @"
        resource "local_file" "init" {
            content = "init_result"
            filename = "init_test"
        }
"@

            mkdir "init"
            Set-Location "init"
            Set-Content -Path "main.tf" -Value $Content
            $Output = Invoke-Terrafun init -no-color | Out-String

            $Output | Should -BeLike "*Terraform has been successfully initialized!*"
        }

        It 'Successfully executes the "terraform plan" command' {
            $Content = @"
        resource "local_file" "plan" {
            content = "plan_result"
            filename = "plan_test"
        }
"@

            mkdir "plan"
            Set-Location "plan"
            Set-Content -Path "main.tf" -Value $Content
            Invoke-Terrafun init 
            $Output = Invoke-Terrafun plan -no-color | Out-String

            $Output | Should -BeLike "*Plan: 1 to add, 0 to change, 0 to destroy*"
        }

        It 'Successfully executes the "terraform apply" command' {
            $Content = @"
        resource "local_file" "apply" {
            content = "apply_result"
            filename = "apply_test"
        }
"@

            mkdir "apply"
            Set-Location "apply"
            Set-Content -Path "main.tf" -Value $Content
            Invoke-Terrafun init 
            $Output = Invoke-Terrafun apply --auto-approve -no-color | Out-String
            $Content = Get-Content -path "apply_test"

            $Content | Should -Be "apply_result"
            $Output | Should -BeLike "*Apply complete! Resources: 1 added, 0 changed, 0 destroyed*"
        }

    }


}