Skip to content

Commit 6d4396b

Browse files
author
Katrina Owen
authored
Merge pull request #642 from exercism/download-guards
Add more guards to the download command
2 parents f6e8b73 + 7d1a115 commit 6d4396b

4 files changed

Lines changed: 45 additions & 12 deletions

File tree

cmd/cmd.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@ const msgWelcomePleaseConfigure = `
1414
%s configure --token=YOUR_TOKEN
1515
1616
`
17+
18+
// Running configure without any arguments will attempt to
19+
// set the default workspace. If the default workspace directory
20+
// risks clobbering an existing directory, it will print an
21+
// error message that explains how to proceed.
22+
const msgRerunConfigure = `
23+
24+
Please re-run the configure command to define where
25+
to download the exercises.
26+
27+
%s configure
28+
`

cmd/download.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func runDownload(cfg config.Configuration, flags *pflag.FlagSet, args []string)
5252
tokenURL := config.InferSiteURL(usrCfg.GetString("apibaseurl")) + "/my/settings"
5353
return fmt.Errorf(msgWelcomePleaseConfigure, tokenURL, BinaryName)
5454
}
55+
if usrCfg.GetString("workspace") == "" || usrCfg.GetString("apibaseurl") == "" {
56+
return fmt.Errorf(msgRerunConfigure, BinaryName)
57+
}
5558

5659
uuid, err := flags.GetString("uuid")
5760
if err != nil {

cmd/download_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,38 @@ func TestDownloadWithoutToken(t *testing.T) {
2626
}
2727
}
2828

29+
func TestDownloadWithoutWorkspace(t *testing.T) {
30+
v := viper.New()
31+
v.Set("token", "abc123")
32+
cfg := config.Configuration{
33+
UserViperConfig: v,
34+
}
35+
36+
err := runDownload(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{})
37+
if assert.Error(t, err) {
38+
assert.Regexp(t, "re-run the configure", err.Error())
39+
}
40+
}
41+
42+
func TestDownloadWithoutBaseURL(t *testing.T) {
43+
v := viper.New()
44+
v.Set("token", "abc123")
45+
v.Set("workspace", "/home/whatever")
46+
cfg := config.Configuration{
47+
UserViperConfig: v,
48+
}
49+
50+
err := runDownload(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{})
51+
if assert.Error(t, err) {
52+
assert.Regexp(t, "re-run the configure", err.Error())
53+
}
54+
}
55+
2956
func TestDownloadWithoutFlags(t *testing.T) {
3057
v := viper.New()
3158
v.Set("token", "abc123")
59+
v.Set("workspace", "/home/username")
60+
v.Set("apibaseurl", "http://example.com")
3261

3362
cfg := config.Configuration{
3463
UserViperConfig: v,

cmd/submit.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,7 @@ func runSubmit(cfg config.Configuration, flags *pflag.FlagSet, args []string) er
5858
}
5959

6060
if usrCfg.GetString("workspace") == "" {
61-
// Running configure without any arguments will attempt to
62-
// set the default workspace. If the default workspace directory
63-
// risks clobbering an existing directory, it will print an
64-
// error message that explains how to proceed.
65-
msg := `
66-
67-
Please re-run the configure command to define where
68-
to download the exercises.
69-
70-
%s configure
71-
`
72-
return fmt.Errorf(msg, BinaryName)
61+
return fmt.Errorf(msgRerunConfigure, BinaryName)
7362
}
7463

7564
for i, arg := range args {

0 commit comments

Comments
 (0)