Skip to content

Commit

Permalink
Add test reporting to component generation
Browse files Browse the repository at this point in the history
  • Loading branch information
devpow112 committed Jul 30, 2024
1 parent 1de8507 commit 760cab1
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 36 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"author": "D2L Corporation",
"license": "Apache-2.0",
"dependencies": {
"deepmerge": "^4",
"prompts": "^2"
},
"devDependencies": {
Expand Down
46 changes: 40 additions & 6 deletions src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import { run as setupElement } from './generators/wc-lit-element/index.js';
import { run as setupLocalization } from './generators/localization/index.js';
import { run as setupRelease } from './generators/release/index.js';
import { run as setupStaticSite } from './generators/static-site/index.js';
import { run as setupTestReporting } from './generators/test-reporting/index.js';
import { run as setupTestUnitAxe } from './generators/test-unit-axe/index.js';
import { run as setupTestVdiff } from './generators/test-vdiff/index.js';
import { run as setupTestVDiff } from './generators/test-vdiff/index.js';

const generatorTypes = {
component: 'component',
Expand Down Expand Up @@ -78,7 +79,7 @@ async function getGeneratorType() {
}

async function getComponentOptions() {
const questions = [
let templateData = await prompts([
{
type: 'text',
name: 'hyphenatedName',
Expand Down Expand Up @@ -121,12 +122,45 @@ async function getComponentOptions() {
{ title: 'No', value: false }
]
},
];
return await prompts(questions, {
{
type: 'select',
name: 'testReporting',
message: 'Would you like test reporting set up?',
choices: [
{ title: 'Yes', value: true },
{ title: 'No', value: false }
]
}
], {
onCancel: () => {
process.exit();
},
});

if (templateData.testReporting) {
const testReportingTemplateData = await prompts([
{
type: 'text',
name: 'testReportingTool',
message: 'What is your components tool based on the taxonomy in https://expanse.desire2learn.com/pages/source/source.html'
}, {
type: 'text',
name: 'testReportingExperience',
message: 'What is your components experience based on the taxonomy in https://expanse.desire2learn.com/pages/source/source.html'
},
], {
onCancel: () => {
process.exit();
},
});

templateData = {
...templateData,
...testReportingTemplateData
};
}

return templateData;
}

async function executeComponentGenerator() {
Expand All @@ -148,12 +182,12 @@ async function executeComponentGenerator() {

setupDefaultContent(options);
setupElement(options);
if (options.testReporting) setupTestReporting(options);
setupTestUnitAxe(options);
if (options.vdiff) setupTestVdiff(options);
if (options.vdiff) setupTestVDiff(options);
setupDemo(options);
if (options.localization) setupLocalization(options);
setupRelease(options);

}

async function executeStaticSiteGenerator() {
Expand Down
3 changes: 2 additions & 1 deletion src/generators/demo/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copyFile, getDestinationPath, mergeJSON, mergeText, replaceText } from '../../helper.js';
import { copyFile, getDestinationPath, mergeJSON, mergeText, replaceText, sortJSONMembers } from '../../helper.js';

export function run(templateData) {
mergeJSON(
Expand All @@ -15,4 +15,5 @@ export function run(templateData) {
`${getDestinationPath(templateData.hyphenatedName)}/demo/index.html`
);
replaceText(`${getDestinationPath(templateData.hyphenatedName)}/demo/index.html`, templateData);
sortJSONMembers(`${getDestinationPath(templateData.hyphenatedName)}/package.json`, ['dependencies', 'devDependencies']);
}
19 changes: 19 additions & 0 deletions src/generators/test-reporting/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { copyFile, getDestinationPath, mergeText, replaceText } from '../../helper.js';

export function run(templateData) {
templateData = {
...templateData,
testReportingTool: templateData.testReportingTool?.trim() || 'Unknown',
testReportingExperience: templateData.testReportingExperience?.trim() || 'Unknown'
};

mergeText(
`${__dirname}/templates/configured/_gitignore`,
`${getDestinationPath(templateData.hyphenatedName)}/.gitignore`
);
copyFile(
`${__dirname}/templates/configured/_d2l-test-reporting.config.json`,
`${getDestinationPath(templateData.hyphenatedName)}/d2l-test-reporting.config.json`
);
replaceText(`${getDestinationPath(templateData.hyphenatedName)}/d2l-test-reporting.config.json`, templateData);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tool": "<%= testReportingTool %>",
"experience": "<%= testReportingExperience %>"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d2l-test-report.json
21 changes: 19 additions & 2 deletions src/generators/test-unit-axe/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copyFile, copyFilesInDir, getDestinationPath, mergeJSON, mergeText, replaceText } from '../../helper.js';
import { copyFile, getDestinationPath, mergeJSON, mergeText, replaceText, sortJSONMembers } from '../../helper.js';

export function run(templateData) {
mergeJSON(
Expand All @@ -17,6 +17,23 @@ export function run(templateData) {
`${__dirname}/templates/configured/_element.axe.js`,
`${getDestinationPath(templateData.hyphenatedName)}/test/${templateData.hyphenatedName}.axe.js`
);

if (templateData.testReporting) {
mergeJSON(
`${__dirname}/templates/configured/_d2l-test-reporting.config.json`,
`${getDestinationPath(templateData.hyphenatedName)}/d2l-test-reporting.config.json`
);
copyFile(
`${__dirname}/templates/static/.github/workflows/ci-test-reporting.yml`,
`${getDestinationPath(templateData.hyphenatedName)}/.github/workflows/ci.yml`
);
} else {
copyFile(
`${__dirname}/templates/static/.github/workflows/ci.yml`,
`${getDestinationPath(templateData.hyphenatedName)}/.github/workflows/ci.yml`
);
}

replaceText(`${getDestinationPath(templateData.hyphenatedName)}/test/${templateData.hyphenatedName}.test.js`, templateData);
copyFilesInDir(`${__dirname}/templates/static`, getDestinationPath(templateData.hyphenatedName));
sortJSONMembers(`${getDestinationPath(templateData.hyphenatedName)}/package.json`, ['dependencies', 'devDependencies']);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"overrides": [
{
"pattern": "**/test/*.test.js",
"type": "UI Unit"
},
{
"pattern": "**/test/*.axe.js",
"type": "UI Accessibility"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI
on: pull_request
jobs:
test:
name: Test
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: Brightspace/third-party-actions@actions/checkout
- name: Setup node
uses: Brightspace/third-party-actions@actions/setup-node
with:
node-version-file: .nvmrc
- name: Install dependencies
run: npm install
- name: Lint (JavaScript)
run: npm run lint:eslint
- name: Lint (CSS)
run: npm run lint:style
- name: Accessibility tests
id: at
run: npm run test:axe
- name: Upload test report
if: >
always() &&
github.triggering_actor != 'dependabot[bot]' &&
(steps.at.outcome == 'failure' || steps.at.outcome == 'success')
uses: Brightspace/test-reporting-action@main
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }}
- name: Unit tests
id: ut
run: npm run test:unit
- name: Upload test report
if: >
always() &&
github.triggering_actor != 'dependabot[bot]' &&
(steps.ut.outcome == 'failure' || steps.ut.outcome == 'success')
uses: Brightspace/test-reporting-action@main
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }}
20 changes: 18 additions & 2 deletions src/generators/test-vdiff/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copyFile, copyFilesInDir, getDestinationPath, mergeJSON, mergeText, replaceText } from '../../helper.js';
import { copyFile, getDestinationPath, mergeJSON, mergeText, replaceText, sortJSONMembers } from '../../helper.js';

export function run(templateData) {
mergeJSON(
Expand All @@ -20,5 +20,21 @@ export function run(templateData) {
);
replaceText(`${getDestinationPath(templateData.hyphenatedName)}/test/${templateData.hyphenatedName}.vdiff.js`, templateData);

copyFilesInDir(`${__dirname}/templates/static`, getDestinationPath(templateData.hyphenatedName));
if (templateData.testReporting) {
mergeJSON(
`${__dirname}/templates/configured/_d2l-test-reporting.config.json`,
`${getDestinationPath(templateData.hyphenatedName)}/d2l-test-reporting.config.json`
);
copyFile(
`${__dirname}/templates/static/.github/workflows/vdiff-test-reporting.yml`,
`${getDestinationPath(templateData.hyphenatedName)}/.github/workflows/vdiff.yml`
);
} else {
copyFile(
`${__dirname}/templates/static/.github/workflows/vdiff.yml`,
`${getDestinationPath(templateData.hyphenatedName)}/.github/workflows/vdiff.yml`
);
}

sortJSONMembers(`${getDestinationPath(templateData.hyphenatedName)}/package.json`, ['dependencies', 'devDependencies']);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"overrides": [
{
"pattern": "**/test/*.vdiff.js",
"type": "UI Visual Diff"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: vdiff
on: pull_request
jobs:
vdiff:
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- uses: Brightspace/third-party-actions@actions/checkout
- uses: Brightspace/third-party-actions@actions/setup-node
with:
node-version-file: .nvmrc
- name: Install Dependencies
run: npm install
- name: vdiff Tests
uses: BrightspaceUI/actions/vdiff@main
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Upload test report
if: failure() || success()
uses: Brightspace/test-reporting-action@main
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }}
3 changes: 2 additions & 1 deletion src/generators/wc-lit-element/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copyFile, copyFilesInDir, getDestinationPath, mergeJSON, replaceText } from '../../helper.js';
import { copyFile, copyFilesInDir, getDestinationPath, mergeJSON, replaceText, sortJSONMembers } from '../../helper.js';

export function run(templateData) {
mergeJSON(
Expand All @@ -20,4 +20,5 @@ export function run(templateData) {
replaceText(`${getDestinationPath(templateData.hyphenatedName)}/${templateData.hyphenatedName}.js`, templateDataElement);

copyFilesInDir(`${__dirname}/templates/static`, getDestinationPath(templateData.hyphenatedName));
sortJSONMembers(`${getDestinationPath(templateData.hyphenatedName)}/package.json`, ['dependencies', 'devDependencies']);
}
45 changes: 21 additions & 24 deletions src/helper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import deepMerge from 'deepmerge';
import fs from 'fs';
import path from 'path';

Expand All @@ -11,31 +12,9 @@ export function mergeJSON(filePathNewJSON, filePathOriginalJSON) {

const newContentJSON = JSON.parse(newContent);
const originalContentJSON = JSON.parse(originalContent);
const mergedContentJSON = deepMerge(originalContentJSON, newContentJSON);

Object.keys(originalContentJSON).forEach(key => {
if (newContentJSON[key]) {
let changes = false;
Object.keys(newContentJSON[key]).forEach(subkey => {
originalContentJSON[key][subkey] = newContentJSON[key][subkey];
changes = true;
});
if (changes) {
const ordered = {};
Object.keys(originalContentJSON[key]).sort().forEach(subkey => {
ordered[subkey] = originalContentJSON[key][subkey];
});
originalContentJSON[key] = ordered;
}
}
});

Object.keys(newContentJSON).forEach(key => {
if (!originalContentJSON[key]) {
originalContentJSON[key] = newContentJSON[key];
}
});

fs.writeFileSync(filePathOriginalJSON, JSON.stringify(originalContentJSON, null, 2));
fs.writeFileSync(filePathOriginalJSON, JSON.stringify(mergedContentJSON, null, 2));
}

export function mergeText(filePathNewText, filePathOriginalText) {
Expand Down Expand Up @@ -81,6 +60,24 @@ export function replaceText(source, replacements) {
fs.writeFileSync(source, result, 'utf8');
}

export const sortJSONMembers = (source, members) => {
const fileContent = fs.readFileSync(source, 'utf8');
const result = JSON.parse(fileContent);

members.forEach(member => {
if (!result[member]) {
return;
}
const ordered = {};
Object.keys(result[member]).sort().forEach(key => {
ordered[key] = result[member][key];
});
result[member] = ordered;
});

fs.writeFileSync(source, JSON.stringify(result, null, 2));
};

function copyAndProcessFile(sourceRoot, destinationRoot, relativePath, fileName, plugins = []) {
const context = {
sourceRoot,
Expand Down

0 comments on commit 760cab1

Please sign in to comment.