Skip to content

Commit

Permalink
Exercise dirname matches metadata exercise slug (#774)
Browse files Browse the repository at this point in the history
* Exercise dirname matches metadata exercise slug

* Avoid panic when err nil on test failure

* Make error message actionable
  • Loading branch information
jdsutherland authored and nywilken committed Dec 12, 2018
1 parent eb097f6 commit 0500cf5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
return err
}

if exercise.Slug != metadata.Exercise {
// TODO: error msg should suggest running future doctor command
msg := `
The exercise directory does not match exercise slug in metadata:
expected '%[1]s' but got '%[2]s'
Please rename the directory '%[1]s' to '%[2]s' and try again.
`
return fmt.Errorf(msg, exercise.Slug, metadata.Exercise)
}

if !metadata.IsRequester {
// TODO: add test
msg := `
Expand Down
34 changes: 34 additions & 0 deletions cmd/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,40 @@ func TestSubmitRelativePath(t *testing.T) {
assert.Equal(t, "This is a file.", submittedFiles["file.txt"])
}

func TestExerciseDirnameMatchesMetadataSlug(t *testing.T) {
submittedFiles := map[string]string{}
ts := fakeSubmitServer(t, submittedFiles)
defer ts.Close()

tmpDir, err := ioutil.TempDir("", "submit-files")
defer os.RemoveAll(tmpDir)
assert.NoError(t, err)

dir := filepath.Join(tmpDir, "bogus-track", "bogus-exercise-doesnt-match-metadata-slug")
os.MkdirAll(filepath.Join(dir, "subdir"), os.FileMode(0755))
writeFakeMetadata(t, dir, "bogus-track", "bogus-exercise")

file1 := filepath.Join(dir, "file-1.txt")
err = ioutil.WriteFile(file1, []byte("This is file 1."), os.FileMode(0755))
assert.NoError(t, err)

v := viper.New()
v.Set("token", "abc123")
v.Set("workspace", tmpDir)
v.Set("apibaseurl", ts.URL)

cfg := config.Config{
Persister: config.InMemoryPersister{},
Dir: tmpDir,
UserViperConfig: v,
}

err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{file1})
if assert.Error(t, err) {
assert.Regexp(t, "directory does not match exercise slug", err.Error())
}
}

func writeFakeMetadata(t *testing.T, dir, trackID, exerciseSlug string) {
metadata := &workspace.ExerciseMetadata{
ID: "bogus-solution-uuid",
Expand Down

0 comments on commit 0500cf5

Please sign in to comment.