Skip to content

Commit

Permalink
fix(testing): fix tests for Windows (#165)
Browse files Browse the repository at this point in the history
In this PR, I identified and fixed various issue while running tests in
the Windows environment:

- shell needs to be specified to run programs in command shell on
Windows, as described
[here](https://stackoverflow.com/questions/60386867/node-spawn-child-process-not-working-in-windows)
- path notation on Windows is different
- JSON formatting is different for UNIX and Windows
- writing into files replaced by echo command which works seamlessly on
Windows
- glob patterns require extra quotation marks for argument expansion on
Linux
- recreating tmp folder caused issues for me so I partially fixed it via
adding the recursive option

Note: This is the same scope as #131. Unfortunately, merging changes
from the test refactor did not go well so I started off a clean branch
and re-applied the changes.
  • Loading branch information
Tlacenka authored Oct 27, 2023
1 parent 7c81f81 commit b80255b
Show file tree
Hide file tree
Showing 29 changed files with 200 additions and 1,160 deletions.
1,002 changes: 0 additions & 1,002 deletions e2e/cli-e2e/tests/__snapshots__/collect.spec.ts.snap

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions e2e/cli-e2e/tests/collect.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { join } from 'path';
import { afterEach, beforeEach, vi } from 'vitest';
import { PluginReport, Report, reportSchema } from '@code-pushup/models';
import { executeProcess, readJsonFile, readTextFile } from '@code-pushup/utils';
Expand All @@ -21,18 +22,21 @@ describe('CLI collect', () => {
plugins: report.plugins.map(omitVariableData) as PluginReport[],
});

const cliPath = join('..', '..', 'dist', 'packages', 'cli');

beforeEach(async () => {
vi.clearAllMocks();
cleanFolderPutGitKeep();
});

afterEach(() => {
cleanFolderPutGitKeep();
});

it('should run ESLint plugin and create report.json', async () => {
const { code, stderr } = await executeProcess({
command: 'npx',
args: ['../../dist/packages/cli', 'collect'],
args: [cliPath, 'collect'],
cwd: 'examples/react-todos-app',
});

Expand All @@ -48,7 +52,7 @@ describe('CLI collect', () => {
it('should create report.md', async () => {
const { code, stderr } = await executeProcess({
command: 'npx',
args: ['../../dist/packages/cli', 'collect', '--persist.format=md'],
args: [cliPath, 'collect', '--persist.format=md'],
cwd: 'examples/react-todos-app',
});

Expand All @@ -65,12 +69,7 @@ describe('CLI collect', () => {
it('should print report summary to stdout', async () => {
const { code, stdout, stderr } = await executeProcess({
command: 'npx',
args: [
'../../dist/packages/cli',
'collect',
'--verbose',
'--persist.format=stdout',
],
args: [cliPath, 'collect', '--verbose', '--persist.format=stdout'],
cwd: 'examples/react-todos-app',
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { join } from 'path';
import { echoRunnerConfig } from '../models/test/fixtures/echo-runner-config.mock';

const outputDir = 'tmp';

export default {
Expand Down Expand Up @@ -37,35 +40,26 @@ export default {
docsUrl: 'http://www.my-docs.dev?slug=dummy-audit-3',
},
],
runner: {
command: 'node',
args: [
'-e',
`require('fs').writeFileSync('${outputDir}/dummy-plugin-output.json', '${JSON.stringify(
[
{
title: 'Dummy Audit 1',
slug: 'dummy-audit-1',
value: 420,
score: 0.42,
},
{
title: 'Dummy Audit 2',
slug: 'dummy-audit-2',
value: 80,
score: 0,
},
{
title: 'Dummy Audit 3',
slug: 'dummy-audit-3',
value: 12,
score: 0.12,
},
],
)}')`,
runner: echoRunnerConfig(
[
{
slug: 'dummy-audit-1',
value: 420,
score: 0.42,
},
{
slug: 'dummy-audit-2',
value: 80,
score: 0,
},
{
slug: 'dummy-audit-3',
value: 12,
score: 0.12,
},
],
outputFile: `${outputDir}/dummy-plugin-output.json`,
},
join(outputDir, 'dummy-plugin-output.json'),
),
},
],
categories: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('applyConfigMiddleware', () => {
});

it('should throw with invalid config', async () => {
const invalidConfig = 'wrong/path/to/config';
const invalidConfig = join('wrong', 'path', 'to', 'config');
let error: Error = new Error();
await configMiddleware({ config: invalidConfig }).catch(e => (error = e));
expect(error?.message).toContain(invalidConfig);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/test/fs.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function cleanFolder<T extends object>(
content?: { [key in keyof T]: string },
) {
rmSync(dirName, { recursive: true, force: true });
mkdirSync(dirName);
mkdirSync(dirName, { recursive: true });
if (content) {
for (const fileName in content) {
writeFileSync(join(dirName, fileName), content[fileName]);
Expand All @@ -19,7 +19,7 @@ export function cleanFolderPutGitKeep<T extends object>(
content?: { [key in keyof T]: string },
) {
rmSync(dirName, { recursive: true, force: true });
mkdirSync(dirName);
mkdirSync(dirName, { recursive: true });
writeFileSync(join(dirName, '.gitkeep'), '');
if (content) {
for (const fileName in content) {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/lib/implementation/execute-plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { join } from 'path';
import { beforeEach, describe, expect, it } from 'vitest';
import {
AuditReport,
Expand All @@ -6,8 +7,8 @@ import {
} from '@code-pushup/models';
import {
auditReport,
echoRunnerConfig,
pluginConfig,
runnerConfig,
} from '@code-pushup/models/testing';
import { cleanFolder } from '../../../test';
import { executePlugin, executePlugins } from './execute-plugin';
Expand Down Expand Up @@ -45,7 +46,7 @@ describe('executePlugin', () => {
{ p: 42 } as unknown as AuditReport,
];
const pluginCfg = pluginConfig([auditReport()], {
runner: runnerConfig(invalidAuditOutputs),
runner: echoRunnerConfig(invalidAuditOutputs, join('tmp', 'out.json')),
});
await expect(() => executePlugin(pluginCfg)).rejects.toThrowError(
'Plugin output of plugin with slug mock-plugin-slug',
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/fs.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function cleanFolder<T extends object>(
content?: { [key in keyof T]: string },
) {
rmSync(dirName, { recursive: true, force: true });
mkdirSync(dirName);
mkdirSync(dirName, { recursive: true });
if (content) {
for (const fileName in content) {
writeFileSync(join(dirName, fileName), content[fileName]);
Expand All @@ -19,7 +19,7 @@ export function cleanFolderPutGitKeep<T extends object>(
content?: { [key in keyof T]: string },
) {
rmSync(dirName, { recursive: true, force: true });
mkdirSync(dirName);
mkdirSync(dirName, { recursive: true });
writeFileSync(join(dirName, '.gitkeep'), '');
if (content) {
for (const fileName in content) {
Expand Down
28 changes: 12 additions & 16 deletions packages/models/test/fixtures/code-pushup.config.mock.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { join } from 'path';
import { echoRunnerConfig } from './echo-runner-config.mock';

const outputDir = 'tmp';
module.exports = {
upload: {
Expand All @@ -17,23 +20,16 @@ module.exports = {
docsUrl: 'http://www.my-docs.dev',
},
],
runner: {
command: 'node',
args: [
'-e',
`require('fs').writeFileSync('${outputDir}/out.json', '${JSON.stringify(
[
{
title: 'dummy-title',
slug: 'command-object-audit-slug',
value: 0,
score: 0,
},
],
)}')`,
runner: echoRunnerConfig(
[
{
slug: 'command-object-audit-slug',
value: 0,
score: 0,
},
],
outputFile: `${outputDir}/out.json`,
},
join(outputDir, 'out.json'),
),
groups: [],
slug: 'command-object-plugin',
title: 'command-object plugin',
Expand Down
28 changes: 12 additions & 16 deletions packages/models/test/fixtures/code-pushup.config.mock.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { join } from 'path';
import { echoRunnerConfig } from './echo-runner-config.mock';

const outputDir = 'tmp';
export default {
upload: {
Expand All @@ -17,23 +20,16 @@ export default {
docsUrl: 'http://www.my-docs.dev',
},
],
runner: {
command: 'node',
args: [
'-e',
`require('fs').writeFileSync('${outputDir}/out.json', '${JSON.stringify(
[
{
title: 'dummy-title',
slug: 'command-object-audit-slug',
value: 0,
score: 0,
},
],
)}')`,
runner: echoRunnerConfig(
[
{
slug: 'command-object-audit-slug',
value: 0,
score: 0,
},
],
outputFile: `${outputDir}/out.json`,
},
join(outputDir, 'out.json'),
),
groups: [],
slug: 'command-object-plugin',
title: 'command-object plugin',
Expand Down
28 changes: 12 additions & 16 deletions packages/models/test/fixtures/code-pushup.config.mock.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { join } from 'path';
import { echoRunnerConfig } from './echo-runner-config.mock';

const outputDir = 'tmp';
export default {
upload: {
Expand All @@ -17,23 +20,16 @@ export default {
docsUrl: 'http://www.my-docs.dev',
},
],
runner: {
command: 'node',
args: [
'-e',
`require('fs').writeFileSync('${outputDir}/out.json', '${JSON.stringify(
[
{
title: 'dummy-title',
slug: 'command-object-audit-slug',
value: 0,
score: 0,
},
],
)}')`,
runner: echoRunnerConfig(
[
{
slug: 'command-object-audit-slug',
value: 0,
score: 0,
},
],
outputFile: `${outputDir}/out.json`,
},
join(outputDir, 'out.json'),
),
groups: [],
slug: 'command-object-plugin',
title: 'command-object plugin',
Expand Down
5 changes: 3 additions & 2 deletions packages/models/test/fixtures/code-pushup.config.mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join } from 'path';
import { CoreConfig } from '../../src';
import { echoRunnerConfig } from './echo-runner-config.mock';
import { auditReport } from './plugin-config.mock';
import { runnerConfig } from './runner.mock';

const outputDir = 'tmp';
export default {
Expand All @@ -21,7 +22,7 @@ export default {
docsUrl: 'http://www.my-docs.dev',
},
],
runner: runnerConfig([auditReport()], `${outputDir}/out.json`),
runner: echoRunnerConfig([auditReport()], join(outputDir, 'out.json')),
groups: [],
slug: 'command-object-plugin',
title: 'command-object plugin',
Expand Down
17 changes: 17 additions & 0 deletions packages/models/test/fixtures/echo-runner-config.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { platform } from 'os';
import { AuditOutput, RunnerConfig } from '../../src';

export function echoRunnerConfig(
output: AuditOutput[],
outputFile: string,
): RunnerConfig {
const auditOutput =
platform() === 'win32'
? JSON.stringify(output)
: "'" + JSON.stringify(output) + "'";
return {
command: 'echo',
args: [auditOutput, '>', outputFile],
outputFile,
};
}
4 changes: 2 additions & 2 deletions packages/models/test/fixtures/eslint-plugin.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type {
PluginConfig,
PluginReport,
} from '../../src';
import { echoRunnerConfig } from './echo-runner-config.mock';
import { ESLINT_AUDITS_MAP } from './eslint-audits.mock';
import { runnerConfig } from './runner.mock';

const eslintMeta = {
slug: 'eslint',
Expand All @@ -29,7 +29,7 @@ export function eslintPluginConfig(outputDir = 'tmp'): PluginConfig {
);
return {
...eslintMeta,
runner: runnerConfig(
runner: echoRunnerConfig(
Object.values(ESLINT_AUDITS_MAP),
join(outputDir, 'eslint-out.json'),
),
Expand Down
7 changes: 4 additions & 3 deletions packages/models/test/fixtures/lighthouse-plugin.mock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { join } from 'path';
import type { AuditGroup, PluginReport } from '../../src';
import { Audit, PluginConfig } from '../../src';
import { echoRunnerConfig } from './echo-runner-config.mock';
import { LIGHTHOUSE_AUDIT_REPORTS_MAP } from './lighthouse-audits.mock';
import { runnerConfig } from './runner.mock';

const PLUGIN_GROUP_PERFORMANCE: AuditGroup = {
slug: 'performance',
Expand Down Expand Up @@ -50,9 +51,9 @@ export function lighthousePluginConfig(outputDir = 'tmp'): PluginConfig {
);
return {
...lighthousePluginMeta,
runner: runnerConfig(
runner: echoRunnerConfig(
Object.values(LIGHTHOUSE_AUDIT_REPORTS_MAP),
`${outputDir}/lighthouse-out.json`,
join(outputDir, 'lighthouse-out.json'),
),
audits,
groups: [PLUGIN_GROUP_PERFORMANCE],
Expand Down
4 changes: 2 additions & 2 deletions packages/models/test/fixtures/plugin-config.mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'node:path';
import { Audit, AuditReport, PluginConfig } from '../../src';
import { runnerConfig } from './runner.mock';
import { echoRunnerConfig } from './echo-runner-config.mock';

export function pluginConfig(
auditOutputs: AuditReport[],
Expand All @@ -15,7 +15,7 @@ export function pluginConfig(
description: 'Plugin description',
docsUrl: 'https://my-plugin.docs.dev?1',
audits: auditOutputs.map(auditOutput => auditConfig(auditOutput)),
runner: runnerConfig(auditOutputs, pluginOutputPath),
runner: echoRunnerConfig(auditOutputs, pluginOutputPath),
...(opt || {}),
} satisfies PluginConfig;
}
Expand Down
Loading

0 comments on commit b80255b

Please sign in to comment.