2022-09-01 14:25:02 -04:00
|
|
|
package util
|
|
|
|
|
2022-09-02 00:55:47 -04:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
2022-09-01 14:25:02 -04:00
|
|
|
|
2022-09-02 00:55:47 -04:00
|
|
|
// define our test suite struct
|
|
|
|
type FileTestSuite struct {
|
|
|
|
suite.Suite
|
2022-09-02 12:08:45 -04:00
|
|
|
TestFile string
|
2022-09-02 00:55:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// run before tests to set up
|
|
|
|
func (s *FileTestSuite) SetupSuite() {
|
2022-09-02 12:08:45 -04:00
|
|
|
s.TestFile = "testfile.txt"
|
2022-09-02 00:55:47 -04:00
|
|
|
// create the test file for file lock testing
|
2022-09-02 12:08:45 -04:00
|
|
|
file, _ := os.Create(s.TestFile)
|
|
|
|
file.Close() // do this just to make extra sure the file handle is closed
|
2022-09-02 00:55:47 -04:00
|
|
|
}
|
|
|
|
// run after tests to clean up
|
|
|
|
func (s *FileTestSuite) TearDownSuite() {
|
|
|
|
// remove the test file since it's no longer needed
|
2022-09-02 12:08:45 -04:00
|
|
|
os.Remove(s.TestFile)
|
2022-09-02 00:55:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// test the filename extension removal works
|
|
|
|
func (s *FileTestSuite) TestFilenameWithoutExtension() {
|
2022-09-02 12:08:45 -04:00
|
|
|
filename := FilenameWithoutExtension(s.TestFile)
|
2022-09-01 14:25:02 -04:00
|
|
|
|
|
|
|
if filename != "testfile" {
|
2022-09-02 00:55:47 -04:00
|
|
|
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() {
|
2022-09-02 12:08:45 -04:00
|
|
|
file, err := os.Open(s.TestFile)
|
2022-09-02 00:55:47 -04:00
|
|
|
if err != nil {
|
2022-09-02 12:08:45 -04:00
|
|
|
s.T().Logf("Unable to open file %s: %s", s.TestFile, err)
|
2022-09-01 14:25:02 -04:00
|
|
|
}
|
2022-09-02 00:55:47 -04:00
|
|
|
|
2022-09-02 12:08:45 -04:00
|
|
|
assert.True(s.T(), IsFileLocked(s.TestFile))
|
2022-09-02 01:57:05 -04:00
|
|
|
|
|
|
|
file.Close()
|
2022-09-02 00:55:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// test that IsFileLocked returns false when the file is not active
|
|
|
|
func (s *FileTestSuite) TestFileShouldNotBeLocked() {
|
2022-09-02 12:08:45 -04:00
|
|
|
file, err := os.Open(s.TestFile)
|
2022-09-02 00:55:47 -04:00
|
|
|
if err != nil {
|
2022-09-02 12:08:45 -04:00
|
|
|
s.T().Logf("Unable to open file %s: %s", s.TestFile, err)
|
2022-09-02 00:55:47 -04:00
|
|
|
}
|
|
|
|
file.Close() // we want this closed now so it's NOT open!
|
|
|
|
|
2022-09-02 12:08:45 -04:00
|
|
|
assert.False(s.T(), IsFileLocked(s.TestFile))
|
2022-09-02 00:55:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// this is needed to run the test suite
|
|
|
|
func TestFileTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(FileTestSuite))
|
2022-09-01 14:25:02 -04:00
|
|
|
}
|