|
| 1 | +import path from "node:path"; |
| 2 | +import fs from "fs-extra"; |
1 | 3 | import { CSSSelectivity } from "./css-selectivity"; |
2 | 4 | import { JSSelectivity } from "./js-selectivity"; |
3 | 5 | import type { ExistingBrowser } from "../../existing-browser"; |
4 | 6 | import { getTestDependenciesWriter } from "./test-dependencies-writer"; |
5 | 7 | import type { Test, TestDepsContext, TestDepsData } from "../../../types"; |
6 | | -import { mergeSourceDependencies, transformSourceDependencies } from "./utils"; |
| 8 | +import { getSelectivityTestsPath, mergeSourceDependencies, transformSourceDependencies } from "./utils"; |
7 | 9 | import { getHashWriter } from "./hash-writer"; |
8 | 10 | import { Compression } from "./types"; |
9 | 11 | import { getCollectedTestplaneDependencies } from "./testplane-selectivity"; |
10 | 12 | import { getHashReader } from "./hash-reader"; |
11 | 13 | import type { Config } from "../../../config"; |
12 | 14 | import { MasterEvents } from "../../../events"; |
| 15 | +import { debugSelectivity } from "./debug"; |
13 | 16 |
|
14 | 17 | type StopSelectivityFn = (test: Test, shouldWrite: boolean) => Promise<void>; |
15 | 18 |
|
@@ -43,6 +46,71 @@ export const updateSelectivityHashes = async (config: Config): Promise<void> => |
43 | 46 | } |
44 | 47 | }; |
45 | 48 |
|
| 49 | +export const clearUnusedSelectivityDumps = async (config: Config): Promise<void> => { |
| 50 | + const browserIds = config.getBrowserIds(); |
| 51 | + const selectivityRoots: string[] = []; |
| 52 | + |
| 53 | + for (const browserId of browserIds) { |
| 54 | + const browserConfig = config.forBrowser(browserId); |
| 55 | + const { enabled, testDependenciesPath } = browserConfig.selectivity; |
| 56 | + |
| 57 | + if (enabled && !selectivityRoots.includes(testDependenciesPath)) { |
| 58 | + selectivityRoots.push(testDependenciesPath); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + let filesTotal = 0; |
| 63 | + let filesDeleted = 0; |
| 64 | + |
| 65 | + // eslint-disable-next-line no-bitwise |
| 66 | + const rwMode = fs.constants.R_OK | fs.constants.W_OK; |
| 67 | + |
| 68 | + await Promise.all( |
| 69 | + selectivityRoots.map(async selectivityRoot => { |
| 70 | + const testsPath = getSelectivityTestsPath(selectivityRoot); |
| 71 | + const accessError = await fs.access(testsPath, rwMode).catch((err: Error) => err); |
| 72 | + |
| 73 | + if (accessError) { |
| 74 | + if (!("code" in accessError && accessError.code === "ENOENT")) { |
| 75 | + debugSelectivity(`Couldn't access "${testsPath}" to clear stale files: %O`, accessError); |
| 76 | + } |
| 77 | + |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const testsFileNames = await fs.readdir(testsPath); |
| 82 | + |
| 83 | + filesTotal += testsFileNames.length; |
| 84 | + |
| 85 | + for (const testFileName of testsFileNames) { |
| 86 | + const filePath = path.join(testsPath, testFileName); |
| 87 | + const fileStat = await fs.stat(filePath).catch(() => null); |
| 88 | + |
| 89 | + if (!fileStat) { |
| 90 | + debugSelectivity(`Couldn't access file "${filePath}" to check if it was used. Skipping`); |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + // File was not used in this run |
| 95 | + if (fileStat.atimeMs < performance.timeOrigin && fileStat.isFile()) { |
| 96 | + await fs |
| 97 | + .unlink(filePath) |
| 98 | + .then(() => { |
| 99 | + filesDeleted++; |
| 100 | + }) |
| 101 | + .catch(err => { |
| 102 | + debugSelectivity(`Couldn't remove stale file "${filePath}": %O`, err); |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | + }), |
| 107 | + ); |
| 108 | + |
| 109 | + if (filesDeleted) { |
| 110 | + debugSelectivity(`Out of ${filesTotal} files, ${filesDeleted} were considered as outdated and deleted`); |
| 111 | + } |
| 112 | +}; |
| 113 | + |
46 | 114 | export const startSelectivity = async (browser: ExistingBrowser): Promise<StopSelectivityFn> => { |
47 | 115 | const { enabled, compression, sourceRoot, testDependenciesPath, mapDependencyRelativePath } = |
48 | 116 | browser.config.selectivity; |
|
0 commit comments