Skip to content

Commit

Permalink
Merge pull request #642 from exercism/download-guards
Browse files Browse the repository at this point in the history
Add more guards to the download command
  • Loading branch information
Katrina Owen authored Jul 14, 2018
2 parents f6e8b73 + 7d1a115 commit 6d4396b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
12 changes: 12 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@ const msgWelcomePleaseConfigure = `
%s configure --token=YOUR_TOKEN
`

// Running configure without any arguments will attempt to
// set the default workspace. If the default workspace directory
// risks clobbering an existing directory, it will print an
// error message that explains how to proceed.
const msgRerunConfigure = `
Please re-run the configure command to define where
to download the exercises.
%s configure
`
3 changes: 3 additions & 0 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func runDownload(cfg config.Configuration, flags *pflag.FlagSet, args []string)
tokenURL := config.InferSiteURL(usrCfg.GetString("apibaseurl")) + "/my/settings"
return fmt.Errorf(msgWelcomePleaseConfigure, tokenURL, BinaryName)
}
if usrCfg.GetString("workspace") == "" || usrCfg.GetString("apibaseurl") == "" {
return fmt.Errorf(msgRerunConfigure, BinaryName)
}

uuid, err := flags.GetString("uuid")
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions cmd/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,38 @@ func TestDownloadWithoutToken(t *testing.T) {
}
}

func TestDownloadWithoutWorkspace(t *testing.T) {
v := viper.New()
v.Set("token", "abc123")
cfg := config.Configuration{
UserViperConfig: v,
}

err := runDownload(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{})
if assert.Error(t, err) {
assert.Regexp(t, "re-run the configure", err.Error())
}
}

func TestDownloadWithoutBaseURL(t *testing.T) {
v := viper.New()
v.Set("token", "abc123")
v.Set("workspace", "/home/whatever")
cfg := config.Configuration{
UserViperConfig: v,
}

err := runDownload(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{})
if assert.Error(t, err) {
assert.Regexp(t, "re-run the configure", err.Error())
}
}

func TestDownloadWithoutFlags(t *testing.T) {
v := viper.New()
v.Set("token", "abc123")
v.Set("workspace", "/home/username")
v.Set("apibaseurl", "http://example.com")

cfg := config.Configuration{
UserViperConfig: v,
Expand Down
13 changes: 1 addition & 12 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,7 @@ func runSubmit(cfg config.Configuration, flags *pflag.FlagSet, args []string) er
}

if usrCfg.GetString("workspace") == "" {
// Running configure without any arguments will attempt to
// set the default workspace. If the default workspace directory
// risks clobbering an existing directory, it will print an
// error message that explains how to proceed.
msg := `
Please re-run the configure command to define where
to download the exercises.
%s configure
`
return fmt.Errorf(msg, BinaryName)
return fmt.Errorf(msgRerunConfigure, BinaryName)
}

for i, arg := range args {
Expand Down

0 comments on commit 6d4396b

Please sign in to comment.