github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/testing/checkers/file_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package checkers_test
     5  
     6  import (
     7  	"fmt"
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  
    12  	gc "launchpad.net/gocheck"
    13  
    14  	jc "launchpad.net/juju-core/testing/checkers"
    15      "launchpad.net/juju-core/utils"
    16  )
    17  
    18  type FileSuite struct{}
    19  
    20  var _ = gc.Suite(&FileSuite{})
    21  
    22  func (s *FileSuite) TestIsNonEmptyFile(c *gc.C) {
    23  	file, err := ioutil.TempFile(c.MkDir(), "")
    24  	c.Assert(err, gc.IsNil)
    25  	fmt.Fprintf(file, "something")
    26  	file.Close()
    27  
    28  	c.Assert(file.Name(), jc.IsNonEmptyFile)
    29  }
    30  
    31  func (s *FileSuite) TestIsNonEmptyFileWithEmptyFile(c *gc.C) {
    32  	file, err := ioutil.TempFile(c.MkDir(), "")
    33  	c.Assert(err, gc.IsNil)
    34  	file.Close()
    35  
    36  	result, message := jc.IsNonEmptyFile.Check([]interface{}{file.Name()}, nil)
    37  	c.Assert(result, jc.IsFalse)
    38  	c.Assert(message, gc.Equals, file.Name()+" is empty")
    39  }
    40  
    41  func (s *FileSuite) TestIsNonEmptyFileWithMissingFile(c *gc.C) {
    42  	name := filepath.Join(c.MkDir(), "missing")
    43  
    44  	result, message := jc.IsNonEmptyFile.Check([]interface{}{name}, nil)
    45  	c.Assert(result, jc.IsFalse)
    46  	c.Assert(message, gc.Equals, name+" does not exist")
    47  }
    48  
    49  func (s *FileSuite) TestIsNonEmptyFileWithNumber(c *gc.C) {
    50  	result, message := jc.IsNonEmptyFile.Check([]interface{}{42}, nil)
    51  	c.Assert(result, jc.IsFalse)
    52  	c.Assert(message, gc.Equals, "obtained value is not a string and has no .String(), int:42")
    53  }
    54  
    55  func (s *FileSuite) TestIsDirectory(c *gc.C) {
    56  	dir := c.MkDir()
    57  	c.Assert(dir, jc.IsDirectory)
    58  }
    59  
    60  func (s *FileSuite) TestIsDirectoryMissing(c *gc.C) {
    61  	absentDir := filepath.Join(c.MkDir(), "foo")
    62  
    63  	result, message := jc.IsDirectory.Check([]interface{}{absentDir}, nil)
    64  	c.Assert(result, jc.IsFalse)
    65  	c.Assert(message, gc.Equals, absentDir+" does not exist")
    66  }
    67  
    68  func (s *FileSuite) TestIsDirectoryWithFile(c *gc.C) {
    69  	file, err := ioutil.TempFile(c.MkDir(), "")
    70  	c.Assert(err, gc.IsNil)
    71  	file.Close()
    72  
    73  	result, message := jc.IsDirectory.Check([]interface{}{file.Name()}, nil)
    74  	c.Assert(result, jc.IsFalse)
    75  	c.Assert(message, gc.Equals, file.Name()+" is not a directory")
    76  }
    77  
    78  func (s *FileSuite) TestIsDirectoryWithNumber(c *gc.C) {
    79  	result, message := jc.IsDirectory.Check([]interface{}{42}, nil)
    80  	c.Assert(result, jc.IsFalse)
    81  	c.Assert(message, gc.Equals, "obtained value is not a string and has no .String(), int:42")
    82  }
    83  
    84  func (s *FileSuite) TestDoesNotExist(c *gc.C) {
    85  	absentDir := filepath.Join(c.MkDir(), "foo")
    86  	c.Assert(absentDir, jc.DoesNotExist)
    87  }
    88  
    89  func (s *FileSuite) TestDoesNotExistWithPath(c *gc.C) {
    90  	dir := c.MkDir()
    91  	result, message := jc.DoesNotExist.Check([]interface{}{dir}, nil)
    92  	c.Assert(result, jc.IsFalse)
    93  	c.Assert(message, gc.Equals, dir+" exists")
    94  }
    95  
    96  func (s *FileSuite) TestDoesNotExistWithSymlink(c *gc.C) {
    97  	dir := c.MkDir()
    98  	deadPath := filepath.Join(dir, "dead")
    99  	symlinkPath := filepath.Join(dir, "a-symlink")
   100  	err := utils.Symlink(deadPath, symlinkPath)
   101  	c.Assert(err, gc.IsNil)
   102  	// A valid symlink pointing to something that doesn't exist passes.
   103  	// Use SymlinkDoesNotExist to check for the non-existence of the link itself.
   104  	c.Assert(symlinkPath, jc.DoesNotExist)
   105  }
   106  
   107  func (s *FileSuite) TestDoesNotExistWithNumber(c *gc.C) {
   108  	result, message := jc.DoesNotExist.Check([]interface{}{42}, nil)
   109  	c.Assert(result, jc.IsFalse)
   110  	c.Assert(message, gc.Equals, "obtained value is not a string and has no .String(), int:42")
   111  }
   112  
   113  func (s *FileSuite) TestSymlinkDoesNotExist(c *gc.C) {
   114  	absentDir := filepath.Join(c.MkDir(), "foo")
   115  	c.Assert(absentDir, jc.SymlinkDoesNotExist)
   116  }
   117  
   118  func (s *FileSuite) TestSymlinkDoesNotExistWithPath(c *gc.C) {
   119  	dir := c.MkDir()
   120  	result, message := jc.SymlinkDoesNotExist.Check([]interface{}{dir}, nil)
   121  	c.Assert(result, jc.IsFalse)
   122  	c.Assert(message, gc.Equals, dir+" exists")
   123  }
   124  
   125  func (s *FileSuite) TestSymlinkDoesNotExistWithSymlink(c *gc.C) {
   126  	dir := c.MkDir()
   127  	deadPath := filepath.Join(dir, "dead")
   128  	symlinkPath := filepath.Join(dir, "a-symlink")
   129  	err := utils.Symlink(deadPath, symlinkPath)
   130  	c.Assert(err, gc.IsNil)
   131  
   132  	result, message := jc.SymlinkDoesNotExist.Check([]interface{}{symlinkPath}, nil)
   133  	c.Assert(result, jc.IsFalse)
   134  	c.Assert(message, gc.Equals, symlinkPath+" exists")
   135  }
   136  
   137  func (s *FileSuite) TestSymlinkDoesNotExistWithNumber(c *gc.C) {
   138  	result, message := jc.SymlinkDoesNotExist.Check([]interface{}{42}, nil)
   139  	c.Assert(result, jc.IsFalse)
   140  	c.Assert(message, gc.Equals, "obtained value is not a string and has no .String(), int:42")
   141  }
   142  
   143  func (s *FileSuite) TestIsSymlink(c *gc.C) {
   144  	file, err := ioutil.TempFile(c.MkDir(), "")
   145  	c.Assert(err, gc.IsNil)
   146  	c.Log(file.Name())
   147  	c.Log(filepath.Dir(file.Name()))
   148  	symlinkPath := filepath.Join(filepath.Dir(file.Name()), "a-symlink")
   149  	err = utils.Symlink(file.Name(), symlinkPath)
   150  	c.Assert(err, gc.IsNil)
   151  
   152  	c.Assert(symlinkPath, jc.IsSymlink)
   153  }
   154  
   155  func (s *FileSuite) TestIsSymlinkWithFile(c *gc.C) {
   156  	file, err := ioutil.TempFile(c.MkDir(), "")
   157  	c.Assert(err, gc.IsNil)
   158  	result, message := jc.IsSymlink.Check([]interface{}{file.Name()}, nil)
   159  	c.Assert(result, jc.IsFalse)
   160  	c.Assert(message, jc.Contains, " is not a symlink")
   161  }
   162  
   163  func (s *FileSuite) TestIsSymlinkWithDir(c *gc.C) {
   164  	result, message := jc.IsSymlink.Check([]interface{}{c.MkDir()}, nil)
   165  	c.Assert(result, jc.IsFalse)
   166  	c.Assert(message, jc.Contains, " is not a symlink")
   167  }