package util import ( "os" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) // define our test suite struct type FileTestSuite struct { suite.Suite } // run before tests to set up func (s *FileTestSuite) SetupSuite() { // create the test file for file lock testing file1, _ := os.Create("testfile-locked.txt") file1.Close() // do this just to make extra sure the file handle is closed file2, _ := os.Create("testfile-unlocked.txt") file2.Close() } // run after tests to clean up func (s *FileTestSuite) TearDownSuite() { // remove the test file since it's no longer needed os.Remove("testfile-locked.txt") os.Remove("testfile-unlocked.txt") } // test the filename extension removal works func (s *FileTestSuite) TestFilenameWithoutExtension() { filename := FilenameWithoutExtension("testfile.txt") if filename != "testfile" { s.T().Logf("FilenameWithoutExtension returned '%s'; it should be 'testfile'.", filename) } } // test that IsFileLocked returns true when the file is active func (s *FileTestSuite) TestFileShouldBeLocked() { file, err := os.Open("testfile-locked.txt") if err != nil { s.T().Logf("Unable to open file %s: %s", "testfile-locked.txt", err) } assert.True(s.T(), IsFileLocked("testfile-locked.txt")) file.Close() } // test that IsFileLocked returns false when the file is not active func (s *FileTestSuite) TestFileShouldNotBeLocked() { file, err := os.Open("testfile-unlocked.txt") if err != nil { s.T().Logf("Unable to open file %s: %s", "testfile-unlocked.txt", err) } file.Close() // we want this closed now so it's NOT open! assert.False(s.T(), IsFileLocked("testfile-unlocked.txt")) } // this is needed to run the test suite func TestFileTestSuite(t *testing.T) { suite.Run(t, new(FileTestSuite)) }