@@ -23,6 +23,7 @@ func TestProvisionerPrepare_extractScript(t *testing.T) {
23
23
config := testConfig ()
24
24
p := new (Provisioner )
25
25
_ = p .Prepare (config )
26
+ p .generatedData = generatedData ()
26
27
file , err := extractScript (p )
27
28
defer os .Remove (file )
28
29
if err != nil {
@@ -35,13 +36,15 @@ func TestProvisionerPrepare_extractScript(t *testing.T) {
35
36
36
37
// File contents should contain 2 lines concatenated by newlines: foo\nbar
37
38
readFile , err := os .ReadFile (file )
38
- expectedContents := "foo\n bar\n "
39
+ expectedContents := "\n try {\n $results = . {\n \t foo\n bar\n \n \t }\n } catch {\n $errorMessage = $_.Exception.Message\n \t throw \" Script failed with error: $errorMessage\" \n \t \n }\n \n Write-Host $results\n if ($global:LastExitCode -ne 0) {\n \t Write-Host \" Script failed with exit code: $global:LastExitCode\" \n \t throw \" Script failed with exit code: $global:LastExitCode\" \n }\n exit 0\n "
40
+ normalizedExpectedContent := normalizeWhiteSpace (expectedContents )
39
41
if err != nil {
40
42
t .Fatalf ("Should not be error: %s" , err )
41
43
}
42
44
s := string (readFile [:])
43
- if s != expectedContents {
44
- t .Fatalf ("Expected generated inlineScript to equal '%s', got '%s'" , expectedContents , s )
45
+ normalizedString := normalizeWhiteSpace (s )
46
+ if normalizedString != normalizedExpectedContent {
47
+ t .Fatalf ("Expected generated inlineScript to equal '%s', got '%s'" , normalizedExpectedContent , normalizedString )
45
48
}
46
49
}
47
50
@@ -74,11 +77,11 @@ func TestProvisionerPrepare_Defaults(t *testing.T) {
74
77
t .Error ("expected elevated_password to be empty" )
75
78
}
76
79
77
- if p .config .ExecuteCommand != `powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; &' {{.Path}}' ; exit $LastExitCode } "` {
78
- t .Fatalf (`Default command should be 'powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; &' {{.Path}}' ; exit $LastExitCode }" ', but got '%s'` , p .config .ExecuteCommand )
80
+ if p .config .ExecuteCommand != `powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; Set-Variable -Name LastExitCode -Value 0 -Scope Global; . {{.Path}}; exit $global: LastExitCode; }; "` {
81
+ t .Fatalf (`Default command should be 'powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; Set-Variable -Name LastExitCode -Value 0 -Scope Global; . {{.Path}}; exit $global: LastExitCode; }; ', but got '%s'` , p .config .ExecuteCommand )
79
82
}
80
83
81
- if p .config .ElevatedExecuteCommand != `powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; &' {{.Path}}' ; exit $LastExitCode } "` {
84
+ if p .config .ElevatedExecuteCommand != `powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; Set-Variable -Name LastExitCode -Value 0 -Scope Global; . {{.Path}}; exit $global: LastExitCode; }; "` {
82
85
t .Fatalf (`Default command should be 'powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; &'{{.Path}}'; exit $LastExitCode }"', but got '%s'` , p .config .ElevatedExecuteCommand )
83
86
}
84
87
@@ -119,8 +122,10 @@ func TestProvisionerPrepare_DebugMode(t *testing.T) {
119
122
if err != nil {
120
123
t .Fatalf ("err: %s" , err )
121
124
}
125
+ // powershell -executionpolicy bypass "& { if (" +
126
+ // "Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};Set-PsDebug -Trace 1;. {{.Vars}}; &'{{.Path}}'; exit $LastExitCode }"
122
127
123
- command := `powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};Set-PsDebug -Trace 1;. {{.Vars}}; &' {{.Path}}' ; exit $LastExitCode } "`
128
+ command := `powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};Set-PsDebug -Trace 1;. {{.Vars}}; Set-Variable -Name LastExitCode -Value 0 -Scope Global; . {{.Path}}; exit $global: LastExitCode; }; "`
124
129
if p .config .ExecuteCommand != command {
125
130
t .Fatalf (fmt .Sprintf (`Expected command should be '%s' but got '%s'` , command , p .config .ExecuteCommand ))
126
131
}
@@ -483,7 +488,8 @@ func TestProvisionerProvision_Inline(t *testing.T) {
483
488
}
484
489
485
490
cmd := comm .StartCmd .Command
486
- re := regexp .MustCompile (`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &'c:/Windows/Temp/inlineScript.ps1'; exit \$LastExitCode }"` )
491
+ re := regexp .MustCompile (`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/inlineScript.ps1; exit \$global:LastExitCode; };"` )
492
+
487
493
matched := re .MatchString (cmd )
488
494
if ! matched {
489
495
t .Fatalf ("Got unexpected command: %s" , cmd )
@@ -503,7 +509,7 @@ func TestProvisionerProvision_Inline(t *testing.T) {
503
509
}
504
510
505
511
cmd = comm .StartCmd .Command
506
- re = regexp .MustCompile (`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &' c:/Windows/Temp/inlineScript.ps1' ; exit \$LastExitCode } "` )
512
+ re = regexp .MustCompile (`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/inlineScript.ps1; exit \$global: LastExitCode; }; "` )
507
513
matched = re .MatchString (cmd )
508
514
if ! matched {
509
515
t .Fatalf ("Got unexpected command: %s" , cmd )
@@ -533,7 +539,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) {
533
539
}
534
540
535
541
cmd := comm .StartCmd .Command
536
- re := regexp .MustCompile (`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &' c:/Windows/Temp/script.ps1' ; exit \$LastExitCode } "` )
542
+ re := regexp .MustCompile (`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/script.ps1; exit \$global: LastExitCode; }; "` )
537
543
matched := re .MatchString (cmd )
538
544
if ! matched {
539
545
t .Fatalf ("Got unexpected command: %s" , cmd )
@@ -570,7 +576,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
570
576
}
571
577
572
578
cmd := comm .StartCmd .Command
573
- re := regexp .MustCompile (`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &' c:/Windows/Temp/script.ps1' ; exit \$LastExitCode } "` )
579
+ re := regexp .MustCompile (`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/script.ps1; exit \$global: LastExitCode; }; "` )
574
580
matched := re .MatchString (cmd )
575
581
if ! matched {
576
582
t .Fatalf ("Got unexpected command: %s" , cmd )
@@ -595,11 +601,11 @@ func TestProvisionerProvision_SkipClean(t *testing.T) {
595
601
}{
596
602
{
597
603
SkipClean : true ,
598
- LastExecutedCommandRegex : `powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &' c:/Windows/Temp/script.ps1' ; exit \$LastExitCode } "` ,
604
+ LastExecutedCommandRegex : `powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/script.ps1; exit \$global: LastExitCode; }; "` ,
599
605
},
600
606
{
601
607
SkipClean : false ,
602
- LastExecutedCommandRegex : `powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &' c:/Windows/Temp/packer-cleanup-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1' ; exit \$LastExitCode } "` ,
608
+ LastExecutedCommandRegex : `powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/packer-cleanup-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; exit \$global: LastExitCode; }; "` ,
603
609
},
604
610
}
605
611
@@ -917,7 +923,7 @@ func TestProvision_createCommandText(t *testing.T) {
917
923
p .generatedData = make (map [string ]interface {})
918
924
cmd , _ := p .createCommandText ()
919
925
920
- re := regexp .MustCompile (`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &' c:/Windows/Temp/script.ps1' ; exit \$LastExitCode } "` )
926
+ re := regexp .MustCompile (`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/script.ps1; exit \$global: LastExitCode; }; "` )
921
927
matched := re .MatchString (cmd )
922
928
if ! matched {
923
929
t .Fatalf ("Got unexpected command: %s" , cmd )
@@ -976,3 +982,18 @@ func generatedData() map[string]interface{} {
976
982
"PackerHTTPPort" : commonsteps .HttpPortNotImplemented ,
977
983
}
978
984
}
985
+
986
+ func normalizeWhiteSpace (s string ) string {
987
+ // Replace multiple spaces/tabs with a single space
988
+ re := regexp .MustCompile (`[\t ]+` )
989
+ s = re .ReplaceAllString (s , " " )
990
+
991
+ // Trim leading/trailing spaces and newlines
992
+ s = strings .TrimSpace (s )
993
+
994
+ // Normalize line breaks (remove excessive empty lines)
995
+ s = strings .ReplaceAll (s , "\r \n " , "\n " ) // Convert Windows line endings to Unix
996
+ s = strings .ReplaceAll (s , "\r " , "\n " ) // Convert old Mac line endings to Unix
997
+
998
+ return s
999
+ }
0 commit comments