-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_test.go
More file actions
199 lines (178 loc) · 6.86 KB
/
Copy pathexec_test.go
File metadata and controls
199 lines (178 loc) · 6.86 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package integration
import (
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/flexigpt/llmtools-go/exectool"
"github.com/flexigpt/llmtools-go/fstool"
"github.com/flexigpt/llmtools-go/internal/toolutil"
)
// Shell sessions + env persistence + runscript.
const (
execTestShellVarName = "MYVAR"
execTestShellHelloValue = "hello"
execTestShellByeValue = "bye"
execTestShellTxtPath = "shell.txt"
execTestShellFromShell = "from-shell"
execTestShellScriptsDir = "scripts"
execTestShellPS1FileName = "hello.ps1"
execTestShellSHFileName = "hello.sh"
execTestShellWorldArg = "world"
execTestShellWriteOutput = "Write-Output $env:" + execTestShellVarName
execTestShellEchoEnv = "echo \"$" + execTestShellVarName + "\""
execTestShellSetContent = "Set-Content -NoNewline -Path \"" + execTestShellTxtPath + "\" -Value \"" + execTestShellFromShell + "\""
execTestShellPrintf = "printf \"%s\" \"" + execTestShellFromShell + "\" > " + execTestShellTxtPath
execTestShellPS1Content = "param([string]$Name)\n" +
"Write-Output \"NAME=$Name\"\n" +
"Write-Output \"ENV=$env:" + execTestShellVarName + "\"\n"
execTestShellSHContent = "echo \"NAME=$1\"\n" +
"echo \"ENV=$" + execTestShellVarName + "\"\n"
)
func TestE2E_Exec_ShellCommand_SessionEnvWorkdir(t *testing.T) {
base := t.TempDir()
h := newHarness(t, base)
var shell exectool.ShellName
var cmds1, cmds2, cmds3 []string
var expectEnv1, expectEnv2 string
if runtime.GOOS == toolutil.GOOSWindows {
shell = exectool.ShellNamePowershell
cmds1 = []string{
execTestShellSetContent,
execTestShellWriteOutput,
}
cmds2 = []string{
execTestShellWriteOutput,
}
cmds3 = []string{
execTestShellWriteOutput,
}
expectEnv1 = execTestShellHelloValue
expectEnv2 = execTestShellByeValue
} else {
shell = exectool.ShellNameSh
cmds1 = []string{
execTestShellPrintf,
execTestShellEchoEnv,
}
cmds2 = []string{execTestShellEchoEnv}
cmds3 = []string{execTestShellEchoEnv}
expectEnv1 = execTestShellHelloValue
expectEnv2 = execTestShellByeValue
}
// 1) First call: create session + set env.
resp1 := callJSON[exectool.ShellCommandOut](t, h.r, integrationToolSlugShellCommand, exectool.ShellCommandArgs{
Shell: shell,
Commands: cmds1,
Env: map[string]string{execTestShellVarName: execTestShellHelloValue},
})
if strings.TrimSpace(resp1.SessionID) == "" {
t.Fatalf("expected sessionID, got: %s", debugJSON(t, resp1))
}
if len(resp1.Results) < 2 {
t.Fatalf("expected >=2 results, got: %s", debugJSON(t, resp1))
}
if !strings.Contains(resp1.Results[len(resp1.Results)-1].Stdout, expectEnv1) {
t.Fatalf("expected stdout to contain %q, got: %s", expectEnv1, debugJSON(t, resp1.Results))
}
// Verify file created via shell by reading it with readfile.
out := callRaw(
t,
h.r,
integrationToolSlugReadFile,
fstool.ReadFileArgs{Path: execTestShellTxtPath, Encoding: integrationEncodingText},
)
got := requireSingleTextOutput(t, out)
if got != execTestShellFromShell {
t.Fatalf("shell.txt content mismatch: got=%q want=%q", got, execTestShellFromShell)
}
// 2) Second call: same session, no env provided => session env should persist.
resp2 := callJSON[exectool.ShellCommandOut](t, h.r, integrationToolSlugShellCommand, exectool.ShellCommandArgs{
Shell: shell,
Commands: cmds2,
SessionID: resp1.SessionID,
})
if !strings.Contains(resp2.Results[0].Stdout, expectEnv1) {
t.Fatalf("expected persisted env stdout to contain %q, got: %s", expectEnv1, debugJSON(t, resp2))
}
// 3) Third call: override env, and that override becomes part of session state.
resp3 := callJSON[exectool.ShellCommandOut](t, h.r, integrationToolSlugShellCommand, exectool.ShellCommandArgs{
Shell: shell,
Commands: cmds2,
Env: map[string]string{execTestShellVarName: execTestShellByeValue},
SessionID: resp1.SessionID,
})
if !strings.Contains(resp3.Results[0].Stdout, expectEnv2) {
t.Fatalf("expected overridden env stdout to contain %q, got: %s", expectEnv2, debugJSON(t, resp3))
}
// 4) Fourth call: no env passed => should still be "bye".
resp4 := callJSON[exectool.ShellCommandOut](t, h.r, integrationToolSlugShellCommand, exectool.ShellCommandArgs{
Shell: shell,
Commands: cmds3,
SessionID: resp1.SessionID,
})
if !strings.Contains(resp4.Results[0].Stdout, expectEnv2) {
t.Fatalf("expected session env stdout to contain %q, got: %s", expectEnv2, debugJSON(t, resp4))
}
}
func TestE2E_Exec_RunScript(t *testing.T) {
base := t.TempDir()
// Make runscript robust on Windows by forcing .ps1 to run with ExecutionPolicy Bypass.
var execOpts []exectool.ExecToolOption
if runtime.GOOS == toolutil.GOOSWindows {
pol := exectool.DefaultRunScriptPolicy()
if pol.InterpreterByExtension == nil {
pol.InterpreterByExtension = map[string]exectool.RunScriptInterpreter{}
}
pol.InterpreterByExtension[".ps1"] = exectool.RunScriptInterpreter{
Shell: exectool.ShellNamePwsh,
Mode: exectool.RunScriptModeInterpreter,
Command: "powershell",
Args: []string{"-NoProfile", "-ExecutionPolicy", "Bypass", "-File"},
}
execOpts = append(execOpts, exectool.WithRunScriptPolicy(pol))
}
h := newHarness(t, base, execOpts...)
scriptsDirRel := execTestShellScriptsDir
if runtime.GOOS == toolutil.GOOSWindows {
scriptRel := filepath.Join(scriptsDirRel, execTestShellPS1FileName)
_ = callJSON[fstool.WriteFileOut](t, h.r, integrationToolSlugWriteFile, fstool.WriteFileArgs{
Path: scriptRel,
Encoding: integrationEncodingText,
Content: execTestShellPS1Content,
CreateParents: true,
})
res := callJSON[exectool.RunScriptOut](t, h.r, "runscript", exectool.RunScriptArgs{
Path: execTestShellPS1FileName,
Args: []string{execTestShellWorldArg},
Env: map[string]string{execTestShellVarName: execTestShellHelloValue},
WorkDir: scriptsDirRel,
})
if res.ExitCode != 0 {
t.Fatalf("runscript exit != 0: %s", debugJSON(t, res))
}
if !strings.Contains(res.Stdout, "NAME=world") || !strings.Contains(res.Stdout, "ENV=hello") {
t.Fatalf("unexpected stdout: %s", debugJSON(t, res))
}
} else {
scriptRel := filepath.Join(scriptsDirRel, execTestShellSHFileName)
_ = callJSON[fstool.WriteFileOut](t, h.r, integrationToolSlugWriteFile, fstool.WriteFileArgs{
Path: scriptRel,
Encoding: integrationEncodingText,
Content: execTestShellSHContent,
CreateParents: true,
})
res := callJSON[exectool.RunScriptOut](t, h.r, "runscript", exectool.RunScriptArgs{
Path: execTestShellSHFileName,
Args: []string{execTestShellWorldArg},
Env: map[string]string{execTestShellVarName: execTestShellHelloValue},
WorkDir: scriptsDirRel,
})
if res.ExitCode != 0 {
t.Fatalf("runscript exit != 0: %s", debugJSON(t, res))
}
if !strings.Contains(res.Stdout, "NAME=world") || !strings.Contains(res.Stdout, "ENV=hello") {
t.Fatalf("unexpected stdout: %s", debugJSON(t, res))
}
}
}