Skip to content

Commit dfb839e

Browse files
committed
cleanup
1 parent da23e2b commit dfb839e

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

test/base/cache.ts

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -230,36 +230,15 @@ export class Cache extends EventEmitter {
230230
this.activeLayer = (async () => {
231231
const execAsync = promisify(exec);
232232

233-
if (this.externalLayersPath) {
234-
// Check git for an uncommitted layer database file in external layers path
235-
try {
236-
const { stdout: revParseStdout } = await execAsync('git rev-parse --show-toplevel', { cwd: this.externalLayersPath });
237-
const gitRoot = revParseStdout.trim();
238-
const relativePath = path.relative(gitRoot, this.externalLayersPath);
239-
const { stdout: statusStdout } = await execAsync(`git status -z ${relativePath}/*`, { cwd: gitRoot });
240-
if (statusStdout !== '') {
241-
const layerDatabaseEntries = statusStdout.split('\0').filter(entry => entry.endsWith('.sqlite'));
242-
if (layerDatabaseEntries.length > 0) {
243-
const regex = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.sqlite$/;
244-
const match = layerDatabaseEntries[0].match(regex);
245-
if (match && this.layers.has(match[1])) {
246-
return this.layers.get(match[1])!;
247-
}
248-
}
249-
}
250-
} catch (error) {
251-
// If git operations fail for external path, continue to create new layer
252-
}
253-
254-
// Create a new layer database in external path
255-
const uuid = generateUuid();
256-
const activeLayer = new Keyv(new KeyvSqlite(path.join(this.externalLayersPath, `${uuid}.sqlite`)));
257-
this.layers.set(uuid, activeLayer);
258-
return activeLayer;
259-
} else {
260-
// Check git for an uncommitted layer database file in default path
261-
const { stdout: revParseStdout } = await execAsync('git rev-parse --show-toplevel');
262-
const { stdout: statusStdout } = await execAsync('git status -z test/simulation/cache/layers/*', { cwd: revParseStdout.trim() });
233+
const targetPath = this.externalLayersPath || this.layersPath;
234+
const gitStatusPath = this.externalLayersPath
235+
? `${path.relative(await this._getGitRoot(targetPath), targetPath)}/*`
236+
: 'test/simulation/cache/layers/*';
237+
238+
// Check git for an uncommitted layer database file
239+
try {
240+
const gitRoot = await this._getGitRoot(targetPath);
241+
const { stdout: statusStdout } = await execAsync(`git status -z ${gitStatusPath}`, { cwd: gitRoot });
263242
if (statusStdout !== '') {
264243
const layerDatabaseEntries = statusStdout.split('\0').filter(entry => entry.endsWith('.sqlite'));
265244
if (layerDatabaseEntries.length > 0) {
@@ -270,19 +249,27 @@ export class Cache extends EventEmitter {
270249
}
271250
}
272251
}
273-
274-
// Create a new layer database in default path
275-
const uuid = generateUuid();
276-
const activeLayer = new Keyv(new KeyvSqlite(path.join(this.layersPath, `${uuid}.sqlite`)));
277-
this.layers.set(uuid, activeLayer);
278-
return activeLayer;
252+
} catch (error) {
253+
// If git operations fail, continue to create new layer
279254
}
255+
256+
// Create a new layer database
257+
const uuid = generateUuid();
258+
const activeLayer = new Keyv(new KeyvSqlite(path.join(targetPath, `${uuid}.sqlite`)));
259+
this.layers.set(uuid, activeLayer);
260+
return activeLayer;
280261
})();
281262
}
282263

283264
return this.activeLayer;
284265
}
285266

267+
private async _getGitRoot(cwd: string): Promise<string> {
268+
const execAsync = promisify(exec);
269+
const { stdout } = await execAsync('git rev-parse --show-toplevel', { cwd });
270+
return stdout.trim();
271+
}
272+
286273
private async _compress(value: string): Promise<string> {
287274
const buffer = await compress(value, { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 6, } });
288275
return buffer.toString('base64');

0 commit comments

Comments
 (0)