Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: print lock file on failed packages #1015

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Options:
-g, --gid <uid> Set the gid (posix only)
-a, --append Turns on append results to file mode rather than replace
--tmpDir <path> Directory to test modules in
--printLockFile Print the failed module's package-lock.json/yarn.lock file
```

### Examples:
Expand Down
1 change: 1 addition & 0 deletions lib/bin/citgm-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const options = {
tmpDir: app.tmpDir,
customTest: app.customTest,
yarn: app.yarn,
printLockFile: app.printLockFile,
includeTags: app.includeTags || [],
excludeTags: app.excludeTags || []
};
Expand Down
3 changes: 2 additions & 1 deletion lib/bin/citgm.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const options = {
sha: app.sha,
tmpDir: app.tmpDir,
customTest: app.customTest,
yarn: app.yarn
yarn: app.yarn,
printLockFile: app.printLockFile
};

if (!windows) {
Expand Down
14 changes: 14 additions & 0 deletions lib/citgm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { grabModuleData } from './grab-module-data.js';
import { grabProject } from './grab-project.js';
import { lookup } from './lookup.js';
import {
getLockFileContent,
getPackageManagers,
pkgInstall,
pkgTest
Expand Down Expand Up @@ -115,6 +116,19 @@ export class Tester extends EventEmitter {
if (!payload.expectFail) {
this.emit('fail', err);
payload.error = err;

if (this.options.printLockFile) {
const lockFile = await getLockFileContent(this);

if (lockFile?.content) {
// TODO - multiline output is not handled well
this.emit(
'data',
'info',
`${this.module.name} ${lockFile.fileName} lock file content ${lockFile?.content}`
);
}
}
}
} else if (payload.expectFail) {
this.emit('fail', 'this module should have failed');
Expand Down
5 changes: 5 additions & 0 deletions lib/common-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ export function commonArgs() {
description: 'Install and test the project using yarn instead of npm',
default: false
})
.option('printLockFile', {
type: 'boolean',
description: `Print the failed module's package-lock.json/yarn.lock file`,
default: false
})
.example(
'citgm-all --customTest /path/to/customTest.js',
'Runs a custom node test script instead of "npm test"'
Expand Down
31 changes: 31 additions & 0 deletions lib/package-manager/get-lock-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';

export async function getLockFile(packageManager, context) {
let lockFileName = 'package-lock.json';

if (packageManager === 'yarn') {
lockFileName = 'yarn.lock';
}
const lockFilePath = join(context.path, context.module.name, lockFileName);

try {
const fileContent = await readFile(lockFilePath, 'utf8');

return {
fileName: lockFileName,
content: fileContent.toString()
};
} catch (err) {
if (err.code === 'ENOENT') {
return undefined;
}

context.emit(
'data',
'warn',
`${context.module.name} ${packageManager}-get-lock-file:`,
`Failed to read lock file ${lockFileName} ${err.message}`
);
}
}
9 changes: 9 additions & 0 deletions lib/package-manager/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import install from './install.js';
import { test } from './test.js';
import { getExecutable } from './get-executable.js';
import { getLockFile } from './get-lock-file.js';

export function pkgInstall(context) {
if (context.options.yarn || context.module.useYarn) {
Expand All @@ -25,3 +26,11 @@ export async function getPackageManagers() {
]);
return { npm, yarn };
}

export function getLockFileContent(context) {
if (context.options.yarn || context.module.useYarn) {
return getLockFile('yarn', context);
} else {
return getLockFile('npm', context);
}
}
Loading