Files
privacy.sexy/tests/unit/application/collections/NoUnintentedInlining.spec.ts
undergroundwires 31f70913a2 Refactor to improve iterations
- Use function abstractions (such as map, reduce, filter etc.) over
  for-of loops to gain benefits of having less side effects and easier
  readability.
- Enable `downLevelIterations` for writing modern code with lazy evaluation.
- Refactor for of loops to named abstractions to clearly express their
  intentions without needing to analyse the loop itself.
- Add missing cases for changes that had no tests.
2022-01-04 21:45:22 +01:00

67 lines
2.1 KiB
TypeScript

import 'mocha';
import { expect } from 'chai';
import WindowsData from 'raw-loader!@/application/collections/windows.yaml';
import MacOsData from 'raw-loader!@/application/collections/macos.yaml';
/*
A common mistake when working with yaml files to forget mentioning that a value should
be interpreted as multi-line string using "|".
E.g.
```
code: |-
echo Hello
echo World
```
If "|" is missing then the code is inlined like `echo Hello echo World``, which can be
unintended. This test checks for similar issues in collection yaml files.
These tests can be considered as "linter" more than "unit-test" and therefore can lead
to false-positives.
*/
describe('collection files to have no unintended inlining', async () => {
// arrange
const testCases = [{
name: 'macos',
fileContent: MacOsData,
}, {
name: 'windows',
fileContent: WindowsData,
},
];
for (const testCase of testCases) {
it(`${testCase.name}`, async () => {
const lines = await findBadLineNumbers(testCase.fileContent);
// assert
expect(lines).to.be.have.lengthOf(0, printMessage());
function printMessage(): string {
return 'Did you intend to have multi-lined string in lines: ' // eslint-disable-line prefer-template
+ lines.map(((line) => line.toString())).join(', ');
}
});
}
});
async function findBadLineNumbers(fileContent: string): Promise<number[]> {
return [
...findLineNumbersEndingWith(fileContent, 'revertCode:'),
...findLineNumbersEndingWith(fileContent, 'code:'),
];
}
function findLineNumbersEndingWith(content: string, ending: string): number[] {
sanityCheck(content, ending);
return content
.split(/\r\n|\r|\n/)
.map((line, index) => ({ text: line, index }))
.filter((line) => line.text.trim().endsWith(ending))
.map((line) => line.index + 1 /* first line is 1, not 0 */);
}
function sanityCheck(content: string, ending: string): void {
if (!content.includes(ending)) {
throw new Error(
`File does not contain string "${ending}" string at all.`
+ `Did the word "${ending}" change? Or is this sanity check wrong?`,
);
}
}