Skip to content

Commit

Permalink
Merge pull request sequelize#722 from exercism/numeric-suffix
Browse files Browse the repository at this point in the history
 Handle exercise directories with numeric suffixes
  • Loading branch information
Katrina Owen authored Aug 28, 2018
2 parents 32d8ff8 + 3ed08d2 commit e9877e2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
10 changes: 10 additions & 0 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
netURL "net/url"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/exercism/cli/api"
Expand Down Expand Up @@ -200,6 +201,15 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
// TODO: if there's a collision, interactively resolve (show diff, ask if overwrite).
// TODO: handle --force flag to overwrite without asking.

// Work around a path bug due to an early design decision (later reversed) to
// allow numeric suffixes for exercise directories, allowing people to have
// multiple parallel versions of an exercise.
pattern := fmt.Sprintf(`\A.*[/\\]%s-\d*/`, solution.Exercise)
rgxNumericSuffix := regexp.MustCompile(pattern)
if rgxNumericSuffix.MatchString(file) {
file = string(rgxNumericSuffix.ReplaceAll([]byte(file), []byte("")))
}

// Rewrite paths submitted with an older, buggy client where the Windows path is being treated as part of the filename.
file = strings.Replace(file, "\\", "/", -1)

Expand Down
12 changes: 11 additions & 1 deletion cmd/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ func fakeDownloadServer(requestor, teamSlug string) *httptest.Server {
fmt.Fprint(w, "this is file 2")
})

mux.HandleFunc("/full/path/with/numeric-suffix/bogus-track/bogus-exercise-12345/subdir/numeric.txt", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "with numeric suffix")
})

mux.HandleFunc("/special-char-filename#.txt", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "this is a special file")
})
Expand Down Expand Up @@ -217,6 +221,11 @@ func assertDownloadedCorrectFiles(t *testing.T, targetDir string) {
path: filepath.Join(targetDir, "bogus-track", "bogus-exercise", "subdir", "file-2.txt"),
contents: "this is file 2",
},
{
desc: "a path with a numeric suffix",
path: filepath.Join(targetDir, "bogus-track", "bogus-exercise", "subdir", "numeric.txt"),
contents: "with numeric suffix",
},
{
desc: "a file that requires URL encoding",
path: filepath.Join(targetDir, "bogus-track", "bogus-exercise", "special-char-filename#.txt"),
Expand Down Expand Up @@ -278,7 +287,8 @@ const payloadTemplate = `
"/with-leading-slash.txt",
"\\with-leading-backslash.txt",
"\\with\\backslashes\\in\\path.txt",
"file-3.txt"
"file-3.txt",
"/full/path/with/numeric-suffix/bogus-track/bogus-exercise-12345/subdir/numeric.txt"
],
"iteration": {
"submitted_at": "2017-08-21t10:11:12.130z"
Expand Down
3 changes: 0 additions & 3 deletions workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)

Expand All @@ -16,8 +15,6 @@ func IsMissingMetadata(err error) bool {
return err == errMissingMetadata
}

var rgxSerialSuffix = regexp.MustCompile(`-\d*$`)

// Workspace represents a user's Exercism workspace.
// It may contain a user's own exercises, and other people's
// exercises that they've downloaded to look at or run locally.
Expand Down

0 comments on commit e9877e2

Please sign in to comment.