github.com/aclaygray/packer@v1.3.2/common/powershell/powershell_test.go (about)

     1  package powershell
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  func TestOutput(t *testing.T) {
     9  
    10  	var ps PowerShellCmd
    11  
    12  	powershellAvailable, _, _ := IsPowershellAvailable()
    13  
    14  	if !powershellAvailable {
    15  		t.Skipf("powershell not installed")
    16  		return
    17  	}
    18  
    19  	cmdOut, err := ps.Output("")
    20  	if err != nil {
    21  		t.Fatalf("should not have error: %s", err)
    22  	}
    23  
    24  	if cmdOut != "" {
    25  		t.Fatalf("output '%v' is not ''", cmdOut)
    26  	}
    27  
    28  	trueOutput, err := ps.Output("$True")
    29  	if err != nil {
    30  		t.Fatalf("should not have error: %s", err)
    31  	}
    32  
    33  	if trueOutput != "True" {
    34  		t.Fatalf("output '%v' is not 'True'", trueOutput)
    35  	}
    36  
    37  	falseOutput, err := ps.Output("$False")
    38  	if err != nil {
    39  		t.Fatalf("should not have error: %s", err)
    40  	}
    41  
    42  	if falseOutput != "False" {
    43  		t.Fatalf("output '%v' is not 'False'", falseOutput)
    44  	}
    45  }
    46  
    47  func TestRunFile(t *testing.T) {
    48  	var ps PowerShellCmd
    49  
    50  	powershellAvailable, _, _ := IsPowershellAvailable()
    51  
    52  	if !powershellAvailable {
    53  		t.Skipf("powershell not installed")
    54  		return
    55  	}
    56  
    57  	var blockBuffer bytes.Buffer
    58  	blockBuffer.WriteString(`param([string]$a, [string]$b, [int]$x, [int]$y) if (Test-Path variable:global:ProgressPreference){$ProgressPreference="SilentlyContinue"}; $n = $x + $y; Write-Output "$a $b $n";`)
    59  
    60  	cmdOut, err := ps.Output(blockBuffer.String(), "a", "b", "5", "10")
    61  
    62  	if err != nil {
    63  		t.Fatalf("should not have error: %s", err)
    64  	}
    65  
    66  	if cmdOut != "a b 15" {
    67  		t.Fatalf("output '%v' is not 'a b 15'", cmdOut)
    68  	}
    69  }