Skip to content

Commit

Permalink
Do not overwrite existing files.
Browse files Browse the repository at this point in the history
  • Loading branch information
kytrinyx committed Dec 1, 2013
1 parent dfe5fe8 commit 2f7cae4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
12 changes: 8 additions & 4 deletions assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ func SaveAssignment(dir string, a Assignment) (err error) {
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 file %v: [%v]", name, err)
return
if _, err = os.Stat(file); err != nil {
if os.IsNotExist(err) {
err = ioutil.WriteFile(file, []byte(text), 0644)
if err != nil {
err = fmt.Errorf("Error writing file %v: [%v]", name, err)
return
}
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions assignment_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package main

import (
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

func TestSavingAssignment(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "")
assert.NoError(t, err)

prepareFixture(t, fmt.Sprintf("%s/ruby/bob/stub.rb", tmpDir), "Existing stub")

assignment := Assignment{
Track: "ruby",
Slug: "bob",
Files: map[string]string{
"bob_test.rb": "Tests text",
"README.md": "Readme text",
"path/to/file.rb": "File text",
"stub.rb": "New version of stub",
},
}

Expand All @@ -34,4 +40,21 @@ func TestSavingAssignment(t *testing.T) {
fileInDir, err := ioutil.ReadFile(tmpDir + "/ruby/bob/path/to/file.rb")
assert.NoError(t, err)
assert.Equal(t, string(fileInDir), "File text")

stubFile, err := ioutil.ReadFile(tmpDir + "/ruby/bob/stub.rb")
assert.NoError(t, err)
assert.Equal(t, string(stubFile), "Existing stub")
}

func prepareFixture(t *testing.T, fixture, s string) {
err := os.MkdirAll(filepath.Dir(fixture), 0755)
assert.NoError(t, err)

err = ioutil.WriteFile(fixture, []byte(s), 0644)
assert.NoError(t, err)

// ensure fixture is set up correctly
fixtureContents, err := ioutil.ReadFile(fixture)
assert.NoError(t, err)
assert.Equal(t, string(fixtureContents), s)
}

0 comments on commit 2f7cae4

Please sign in to comment.