File: powershell_test.go

package info (click to toggle)
packer 1.6.6%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 32,016 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (69 lines) | stat: -rw-r--r-- 1,430 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package powershell

import (
	"bytes"
	"testing"
)

func TestOutput(t *testing.T) {

	var ps PowerShellCmd

	powershellAvailable, _, _ := IsPowershellAvailable()

	if !powershellAvailable {
		t.Skipf("powershell not installed")
		return
	}

	cmdOut, err := ps.Output("")
	if err != nil {
		t.Fatalf("should not have error: %s", err)
	}

	if cmdOut != "" {
		t.Fatalf("output '%v' is not ''", cmdOut)
	}

	trueOutput, err := ps.Output("$True")
	if err != nil {
		t.Fatalf("should not have error: %s", err)
	}

	if trueOutput != "True" {
		t.Fatalf("output '%v' is not 'True'", trueOutput)
	}

	falseOutput, err := ps.Output("$False")
	if err != nil {
		t.Fatalf("should not have error: %s", err)
	}

	if falseOutput != "False" {
		t.Fatalf("output '%v' is not 'False'", falseOutput)
	}
}

func TestRunFile(t *testing.T) {
	var ps PowerShellCmd

	powershellAvailable, _, _ := IsPowershellAvailable()

	if !powershellAvailable {
		t.Skipf("powershell not installed")
		return
	}

	var blockBuffer bytes.Buffer
	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";`)

	cmdOut, err := ps.Output(blockBuffer.String(), "a", "b", "5", "10")

	if err != nil {
		t.Fatalf("should not have error: %s", err)
	}

	if cmdOut != "a b 15" {
		t.Fatalf("output '%v' is not 'a b 15'", cmdOut)
	}
}