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

Function fakeGH

script/ghmock/run_test.go:86–249  ·  view source on GitHub ↗

fakeGH implements the few commands the run tests need.

(args []string)

Source from the content-addressed store, hash-verified

84
85// fakeGH implements the few commands the run tests need.
86func fakeGH(args []string) int {
87 if len(args) == 0 {
88 return 2
89 }
90 switch args[0] {
91 case "--version":
92 if os.Getenv("GHMOCK_FAKE_VARIANT") == "config-probe" {
93 if err := os.WriteFile(filepath.Join(os.Getenv("GH_CONFIG_DIR"), "probe.txt"), []byte("version probe"), 0o600); err != nil {
94 fmt.Fprintln(os.Stderr, err)
95 return 1
96 }
97 }
98 fmt.Printf("gh version 0.0.0-%s\n", os.Getenv("GHMOCK_FAKE_VARIANT"))
99 case "config":
100 fmt.Printf("GH_CONFIG_DIR=%s\n", os.Getenv("GH_CONFIG_DIR"))
101 for _, name := range []string{"config.yml", "hosts.yml", "probe.txt"} {
102 data, err := os.ReadFile(filepath.Join(os.Getenv("GH_CONFIG_DIR"), name))
103 if errors.Is(err, os.ErrNotExist) {
104 fmt.Printf("%s=(none)\n", name)
105 continue
106 }
107 if err != nil {
108 fmt.Fprintln(os.Stderr, err)
109 return 1
110 }
111 fmt.Printf("%s=%q\n", name, data)
112 }
113 if len(args) > 1 {
114 data := []byte("changed-by-" + os.Getenv("GHMOCK_FAKE_VARIANT") + "\n")
115 if err := os.WriteFile(filepath.Join(os.Getenv("GH_CONFIG_DIR"), "hosts.yml"), data, 0o600); err != nil {
116 fmt.Fprintln(os.Stderr, err)
117 return 1
118 }
119 if args[1] == "mutate-fail" {
120 return 7
121 }
122 }
123 case "env":
124 wd, _ := os.Getwd()
125 for _, kv := range [][2]string{
126 {"GH_HOST", os.Getenv("GH_HOST")},
127 {"GH_TOKEN", os.Getenv("GH_TOKEN")},
128 {"GITHUB_TOKEN", os.Getenv("GITHUB_TOKEN")},
129 {"GH_FORCE_TTY", os.Getenv("GH_FORCE_TTY")},
130 {"NO_COLOR", os.Getenv("NO_COLOR")},
131 {"TERM", os.Getenv("TERM")},
132 {"COLORTERM", os.Getenv("COLORTERM")},
133 {"SSL_CERT_FILE", os.Getenv("SSL_CERT_FILE")},
134 {"SSL_CERT_DIR", os.Getenv("SSL_CERT_DIR")},
135 {"GIT_SSL_CAINFO", os.Getenv("GIT_SSL_CAINFO")},
136 {"HOME", os.Getenv("HOME")},
137 {"PWD", wd},
138 } {
139 fmt.Printf("%s=%s\n", kv[0], kv[1])
140 }
141 for _, name := range []string{"config.yml", "hosts.yml"} {
142 path := filepath.Join(os.Getenv("GH_CONFIG_DIR"), name)
143 fmt.Printf("--- %s\n", path)

Callers 1

TestMainFunction · 0.85

Calls 12

processExitCodeFunction · 0.85
fakeGetFunction · 0.85
WriteFileMethod · 0.80
JoinMethod · 0.80
PrintlnMethod · 0.80
ModeMethod · 0.80
CommandMethod · 0.80
PrintfMethod · 0.65
WriteMethod · 0.65
RunMethod · 0.65
GetMethod · 0.65
CopyMethod · 0.45

Tested by

no test coverage detected