Skip to content

Commit

Permalink
Support directory trees in assignments
Browse files Browse the repository at this point in the history
JVM languages require a particular directory structure, so this lets us
create idiomatic scala, clojure, etc exercises.
  • Loading branch information
kytrinyx committed Nov 9, 2013
1 parent 35a72b8 commit 371521f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
20 changes: 11 additions & 9 deletions assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)

type Assignment struct {
Expand All @@ -13,18 +14,19 @@ type Assignment struct {
}

func SaveAssignment(dir string, a Assignment) (err error) {
assignmentPath := fmt.Sprintf("%s/%s/%s", dir, a.Track, a.Slug)
err = os.MkdirAll(assignmentPath, 0744)
if err != nil {
err = fmt.Errorf("Error creating assignment directory: [%s]", err)
return
}
root := fmt.Sprintf("%s/%s/%s", dir, a.Track, a.Slug)

for name, text := range a.Files {
filePath := fmt.Sprintf("%s/%s", assignmentPath, name)
err = ioutil.WriteFile(filePath, []byte(text), 0644)
file := fmt.Sprintf("%s/%s", root, name)
dir := filepath.Dir(file)
err = os.MkdirAll(dir, 0755)
if err != nil {
err = fmt.Errorf("Error making directory %v: [%v]", dir, err)
return
}
err = ioutil.WriteFile(file, []byte(text), 0644)
if err != nil {
err = fmt.Errorf("Error writing %v file: [%v]", name, err)
err = fmt.Errorf("Error writing file %v: [%v]", name, err)
return
}
}
Expand Down
9 changes: 7 additions & 2 deletions assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ func TestSavingAssignment(t *testing.T) {
Track: "ruby",
Slug: "bob",
Files: map[string]string{
"bob_test.rb": "Tests text",
"README.md": "Readme text",
"bob_test.rb": "Tests text",
"README.md": "Readme text",
"/path/to/file.rb": "File text",
},
}

Expand All @@ -29,4 +30,8 @@ func TestSavingAssignment(t *testing.T) {
tests, err := ioutil.ReadFile(tmpDir + "/ruby/bob/bob_test.rb")
assert.NoError(t, err)
assert.Equal(t, string(tests), "Tests text")

fileInDir, err := ioutil.ReadFile(tmpDir + "/ruby/bob/path/to/file.rb")
assert.NoError(t, err)
assert.Equal(t, string(fileInDir), "File text")
}

0 comments on commit 371521f

Please sign in to comment.