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

277 add test to verify license identifier placement in files #278

Merged
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
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
Loading