X Tutup
Skip to content

Commit f42c9d4

Browse files
committed
Allow stubbing multiple config files
1 parent 43a8b31 commit f42c9d4

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed

command/config_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestConfigSet(t *testing.T) {
5050
initBlankContext("", "OWNER/REPO", "master")
5151

5252
buf := bytes.NewBufferString("")
53-
defer config.StubWriteConfig(buf)()
53+
defer config.StubWriteConfig(buf, nil)()
5454
output, err := RunCommand("config set editor ed")
5555
if err != nil {
5656
t.Fatalf("error running command `config set editor ed`: %v", err)
@@ -80,7 +80,7 @@ editor: ed
8080
initBlankContext(cfg, "OWNER/REPO", "master")
8181

8282
buf := bytes.NewBufferString("")
83-
defer config.StubWriteConfig(buf)()
83+
defer config.StubWriteConfig(buf, nil)()
8484

8585
output, err := RunCommand("config set editor vim")
8686
if err != nil {
@@ -142,7 +142,7 @@ func TestConfigSetHost(t *testing.T) {
142142
initBlankContext("", "OWNER/REPO", "master")
143143

144144
buf := bytes.NewBufferString("")
145-
defer config.StubWriteConfig(buf)()
145+
defer config.StubWriteConfig(buf, nil)()
146146
output, err := RunCommand("config set -hgithub.com git_protocol ssh")
147147
if err != nil {
148148
t.Fatalf("error running command `config set editor ed`: %v", err)
@@ -172,7 +172,7 @@ hosts:
172172
initBlankContext(cfg, "OWNER/REPO", "master")
173173

174174
buf := bytes.NewBufferString("")
175-
defer config.StubWriteConfig(buf)()
175+
defer config.StubWriteConfig(buf, nil)()
176176

177177
output, err := RunCommand("config set -hgithub.com git_protocol https")
178178
if err != nil {

command/testing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func initBlankContext(cfg, repo, branch string) {
8888

8989
// NOTE we are not restoring the original readConfig; we never want to touch the config file on
9090
// disk during tests.
91-
config.StubConfig(cfg)
91+
config.StubConfig(cfg, "")
9292

9393
return ctx
9494
}

context/blank_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type blankContext struct {
2323
}
2424

2525
func (c *blankContext) Config() (config.Config, error) {
26-
cfg, err := config.ParseConfig("boom.txt")
26+
cfg, err := config.ParseConfig("config.yml")
2727
if err != nil {
2828
panic(fmt.Sprintf("failed to parse config during tests. did you remember to stub? error: %s", err))
2929
}

internal/config/config_file_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ hosts:
2222
github.com:
2323
user: monalisa
2424
oauth_token: OTOKEN
25-
`)()
26-
config, err := ParseConfig("filename")
25+
`, "")()
26+
config, err := ParseConfig("config.yml")
2727
eq(t, err, nil)
2828
user, err := config.Get("github.com", "user")
2929
eq(t, err, nil)
@@ -42,8 +42,8 @@ hosts:
4242
github.com:
4343
user: monalisa
4444
oauth_token: OTOKEN
45-
`)()
46-
config, err := ParseConfig("filename")
45+
`, "")()
46+
config, err := ParseConfig("config.yml")
4747
eq(t, err, nil)
4848
user, err := config.Get("github.com", "user")
4949
eq(t, err, nil)
@@ -59,8 +59,8 @@ hosts:
5959
example.com:
6060
user: wronguser
6161
oauth_token: NOTTHIS
62-
`)()
63-
config, err := ParseConfig("filename")
62+
`, "")()
63+
config, err := ParseConfig("config.yml")
6464
eq(t, err, nil)
6565
_, err = config.Get("github.com", "user")
6666
eq(t, err, errors.New(`could not find config entry for "github.com"`))
@@ -79,11 +79,11 @@ github.com:
7979
}
8080

8181
buf := bytes.NewBufferString("")
82-
defer StubWriteConfig(buf)()
82+
defer StubWriteConfig(buf, nil)()
8383

8484
defer StubBackupConfig()()
8585

86-
err = migrateConfig("boom.txt", &root)
86+
err = migrateConfig("config.yml", &root)
8787
eq(t, err, nil)
8888

8989
expected := `hosts:

internal/config/testing.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package config
22

33
import (
4+
"fmt"
45
"io"
6+
"os"
7+
"path"
58
)
69

710
func StubBackupConfig() func() {
@@ -15,21 +18,41 @@ func StubBackupConfig() func() {
1518
}
1619
}
1720

18-
func StubWriteConfig(w io.Writer) func() {
21+
func StubWriteConfig(wc io.Writer, wh io.Writer) func() {
1922
orig := WriteConfigFile
2023
WriteConfigFile = func(fn string, data []byte) error {
21-
_, err := w.Write(data)
22-
return err
24+
switch path.Base(fn) {
25+
case "config.yml":
26+
_, err := wc.Write(data)
27+
return err
28+
case "hosts.yml":
29+
_, err := wh.Write(data)
30+
return err
31+
default:
32+
return fmt.Errorf("write to unstubbed file: %q", fn)
33+
}
2334
}
2435
return func() {
2536
WriteConfigFile = orig
2637
}
2738
}
2839

29-
func StubConfig(content string) func() {
40+
func StubConfig(main, hosts string) func() {
3041
orig := ReadConfigFile
3142
ReadConfigFile = func(fn string) ([]byte, error) {
32-
return []byte(content), nil
43+
switch path.Base(fn) {
44+
case "config.yml":
45+
return []byte(main), nil
46+
case "hosts.yml":
47+
if hosts == "" {
48+
return []byte(nil), os.ErrNotExist
49+
} else {
50+
return []byte(hosts), nil
51+
}
52+
default:
53+
return []byte(nil), fmt.Errorf("read from unstubbed file: %q", fn)
54+
}
55+
3356
}
3457
return func() {
3558
ReadConfigFile = orig

0 commit comments

Comments
 (0)
X Tutup