Skip to content

Commit e481a96

Browse files
committed
fix(tests): update database location assertions to use fs.realpathSync on macOS and rm with retry on windows
1 parent 635e9c2 commit e481a96

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

test/backup.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe("Backup functionality", () => {
6262
retryDelay: 500,
6363
});
6464
}
65-
});
65+
}, 10000); // Increase timeout to 10 seconds
6666

6767
it("should create a backup of the database", async () => {
6868
// Perform backup

test/database.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ describe("File-based Database Tests", () => {
181181

182182
expect(db).toBeInstanceOf(DatabaseSync);
183183
expect(db.isOpen).toBe(true);
184-
expect(db.location()).toBe(dbPath);
184+
expect(db.location()).toBe(fs.realpathSync(dbPath));
185185
expect(fs.existsSync(dbPath)).toBe(true);
186186

187187
db.close();
@@ -292,7 +292,7 @@ describe("File-based Database Tests", () => {
292292
test("database location property reflects actual path", () => {
293293
const db = new DatabaseSync(dbPath);
294294

295-
expect(db.location()).toBe(dbPath);
295+
expect(db.location()).toBe(fs.realpathSync(dbPath));
296296

297297
db.close();
298298
});
@@ -348,7 +348,7 @@ describe("Database Configuration Tests", () => {
348348
});
349349

350350
expect(db.isOpen).toBe(true);
351-
expect(db.location()).toBe(dbPath);
351+
expect(db.location()).toBe(fs.realpathSync(dbPath));
352352

353353
// Test that we can create tables (not readonly)
354354
expect(() => {
@@ -426,7 +426,7 @@ describe("Database Configuration Tests", () => {
426426
const db = new DatabaseSync(dbPath);
427427

428428
expect(db.isOpen).toBe(true);
429-
expect(db.location()).toBe(dbPath);
429+
expect(db.location()).toBe(fs.realpathSync(dbPath));
430430

431431
// Should be able to create and modify data (not readonly by default)
432432
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY)");
@@ -607,7 +607,7 @@ describe("Database Configuration Tests", () => {
607607
});
608608

609609
expect(db.isOpen).toBe(true);
610-
expect(db.location()).toBe(dbPath);
610+
expect(db.location()).toBe(fs.realpathSync(dbPath));
611611

612612
// Should be able to read data
613613
const stmt = db.prepare("SELECT * FROM child WHERE id = ?");
@@ -641,7 +641,7 @@ describe("Database Configuration Tests", () => {
641641
});
642642

643643
expect(db.isOpen).toBe(true);
644-
expect(db.location()).toBe(dbPath);
644+
expect(db.location()).toBe(fs.realpathSync(dbPath));
645645

646646
// Should be readonly
647647
expect(() => {

test/location-method.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ describe("Enhanced location() method tests", () => {
3131

3232
test("location() returns main database path by default", () => {
3333
const db = new DatabaseSync(dbPath);
34-
expect(db.location()).toBe(dbPath);
35-
expect(db.location("main")).toBe(dbPath);
34+
expect(db.location()).toBe(fs.realpathSync(dbPath));
35+
expect(db.location("main")).toBe(fs.realpathSync(dbPath));
3636
db.close();
3737
});
3838

@@ -50,11 +50,11 @@ describe("Enhanced location() method tests", () => {
5050
db.exec(`ATTACH DATABASE '${attachedDbPath}' AS attached_db`);
5151

5252
// Main database should still return the main path
53-
expect(db.location()).toBe(dbPath);
54-
expect(db.location("main")).toBe(dbPath);
53+
expect(db.location()).toBe(fs.realpathSync(dbPath));
54+
expect(db.location("main")).toBe(fs.realpathSync(dbPath));
5555

5656
// Attached database should return its path
57-
expect(db.location("attached_db")).toBe(attachedDbPath);
57+
expect(db.location("attached_db")).toBe(fs.realpathSync(attachedDbPath));
5858

5959
db.close();
6060
});
@@ -78,8 +78,8 @@ describe("Enhanced location() method tests", () => {
7878
db.exec(`ATTACH DATABASE '${secondAttachedPath}' AS second_attached`);
7979

8080
// Test all database locations
81-
expect(db.location()).toBe(dbPath);
82-
expect(db.location("main")).toBe(dbPath);
81+
expect(db.location()).toBe(fs.realpathSync(dbPath));
82+
expect(db.location("main")).toBe(fs.realpathSync(dbPath));
8383
expect(db.location("first_attached")).toBe(attachedDbPath);
8484
expect(db.location("second_attached")).toBe(secondAttachedPath);
8585

@@ -123,13 +123,13 @@ describe("Enhanced location() method tests", () => {
123123

124124
// Attach and then detach a database
125125
db.exec(`ATTACH DATABASE '${attachedDbPath}' AS temp_db`);
126-
expect(db.location("temp_db")).toBe(attachedDbPath);
126+
expect(db.location("temp_db")).toBe(fs.realpathSync(attachedDbPath));
127127

128128
db.exec("DETACH DATABASE temp_db");
129129
expect(db.location("temp_db")).toBeNull();
130130

131131
// Main database should still work
132-
expect(db.location()).toBe(dbPath);
132+
expect(db.location()).toBe(fs.realpathSync(dbPath));
133133

134134
db.close();
135135
});
@@ -157,7 +157,7 @@ describe("Enhanced location() method tests", () => {
157157
const specialPath = path.join(tempDir, "special-db_123.db");
158158
db.exec(`ATTACH DATABASE '${specialPath}' AS "special_db_123"`);
159159

160-
expect(db.location("special_db_123")).toBe(specialPath);
160+
expect(db.location("special_db_123")).toBe(fs.realpathSync(specialPath));
161161

162162
db.close();
163163

test/path-validation.test.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@ describe("Path Validation", () => {
1212
});
1313

1414
afterEach(async () => {
15+
// Wait for Windows file handles to be released
16+
if (process.platform === "win32") {
17+
await new Promise((resolve) => setTimeout(resolve, 100));
18+
}
19+
1520
if (tmpDir) {
16-
await rm(tmpDir, { recursive: true, force: true });
21+
await rm(tmpDir, {
22+
recursive: true,
23+
force: true,
24+
maxRetries: 3,
25+
retryDelay: 500,
26+
});
1727
}
1828
});
1929

@@ -138,17 +148,35 @@ describe("Path Validation", () => {
138148

139149
describe("Backup path validation", () => {
140150
let sourceDb: DatabaseSyncInstance;
151+
const openDbs: DatabaseSyncInstance[] = [];
141152

142153
beforeEach(() => {
143154
sourceDb = new DatabaseSync(":memory:");
144155
sourceDb.exec("CREATE TABLE test (id INTEGER, value TEXT)");
145156
sourceDb.exec("INSERT INTO test VALUES (1, 'test')");
146157
});
147158

148-
afterEach(() => {
149-
if (sourceDb.isOpen) {
159+
afterEach(async () => {
160+
// Close all databases opened during tests
161+
for (const db of openDbs) {
162+
try {
163+
if (db.isOpen) {
164+
db.close();
165+
}
166+
} catch {
167+
// Ignore close errors
168+
}
169+
}
170+
openDbs.length = 0;
171+
172+
if (sourceDb?.isOpen) {
150173
sourceDb.close();
151174
}
175+
176+
// Wait for Windows file handles to be released
177+
if (process.platform === "win32") {
178+
await new Promise((resolve) => setTimeout(resolve, 100));
179+
}
152180
});
153181

154182
it("should accept string destination paths", async () => {
@@ -157,6 +185,7 @@ describe("Path Validation", () => {
157185

158186
// Verify backup was created
159187
const verifyDb = new DatabaseSync(destPath);
188+
openDbs.push(verifyDb);
160189
const result = verifyDb.prepare("SELECT * FROM test").get();
161190
expect(result).toEqual({ id: 1, value: "test" });
162191
verifyDb.close();
@@ -169,6 +198,7 @@ describe("Path Validation", () => {
169198

170199
// Verify backup was created
171200
const verifyDb = new DatabaseSync(destPath);
201+
openDbs.push(verifyDb);
172202
const result = verifyDb.prepare("SELECT * FROM test").get();
173203
expect(result).toEqual({ id: 1, value: "test" });
174204
verifyDb.close();
@@ -181,6 +211,7 @@ describe("Path Validation", () => {
181211

182212
// Verify backup was created
183213
const verifyDb = new DatabaseSync(destPath);
214+
openDbs.push(verifyDb);
184215
const result = verifyDb.prepare("SELECT * FROM test").get();
185216
expect(result).toEqual({ id: 1, value: "test" });
186217
verifyDb.close();

0 commit comments

Comments
 (0)