Skip to content

Commit

Permalink
Move build scripts into its own folder; add unittests (mdn#21638)
Browse files Browse the repository at this point in the history
* Move build scripts into its own folder; add unittests

* Apply suggestions from code review

Co-authored-by: Florian Scholz <[email protected]>

---------

Co-authored-by: Florian Scholz <[email protected]>
  • Loading branch information
queengooborg and Elchi3 authored Dec 18, 2023
1 parent c95c9bc commit cf69a62
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 13 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ __enumerating__*/
.idea
*.log
/node_modules/
build/
/build/
!/scripts/build/
yarn.lock
.nyc_output/
coverage.lcov
coverage/
types/types.d.ts
.DS_Store
.DS_Store
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
*.json
!/*.json
!/schemas/*.json
!/scripts/build/

LICENSE
.*ignore
.editorconfig
/package-lock.json
/CODE_OF_CONDUCT.md
build/
/build/
coverage/
.features.json
types.d.ts
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"lint:fix": "node --loader=ts-node/esm --no-warnings=ExperimentalWarning scripts/fix/index.ts",
"fix": "npm run format:fix && npm run lint:fix",
"stats": "node --loader=ts-node/esm --no-warnings=ExperimentalWarning scripts/statistics.ts",
"build": "node --loader=ts-node/esm --no-warnings=ExperimentalWarning scripts/release/build.ts",
"build": "node --loader=ts-node/esm --no-warnings=ExperimentalWarning scripts/build/index.ts",
"gentypes": "node --loader=ts-node/esm --no-warnings=ExperimentalWarning scripts/generate-types.ts",
"release": "node --loader=ts-node/esm --no-warnings=ExperimentalWarning scripts/release/index.ts",
"remove-redundant-flags": "node --loader=ts-node/esm --no-warnings=ExperimentalWarning scripts/remove-redundant-flags.ts",
Expand Down
116 changes: 116 additions & 0 deletions scripts/build/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */

import assert from 'node:assert/strict';

import { walk } from '../../utils/index.js';

import {
generateMeta,
applyMirroring,
addVersionLast,
createManifest,
} from './index.js';

describe('Build functions', () => {
it('generateMeta', () => {
const result = generateMeta();
assert.ok(result.version);
assert.ok(result.timestamp instanceof Date);
});

it('applyMirroring', () => {
const data: any = {
feature: {
__compat: {
support: {
chrome: {
version_added: '90',
},
edge: 'mirror',
firefox: {
version_added: '40',
},
ie: {
version_added: false,
},
opera: 'mirror',
safari: {
version_added: '10',
},
},
},
},
};

const walker = walk(undefined, data);
for (const feature of walker) {
applyMirroring(feature);
}

assert.equal(data.feature.__compat.support.edge.version_added, '90');
assert.equal(data.feature.__compat.support.opera.version_added, '76');
});

it('addVersionLast', () => {
const data: any = {
feature: {
__compat: {
support: {
chrome: {
version_added: '10',
version_removed: '20',
},
firefox: [
{
version_added: '18',
},
{
version_added: '1',
version_removed: '4',
},
],
safari: {
version_added: '10',
version_removed: 'preview',
version_last: 'preview',
},
edge: {
version_added: '12',
version_removed: true,
version_last: true,
},
},
},
},
};

const walker = walk(undefined, data);
for (const feature of walker) {
addVersionLast(feature);
}

assert.equal(data.feature.__compat.support.chrome.version_last, '19');
assert.equal(
data.feature.__compat.support.firefox[0].version_last,
undefined,
);
assert.equal(data.feature.__compat.support.firefox[1].version_last, '3.6');
assert.equal(data.feature.__compat.support.safari.version_last, 'preview');
assert.equal(data.feature.__compat.support.edge.version_last, true);
});
it('createManifest', () => {
const manifest = createManifest();
assert.ok(manifest.main);
assert.ok(manifest.exports);
assert.ok(manifest.types);
assert.ok(manifest.name);
assert.ok(manifest.description);
assert.ok(manifest.repository);
assert.ok(manifest.keywords);
assert.ok(manifest.author);
assert.ok(manifest.license);
assert.ok(manifest.bugs);
assert.ok(manifest.homepage);
});
});
20 changes: 14 additions & 6 deletions scripts/release/build.ts → scripts/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,26 @@ export const addVersionLast = (feature: WalkOutput): void => {
};

/**
* Generate a BCD data bundle
* @returns An object containing the prepared BCD data
* Applies transforms to the given data.
* @param data - The data to apply transforms to.
*/
export const createDataBundle = async (): Promise<CompatData> => {
const { default: bcd } = await import('../../index.js');

const walker = walk(undefined, bcd);
export const applyTransforms = (data): void => {
const walker = walk(undefined, data);

for (const feature of walker) {
applyMirroring(feature);
addVersionLast(feature);
}
};

/**
* Generate a BCD data bundle
* @returns An object containing the prepared BCD data
*/
export const createDataBundle = async (): Promise<CompatData> => {
const { default: bcd } = await import('../../index.js');

applyTransforms(bcd);

return {
...bcd,
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion scripts/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { query } from '../utils/index.js';
import { SupportStatement, Identifier, BrowserName } from '../types/types.js';

import { getMergeBase, getFileContent, getGitDiffStatuses } from './lib/git.js';
import mirror from './release/mirror.js';
import mirror from './build/mirror.js';

interface Contents {
base: string;
Expand Down
2 changes: 1 addition & 1 deletion scripts/fix/mirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '../../types/index.js';
import bcd from '../../index.js';
import { walk } from '../../utils/index.js';
import mirrorSupport from '../release/mirror.js';
import mirrorSupport from '../build/mirror.js';

const downstreamBrowsers = (
Object.keys(bcd.browsers) as (keyof typeof bcd.browsers)[]
Expand Down
2 changes: 1 addition & 1 deletion test/linter/test-consistency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
InternalSupportStatement,
} from '../../types/index.js';
import { query } from '../../utils/index.js';
import mirrorSupport from '../../scripts/release/mirror.js';
import mirrorSupport from '../../scripts/build/mirror.js';

type ErrorType =
| 'unsupported'
Expand Down

0 comments on commit cf69a62

Please sign in to comment.