Improve compiler output for line validation
This commit improves feedback when a line is too long to enhance developer/maintainer productivity. - Trim long lines in output to 500 characters - Show character count exceeding max line length - Refactor line formatting for better readability
This commit is contained in:
@@ -3,161 +3,173 @@ import type { CodeLine, InvalidCodeLine } from '@/application/Parser/Executable/
|
||||
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
|
||||
import { analyzeTooLongLines } from '@/application/Parser/Executable/Script/Validation/Analyzers/AnalyzeTooLongLines';
|
||||
import { createCodeLines } from './CreateCodeLines';
|
||||
import { expectSameInvalidCodeLines } from './ExpectSameInvalidCodeLines';
|
||||
import { expectInvalidCodeLines, expectSameInvalidCodeLines } from './ExpectSameInvalidCodeLines';
|
||||
|
||||
describe('AnalyzeTooLongLines', () => {
|
||||
describe('analyzeTooLongLines', () => {
|
||||
describe('batchfile', () => {
|
||||
const MAX_BATCHFILE_LENGTH = 8191;
|
||||
|
||||
it('returns no results for lines within the maximum length', () => {
|
||||
// arrange
|
||||
const expected: InvalidCodeLine[] = [];
|
||||
const context = new TestContext()
|
||||
.withLanguage(ScriptingLanguage.batchfile)
|
||||
.withLines([
|
||||
'A'.repeat(MAX_BATCHFILE_LENGTH),
|
||||
'B'.repeat(8000),
|
||||
'C'.repeat(100),
|
||||
]);
|
||||
// act
|
||||
const actual = context.analyze();
|
||||
// assert
|
||||
expectSameInvalidCodeLines(actual, expected);
|
||||
});
|
||||
|
||||
it('identifies a single line exceeding maximum length', () => {
|
||||
// arrange
|
||||
const expectedLength = MAX_BATCHFILE_LENGTH + 1;
|
||||
const expected: InvalidCodeLine[] = [{
|
||||
lineNumber: 2,
|
||||
error: createTooLongLineError(expectedLength, MAX_BATCHFILE_LENGTH),
|
||||
}];
|
||||
const context = new TestContext()
|
||||
.withLanguage(ScriptingLanguage.batchfile)
|
||||
.withLines([
|
||||
'A'.repeat(MAX_BATCHFILE_LENGTH),
|
||||
'B'.repeat(expectedLength),
|
||||
'C'.repeat(100),
|
||||
]);
|
||||
// act
|
||||
const actual = context.analyze();
|
||||
// assert
|
||||
expectSameInvalidCodeLines(actual, expected);
|
||||
});
|
||||
|
||||
it('identifies multiple lines exceeding maximum length', () => {
|
||||
// arrange
|
||||
const expectedLength1 = MAX_BATCHFILE_LENGTH + 1;
|
||||
const expectedLength2 = MAX_BATCHFILE_LENGTH + 2;
|
||||
const expected: InvalidCodeLine[] = [
|
||||
{
|
||||
lineNumber: 1,
|
||||
error: createTooLongLineError(expectedLength1, MAX_BATCHFILE_LENGTH),
|
||||
},
|
||||
{
|
||||
lineNumber: 3,
|
||||
error: createTooLongLineError(expectedLength2, MAX_BATCHFILE_LENGTH),
|
||||
},
|
||||
];
|
||||
const context = new TestContext()
|
||||
.withLanguage(ScriptingLanguage.batchfile)
|
||||
.withLines([
|
||||
'A'.repeat(expectedLength1),
|
||||
'B'.repeat(MAX_BATCHFILE_LENGTH),
|
||||
'C'.repeat(expectedLength2),
|
||||
]);
|
||||
// act
|
||||
const actual = context.analyze();
|
||||
// assert
|
||||
expectSameInvalidCodeLines(actual, expected);
|
||||
describe('returns no results for lines within the maximum length', () => {
|
||||
createScriptLanguageScenarios().forEach((scenario) => {
|
||||
it(scenario.description, () => {
|
||||
// arrange
|
||||
const expected: InvalidCodeLine[] = [];
|
||||
const context = new TestContext()
|
||||
.withLanguage(scenario.language)
|
||||
.withLines([
|
||||
'A'.repeat(scenario.maxLength),
|
||||
'B'.repeat(scenario.maxLength - 1),
|
||||
'C'.repeat(100),
|
||||
]);
|
||||
// act
|
||||
const actual = context.analyze();
|
||||
// assert
|
||||
expectSameInvalidCodeLines(actual, expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('shellscript', () => {
|
||||
const MAX_SHELLSCRIPT_LENGTH = 1048576;
|
||||
|
||||
it('returns no results for lines within the maximum length', () => {
|
||||
// arrange
|
||||
const expected: InvalidCodeLine[] = [];
|
||||
const context = new TestContext()
|
||||
.withLanguage(ScriptingLanguage.shellscript)
|
||||
.withLines([
|
||||
'A'.repeat(MAX_SHELLSCRIPT_LENGTH),
|
||||
'B'.repeat(1000000),
|
||||
'C'.repeat(100),
|
||||
]);
|
||||
// act
|
||||
const actual = context.analyze();
|
||||
// assert
|
||||
expectSameInvalidCodeLines(actual, expected);
|
||||
describe('identifies a single line exceeding maximum length', () => {
|
||||
createScriptLanguageScenarios().forEach((scenario) => {
|
||||
it(scenario.description, () => {
|
||||
// arrange
|
||||
const expectedLength = scenario.maxLength + 1;
|
||||
const expectedLineNumber = 2;
|
||||
const context = new TestContext()
|
||||
.withLanguage(scenario.language)
|
||||
.withLines([
|
||||
'A'.repeat(scenario.maxLength),
|
||||
'B'.repeat(expectedLength),
|
||||
'C'.repeat(100),
|
||||
]);
|
||||
// act
|
||||
const actual = context.analyze();
|
||||
// assert
|
||||
expectInvalidCodeLines(actual, [expectedLineNumber]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('identifies a single line exceeding maximum length', () => {
|
||||
// arrange
|
||||
const expectedLength = MAX_SHELLSCRIPT_LENGTH + 1;
|
||||
const expected: InvalidCodeLine[] = [{
|
||||
lineNumber: 2,
|
||||
error: createTooLongLineError(expectedLength, MAX_SHELLSCRIPT_LENGTH),
|
||||
}];
|
||||
const context = new TestContext()
|
||||
.withLanguage(ScriptingLanguage.shellscript)
|
||||
.withLines([
|
||||
'A'.repeat(MAX_SHELLSCRIPT_LENGTH),
|
||||
'B'.repeat(expectedLength),
|
||||
'C'.repeat(100),
|
||||
]);
|
||||
// act
|
||||
const actual = context.analyze();
|
||||
// assert
|
||||
expectSameInvalidCodeLines(actual, expected);
|
||||
describe('identifies multiple lines exceeding maximum length', () => {
|
||||
createScriptLanguageScenarios().forEach((scenario) => {
|
||||
it(scenario.description, () => {
|
||||
// arrange
|
||||
const expectedInvalidLines: readonly {
|
||||
readonly lineNumber: number,
|
||||
readonly length: number,
|
||||
}[] = [
|
||||
{ lineNumber: 1, length: scenario.maxLength + 1 },
|
||||
{ lineNumber: 3, length: scenario.maxLength + 2 },
|
||||
];
|
||||
const context = new TestContext()
|
||||
.withLanguage(scenario.language)
|
||||
.withLines([
|
||||
'A'.repeat(expectedInvalidLines[0].length),
|
||||
'B'.repeat(scenario.maxLength),
|
||||
'C'.repeat(expectedInvalidLines[1].length),
|
||||
]);
|
||||
// act
|
||||
const actual = context.analyze();
|
||||
// assert
|
||||
expectInvalidCodeLines(actual, expectedInvalidLines.map((l) => l.lineNumber));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('identifies multiple lines exceeding maximum length', () => {
|
||||
// arrange
|
||||
const expectedLength1 = MAX_SHELLSCRIPT_LENGTH + 1;
|
||||
const expectedLength2 = MAX_SHELLSCRIPT_LENGTH + 2;
|
||||
const expected: InvalidCodeLine[] = [
|
||||
{
|
||||
lineNumber: 1,
|
||||
error: createTooLongLineError(expectedLength1, MAX_SHELLSCRIPT_LENGTH),
|
||||
},
|
||||
{
|
||||
lineNumber: 3,
|
||||
error: createTooLongLineError(expectedLength2, MAX_SHELLSCRIPT_LENGTH),
|
||||
},
|
||||
];
|
||||
const context = new TestContext()
|
||||
.withLanguage(ScriptingLanguage.shellscript)
|
||||
.withLines([
|
||||
'A'.repeat(expectedLength1),
|
||||
'B'.repeat(MAX_SHELLSCRIPT_LENGTH),
|
||||
'C'.repeat(expectedLength2),
|
||||
]);
|
||||
// act
|
||||
const actual = context.analyze();
|
||||
// assert
|
||||
expectSameInvalidCodeLines(actual, expected);
|
||||
describe('error construction', () => {
|
||||
describe('outputs actual character length', () => {
|
||||
createScriptLanguageScenarios().forEach((scenario) => {
|
||||
it(scenario.description, () => {
|
||||
// arrange
|
||||
const expectedLength = scenario.maxLength + 1;
|
||||
const expectedErrorPart = `Line is too long (${expectedLength}).`;
|
||||
const context = new TestContext()
|
||||
.withLanguage(scenario.language)
|
||||
.withLines([
|
||||
'B'.repeat(expectedLength),
|
||||
]);
|
||||
// act
|
||||
const actual = context.analyze();
|
||||
// assert
|
||||
expect(actual).to.have.lengthOf(1);
|
||||
const actualError = actual[0].error;
|
||||
expect(actualError).to.include(expectedErrorPart);
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('outputs maximum allowed character length', () => {
|
||||
createScriptLanguageScenarios().forEach((scenario) => {
|
||||
it(scenario.description, () => {
|
||||
// arrange
|
||||
const expectedLength = scenario.maxLength + 1;
|
||||
const expectedErrorPart = `It exceed maximum allowed length ${scenario.maxLength}`;
|
||||
const context = new TestContext()
|
||||
.withLanguage(scenario.language)
|
||||
.withLines([
|
||||
'B'.repeat(expectedLength),
|
||||
]);
|
||||
// act
|
||||
const actual = context.analyze();
|
||||
// assert
|
||||
expect(actual).to.have.lengthOf(1);
|
||||
const actualError = actual[0].error;
|
||||
expect(actualError).to.include(expectedErrorPart);
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('outputs total exceeding characters', () => {
|
||||
createScriptLanguageScenarios().forEach((scenario) => {
|
||||
it(scenario.description, () => {
|
||||
// arrange
|
||||
const expectedExceedingCharacters = 5;
|
||||
const expectedErrorPart = `by ${expectedExceedingCharacters} characters.`;
|
||||
const lineLength = scenario.maxLength + expectedExceedingCharacters;
|
||||
const context = new TestContext()
|
||||
.withLanguage(scenario.language)
|
||||
.withLines([
|
||||
'B'.repeat(lineLength),
|
||||
]);
|
||||
// act
|
||||
const actual = context.analyze();
|
||||
// assert
|
||||
expect(actual).to.have.lengthOf(1);
|
||||
const actualError = actual[0].error;
|
||||
expect(actualError).to.include(expectedErrorPart);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('throws an error for unsupported language', () => {
|
||||
// arrange
|
||||
const unsupportedLanguage = 'unsupported' as unknown as ScriptingLanguage;
|
||||
const expectedError = `Unsupported language: ${ScriptingLanguage[unsupportedLanguage]} (${unsupportedLanguage})`;
|
||||
const context = new TestContext()
|
||||
.withLanguage('unsupported' as unknown as ScriptingLanguage)
|
||||
.withLines(['A', 'B', 'C']);
|
||||
// act & assert
|
||||
expect(() => context.analyze()).to.throw('Unsupported language: unsupported');
|
||||
// act
|
||||
const act = () => context.analyze();
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function createTooLongLineError(actualLength: number, maxAllowedLength: number): string {
|
||||
return [
|
||||
`Line is too long (${actualLength}).`,
|
||||
`It exceed maximum allowed length ${maxAllowedLength}.`,
|
||||
'This may cause bugs due to unintended trimming by operating system, shells or terminal emulators.',
|
||||
].join(' ');
|
||||
interface ScriptLanguageScenario {
|
||||
readonly description: string;
|
||||
readonly maxLength: number;
|
||||
readonly language: ScriptingLanguage;
|
||||
}
|
||||
|
||||
function createScriptLanguageScenarios(): readonly ScriptLanguageScenario[] {
|
||||
const maxLengths: Record< // `Record` catches missing entries at compile-time
|
||||
ScriptingLanguage, number> = {
|
||||
[ScriptingLanguage.batchfile]: 8191,
|
||||
[ScriptingLanguage.shellscript]: 1048576,
|
||||
};
|
||||
return Object.entries(maxLengths).map(([language, length]): ScriptLanguageScenario => ({
|
||||
description: `${ScriptingLanguage[language]} (max: ${length})`,
|
||||
language: Number.parseInt(language, 10) as ScriptingLanguage,
|
||||
maxLength: length,
|
||||
}));
|
||||
}
|
||||
|
||||
class TestContext {
|
||||
|
||||
Reference in New Issue
Block a user