mirror of
https://github.com/kopia/kopia.git
synced 2025-12-23 22:57:50 -05:00
* cli: fixed remaining testability indirections for output and logging * cli: added cli.RunSubcommand() which is used in testing to execute a subcommand in the same process * tests: refactored most e2e tests to invoke kopia subcommands in-process * Makefile: enable code coverage for cli/ and internal/ * testing: pass 'testing' tag to unit tests which uses much faster (insecure) password hashing scheme * Makefile: push coverage from PRs again * tests: disable buffer management to reduce memory usage on ARM * cli: fixed misaligned atomic field on ARMHF also temporarily fixed statup-time benign race condition when setting default on the timeZone variable, which is the last global variable.
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
"github.com/alecthomas/kingpin"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/repo/logging"
|
|
)
|
|
|
|
// RunSubcommand executes the subcommand asynchronously in current process
|
|
// with flags in an isolated CLI environment and returns standard output and standard error.
|
|
func (c *App) RunSubcommand(ctx context.Context, argsAndFlags []string) (stdout, stderr io.Reader, wait func() error, kill func()) {
|
|
kpapp := kingpin.New("test", "test")
|
|
|
|
stdoutReader, stdoutWriter := io.Pipe()
|
|
stderrReader, stderrWriter := io.Pipe()
|
|
|
|
c.stdoutWriter = stdoutWriter
|
|
c.stderrWriter = stderrWriter
|
|
c.rootctx = logging.WithLogger(ctx, logging.Writer(stderrWriter))
|
|
|
|
c.Attach(kpapp)
|
|
|
|
var exitCode int
|
|
|
|
resultErr := make(chan error, 1)
|
|
|
|
c.osExit = func(ec int) {
|
|
exitCode = ec
|
|
}
|
|
|
|
go func() {
|
|
defer close(resultErr)
|
|
defer stderrWriter.Close() //nolint:errcheck
|
|
defer stdoutWriter.Close() //nolint:errcheck
|
|
|
|
_, err := kpapp.Parse(argsAndFlags)
|
|
if err != nil {
|
|
resultErr <- err
|
|
return
|
|
}
|
|
|
|
if exitCode != 0 {
|
|
resultErr <- errors.Errorf("exit code %v", exitCode)
|
|
return
|
|
}
|
|
}()
|
|
|
|
return stdoutReader, stderrReader, func() error {
|
|
return <-resultErr
|
|
}, func() {}
|
|
}
|