Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for mfsr.go #3618

Merged
merged 4 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this because I noticed that the only caller of the method (https://github.com/Zanadar/go-ipfs/blob/77e46b40b1ec1e7799915c6dd1a4a38e9d741622/repo/fsrepo/fsrepo.go#L142) explicitly checks for os.IsNotExist(err) and when we transform the error here, that check in the caller never returns true.

On second consideration, it may be better to check in the caller for err == VersionFileNotFound as this error gives a little more context.

Hope that clarifies.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes sense then.

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.")
}