Skip to content

Commit c3a3041

Browse files
committed
fix: resolve data race in snapshot directory across concurrent tests
1 parent a9b6478 commit c3a3041

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

src/differ.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
const fs = require('fs').promises;
22
const path = require('path');
33

4-
const SNAPSHOTS_DIR = '.licensepulse/snapshots';
4+
function getSnapshotsDir() {
5+
return process.env.LICENSEPULSE_SNAPSHOTS_DIR || '.licensepulse/snapshots';
6+
}
57

68
function getSnapshotFilename(repoId) {
79
return repoId.replace('/', '-') + '.json';
810
}
911

1012
function getSnapshotPath(repoId) {
11-
return path.join(SNAPSHOTS_DIR, getSnapshotFilename(repoId));
13+
return path.join(getSnapshotsDir(), getSnapshotFilename(repoId));
1214
}
1315

1416
async function ensureSnapshotsDir() {
1517
try {
1618
const path = require('path');
17-
const baseDir = path.dirname(SNAPSHOTS_DIR);
19+
const dir = getSnapshotsDir();
20+
const baseDir = path.dirname(dir);
1821
await fs.mkdir(baseDir, { recursive: true });
19-
await fs.mkdir(SNAPSHOTS_DIR, { recursive: true });
22+
await fs.mkdir(dir, { recursive: true });
2023
} catch (error) {
2124
// Ignore errors if directory already exists
2225
}
@@ -57,12 +60,13 @@ async function saveSnapshot(repoId, licenseData) {
5760
async function getAllSnapshots() {
5861
try {
5962
await ensureSnapshotsDir();
60-
const files = await fs.readdir(SNAPSHOTS_DIR);
63+
const dir = getSnapshotsDir();
64+
const files = await fs.readdir(dir);
6165

6266
const snapshots = [];
6367
for (const file of files) {
6468
if (file.endsWith('.json')) {
65-
const filePath = path.join(SNAPSHOTS_DIR, file);
69+
const filePath = path.join(dir, file);
6670
const data = await fs.readFile(filePath, 'utf8');
6771
snapshots.push(JSON.parse(data));
6872
}

tests/integration/workflow.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
process.env.LICENSEPULSE_SNAPSHOTS_DIR = '.licensepulse/snapshots-integration';
12
const Watchlist = require('../../src/watchlist');
23
const { fetchLicense } = require('../../src/watcher');
34
const { checkAndUpdate, loadSnapshot } = require('../../src/differ');
@@ -9,7 +10,7 @@ const axios = require('axios');
910

1011
describe('Integration: Complete Workflow', () => {
1112
const testWatchlistPath = 'test-integration-watchlist.json';
12-
const snapshotsDir = '.licensepulse/snapshots';
13+
const snapshotsDir = process.env.LICENSEPULSE_SNAPSHOTS_DIR;
1314

1415
beforeAll(() => {
1516
process.env.GITHUB_TOKEN = 'test-token';

tests/unit/differ.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
process.env.LICENSEPULSE_SNAPSHOTS_DIR = '.licensepulse/snapshots-unit';
12
const fs = require('fs').promises;
23
const path = require('path');
34
const {
@@ -9,7 +10,7 @@ const {
910
getSnapshotPath
1011
} = require('../../src/differ');
1112

12-
const SNAPSHOTS_DIR = '.licensepulse/snapshots';
13+
const SNAPSHOTS_DIR = process.env.LICENSEPULSE_SNAPSHOTS_DIR;
1314

1415
describe('Differ', () => {
1516
beforeEach(async () => {

0 commit comments

Comments
 (0)