33 */
44
55import { Database } from "bun:sqlite" ;
6- import { afterEach , beforeEach , describe , expect , mock , test } from "bun:test" ;
7- import { mkdirSync , rmSync } from "node:fs" ;
6+ import { describe , expect , mock , test } from "bun:test" ;
87import { join } from "node:path" ;
98import { fixCommand } from "../../../src/commands/cli/fix.js" ;
10- import {
11- CONFIG_DIR_ENV_VAR ,
12- closeDatabase ,
13- } from "../../../src/lib/db/index.js" ;
9+ import { closeDatabase } from "../../../src/lib/db/index.js" ;
1410import {
1511 EXPECTED_TABLES ,
1612 generatePreMigrationTableDDL ,
1713 initSchema ,
1814} from "../../../src/lib/db/schema.js" ;
15+ import { useTestConfigDir } from "../../helpers.js" ;
1916
2017/**
2118 * Generate DDL for creating a database with pre-migration tables.
@@ -64,46 +61,12 @@ function createDatabaseWithMissingTables(
6461 ) . run ( ) ;
6562}
6663
67- let testDir : string ;
68- let originalConfigDir : string | undefined ;
69-
70- beforeEach ( ( ) => {
71- // Save original config dir
72- originalConfigDir = process . env [ CONFIG_DIR_ENV_VAR ] ;
73-
74- // Close any existing database connection
75- closeDatabase ( ) ;
76-
77- // Create unique test directory
78- const baseDir = originalConfigDir ?? "/tmp/sentry-cli-test" ;
79- testDir = join (
80- baseDir ,
81- `fix-test-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } `
82- ) ;
83- mkdirSync ( testDir , { recursive : true } ) ;
84- process . env [ CONFIG_DIR_ENV_VAR ] = testDir ;
85- } ) ;
86-
87- afterEach ( ( ) => {
88- closeDatabase ( ) ;
89- // Restore original config dir
90- if ( originalConfigDir ) {
91- process . env [ CONFIG_DIR_ENV_VAR ] = originalConfigDir ;
92- } else {
93- delete process . env [ CONFIG_DIR_ENV_VAR ] ;
94- }
95- // Clean up test directory
96- try {
97- rmSync ( testDir , { recursive : true , force : true } ) ;
98- } catch {
99- // Ignore cleanup errors
100- }
101- } ) ;
64+ const getTestDir = useTestConfigDir ( "fix-test-" ) ;
10265
10366describe ( "sentry cli fix" , ( ) => {
10467 test ( "reports no issues for healthy database" , async ( ) => {
10568 // Create healthy database
106- const db = new Database ( join ( testDir , "cli.db" ) ) ;
69+ const db = new Database ( join ( getTestDir ( ) , "cli.db" ) ) ;
10770 initSchema ( db ) ;
10871 db . close ( ) ;
10972
@@ -124,7 +87,7 @@ describe("sentry cli fix", () => {
12487
12588 test ( "detects and reports missing columns in dry-run mode" , async ( ) => {
12689 // Create database with pre-migration tables (missing v4 columns)
127- const db = new Database ( join ( testDir , "cli.db" ) ) ;
90+ const db = new Database ( join ( getTestDir ( ) , "cli.db" ) ) ;
12891 createPreMigrationDatabase ( db ) ;
12992 db . close ( ) ;
13093
@@ -148,7 +111,7 @@ describe("sentry cli fix", () => {
148111
149112 test ( "fixes missing columns when not in dry-run mode" , async ( ) => {
150113 // Create database with pre-migration tables (missing v4 columns)
151- const db = new Database ( join ( testDir , "cli.db" ) ) ;
114+ const db = new Database ( join ( getTestDir ( ) , "cli.db" ) ) ;
152115 createPreMigrationDatabase ( db ) ;
153116 db . close ( ) ;
154117
@@ -169,7 +132,7 @@ describe("sentry cli fix", () => {
169132
170133 // Verify the column was actually added
171134 closeDatabase ( ) ;
172- const verifyDb = new Database ( join ( testDir , "cli.db" ) ) ;
135+ const verifyDb = new Database ( join ( getTestDir ( ) , "cli.db" ) ) ;
173136 const cols = verifyDb . query ( "PRAGMA table_info(dsn_cache)" ) . all ( ) as Array < {
174137 name : string ;
175138 } > ;
@@ -188,7 +151,7 @@ describe("sentry cli fix", () => {
188151 // that was previously missing tables (now fixed by auto-repair at startup).
189152 test ( "handles database that was auto-repaired at startup" , async ( ) => {
190153 // Create database missing dsn_cache - initSchema will create it when command runs
191- const db = new Database ( join ( testDir , "cli.db" ) ) ;
154+ const db = new Database ( join ( getTestDir ( ) , "cli.db" ) ) ;
192155 createDatabaseWithMissingTables ( db , [ "dsn_cache" ] ) ;
193156 db . close ( ) ;
194157
@@ -210,7 +173,7 @@ describe("sentry cli fix", () => {
210173
211174 // Verify the table was created (by initSchema auto-repair)
212175 closeDatabase ( ) ;
213- const verifyDb = new Database ( join ( testDir , "cli.db" ) ) ;
176+ const verifyDb = new Database ( join ( getTestDir ( ) , "cli.db" ) ) ;
214177 const tables = verifyDb
215178 . query (
216179 "SELECT name FROM sqlite_master WHERE type='table' AND name='dsn_cache'"
@@ -221,7 +184,7 @@ describe("sentry cli fix", () => {
221184 } ) ;
222185
223186 test ( "shows database path in output" , async ( ) => {
224- const db = new Database ( join ( testDir , "cli.db" ) ) ;
187+ const db = new Database ( join ( getTestDir ( ) , "cli.db" ) ) ;
225188 initSchema ( db ) ;
226189 db . close ( ) ;
227190
@@ -237,6 +200,6 @@ describe("sentry cli fix", () => {
237200
238201 const output = stdoutWrite . mock . calls . map ( ( c ) => c [ 0 ] ) . join ( "" ) ;
239202 expect ( output ) . toContain ( "Database:" ) ;
240- expect ( output ) . toContain ( testDir ) ;
203+ expect ( output ) . toContain ( getTestDir ( ) ) ;
241204 } ) ;
242205} ) ;
0 commit comments