| 1 |
package plugin_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"os" |
| 7 |
"path/filepath" |
| 8 |
"strings" |
| 9 |
"sync" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/rogpeppe/go-internal/testscript" |
| 13 |
"github.com/rogpeppe/go-internal/testscript/plugin" |
| 14 |
) |
| 15 |
|
| 16 |
func TestMain(m *testing.M) { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
testscript.Main(m, map[string]func(){ |
| 21 |
"testscript-plugin-demo": func() { |
| 22 |
if err := plugin.Serve(demoPlugin{}); err != nil { |
| 23 |
log.Fatal(err) |
| 24 |
} |
| 25 |
}, |
| 26 |
"testscript-plugin-kv": func() { |
| 27 |
if err := plugin.Serve(kvPlugin{}); err != nil { |
| 28 |
log.Fatal(err) |
| 29 |
} |
| 30 |
}, |
| 31 |
}) |
| 32 |
} |
| 33 |
|
| 34 |
func TestPlugin(t *testing.T) { |
| 35 |
p := testscript.Params{ |
| 36 |
Dir: "testdata", |
| 37 |
} |
| 38 |
cleanup, err := plugin.Setup(&p) |
| 39 |
if err != nil { |
| 40 |
t.Fatal(err) |
| 41 |
} |
| 42 |
t.Cleanup(cleanup) |
| 43 |
testscript.Run(t, p) |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
type demoPlugin struct{} |
| 52 |
|
| 53 |
func (demoPlugin) Info() plugin.PluginInfo { |
| 54 |
return plugin.PluginInfo{ |
| 55 |
RequiredEnv: map[string]bool{"GREETING": true}, |
| 56 |
ResultEnv: map[string]bool{"DEMO_MESSAGE": true, "DEMO_TOKEN": true}, |
| 57 |
Cmds: map[string]plugin.CmdInfo{ |
| 58 |
"echo": {WritesOutput: true}, |
| 59 |
"warn": {WritesOutput: true}, |
| 60 |
"cwd": {WritesOutput: true}, |
| 61 |
"settoken": {}, |
| 62 |
"fail": {WritesOutput: true}, |
| 63 |
}, |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func (demoPlugin) NewTestInstance(p plugin.TestParams) (plugin.TestInstance, error) { |
| 68 |
name := "anonymous" |
| 69 |
if p.PluginParamsDir != "" { |
| 70 |
data, err := os.ReadFile(filepath.Join(p.PluginParamsDir, "name.txt")) |
| 71 |
if err != nil { |
| 72 |
return nil, fmt.Errorf("cannot read plugin parameter directory: %v", err) |
| 73 |
} |
| 74 |
name = strings.TrimSpace(string(data)) |
| 75 |
} |
| 76 |
greeting := p.Env["GREETING"] |
| 77 |
if greeting == "" { |
| 78 |
greeting = "hello" |
| 79 |
} |
| 80 |
return &demoInstance{message: greeting + ", " + name}, nil |
| 81 |
} |
| 82 |
|
| 83 |
func (demoPlugin) Close() {} |
| 84 |
|
| 85 |
type demoInstance struct { |
| 86 |
message string |
| 87 |
} |
| 88 |
|
| 89 |
func (inst *demoInstance) Env() map[string]string { |
| 90 |
return map[string]string{"DEMO_MESSAGE": inst.message} |
| 91 |
} |
| 92 |
|
| 93 |
func (inst *demoInstance) RunCmd(p plugin.CmdParams) (plugin.CmdResult, error) { |
| 94 |
switch p.Name { |
| 95 |
case "echo": |
| 96 |
return plugin.CmdResult{Stdout: []byte(strings.Join(p.Args, " ") + "\n")}, nil |
| 97 |
case "warn": |
| 98 |
return plugin.CmdResult{Stderr: []byte(strings.Join(p.Args, " ") + "\n")}, nil |
| 99 |
case "cwd": |
| 100 |
return plugin.CmdResult{Stdout: []byte(p.Dir + "\n")}, nil |
| 101 |
case "settoken": |
| 102 |
if len(p.Args) != 1 { |
| 103 |
return plugin.CmdResult{}, fmt.Errorf("usage: settoken <value>") |
| 104 |
} |
| 105 |
return plugin.CmdResult{Env: map[string]string{"DEMO_TOKEN": p.Args[0]}}, nil |
| 106 |
case "fail": |
| 107 |
return plugin.CmdResult{Stderr: []byte("deliberate failure\n"), Err: "exit status 1"}, nil |
| 108 |
} |
| 109 |
return plugin.CmdResult{}, fmt.Errorf("unrecognized command %q", p.Name) |
| 110 |
} |
| 111 |
|
| 112 |
func (inst *demoInstance) Close() {} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
type kvPlugin struct{} |
| 119 |
|
| 120 |
func (kvPlugin) Info() plugin.PluginInfo { |
| 121 |
return plugin.PluginInfo{ |
| 122 |
Cmds: map[string]plugin.CmdInfo{ |
| 123 |
"set": {}, |
| 124 |
"get": {WritesOutput: true}, |
| 125 |
}, |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func (kvPlugin) NewTestInstance(p plugin.TestParams) (plugin.TestInstance, error) { |
| 130 |
return &kvInstance{store: make(map[string]string)}, nil |
| 131 |
} |
| 132 |
|
| 133 |
func (kvPlugin) Close() {} |
| 134 |
|
| 135 |
type kvInstance struct { |
| 136 |
|
| 137 |
mu sync.Mutex |
| 138 |
store map[string]string |
| 139 |
} |
| 140 |
|
| 141 |
func (inst *kvInstance) Env() map[string]string { return nil } |
| 142 |
|
| 143 |
func (inst *kvInstance) RunCmd(p plugin.CmdParams) (plugin.CmdResult, error) { |
| 144 |
inst.mu.Lock() |
| 145 |
defer inst.mu.Unlock() |
| 146 |
switch p.Name { |
| 147 |
case "set": |
| 148 |
if len(p.Args) != 2 { |
| 149 |
return plugin.CmdResult{}, fmt.Errorf("usage: set <key> <value>") |
| 150 |
} |
| 151 |
inst.store[p.Args[0]] = p.Args[1] |
| 152 |
return plugin.CmdResult{}, nil |
| 153 |
case "get": |
| 154 |
if len(p.Args) != 1 { |
| 155 |
return plugin.CmdResult{}, fmt.Errorf("usage: get <key>") |
| 156 |
} |
| 157 |
return plugin.CmdResult{Stdout: []byte(inst.store[p.Args[0]] + "\n")}, nil |
| 158 |
} |
| 159 |
return plugin.CmdResult{}, fmt.Errorf("unrecognized command %q", p.Name) |
| 160 |
} |
| 161 |
|
| 162 |
func (inst *kvInstance) Close() {} |