Fixed the FileShouldBeLocked test for CI
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Gregory Ballantine 2022-09-02 01:56:04 -04:00
parent 925ad42cb1
commit af65a9a92f

View File

@ -10,25 +10,26 @@ import (
// define our test suite struct // define our test suite struct
type FileTestSuite struct { type FileTestSuite struct {
suite.Suite suite.Suite
TestFile string
} }
// run before tests to set up // run before tests to set up
func (s *FileTestSuite) SetupSuite() { func (s *FileTestSuite) SetupSuite() {
s.TestFile = "testfile.txt"
// create the test file for file lock testing // create the test file for file lock testing
file, _ := os.Create(s.TestFile) file1, _ := os.Create("testfile-locked.txt")
file.Close() // do this just to make extra sure the file handle is closed 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 // run after tests to clean up
func (s *FileTestSuite) TearDownSuite() { func (s *FileTestSuite) TearDownSuite() {
// remove the test file since it's no longer needed // remove the test file since it's no longer needed
os.Remove(s.TestFile) os.Remove("testfile-locked.txt")
os.Remove("testfile-unlocked.txt")
} }
// test the filename extension removal works // test the filename extension removal works
func (s *FileTestSuite) TestFilenameWithoutExtension() { func (s *FileTestSuite) TestFilenameWithoutExtension() {
filename := FilenameWithoutExtension(s.TestFile) filename := FilenameWithoutExtension("testfile.txt")
if filename != "testfile" { if filename != "testfile" {
s.T().Logf("FilenameWithoutExtension returned '%s'; it should be 'testfile'.", filename) s.T().Logf("FilenameWithoutExtension returned '%s'; it should be 'testfile'.", filename)
@ -37,24 +38,24 @@ func (s *FileTestSuite) TestFilenameWithoutExtension() {
// test that IsFileLocked returns true when the file is active // test that IsFileLocked returns true when the file is active
func (s *FileTestSuite) TestFileShouldBeLocked() { func (s *FileTestSuite) TestFileShouldBeLocked() {
file, err := os.Open(s.TestFile) file, err := os.Open("testfile-locked.txt")
if err != nil { if err != nil {
s.T().Logf("Unable to open file %s: %s", s.TestFile, err) s.T().Logf("Unable to open file %s: %s", "testfile-locked.txt", err)
} }
defer file.Close() defer file.Close()
assert.True(s.T(), IsFileLocked(s.TestFile)) assert.True(s.T(), IsFileLocked("testfile-locked.txt"))
} }
// test that IsFileLocked returns false when the file is not active // test that IsFileLocked returns false when the file is not active
func (s *FileTestSuite) TestFileShouldNotBeLocked() { func (s *FileTestSuite) TestFileShouldNotBeLocked() {
file, err := os.Open(s.TestFile) file, err := os.Open("testfile-unlocked.txt")
if err != nil { if err != nil {
s.T().Logf("Unable to open file %s: %s", s.TestFile, err) 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! file.Close() // we want this closed now so it's NOT open!
assert.False(s.T(), IsFileLocked(s.TestFile)) assert.False(s.T(), IsFileLocked("testfile-unlocked.txt"))
} }
// this is needed to run the test suite // this is needed to run the test suite