Skip to content

Commit e203cc3

Browse files
author
Joe Previte
committed
fixup!: set mtime to 0 and check for update
1 parent b14024f commit e203cc3

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

test/unit/node/heart.test.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { logger } from "@coder/logger"
2-
import { readFile, writeFile, stat } from "fs/promises"
2+
import { readFile, writeFile, stat, open } from "fs/promises"
33
import { Heart, heartbeatTimer } from "../../../src/node/heart"
44
import { clean, mockLogger, tmpdir } from "../../utils/helpers"
55

@@ -33,7 +33,12 @@ describe("Heart", () => {
3333
const pathToFile = `${testDir}/file.txt`
3434
await writeFile(pathToFile, text)
3535
const fileContents = await readFile(pathToFile, { encoding: "utf8" })
36-
const fileStatusBeforeEdit = await stat(pathToFile)
36+
// Explicitly set the modified time to 0 so that we can check
37+
// that the file was indeed modified after calling heart.beat().
38+
// This works around any potential race conditions.
39+
const fileHandle = await open(pathToFile, "r+")
40+
await fileHandle.utimes(0, 0)
41+
3742
expect(fileContents).toBe(text)
3843

3944
heart = new Heart(pathToFile, mockIsActive(true))
@@ -47,7 +52,7 @@ describe("Heart", () => {
4752
expect(fileContentsAfterBeat).not.toBe(text)
4853
// Make sure the modified timestamp was updated.
4954
const fileStatusAfterEdit = await stat(pathToFile)
50-
expect(fileStatusAfterEdit.mtimeMs).toBeGreaterThanOrEqual(fileStatusBeforeEdit.mtimeMs)
55+
expect(fileStatusAfterEdit.mtimeMs).toBeGreaterThan(0)
5156
})
5257
it("should log a warning when given an invalid file path", async () => {
5358
heart = new Heart(`fakeDir/fake.txt`, mockIsActive(false))

0 commit comments

Comments
 (0)