Passed
Push — trunk ( b8f30c...432645 )
by Christian
20:10 queued 06:14
created

src/Administration/Resources/app/administration/src/meta/meta.spec.ts   A

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 68
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 52
mnd 3
bc 3
fnc 0
dl 0
loc 68
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import * as fs from 'fs';
2
import * as path from 'path';
3
import baseline from './baseline';
4
5
6
const getAllFiles = (dirPath: string, arrayOfFiles: Array<string> = null): Array<string> => {
7
    const files = fs.readdirSync(dirPath);
8
9
    arrayOfFiles = arrayOfFiles || [];
10
11
    files.forEach((file): void => {
12
        if (fs.statSync(`${dirPath}/${file}`).isDirectory()) {
13
            arrayOfFiles = getAllFiles(`${dirPath}/${file}`, arrayOfFiles);
14
        } else {
15
            arrayOfFiles.push(path.join(dirPath, '/', file));
16
        }
17
    });
18
19
    return arrayOfFiles;
20
};
21
22
const rootPath = 'src';
23
// @ts-expect-error
24
// eslint-disable-next-line no-undef
25
const testAbleFiles = getAllFiles(path.join(adminPath, rootPath)).filter(file => {
26
    return file.match(/^.*(?<!\.spec)(?<!\/acl\/index)(?<!\.d)\.(js|ts)$/);
27
});
28
29
describe('Administration meta test', () => {
30
    it.each(testAbleFiles)('should have a spec file for %s', (file) => {
31
        // Match 0 holds the whole file path
32
        // Match 1 holds the last folder name e.g. "adapter"
33
        // Match 2 holds the file name e.g. "view.adapter.ts"
34
        // Match 3 holds the file name without extension e.g. "view.adapter"
35
        // Match 4 holds the file extension e.g. "ts"
36
        const regex = /^.*\/(.*)\/((.*)\.(js|ts))$/;
37
38
        const [whole, lastFolder, fileName, fileNameWithoutExtension, extension] = file.match(regex);
39
        if (baseline.includes(fileName) || baseline.includes(`${lastFolder}/${fileName}`)) {
40
            expect(true).toBe(true);
41
42
            return;
43
        }
44
45
        const specFile = whole.replace(fileName, `${fileNameWithoutExtension}.spec.${extension}`);
46
        const specFileExists = fs.existsSync(specFile);
47
48
        const specFileWithFolderName = whole.replace(fileName, `${lastFolder}.spec.${extension}`);
49
        const specFileWithFolderNameExists = fs.existsSync(specFileWithFolderName);
50
51
        let specFileAlternativeExtension = '';
52
        let specFileWithFolderNameAlternativeExtension = '';
53
        if (extension === 'js') {
54
            specFileAlternativeExtension = specFile.replace('.js', '.ts');
55
            specFileWithFolderNameAlternativeExtension = specFileWithFolderName.replace('.js', '.ts');
56
        } else {
57
            specFileAlternativeExtension = specFile.replace('.ts', '.js');
58
            specFileWithFolderNameAlternativeExtension = specFileWithFolderName.replace('.ts', '.js');
59
        }
60
        const specFileAlternativeExtensionExists = fs.existsSync(specFileAlternativeExtension);
61
        const specFileWithFolderNameAlternativeExtensionExists = fs.existsSync(specFileWithFolderNameAlternativeExtension);
62
63
        const fileIsTested = specFileExists || specFileWithFolderNameExists || specFileAlternativeExtensionExists || specFileWithFolderNameAlternativeExtensionExists;
64
65
        expect(fileIsTested).toBeTruthy();
66
    });
67
});
68