|
7 | 7 | * SPDX-License-Identifier: MIT |
8 | 8 | */ |
9 | 9 |
|
| 10 | +import { readFileSync } from 'fs'; |
| 11 | +import { getFilesInDirectory } from './fileloader'; |
| 12 | + |
| 13 | +const readSvelteFileScript = (fileLocation: string) => { |
| 14 | + const fullContent = readFileSync(fileLocation).toString(); |
| 15 | + const scriptMatches = fullContent.match(/<script[^>]*>([\s\S]*?)<\/script>/g); |
| 16 | + return scriptMatches?.join(''); |
| 17 | +}; |
| 18 | + |
10 | 19 | describe('Dependency direction test', () => { |
11 | | - test('Files in lib should not depend on files in components', () => {}); |
| 20 | + // A crude way to enforce direction of dependencies, inspired by ArchUnit for java |
| 21 | + test('Files in lib should not depend on files in components', () => { |
| 22 | + const libFiles = getFilesInDirectory('./src/lib'); |
| 23 | + const libContent = libFiles.map(e => { |
| 24 | + return { |
| 25 | + file: e, |
| 26 | + content: readFileSync(e), |
| 27 | + }; |
| 28 | + }); |
| 29 | + const componentFiles = getFilesInDirectory('./src/components'); |
| 30 | + const componentContent = componentFiles.map(e => { |
| 31 | + if (e.endsWith('.svelte')) { |
| 32 | + return { |
| 33 | + file: e, |
| 34 | + content: readSvelteFileScript(e), |
| 35 | + }; |
| 36 | + } else { |
| 37 | + return { |
| 38 | + file: e, |
| 39 | + content: readFileSync(e), |
| 40 | + }; |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + const violatingFiles = libContent.filter(e => e.content.includes('components/')); |
| 45 | + |
| 46 | + const violationMessage = violatingFiles.reduce((pre, file) => { |
| 47 | + return ( |
| 48 | + pre + |
| 49 | + `\n \u001b[35m${file.file}\t \u001b[0mis dependent on components, but is located in lib folder` |
| 50 | + ); |
| 51 | + }, ''); |
| 52 | + expect(violatingFiles.length, violationMessage).toBe(0); |
| 53 | + }); |
12 | 54 | }); |
0 commit comments