-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #278 from microbit-foundation/277-add-test-to-veri…
…fy-license-identifier-placement-in-files 277 add test to verify license identifier placement in files
- Loading branch information
Showing
2 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters