adept/util/file_test.go

65 lines
1.6 KiB
Go
Raw Normal View History

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
2022-09-02 12:08:45 -04:00
TestFile string
}
// run before tests to set up
func (s *FileTestSuite) SetupSuite() {
2022-09-02 12:08:45 -04:00
s.TestFile = "testfile.txt"
// 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
}
// 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)
}
// test the filename extension removal works
func (s *FileTestSuite) TestFilenameWithoutExtension() {
2022-09-02 12:08:45 -04:00
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() {
2022-09-02 12:08:45 -04:00
file, err := os.Open(s.TestFile)
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 12:08:45 -04:00
assert.True(s.T(), IsFileLocked(s.TestFile))
file.Close()
}
// 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)
if err != nil {
2022-09-02 12:08:45 -04:00
s.T().Logf("Unable to open file %s: %s", s.TestFile, err)
}
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))
}
// this is needed to run the test suite
func TestFileTestSuite(t *testing.T) {
suite.Run(t, new(FileTestSuite))
}