Skip to content

Commit 8e328c5

Browse files
authored
Decompose and organize import-tests script (#1165)
* Separate git operations * separate out test parser * Single function for test parsing * Break test parser up * Reapply test plan operation changes * Use strategy pattern for test parsing, add some jsdocs * Additional comments and use JSDoc types more * Only export as necessary, keep V1 naming, add tmp to gitignore * Update deprecation date comparison
1 parent 84e269d commit 8e328c5

File tree

8 files changed

+1124
-781
lines changed

8 files changed

+1124
-781
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,6 @@ server/migrations/test_plan_target_id.csv
127127
jwt-signing-key.pem
128128

129129
client/resources
130+
131+
#temp files for import-tests
132+
server/scripts/import-tests/tmp
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* eslint no-console: 0 */
2+
3+
const spawn = require('cross-spawn');
4+
5+
const ariaAtRepo = 'https://github.com/w3c/aria-at.git';
6+
const ariaAtDefaultBranch = 'master';
7+
8+
/**
9+
* Executes a git command and returns its output.
10+
* @param {string} args - The git command arguments as a string.
11+
* @param {string} cwd - The current working directory for the git command.
12+
* @returns {string} The trimmed output of the git command.
13+
* @throws {Error} If the git command fails.
14+
*/
15+
function gitRun(args, cwd) {
16+
const gitRunOutput = spawn.sync('git', args.split(' '), { cwd });
17+
18+
if (gitRunOutput.error) {
19+
console.info(`'git ${args}' failed with error ${gitRunOutput.error}`);
20+
process.exit(1);
21+
}
22+
23+
return gitRunOutput.stdout.toString().trimEnd();
24+
}
25+
26+
/**
27+
* Clones the aria-at repository to the specified directory.
28+
* @param {string} gitCloneDirectory - The directory where the repo will be cloned.
29+
* @throws {Error} If the cloning process fails.
30+
*/
31+
function cloneRepo(gitCloneDirectory) {
32+
console.info('Cloning aria-at repo ...');
33+
const cloneOutput = spawn.sync('git', [
34+
'clone',
35+
ariaAtRepo,
36+
gitCloneDirectory
37+
]);
38+
39+
if (cloneOutput.error) {
40+
console.info(
41+
`git clone ${ariaAtRepo} ${gitCloneDirectory} failed with error ${cloneOutput.error}`
42+
);
43+
process.exit(1);
44+
}
45+
console.info('Cloning aria-at repo complete.');
46+
}
47+
48+
/**
49+
* Checks out a specific commit and retrieves its date.
50+
* @param {string} gitCloneDirectory - The directory of the cloned repo.
51+
* @param {string|null} commit - The commit to checkout. If null, uses the default branch.
52+
* @returns {Promise<{gitCommitDate: Date}>} An object containing the commit date.
53+
*/
54+
async function readCommit(gitCloneDirectory, commit) {
55+
gitRun(`checkout ${commit ?? ariaAtDefaultBranch}`, gitCloneDirectory);
56+
const gitCommitDate = new Date(
57+
gitRun(`log --format=%aI -n 1`, gitCloneDirectory)
58+
);
59+
60+
return { gitCommitDate };
61+
}
62+
63+
/**
64+
* Reads git information for a specific directory.
65+
* @param {string} directoryPath - The path to the directory to read git info from.
66+
* @returns {{gitSha: string, gitMessage: string, gitCommitDate: Date}} An object containing git information.
67+
*/
68+
function readDirectoryGitInfo(directoryPath) {
69+
const gitSha = gitRun(`log -1 --format=%H -- .`, directoryPath);
70+
const gitMessage = gitRun(`log -1 --format=%s -- .`, directoryPath);
71+
const gitCommitDate = new Date(
72+
gitRun(`log -1 --format=%aI -- .`, directoryPath)
73+
);
74+
75+
return { gitSha, gitMessage, gitCommitDate };
76+
}
77+
78+
module.exports = {
79+
cloneRepo,
80+
readCommit,
81+
readDirectoryGitInfo
82+
};

0 commit comments

Comments
 (0)