|
7 | 7 | "log/slog" |
8 | 8 | "os/exec" |
9 | 9 | "path/filepath" |
| 10 | + "strings" |
10 | 11 | "testing" |
11 | 12 | "time" |
12 | 13 |
|
@@ -847,3 +848,77 @@ func TestRunAgentCmdReportsTimeout(t *testing.T) { |
847 | 848 | assert.Equal(t, ReasonCLINotOnPath, blocked.Reason) |
848 | 849 | assert.Equal(t, "timed out after 1ms", blocked.Detail) |
849 | 850 | } |
| 851 | + |
| 852 | +// copilotPluginHelp is the commander-style shape of `copilot plugin --help`. |
| 853 | +const copilotPluginHelp = "Usage: copilot plugin [options] [command]\n\nManage plugins\n\nCommands:\n install <plugin> Install a plugin\n uninstall <plugin> Uninstall a plugin\n marketplace Manage marketplaces\n" |
| 854 | + |
| 855 | +// codexPluginHelp is the clap-style shape of `codex plugin --help`, which |
| 856 | +// lists add rather than install. |
| 857 | +const codexPluginHelp = "Manage plugins\n\nUsage: codex plugin [OPTIONS] <COMMAND>\n\nCommands:\n add Install a plugin\n marketplace Manage plugin marketplaces\n help Print this message or the help of the given subcommand(s)\n\nOptions:\n -h, --help Print help\n" |
| 858 | + |
| 859 | +func TestProbeWindowsShimPrintedHelp(t *testing.T) { |
| 860 | + copilot := pluginAgent(agents.NameCopilot, "GitHub Copilot", "copilot") |
| 861 | + tests := []struct { |
| 862 | + name string |
| 863 | + agent *agents.Agent |
| 864 | + bin string |
| 865 | + stdout string |
| 866 | + stderr string |
| 867 | + shim bool |
| 868 | + }{ |
| 869 | + {name: "windows .bat shim printed help and exited 1", agent: copilot, bin: `C:\npm\copilot.bat`, stdout: copilotPluginHelp, shim: true}, |
| 870 | + {name: "windows .cmd shim printed help and exited 1", agent: copilot, bin: `C:\npm\copilot.cmd`, stdout: copilotPluginHelp, shim: true}, |
| 871 | + {name: "windows shim under a path with a space", agent: copilot, bin: `C:\Users\Jane Doe\AppData\Roaming\npm\copilot.cmd`, stdout: copilotPluginHelp, shim: true}, |
| 872 | + {name: "windows shim that printed an error on stderr", agent: copilot, bin: `C:\npm\copilot.cmd`, stdout: copilotPluginHelp, stderr: " node: bad option\n", shim: true}, |
| 873 | + {name: "help on stderr is not a shim that printed help", agent: copilot, bin: `C:\npm\copilot.cmd`, stderr: copilotPluginHelp}, |
| 874 | + {name: "windows codex .cmd shim printed clap help and exited 1", agent: codexAgent(), bin: `C:\npm\codex.cmd`, stdout: codexPluginHelp, shim: true}, |
| 875 | + {name: "windows shim without a plugin command", agent: codexAgent(), bin: `C:\npm\codex.cmd`, stdout: "error: unknown command 'plugin'\n"}, |
| 876 | + {name: "unix binary is not a shim", agent: copilot, bin: "/usr/bin/copilot", stdout: copilotPluginHelp}, |
| 877 | + {name: "windows .exe is not a shim", agent: copilot, bin: `C:\bin\copilot.exe`, stdout: copilotPluginHelp}, |
| 878 | + {name: "error that mentions both words", agent: copilot, bin: `C:\npm\copilot.bat`, stdout: "Error: cannot install plugins: marketplace disabled by policy\n"}, |
| 879 | + {name: "usage line without a command list", agent: copilot, bin: `C:\npm\copilot.bat`, stdout: "Usage: copilot plugin\nerror: install and marketplace are not available\n"}, |
| 880 | + {name: "unindented words after blank lines", agent: copilot, bin: `C:\npm\copilot.bat`, stdout: "Usage: copilot plugin [options]\n\nmarketplace: disabled by policy\n\ninstall is not permitted\n"}, |
| 881 | + } |
| 882 | + for _, tc := range tests { |
| 883 | + t.Run(tc.name, func(t *testing.T) { |
| 884 | + origLook := lookPath |
| 885 | + t.Cleanup(func() { lookPath = origLook }) |
| 886 | + lookPath = func(string) (string, error) { return tc.bin, nil } |
| 887 | + |
| 888 | + ctx, stub := process.WithStub(t.Context()) |
| 889 | + stub.WithCallback(func(*exec.Cmd) error { return nil }) |
| 890 | + stub.WithStdoutFor("plugin --help", tc.stdout) |
| 891 | + stub.WithStderrFor("plugin --help", tc.stderr) |
| 892 | + stub.WithFailureFor("plugin --help", errors.New("exit status 1")) |
| 893 | + |
| 894 | + _, err := InstallPluginForAgent(ctx, tc.agent, "", "v0.2.6") |
| 895 | + var be *BlockedError |
| 896 | + require.ErrorAs(t, err, &be) |
| 897 | + if tc.shim { |
| 898 | + assert.Equal(t, ReasonShimExitedWithError, be.Reason) |
| 899 | + assert.False(t, be.TimedOut) |
| 900 | + wantDetail := "printed its plugin help but exited with an error (exit status 1)" |
| 901 | + if tc.stderr != "" { |
| 902 | + wantDetail += ": " + strings.TrimSpace(tc.stderr) |
| 903 | + } |
| 904 | + wantDetail += "; to see the error and exit code, run `& \"" + tc.bin + "\" plugin --help; $LASTEXITCODE` in PowerShell or `\"" + tc.bin + "\" plugin --help & echo %ERRORLEVEL%` in Command Prompt, and re-run this command once it exits with 0" |
| 905 | + assert.Equal(t, wantDetail, be.Detail) |
| 906 | + } else { |
| 907 | + assert.Equal(t, ReasonCLINotOnPath, be.Reason) |
| 908 | + } |
| 909 | + assert.Len(t, stub.Commands(), 1, "nothing runs after a failed probe") |
| 910 | + }) |
| 911 | + } |
| 912 | +} |
| 913 | + |
| 914 | +func TestWindowsShimPrintedHelpRejectsTimeout(t *testing.T) { |
| 915 | + // A shim that printed the help and then hung until its deadline reports the |
| 916 | + // timeout, even though the same output without the timeout is a shim error. |
| 917 | + perr := &process.ProcessError{Command: "copilot.bat plugin --help", Err: errors.New("exit status 1"), Stdout: copilotPluginHelp} |
| 918 | + bin := `C:\npm\copilot.bat` |
| 919 | + got, ok := windowsShimPrintedHelp(bin, perr) |
| 920 | + assert.True(t, ok) |
| 921 | + assert.Same(t, perr, got) |
| 922 | + _, ok = windowsShimPrintedHelp(bin, &agentCmdTimeoutError{timeout: time.Second, err: perr}) |
| 923 | + assert.False(t, ok) |
| 924 | +} |
0 commit comments