diff --git a/repo/fsrepo/migrations/mfsr.go b/repo/fsrepo/migrations/mfsr.go index 3e9329f17ca..c0f3b8b4862 100644 --- a/repo/fsrepo/migrations/mfsr.go +++ b/repo/fsrepo/migrations/mfsr.go @@ -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) @@ -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 @@ -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) -} diff --git a/repo/fsrepo/migrations/mfsr_test.go b/repo/fsrepo/migrations/mfsr_test.go new file mode 100644 index 00000000000..0f66f71ce28 --- /dev/null +++ b/repo/fsrepo/migrations/mfsr_test.go @@ -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.") +}