github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/cmd/lara/pull_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	. "gopkg.in/check.v1"
    10  )
    11  
    12  type PullTests struct {
    13  	BaseTests
    14  	repoDir     string
    15  	repoName    string
    16  	testFile    string
    17  	testContent []byte
    18  }
    19  
    20  var _ = Suite(&PullTests{BaseTests: BaseTests{}})
    21  
    22  func (t *PullTests) TestTooManyArgs(c *C) {
    23  	c.Assert(t.d.run([]string{"pull", "foo"}), Equals, 1)
    24  }
    25  
    26  func (t *PullTests) initializeRepository(c *C) {
    27  	t.repoDir = "repo"
    28  	t.runAndExpectCode(c, []string{"init", t.repoDir}, 0)
    29  	err := os.Chdir(t.repoDir)
    30  	c.Assert(err, IsNil)
    31  
    32  	t.repoName = "example"
    33  	url := t.ts.hostAndPort
    34  	t.in.Write(t.ts.adminSecret)
    35  	t.in.WriteString("\n")
    36  	t.in.WriteString("y\n") // accept fingerprint
    37  	t.runAndExpectCode(c, []string{"register", url, t.repoName}, 0)
    38  
    39  	t.testFile = "foo.txt"
    40  	t.testContent = []byte("Sync works")
    41  	err = ioutil.WriteFile(t.testFile, t.testContent, 0600)
    42  	c.Assert(err, IsNil)
    43  
    44  	t.runAndExpectCode(c, []string{"add", t.testFile}, 0)
    45  
    46  	t.runAndExpectCode(c, []string{"push"}, 0)
    47  	err = os.Remove(t.testFile)
    48  	c.Assert(err, IsNil)
    49  
    50  	err = removeFilesInDir(filepath.Join(".lara", "objects"))
    51  	c.Assert(err, IsNil)
    52  
    53  	err = removeFilesInDir(filepath.Join(".lara", "nibs"))
    54  	c.Assert(err, IsNil)
    55  
    56  	t.runAndExpectCode(c, []string{"checkout"}, 0)
    57  	_, err = os.Stat(t.testFile)
    58  	c.Assert(os.IsNotExist(err), Equals, true)
    59  }
    60  
    61  func (t *PullTests) verifyExpectedDataStructure(c *C) {
    62  	_, err := os.Stat(t.testFile)
    63  	c.Assert(os.IsNotExist(err), Equals, true)
    64  
    65  	t.runAndExpectCode(c, []string{"checkout"}, 0)
    66  	content, err := ioutil.ReadFile(t.testFile)
    67  	c.Assert(err, IsNil)
    68  	c.Assert(content, DeepEquals, t.testContent)
    69  }
    70  
    71  func (t *PullTests) TestPull(c *C) {
    72  	t.initializeRepository(c)
    73  
    74  	t.d.run([]string{"pull"})
    75  	d, _ := ioutil.ReadAll(t.err)
    76  	fmt.Println(string(d))
    77  	t.runAndExpectCode(c, []string{"pull"}, 0)
    78  
    79  	t.verifyExpectedDataStructure(c)
    80  }
    81  
    82  func (t *PullTests) TestPullFull(c *C) {
    83  	t.initializeRepository(c)
    84  	t.runAndExpectCode(c, []string{"pull", "--full"}, 0)
    85  	t.verifyExpectedDataStructure(c)
    86  }