Skip to content

Commit 0500cf5

Browse files
jdsutherlandnywilken
authored andcommitted
Exercise dirname matches metadata exercise slug (#774)
* Exercise dirname matches metadata exercise slug * Avoid panic when err nil on test failure * Make error message actionable
1 parent eb097f6 commit 0500cf5

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

cmd/submit.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
141141
return err
142142
}
143143

144+
if exercise.Slug != metadata.Exercise {
145+
// TODO: error msg should suggest running future doctor command
146+
msg := `
147+
148+
The exercise directory does not match exercise slug in metadata:
149+
150+
expected '%[1]s' but got '%[2]s'
151+
152+
Please rename the directory '%[1]s' to '%[2]s' and try again.
153+
154+
`
155+
return fmt.Errorf(msg, exercise.Slug, metadata.Exercise)
156+
}
157+
144158
if !metadata.IsRequester {
145159
// TODO: add test
146160
msg := `

cmd/submit_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,40 @@ func TestSubmitRelativePath(t *testing.T) {
548548
assert.Equal(t, "This is a file.", submittedFiles["file.txt"])
549549
}
550550

551+
func TestExerciseDirnameMatchesMetadataSlug(t *testing.T) {
552+
submittedFiles := map[string]string{}
553+
ts := fakeSubmitServer(t, submittedFiles)
554+
defer ts.Close()
555+
556+
tmpDir, err := ioutil.TempDir("", "submit-files")
557+
defer os.RemoveAll(tmpDir)
558+
assert.NoError(t, err)
559+
560+
dir := filepath.Join(tmpDir, "bogus-track", "bogus-exercise-doesnt-match-metadata-slug")
561+
os.MkdirAll(filepath.Join(dir, "subdir"), os.FileMode(0755))
562+
writeFakeMetadata(t, dir, "bogus-track", "bogus-exercise")
563+
564+
file1 := filepath.Join(dir, "file-1.txt")
565+
err = ioutil.WriteFile(file1, []byte("This is file 1."), os.FileMode(0755))
566+
assert.NoError(t, err)
567+
568+
v := viper.New()
569+
v.Set("token", "abc123")
570+
v.Set("workspace", tmpDir)
571+
v.Set("apibaseurl", ts.URL)
572+
573+
cfg := config.Config{
574+
Persister: config.InMemoryPersister{},
575+
Dir: tmpDir,
576+
UserViperConfig: v,
577+
}
578+
579+
err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{file1})
580+
if assert.Error(t, err) {
581+
assert.Regexp(t, "directory does not match exercise slug", err.Error())
582+
}
583+
}
584+
551585
func writeFakeMetadata(t *testing.T, dir, trackID, exerciseSlug string) {
552586
metadata := &workspace.ExerciseMetadata{
553587
ID: "bogus-solution-uuid",

0 commit comments

Comments
 (0)