Skip to content

Commit

Permalink
Merge pull request exercism#828 from larson004/remove-duplicate-files
Browse files Browse the repository at this point in the history
Remove duplicate files before submitting
  • Loading branch information
Katrina Owen authored Mar 2, 2019
2 parents 374a570 + 95369d5 commit b23670e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
return err
}

submitPaths = ctx.removeDuplicatePaths(submitPaths)

if err = ctx.validator.filesBelongToSameExercise(submitPaths); err != nil {
return err
}
Expand Down Expand Up @@ -146,6 +148,20 @@ func (s *submitCmdContext) evaluatedSymlinks(submitPaths []string) ([]string, er
return evalSymlinkSubmitPaths, nil
}

func (s *submitCmdContext) removeDuplicatePaths(submitPaths []string) []string {
seen := make(map[string]bool)
result := make([]string, 0, len(submitPaths))

for _, val := range submitPaths {
if _, ok := seen[val]; !ok {
seen[val] = true
result = append(result, val)
}
}

return result
}

// exercise creates an exercise using one of the submitted filepaths.
// This assumes prior verification that submit paths belong to the same exercise.
func (s *submitCmdContext) exercise(aSubmitPath string) (workspace.Exercise, error) {
Expand Down
39 changes: 39 additions & 0 deletions cmd/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,45 @@ func TestSubmitFilesAndDir(t *testing.T) {
}
}

func TestDuplicateFiles(t *testing.T) {
co := newCapturedOutput()
co.override()
defer co.reset()

// The fake endpoint will populate this when it receives the call from the command.
submittedFiles := map[string]string{}
ts := fakeSubmitServer(t, submittedFiles)
defer ts.Close()

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

dir := filepath.Join(tmpDir, "bogus-track", "bogus-exercise")
os.MkdirAll(dir, os.FileMode(0755))

writeFakeMetadata(t, dir, "bogus-track", "bogus-exercise")

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

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

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

err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{file1, file1})
assert.NoError(t, err)

assert.Equal(t, 1, len(submittedFiles))
assert.Equal(t, "This is file 1.", submittedFiles["file-1.txt"])
}

func TestSubmitFiles(t *testing.T) {
co := newCapturedOutput()
co.override()
Expand Down

0 comments on commit b23670e

Please sign in to comment.