Skip to content

Commit b23670e

Browse files
author
Katrina Owen
authored
Merge pull request #828 from larson004/remove-duplicate-files
Remove duplicate files before submitting
2 parents 374a570 + 95369d5 commit b23670e

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

cmd/submit.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
6666
return err
6767
}
6868

69+
submitPaths = ctx.removeDuplicatePaths(submitPaths)
70+
6971
if err = ctx.validator.filesBelongToSameExercise(submitPaths); err != nil {
7072
return err
7173
}
@@ -146,6 +148,20 @@ func (s *submitCmdContext) evaluatedSymlinks(submitPaths []string) ([]string, er
146148
return evalSymlinkSubmitPaths, nil
147149
}
148150

151+
func (s *submitCmdContext) removeDuplicatePaths(submitPaths []string) []string {
152+
seen := make(map[string]bool)
153+
result := make([]string, 0, len(submitPaths))
154+
155+
for _, val := range submitPaths {
156+
if _, ok := seen[val]; !ok {
157+
seen[val] = true
158+
result = append(result, val)
159+
}
160+
}
161+
162+
return result
163+
}
164+
149165
// exercise creates an exercise using one of the submitted filepaths.
150166
// This assumes prior verification that submit paths belong to the same exercise.
151167
func (s *submitCmdContext) exercise(aSubmitPath string) (workspace.Exercise, error) {

cmd/submit_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,45 @@ func TestSubmitFilesAndDir(t *testing.T) {
142142
}
143143
}
144144

145+
func TestDuplicateFiles(t *testing.T) {
146+
co := newCapturedOutput()
147+
co.override()
148+
defer co.reset()
149+
150+
// The fake endpoint will populate this when it receives the call from the command.
151+
submittedFiles := map[string]string{}
152+
ts := fakeSubmitServer(t, submittedFiles)
153+
defer ts.Close()
154+
155+
tmpDir, err := ioutil.TempDir("", "duplicate-files")
156+
defer os.RemoveAll(tmpDir)
157+
assert.NoError(t, err)
158+
159+
dir := filepath.Join(tmpDir, "bogus-track", "bogus-exercise")
160+
os.MkdirAll(dir, os.FileMode(0755))
161+
162+
writeFakeMetadata(t, dir, "bogus-track", "bogus-exercise")
163+
164+
v := viper.New()
165+
v.Set("token", "abc123")
166+
v.Set("workspace", tmpDir)
167+
v.Set("apibaseurl", ts.URL)
168+
169+
cfg := config.Config{
170+
Persister: config.InMemoryPersister{},
171+
UserViperConfig: v,
172+
}
173+
174+
file1 := filepath.Join(dir, "file-1.txt")
175+
err = ioutil.WriteFile(file1, []byte("This is file 1."), os.FileMode(0755))
176+
177+
err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{file1, file1})
178+
assert.NoError(t, err)
179+
180+
assert.Equal(t, 1, len(submittedFiles))
181+
assert.Equal(t, "This is file 1.", submittedFiles["file-1.txt"])
182+
}
183+
145184
func TestSubmitFiles(t *testing.T) {
146185
co := newCapturedOutput()
147186
co.override()

0 commit comments

Comments
 (0)