1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 19:24:14 +08:00

Merge pull request #3618 from Zanadar/test/msfr-test

Tests for mfsr.go
This commit is contained in:
Jeromy Johnson
2017-03-01 17:40:59 -08:00
committed by GitHub
2 changed files with 46 additions and 9 deletions

View File

@ -23,8 +23,8 @@ func (rp RepoPath) Version() (int, error) {
} }
fn := rp.VersionFile() fn := rp.VersionFile()
if _, err := os.Stat(fn); os.IsNotExist(err) { if _, err := os.Stat(fn); err != nil {
return 0, VersionFileNotFound(rp) return 0, err
} }
c, err := ioutil.ReadFile(fn) c, err := ioutil.ReadFile(fn)
@ -43,7 +43,7 @@ func (rp RepoPath) CheckVersion(version int) error {
} }
if v != version { 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 return nil
@ -53,9 +53,3 @@ func (rp RepoPath) WriteVersion(version int) error {
fn := rp.VersionFile() fn := rp.VersionFile()
return ioutil.WriteFile(fn, []byte(fmt.Sprintf("%d\n", version)), 0644) 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)
}

View File

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