Skip to content

Commit

Permalink
add openExistingOrNew unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Arjun Raja Yogidas <[email protected]>
  • Loading branch information
coderbirju committed Nov 19, 2024
1 parent 6264d56 commit b73721a
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions logrotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,55 @@ func TestOpenExisting(t *testing.T) {
fileCount(dir, 1, t)
}

func TestOpenExistingOrNew(t *testing.T) {
dir := makeTempDir("TestOpenExistingOrNew", t)
defer os.RemoveAll(dir)

filename := logFile(dir)
l := &Logger{
Filename: filename,
MaxBytes: 100,
}

// File doesn't exist
err := l.openExistingOrNew()
isNil(err, t)
assert(l.file != nil, t, "Expected file to be opened")
equals(int64(0), l.size, t)

content := []byte("Hello, World!")
_, err = l.file.Write(content)
isNil(err, t)
l.size = int64(len(content))

// Close
err = l.file.Close()
isNil(err, t)
l.file = nil

// File exists and is not over MaxBytes
err = l.openExistingOrNew()
isNil(err, t)
assert(l.file != nil, t, "Expected file to be opened")
equals(int64(len(content)), l.size, t)

// Close
err = l.file.Close()
isNil(err, t)
l.file = nil

// File exists and is over MaxBytes
l.MaxBytes = 10 // Set MaxBytes to a value less than the file size
err = l.openExistingOrNew()
isNil(err, t)
assert(l.file != nil, t, "Expected file to be opened")
equals(int64(0), l.size, t) // New file should be empty

// Clean up
err = l.file.Close()
isNil(err, t)
}

func TestWriteTooLong(t *testing.T) {
currentTime = fakeTime
dir := makeTempDir("TestWriteTooLong", t)
Expand Down

0 comments on commit b73721a

Please sign in to comment.