Added more unit tests; added Testify package to improve testing capabilities
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
This commit is contained in:
@ -1,19 +1,29 @@
|
||||
package util
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
func TestResolveTilde(t *testing.T) {
|
||||
// define our test suite struct
|
||||
type EnvTestSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
// the tilde should expand to user's home directory
|
||||
func (s *EnvTestSuite) TestResolveTilde() {
|
||||
resolvedPath := ResolveTilde("~")
|
||||
|
||||
if resolvedPath == "~" {
|
||||
t.Logf("ResolveTilde returned '%s'; it should have expanded to an absolute path.", resolvedPath)
|
||||
}
|
||||
assert.NotEqual(s.T(), resolvedPath, "~")
|
||||
}
|
||||
|
||||
func TestResolveTildePath(t *testing.T) {
|
||||
// ensure the tilde + relative path gets expanded fully
|
||||
func (s *EnvTestSuite) TestResolveTildePath() {
|
||||
resolvedPath := ResolveTilde("~/test")
|
||||
|
||||
if resolvedPath == "~/test" {
|
||||
t.Logf("ResolveTilde returned '%s'; it should have expanded to an absolute path.", resolvedPath)
|
||||
}
|
||||
assert.NotEqual(s.T(), resolvedPath, "~/test")
|
||||
}
|
||||
|
||||
// this is needed to run the test suite
|
||||
func TestEnvTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(EnvTestSuite))
|
||||
}
|
||||
|
@ -1,11 +1,63 @@
|
||||
package util
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
func TestFilenameWithoutExtension(t *testing.T) {
|
||||
filename := FilenameWithoutExtension("testfile.txt")
|
||||
// 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" {
|
||||
t.Logf("FilenameWithoutExtension returned '%s'; it should be 'testfile'.", filename)
|
||||
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)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
assert.True(s.T(), IsFileLocked(s.TestFile))
|
||||
}
|
||||
|
||||
// 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))
|
||||
}
|
||||
|
Reference in New Issue
Block a user