Commit b1af23ef authored by Arran Walker's avatar Arran Walker

Fix shebang powershell use on docker executor

parent f295676d
......@@ -33,6 +33,7 @@ type PsWriter struct {
indent int
Shell string
EOL string
PassFile bool
}
func psQuote(text string) string {
......@@ -295,10 +296,12 @@ func (p *PsWriter) Finish(trace bool) string {
}
func (p *PsWriter) writeShebang(w io.Writer) {
switch p.Shell {
case SNPwsh:
switch {
case p.Shell == SNPwsh:
_, _ = io.WriteString(w, "#requires -PSEdition Core"+p.EOL+p.EOL)
case SNPowershell:
// Windows Powershell only supports #requires on passed files.
case p.Shell == SNPowershell && p.PassFile:
_, _ = io.WriteString(w, "#requires -PSEdition Desktop"+p.EOL+p.EOL)
}
}
......@@ -345,6 +348,7 @@ func (b *PowerShell) GenerateScript(
Shell: b.Shell,
EOL: b.EOL,
TemporaryPath: info.Build.TmpProjectDir(),
PassFile: info.Build.Runner.Executor != dockerWindowsExecutor,
}
if buildStage == common.BuildStagePrepare {
......
......@@ -50,6 +50,50 @@ func TestPowershell_LineBreaks(t *testing.T) {
}
}
func TestPowershell_Shebang(t *testing.T) {
testCases := map[string]struct {
shell string
passfile bool
expectsRequires bool
}{
"Windows Powershell script file": {
shell: "powershell",
passfile: true,
expectsRequires: true,
},
"Windows Powershell stdin": {
shell: "powershell",
passfile: false,
expectsRequires: false,
},
"Powershell Core script file": {
shell: "pwsh",
passfile: true,
expectsRequires: true,
},
"Powershell Core stdin": {
shell: "pwsh",
passfile: false,
expectsRequires: true,
},
}
for tn, tc := range testCases {
t.Run(tn, func(t *testing.T) {
writer := &PsWriter{Shell: tc.shell, EOL: "\n", PassFile: tc.passfile}
writer.Command("foo", "")
output := writer.Finish(false)
if tc.expectsRequires {
assert.Contains(t, output, "#requires -PSEdition")
} else {
assert.NotContains(t, output, "#requires -PSEdition")
}
})
}
}
func TestPowershell_CommandShellEscapes(t *testing.T) {
writer := &PsWriter{Shell: "powershell", EOL: "\r\n"}
writer.Command("foo", "x&(y)")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment