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

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	. "gopkg.in/check.v1"
     9  )
    10  
    11  type InitTests struct {
    12  	dir string
    13  	out *bytes.Buffer
    14  	d   *Dispatcher
    15  }
    16  
    17  var _ = Suite(&InitTests{})
    18  
    19  func (t *InitTests) SetUpTest(c *C) {
    20  	t.dir = c.MkDir()
    21  	t.out = new(bytes.Buffer)
    22  	t.d = &Dispatcher{stderr: t.out}
    23  }
    24  
    25  func (t *InitTests) TestCwd(c *C) {
    26  	oldpwd, err := os.Getwd()
    27  	c.Assert(err, IsNil)
    28  	os.Chdir(t.dir)
    29  	defer os.Chdir(oldpwd)
    30  	c.Assert(t.out.String(), Equals, "")
    31  	c.Assert(t.d.run([]string{"init"}), Equals, 0)
    32  	s, err := os.Stat(filepath.Join(t.dir, ".lara"))
    33  	c.Assert(err, IsNil)
    34  	c.Assert(s.IsDir(), Equals, true)
    35  }
    36  
    37  func (t *InitTests) TestOtherDir(c *C) {
    38  	path := filepath.Join(t.dir, "foo")
    39  	c.Assert(t.d.run([]string{"init", path}), Equals, 0)
    40  	s, err := os.Stat(filepath.Join(path, ".lara"))
    41  	c.Assert(err, IsNil)
    42  	c.Assert(s.IsDir(), Equals, true)
    43  }
    44  
    45  func (t *InitTests) TestOtherDirExisting(c *C) {
    46  	path := filepath.Join(t.dir, "foo")
    47  	err := os.Mkdir(path, 0700)
    48  	c.Assert(err, IsNil)
    49  	c.Assert(t.d.run([]string{"init", path}), Equals, 1)
    50  	_, err = os.Stat(filepath.Join(path, ".lara"))
    51  	c.Assert(err, Not(IsNil))
    52  }