Skip to content

Commit

Permalink
fix(compiler): normalize recommended collection path for `package.j…
Browse files Browse the repository at this point in the history
…son` validation (#4522)

* normalize recommended path for comparison

* add tests for collection value

* normalize expected path in tests

* I hate paths
  • Loading branch information
tanner-reits committed Jun 27, 2023
1 parent 2d5d98e commit af9639c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/compiler/types/tests/validate-package-json.spec.ts
@@ -1,5 +1,6 @@
import type * as d from '@stencil/core/declarations';
import { mockBuildCtx, mockCompilerCtx, mockValidatedConfig } from '@stencil/core/testing';
import { normalizePath } from '@utils';
import path from 'path';

import * as v from '../validate-build-package-json';
Expand Down Expand Up @@ -99,9 +100,33 @@ describe('validate-package-json', () => {
});

describe('collection', () => {
it('should error when missing collection property', async () => {
it('should produce a warning when missing collection property', async () => {
v.validateCollection(config, compilerCtx, buildCtx, collectionOutputTarget);

expect(buildCtx.diagnostics[0].messageText).toMatch(/package.json "collection" property is required/);
expect(buildCtx.diagnostics[0].level).toBe('warn');
});

it('should produce a warning if the supplied path does not match the recommended path', () => {
buildCtx.packageJson.collection = 'bad/path';

v.validateCollection(config, compilerCtx, buildCtx, collectionOutputTarget);

expect(buildCtx.diagnostics[0].messageText).toBe(
`package.json "collection" property is required when generating a distribution and must be set to: ${normalizePath(
'dist/collection/collection-manifest.json',
false
)}`
);
expect(buildCtx.diagnostics[0].level).toBe('warn');
});

it('should not produce a warning if the normalized paths are the same', () => {
buildCtx.packageJson.collection = './dist/collection/collection-manifest.json';

v.validateCollection(config, compilerCtx, buildCtx, collectionOutputTarget);

expect(buildCtx.diagnostics.length).toEqual(0);
});
});
});
7 changes: 5 additions & 2 deletions src/compiler/types/validate-build-package-json.ts
Expand Up @@ -149,8 +149,11 @@ export const validateCollection = (
outputTarget: d.OutputTargetDistCollection
) => {
if (outputTarget.collectionDir) {
const collectionRel = join(relative(config.rootDir, outputTarget.collectionDir), COLLECTION_MANIFEST_FILE_NAME);
if (!buildCtx.packageJson.collection || normalizePath(buildCtx.packageJson.collection) !== collectionRel) {
const collectionRel = normalizePath(
join(relative(config.rootDir, outputTarget.collectionDir), COLLECTION_MANIFEST_FILE_NAME),
false
);
if (!buildCtx.packageJson.collection || normalizePath(buildCtx.packageJson.collection, false) !== collectionRel) {
const msg = `package.json "collection" property is required when generating a distribution and must be set to: ${collectionRel}`;
packageJsonWarn(config, compilerCtx, buildCtx, msg, `"collection"`);
}
Expand Down

0 comments on commit af9639c

Please sign in to comment.