Skip to content

Commit

Permalink
Merge pull request #3618 from Zanadar/test/msfr-test
Browse files Browse the repository at this point in the history
Tests for mfsr.go
  • Loading branch information
whyrusleeping authored Mar 2, 2017
2 parents 979ce56 + 310c08c commit 473f875
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
12 changes: 3 additions & 9 deletions repo/fsrepo/migrations/mfsr.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func (rp RepoPath) Version() (int, error) {
}

fn := rp.VersionFile()
if _, err := os.Stat(fn); os.IsNotExist(err) {
return 0, VersionFileNotFound(rp)
if _, err := os.Stat(fn); err != nil {
return 0, err
}

c, err := ioutil.ReadFile(fn)
Expand All @@ -43,7 +43,7 @@ func (rp RepoPath) CheckVersion(version int) error {
}

if v != version {
return fmt.Errorf("versions differ (expected: %s, actual:%s)", version, v)
return fmt.Errorf("versions differ (expected: %d, actual:%d)", version, v)
}

return nil
Expand All @@ -53,9 +53,3 @@ func (rp RepoPath) WriteVersion(version int) error {
fn := rp.VersionFile()
return ioutil.WriteFile(fn, []byte(fmt.Sprintf("%d\n", version)), 0644)
}

type VersionFileNotFound string

func (v VersionFileNotFound) Error() string {
return "no version file in repo at " + string(v)
}
43 changes: 43 additions & 0 deletions repo/fsrepo/migrations/mfsr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package mfsr

import (
"io/ioutil"
"os"
"strconv"
"testing"

"github.com/ipfs/go-ipfs/thirdparty/assert"
)

func testVersionFile(v string, t *testing.T) (rp RepoPath) {
name, err := ioutil.TempDir("", v)
if err != nil {
t.Fatal(err)
}
rp = RepoPath(name)
return rp
}

func TestVersion(t *testing.T) {
rp := RepoPath("")
_, err := rp.Version()
assert.Err(err, t, "Should throw an error when path is bad,")

rp = RepoPath("/path/to/nowhere")
_, err = rp.Version()
if !os.IsNotExist(err) {
t.Fatalf("Should throw an `IsNotExist` error when file doesn't exist: %v", err)
}

fsrepoV := 5

rp = testVersionFile(strconv.Itoa(fsrepoV), t)
_, err = rp.Version()
assert.Err(err, t, "Bad VersionFile")

assert.Nil(rp.WriteVersion(fsrepoV), t, "Trouble writing version")

assert.Nil(rp.CheckVersion(fsrepoV), t, "Trouble checking the verion")

assert.Err(rp.CheckVersion(1), t, "Should throw an error for the wrong version.")
}

0 comments on commit 473f875

Please sign in to comment.