github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/repository/clientRepositoryAdditem_test.go (about) 1 package repository 2 3 import ( 4 "io/ioutil" 5 "path/filepath" 6 "runtime" 7 8 "github.com/hoffie/larasync/helpers/path" 9 . "gopkg.in/check.v1" 10 ) 11 12 var _ = Suite(&RepositoryAddItemTests{}) 13 14 type RepositoryAddItemTests struct { 15 dir string 16 r *ClientRepository 17 } 18 19 func (t *RepositoryAddItemTests) SetUpTest(c *C) { 20 t.dir = c.MkDir() 21 t.r = NewClient(t.dir) 22 err := t.r.CreateManagementDir() 23 c.Assert(err, IsNil) 24 err = t.r.keys.CreateSigningKey() 25 c.Assert(err, IsNil) 26 27 err = t.r.keys.CreateEncryptionKey() 28 c.Assert(err, IsNil) 29 30 err = t.r.keys.CreateHashingKey() 31 c.Assert(err, IsNil) 32 } 33 34 func (t *RepositoryAddItemTests) TestWriteEmptyFile(c *C) { 35 fullpath := filepath.Join(t.dir, "foo.txt") 36 err := ioutil.WriteFile(fullpath, []byte{}, 0600) 37 c.Assert(err, IsNil) 38 err = t.r.AddItem(fullpath) 39 c.Assert(err, IsNil) 40 numFiles, err := path.NumFilesInDir(filepath.Join(t.dir, ".lara", "objects")) 41 c.Assert(err, IsNil) 42 c.Assert(numFiles, Equals, 2) 43 nibID, err := t.r.pathToNIBID("foo.txt") 44 c.Assert(err, IsNil) 45 nib, err := t.r.GetNIB(nibID) 46 c.Assert(err, IsNil) 47 rev, err := nib.LatestRevision() 48 c.Assert(err, IsNil) 49 c.Assert(len(rev.ContentIDs), Equals, 1) 50 } 51 52 func (t *RepositoryAddItemTests) TestWriteFileToChunks(c *C) { 53 fullpath := filepath.Join(t.dir, "foo.txt") 54 err := ioutil.WriteFile(fullpath, []byte("foo"), 0600) 55 c.Assert(err, IsNil) 56 numFiles, err := path.NumFilesInDir(filepath.Join(t.dir, ".lara", "objects")) 57 c.Assert(err, IsNil) 58 c.Assert(numFiles, Equals, 0) 59 err = t.r.AddItem(fullpath) 60 c.Assert(err, IsNil) 61 numFiles, err = path.NumFilesInDir(filepath.Join(t.dir, ".lara", "objects")) 62 c.Assert(err, IsNil) 63 c.Assert(numFiles, Equals, 2) 64 } 65 66 func (t *RepositoryAddItemTests) TestAddtoNIBStore(c *C) { 67 fullpath := filepath.Join(t.dir, "foo.txt") 68 err := ioutil.WriteFile(fullpath, []byte("foo"), 0600) 69 c.Assert(err, IsNil) 70 err = t.r.AddItem(fullpath) 71 c.Assert(err, IsNil) 72 tracker, err := t.r.NIBTracker() 73 c.Assert(err, IsNil) 74 d, err := tracker.Get("foo.txt") 75 c.Assert(err, IsNil) 76 c.Assert(d.Path, Equals, "foo.txt") 77 nibID, err := t.r.pathToNIBID("foo.txt") 78 c.Assert(err, IsNil) 79 c.Assert(d.NIBID, Equals, nibID) 80 } 81 82 // TestExistingFileNIBReuse ensures that pre-existing NIBs for a path are 83 // re-used upon updates. 84 func (t *RepositoryAddItemTests) TestExistingFileNIBReuse(c *C) { 85 nibsPath := filepath.Join(t.dir, ".lara", "nibs") 86 filename := "foo.txt" 87 fullpath := filepath.Join(t.dir, filename) 88 err := ioutil.WriteFile(fullpath, []byte("foo"), 0600) 89 c.Assert(err, IsNil) 90 91 numFiles, err := path.NumFilesInDir(nibsPath) 92 c.Assert(err, IsNil) 93 c.Assert(numFiles, Equals, 0) 94 95 err = t.r.AddItem(fullpath) 96 c.Assert(err, IsNil) 97 98 numFiles, err = path.NumFilesInDir(nibsPath) 99 c.Assert(err, IsNil) 100 c.Assert(numFiles, Equals, 1) 101 102 err = ioutil.WriteFile(fullpath, []byte("foo2"), 0600) 103 c.Assert(err, IsNil) 104 105 err = t.r.AddItem(fullpath) 106 c.Assert(err, IsNil) 107 108 numFiles, err = path.NumFilesInDir(nibsPath) 109 c.Assert(err, IsNil) 110 c.Assert(numFiles, Equals, 1) 111 112 nibID, err := t.r.pathToNIBID(filename) 113 c.Assert(err, IsNil) 114 nib, err := t.r.nibStore.Get(nibID) 115 c.Assert(err, IsNil) 116 c.Assert(len(nib.Revisions), Equals, 2) 117 c.Assert(nib.Revisions[0].UTCTimestamp, Not(Equals), int64(0)) 118 c.Assert(nib.Revisions[0].UTCTimestamp <= nib.Revisions[1].UTCTimestamp, 119 Equals, true) 120 } 121 122 // TestExistingFileNoChange ensures that no unnecessary updates 123 // are recorded. 124 func (t *RepositoryAddItemTests) TestExistingFileNoChange(c *C) { 125 nibsPath := filepath.Join(t.dir, ".lara", "nibs") 126 filename := "foo.txt" 127 fullpath := filepath.Join(t.dir, filename) 128 err := ioutil.WriteFile(fullpath, []byte("foo"), 0600) 129 c.Assert(err, IsNil) 130 131 numFiles, err := path.NumFilesInDir(nibsPath) 132 c.Assert(err, IsNil) 133 c.Assert(numFiles, Equals, 0) 134 135 err = t.r.AddItem(fullpath) 136 c.Assert(err, IsNil) 137 138 numFiles, err = path.NumFilesInDir(nibsPath) 139 c.Assert(err, IsNil) 140 c.Assert(numFiles, Equals, 1) 141 142 err = t.r.AddItem(fullpath) 143 c.Assert(err, IsNil) 144 145 numFiles, err = path.NumFilesInDir(nibsPath) 146 c.Assert(err, IsNil) 147 c.Assert(numFiles, Equals, 1) 148 149 nibID, err := t.r.pathToNIBID(filename) 150 c.Assert(err, IsNil) 151 nib, err := t.r.nibStore.Get(nibID) 152 c.Assert(err, IsNil) 153 c.Assert(len(nib.Revisions), Equals, 1) 154 } 155 156 func (t *RepositoryAddItemTests) TestAddDotLara(c *C) { 157 err := t.r.AddItem(filepath.Join(t.r.Path, managementDirName)) 158 c.Assert(err, Equals, ErrRefusingWorkOnDotLara) 159 } 160 161 func (t *RepositoryAddItemTests) TestAddDotLaraModified(c *C) { 162 path := string(filepath.Separator) + filepath.Join(t.r.Path, managementDirName) 163 164 err := t.r.AddItem(path) 165 if runtime.GOOS != "windows" { 166 c.Assert(err, Equals, ErrRefusingWorkOnDotLara) 167 } else { 168 c.Assert(err, NotNil) 169 } 170 } 171 172 func (t *RepositoryAddItemTests) TestAddDotLaraSubdir(c *C) { 173 path := filepath.Join(t.r.Path, managementDirName, nibsDirName) 174 err := t.r.AddItem(path) 175 c.Assert(err, Equals, ErrRefusingWorkOnDotLara) 176 }