github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/core/paths/transientfile/create_test.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  //go:build !windows
     5  
     6  package transientfile
     7  
     8  import (
     9  	"os"
    10  	"path/filepath"
    11  
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  )
    15  
    16  type transientFileSuite struct{}
    17  
    18  var _ = gc.Suite(&transientFileSuite{})
    19  
    20  func (s *transientFileSuite) TestCreateTransientFile(c *gc.C) {
    21  	transientDir := c.MkDir()
    22  	f, err := Create(transientDir, "foo.test")
    23  	c.Assert(err, jc.ErrorIsNil)
    24  	c.Assert(f.Close(), jc.ErrorIsNil)
    25  
    26  	expPath := filepath.Join(transientDir, "foo.test")
    27  	s.assertFileExists(c, expPath)
    28  }
    29  
    30  func (s *transientFileSuite) TestCreateTransientFileInSubdirectory(c *gc.C) {
    31  	transientDir := c.MkDir()
    32  	f, err := Create(transientDir, filepath.Join("1", "2", "foo.test"))
    33  	c.Assert(err, jc.ErrorIsNil)
    34  	c.Assert(f.Close(), jc.ErrorIsNil)
    35  
    36  	expPath := filepath.Join(transientDir, "1", "2", "foo.test")
    37  	s.assertFileExists(c, expPath)
    38  }
    39  
    40  func (*transientFileSuite) assertFileExists(c *gc.C, path string) {
    41  	stat, err := os.Stat(path)
    42  	c.Assert(err, jc.ErrorIsNil, gc.Commentf("stat failed for %q", path))
    43  
    44  	c.Assert(stat.IsDir(), jc.IsFalse, gc.Commentf("%q seems to be a directory", path))
    45  }