github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/repository/clientRepositoryCheckout_test.go (about)

     1  package repository
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/hoffie/larasync/repository/nib"
     9  	. "gopkg.in/check.v1"
    10  )
    11  
    12  var _ = Suite(&RepositoryCheckoutTests{})
    13  
    14  type RepositoryCheckoutTests struct {
    15  	dir      string
    16  	r        *ClientRepository
    17  	testData []byte
    18  	fullPath string
    19  }
    20  
    21  func (t *RepositoryCheckoutTests) SetUpTest(c *C) {
    22  	t.dir = c.MkDir()
    23  	t.r = NewClient(t.dir)
    24  	err := t.r.CreateManagementDir()
    25  	c.Assert(err, IsNil)
    26  	err = t.r.keys.CreateSigningKey()
    27  	c.Assert(err, IsNil)
    28  
    29  	err = t.r.keys.CreateEncryptionKey()
    30  	c.Assert(err, IsNil)
    31  
    32  	err = t.r.keys.CreateHashingKey()
    33  	c.Assert(err, IsNil)
    34  
    35  	t.testData = []byte("foo")
    36  	t.fullPath = filepath.Join(t.dir, "foo.txt")
    37  }
    38  
    39  // TestRemoveFile verifies if empty content and metadata ids
    40  // are being removed.
    41  func (t *RepositoryCheckoutTests) TestRemoveFile(c *C) {
    42  	t.addTestFile(c)
    43  	fullPath := t.fullPath
    44  
    45  	metadataID, err := t.r.writeMetadata(fullPath)
    46  	c.Assert(err, IsNil)
    47  
    48  	n := &nib.NIB{
    49  		ID: "",
    50  		Revisions: []*nib.Revision{
    51  			&nib.Revision{
    52  				MetadataID: metadataID,
    53  				ContentIDs: []string{},
    54  			},
    55  		},
    56  	}
    57  
    58  	err = t.r.checkoutNIB(n)
    59  	c.Assert(err, IsNil)
    60  
    61  	_, err = os.Stat(fullPath)
    62  	c.Assert(err, NotNil)
    63  	c.Assert(os.IsNotExist(err), Equals, true)
    64  }
    65  
    66  func (t *RepositoryCheckoutTests) addTestFile(c *C) {
    67  	err := ioutil.WriteFile(t.fullPath, t.testData, 0600)
    68  	c.Assert(err, IsNil)
    69  }
    70  
    71  func (t *RepositoryCheckoutTests) TestAddFile(c *C) {
    72  	t.addTestFile(c)
    73  	fullPath := t.fullPath
    74  	data := t.testData
    75  
    76  	err := t.r.AddItem(fullPath)
    77  	c.Assert(err, IsNil)
    78  
    79  	err = os.Remove(fullPath)
    80  	c.Assert(err, IsNil)
    81  
    82  	err = t.r.CheckoutPath(fullPath)
    83  	c.Assert(err, IsNil)
    84  
    85  	readData, err := ioutil.ReadFile(fullPath)
    86  	c.Assert(err, IsNil)
    87  
    88  	c.Assert(data, DeepEquals, readData)
    89  }
    90  
    91  func (t *RepositoryCheckoutTests) TestModifyFileWorkdirConflict(c *C) {
    92  	t.addTestFile(c)
    93  
    94  	err := t.r.AddItem(t.fullPath)
    95  	c.Assert(err, IsNil)
    96  
    97  	err = ioutil.WriteFile(t.fullPath, []byte("overwrittenstuff"), 0600)
    98  	c.Assert(err, IsNil)
    99  
   100  	err = t.r.CheckoutPath(t.fullPath)
   101  	c.Assert(err, Equals, ErrWorkDirConflict)
   102  }