Skip to content

Commit

Permalink
Validate the logs
Browse files Browse the repository at this point in the history
  • Loading branch information
octogonz committed Mar 28, 2024
1 parent b99273c commit cafd03c
Showing 1 changed file with 86 additions and 2 deletions.
88 changes: 86 additions & 2 deletions pnpm-sync-api-demo/src/test/pnpmSyncPrepare.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import { FileSystem } from '@rushstack/node-core-library';
import { FileSystem, Path } from '@rushstack/node-core-library';
import { readWantedLockfile, Lockfile } from '@pnpm/lockfile-file';
import {
type ILockfile,
Expand All @@ -9,6 +9,37 @@ import {
ILogMessageCallbackOptions
} from 'pnpm-sync-lib';

function scrubLog(log: Record<string, any>): Record<string, any> {
const scrubbedLog = { ...log };
const repoRootFolder = path.join(__dirname, '..', '..', '..', '..');
for (const key of Object.keys(scrubbedLog)) {
switch (key) {
case 'message':
scrubbedLog[key] = scrubbedLog[key].split(' ')[0] + '...';
break;
case 'details':
scrubbedLog[key] = scrubLog(scrubbedLog[key]);
break;
case 'executionTimeInMs':
scrubbedLog[key] = '[TIMING]';
break;

case 'dotPnpmFolder':
case 'dotPnpmFolderPath':
case 'lockfilePath':
case 'pnpmSyncJsonPath':
case 'projectFolder':
case 'sourcePath':
let scrubbedPath = scrubbedLog[key];
scrubbedPath = scrubbedPath.replace(repoRootFolder, '<root>');
scrubbedPath = Path.convertToSlashes(scrubbedPath);
scrubbedLog[key] = scrubbedPath;
break;
}
}
return scrubbedLog;
}

describe('pnpm-sync-api test', () => {
it('pnpmSyncPrepareAsync should generate .pnpm-sync.json under node_modules folder', async () => {
const lockfilePath = '../pnpm-lock.yaml';
Expand All @@ -28,6 +59,8 @@ describe('pnpm-sync-api test', () => {
expect(fs.existsSync(dotPnpmSyncJsonPathForSampleLib1)).toBe(false);
expect(fs.existsSync(dotPnpmSyncJsonPathForSampleLib2)).toBe(false);

const logs: ILogMessageCallbackOptions[] = [];

await pnpmSyncPrepareAsync({
lockfilePath: lockfilePath,
dotPnpmFolder: dotPnpmFolder,
Expand Down Expand Up @@ -57,10 +90,61 @@ describe('pnpm-sync-api test', () => {
}
},
logMessageCallback: (options: ILogMessageCallbackOptions): void => {
console.log(options.message);
logs.push(options);
}
});

expect(logs.map((x) => scrubLog(x))).toMatchInlineSnapshot(`
Array [
Object {
"details": Object {
"dotPnpmFolder": "<root>/pnpm-sync/node_modules/.pnpm",
"lockfilePath": "<root>/pnpm-sync/pnpm-lock.yaml",
"messageIdentifier": "prepare-starting",
},
"message": "Starting...",
"messageKind": "verbose",
},
Object {
"details": Object {
"messageIdentifier": "prepare-writing-file",
"pnpmSyncJsonPath": "<root>/pnpm-sync/pnpm-sync-api-demo/test-fixtures/sample-lib1/node_modules/.pnpm-sync.json",
"projectFolder": "<root>/pnpm-sync/pnpm-sync-api-demo/test-fixtures/sample-lib1",
},
"message": "Writing...",
"messageKind": "verbose",
},
Object {
"details": Object {
"messageIdentifier": "prepare-writing-file",
"pnpmSyncJsonPath": "<root>/pnpm-sync/pnpm-sync-api-demo/test-fixtures/sample-lib2/node_modules/.pnpm-sync.json",
"projectFolder": "<root>/pnpm-sync/pnpm-sync-api-demo/test-fixtures/sample-lib2",
},
"message": "Writing...",
"messageKind": "verbose",
},
Object {
"details": Object {
"messageIdentifier": "prepare-writing-file",
"pnpmSyncJsonPath": "<root>/pnpm-sync/pnpm-sync-cli-demo/lib1/node_modules/.pnpm-sync.json",
"projectFolder": "<root>/pnpm-sync/pnpm-sync-cli-demo/lib1",
},
"message": "Writing...",
"messageKind": "verbose",
},
Object {
"details": Object {
"dotPnpmFolder": "<root>/pnpm-sync/node_modules/.pnpm",
"executionTimeInMs": "[TIMING]",
"lockfilePath": "<root>/pnpm-sync/pnpm-lock.yaml",
"messageIdentifier": "prepare-finishing",
},
"message": "Regenerated...",
"messageKind": "info",
},
]
`);

// now, read .pnpm-sync.json and check the fields
expect(fs.existsSync(dotPnpmSyncJsonPathForSampleLib1)).toBe(true);
expect(fs.existsSync(dotPnpmSyncJsonPathForSampleLib2)).toBe(true);
Expand Down

0 comments on commit cafd03c

Please sign in to comment.