65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
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
|
|
TestFile string
|
|
}
|
|
|
|
// run before tests to set up
|
|
func (s *FileTestSuite) SetupSuite() {
|
|
s.TestFile = "testfile.txt"
|
|
// create the test file for file lock testing
|
|
file, _ := os.Create(s.TestFile)
|
|
file.Close() // do this just to make extra sure the file handle is closed
|
|
}
|
|
// run after tests to clean up
|
|
func (s *FileTestSuite) TearDownSuite() {
|
|
// remove the test file since it's no longer needed
|
|
os.Remove(s.TestFile)
|
|
}
|
|
|
|
// test the filename extension removal works
|
|
func (s *FileTestSuite) TestFilenameWithoutExtension() {
|
|
filename := FilenameWithoutExtension(s.TestFile)
|
|
|
|
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(s.TestFile)
|
|
if err != nil {
|
|
s.T().Logf("Unable to open file %s: %s", s.TestFile, err)
|
|
}
|
|
|
|
assert.True(s.T(), IsFileLocked(s.TestFile))
|
|
|
|
file.Close()
|
|
}
|
|
|
|
// test that IsFileLocked returns false when the file is not active
|
|
func (s *FileTestSuite) TestFileShouldNotBeLocked() {
|
|
file, err := os.Open(s.TestFile)
|
|
if err != nil {
|
|
s.T().Logf("Unable to open file %s: %s", s.TestFile, err)
|
|
}
|
|
file.Close() // we want this closed now so it's NOT open!
|
|
|
|
assert.False(s.T(), IsFileLocked(s.TestFile))
|
|
}
|
|
|
|
// this is needed to run the test suite
|
|
func TestFileTestSuite(t *testing.T) {
|
|
suite.Run(t, new(FileTestSuite))
|
|
}
|