Skip to content

Commit

Permalink
Merge pull request #278 from microbit-foundation/277-add-test-to-veri…
Browse files Browse the repository at this point in the history
…fy-license-identifier-placement-in-files

277 add test to verify license identifier placement in files
  • Loading branch information
Karlo-Emilo authored Jul 28, 2023
2 parents f4255ff + 48f8bdc commit 1ef7df6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
91 changes: 91 additions & 0 deletions src/__tests__/license-identifiers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* @jest-environment jsdom
*/

import fs from 'fs';
import 'jest-expect-message';
import * as path from 'path';

// Place files you wish to ignore by name in here
const ignoredFiles: string[] = [];

const licenseIdentifierStringContributors =
'Center for Computational Thinking and Design at Aarhus University and contributors';

const licenseIdentifierStringSPDX = 'SPDX-License-Identifier:';

const readFile = (fileLocation: string, expect: string) => {
const fileContent = fs.readFileSync(fileLocation);
return fileContent.toString().toLowerCase().includes(expect.toLowerCase());
};

type DirectoryContents = {
files: string[];
folders: string[];
};

const readDirectory = (directory: string, ignoreList: string[]): DirectoryContents => {
const files: string[] = [];
const folders: string[] = [];
const filesRead = fs.readdirSync(directory);
filesRead.forEach(file => {
if (ignoreList.includes(file)) return;
const fileLocation = path.join(directory, file);
const stats = fs.statSync(fileLocation);
if (stats.isFile()) {
files.push(fileLocation);
} else {
folders.push(fileLocation);
}
});
return { files: files, folders: folders };
};

const flattenDirectory = (directory: string): string[] => {
const files: string[] = [];
const content = readDirectory(directory, ignoredFiles);
const filesFromSubFolders: string[] = [];
content.folders.forEach(value => {
const subFolderFlat = flattenDirectory(value);
subFolderFlat.forEach(value => filesFromSubFolders.push(value));
});
filesFromSubFolders.forEach(value => files.push(value));
content.files.forEach(value => files.push(value));
return files;
};

const filesMissingIdentifier = (files: string[], expects: string[]): string[] => {
const filesWithMissingIdentifier: string[] = [];

for (let i = 0; i < files.length; i++) {
for (const expect of expects) {
if (!readFile('./' + files[i], expect)) {
if (!filesWithMissingIdentifier.includes(files[i])) {
filesWithMissingIdentifier.push(files[i]);
}
}
}
}
return filesWithMissingIdentifier;
};

describe('License identifier tests', () => {
test(
'All files should contain license identifier',
() => {
const flatten = flattenDirectory('./src/');
const faultyFiles = filesMissingIdentifier(flatten, [
licenseIdentifierStringContributors,
licenseIdentifierStringSPDX,
]);
expect(
faultyFiles.length,
'Some files do not contain identifier! ' +
faultyFiles
.map(val => `\n \u001b[35m${val} \u001b[0mis missing license identifier`)
.join(),
).toEqual(0);
},
60000 * 10,
);
});
2 changes: 1 addition & 1 deletion src/__tests__/unusedTranslations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const filesIncludesExpression = (files: string[], expect: string): boolean => {

test(
'All translations should be used',
async () => {
() => {
const translationKeys = Object.getOwnPropertyNames(translations.en);
const flatten = flattenDirectory('./src/');
for (let i = 0; i < translationKeys.length; i++) {
Expand Down

0 comments on commit 1ef7df6

Please sign in to comment.