MCPcopy Create free account
hub / github.com/cli/cli / TestCommandMocking

Function TestCommandMocking

git/client_test.go:1978–2019  ·  view source on GitHub ↗

TestCommandMocking is an invoked test helper that emulates expected behavior for predefined shell commands, erroring when unexpected conditions are encountered.

(t *testing.T)

Source from the content-addressed store, hash-verified

1976
1977// TestCommandMocking is an invoked test helper that emulates expected behavior for predefined shell commands, erroring when unexpected conditions are encountered.
1978func TestCommandMocking(t *testing.T) {
1979 if os.Getenv("GH_WANT_HELPER_PROCESS_RICH") != "1" {
1980 return
1981 }
1982
1983 jsonVar, ok := os.LookupEnv("GH_HELPER_PROCESS_RICH_COMMANDS")
1984 if !ok {
1985 fmt.Fprint(os.Stderr, "missing GH_HELPER_PROCESS_RICH_COMMANDS")
1986 // Exit 1 is used for empty key values in the git config. This is non-breaking in those use cases,
1987 // so this is returning a non-zero exit code to avoid suppressing this error for those use cases.
1988 os.Exit(16)
1989 }
1990
1991 var commands mockedCommands
1992 if err := json.Unmarshal([]byte(jsonVar), &commands); err != nil {
1993 fmt.Fprint(os.Stderr, "failed to unmarshal GH_HELPER_PROCESS_RICH_COMMANDS")
1994 // Exit 1 is used for empty key values in the git config. This is non-breaking in those use cases,
1995 // so this is returning a non-zero exit code to avoid suppressing this error for those use cases.
1996 os.Exit(16)
1997 }
1998
1999 // The discarded args are those for the go test binary itself, e.g. `-test.run=TestHelperProcessRich`
2000 realArgs := os.Args[3:]
2001
2002 commandResult, ok := commands[args(strings.Join(realArgs, " "))]
2003 if !ok {
2004 fmt.Fprintf(os.Stderr, "unexpected command: %s\n", strings.Join(realArgs, " "))
2005 // Exit 1 is used for empty key values in the git config. This is non-breaking in those use cases,
2006 // so this is returning a non-zero exit code to avoid suppressing this error for those use cases.
2007 os.Exit(16)
2008 }
2009
2010 if commandResult.Stdout != "" {
2011 fmt.Fprint(os.Stdout, commandResult.Stdout)
2012 }
2013
2014 if commandResult.Stderr != "" {
2015 fmt.Fprint(os.Stderr, commandResult.Stderr)
2016 }
2017
2018 os.Exit(commandResult.ExitStatus)
2019}
2020
2021func TestHelperProcess(t *testing.T) {
2022 if os.Getenv("GH_WANT_HELPER_PROCESS") != "1" {

Callers

nothing calls this directly

Calls 2

JoinMethod · 0.80
argsTypeAlias · 0.70

Tested by

no test coverage detected