From 4a17a04514e754aded1594d4125264e1e1976c6b Mon Sep 17 00:00:00 2001 From: antichris <881420+antichris@users.noreply.github.com> Date: Tue, 6 Apr 2021 06:46:20 +0300 Subject: complete: enable complete.Complete() output capturing (#138) * complete: test capturing Complete() output Implement an Example test to demonstrate capturing the output of Complete(), which is crucial for integration tests. * complete: do not hard-code the I/O streams at the package initialization Instead of defining the input/output streams as unexported global vars that only get their values assigned to once, at the very initialization of the package, use the values that `os.Stdin` and `os.Stdout` have at the particular moment on every `complete.Complete()` call. Fix #137 * complete: capture and discard output in TestComplete Restore earlier behavior using proper stream redirection this time. * complete: output capturing example: define things in the package scope Define the `stringLookup` func type and `promptEnv` func in the package scope instead of the `ExampleComplete_outputCapturing` test. * complete: rename the `stringLookup` func type to `getEnvFn` --- complete.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'complete.go') diff --git a/complete.go b/complete.go index 2a4c905..0641c39 100644 --- a/complete.go +++ b/complete.go @@ -49,10 +49,8 @@ func (p PredictFunc) Predict(prefix string) []string { } var ( - getEnv = os.Getenv - exit = os.Exit - out io.Writer = os.Stdout - in io.Reader = os.Stdin + getEnv = os.Getenv + exit = os.Exit ) // Complete the command line arguments for the given command in the case that the program @@ -66,6 +64,10 @@ func Complete(name string, cmd Completer) { doUninstall = getEnv("COMP_UNINSTALL") == "1" yes = getEnv("COMP_YES") == "1" ) + var ( + out io.Writer = os.Stdout + in io.Reader = os.Stdin + ) if doInstall || doUninstall { install.Run(name, doUninstall, yes, out, in) exit(0) -- cgit v1.2.3