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

Don't normalize package.json fields #1143

Merged
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
4 changes: 4 additions & 0 deletions node-src/__mocks__/invalidPackageJson/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "chromatic",
"version": "invalid-semver"
}
14 changes: 13 additions & 1 deletion node-src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { execaCommand as execaDefault } from 'execa';
import jsonfile from 'jsonfile';
import { confirm } from 'node-ask';
import fetchDefault from 'node-fetch';
import path from 'path';
import { Readable } from 'stream';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { getGitInfo, runAll } from '.';
import { getGitInfo, run, runAll } from '.';
import * as git from './git/git';
import { DNSResolveAgent } from './io/getDNSResolveAgent';
import * as checkPackageJson from './lib/checkPackageJson';
Expand Down Expand Up @@ -853,3 +854,14 @@ describe('getGitInfo', () => {
});
});
});

describe('parsing package.json', () => {
it('should handle invalid `version` strings', async () => {
vi.spyOn(process, 'cwd').mockReturnValue(
path.resolve('./node-src/__mocks__/invalidPackageJson')
);

const result = await run({ flags: { dryRun: true } });
expect(result.code).toBeDefined();
});
});
4 changes: 3 additions & 1 deletion node-src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ export async function run({
log = createLogger(config.flags, config.extraOptions),
} = extraOptions || {};

const packageInfo = await readPackageUp({ cwd: process.cwd() });
// We don't normalize because if the `version` field isn't a proper semver string, the process
// silently exits.
const packageInfo = await readPackageUp({ cwd: process.cwd(), normalize: false });
if (!packageInfo) {
log.error(noPackageJson());
process.exit(253);
Expand Down
5 changes: 5 additions & 0 deletions node-src/lib/parseArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ export default function parseArguments(argv: string[]) {
argv,
booleanDefault: undefined,
description: false,

// Prevent meow from parsing the project's package.json file because if the `version` field
// isn't a proper semver string, the process silently exits.
pkg: {},

version: pkg.version,
flags: {
// Required options
Expand Down
Loading