Use line endings based on script language #88

Use CRLF in batchfile and LF in shellscript.
This commit is contained in:
undergroundwires
2022-09-28 23:09:23 +02:00
parent bbc6156281
commit 6b3f4659df
6 changed files with 89 additions and 2 deletions

View File

@@ -1,6 +1,5 @@
import { ICodeBuilder } from './ICodeBuilder'; import { ICodeBuilder } from './ICodeBuilder';
const NewLine = '\n';
const TotalFunctionSeparatorChars = 58; const TotalFunctionSeparatorChars = 58;
export abstract class CodeBuilder implements ICodeBuilder { export abstract class CodeBuilder implements ICodeBuilder {
@@ -59,10 +58,12 @@ export abstract class CodeBuilder implements ICodeBuilder {
} }
public toString(): string { public toString(): string {
return this.lines.join(NewLine); return this.lines.join(this.getNewLineTerminator());
} }
protected abstract getCommentDelimiter(): string; protected abstract getCommentDelimiter(): string;
protected abstract writeStandardOut(text: string): string; protected abstract writeStandardOut(text: string): string;
protected abstract getNewLineTerminator(): string;
} }

View File

@@ -8,6 +8,10 @@ export class BatchBuilder extends CodeBuilder {
protected writeStandardOut(text: string): string { protected writeStandardOut(text: string): string {
return `echo ${escapeForEcho(text)}`; return `echo ${escapeForEcho(text)}`;
} }
protected getNewLineTerminator(): string {
return '\r\n';
}
} }
function escapeForEcho(text: string) { function escapeForEcho(text: string) {

View File

@@ -8,6 +8,10 @@ export class ShellBuilder extends CodeBuilder {
protected writeStandardOut(text: string): string { protected writeStandardOut(text: string): string {
return `echo '${escapeForEcho(text)}'`; return `echo '${escapeForEcho(text)}'`;
} }
protected getNewLineTerminator(): string {
return '\n';
}
} }
function escapeForEcho(text: string) { function escapeForEcho(text: string) {

View File

@@ -6,11 +6,18 @@ describe('CodeBuilder', () => {
class CodeBuilderConcrete extends CodeBuilder { class CodeBuilderConcrete extends CodeBuilder {
private commentDelimiter = '//'; private commentDelimiter = '//';
private newLineTerminator = '\n';
public withCommentDelimiter(delimiter: string): CodeBuilderConcrete { public withCommentDelimiter(delimiter: string): CodeBuilderConcrete {
this.commentDelimiter = delimiter; this.commentDelimiter = delimiter;
return this; return this;
} }
public withNewLineTerminator(terminator: string): CodeBuilderConcrete {
this.newLineTerminator = terminator;
return this;
}
protected getCommentDelimiter(): string { protected getCommentDelimiter(): string {
return this.commentDelimiter; return this.commentDelimiter;
} }
@@ -18,6 +25,10 @@ describe('CodeBuilder', () => {
protected writeStandardOut(text: string): string { protected writeStandardOut(text: string): string {
return text; return text;
} }
protected getNewLineTerminator(): string {
return this.newLineTerminator;
}
} }
describe('appendLine', () => { describe('appendLine', () => {
it('when empty appends empty line', () => { it('when empty appends empty line', () => {
@@ -40,6 +51,43 @@ describe('CodeBuilder', () => {
const lines = getLines(result); const lines = getLines(result);
expect(lines[1]).to.equal('str'); expect(lines[1]).to.equal('str');
}); });
describe('append multi-line string as multiple lines', () => {
describe('recognizes different line terminators', () => {
const delimiters = ['\n', '\r\n', '\r'];
for (const delimiter of delimiters) {
it(`using "${JSON.stringify(delimiter)}"`, () => {
// arrange
const line1 = 'line1';
const line2 = 'line2';
const code = `${line1}${delimiter}${line2}`;
const sut = new CodeBuilderConcrete();
// act
sut.appendLine(code);
// assert
const result = sut.toString();
const lines = getLines(result);
expect(lines).to.have.lengthOf(2);
expect(lines[0]).to.equal(line1);
expect(lines[1]).to.equal(line2);
});
}
});
it('normalizes different line terminators', () => {
// arrange
const lineTerminator = '🐒';
const lines = ['line1', 'line2', 'line3', 'line4'];
const code = `${lines[0]}\n${lines[1]}\r\n${lines[2]}\r${lines[3]}`;
const expected = `${lines[0]}${lineTerminator}${lines[1]}${lineTerminator}${lines[2]}${lineTerminator}${lines[3]}`;
const sut = new CodeBuilderConcrete()
.withNewLineTerminator(lineTerminator);
// act
const actual = sut
.appendLine(code)
.toString();
// assert
expect(actual).to.equal(expected);
});
});
}); });
it('appendFunction', () => { it('appendFunction', () => {
// arrange // arrange

View File

@@ -11,6 +11,10 @@ describe('BatchBuilder', () => {
public writeStandardOut(text: string): string { public writeStandardOut(text: string): string {
return super.writeStandardOut(text); return super.writeStandardOut(text);
} }
public getNewLineTerminator(): string {
return super.getNewLineTerminator();
}
} }
describe('getCommentDelimiter', () => { describe('getCommentDelimiter', () => {
it('returns expected', () => { it('returns expected', () => {
@@ -57,4 +61,15 @@ describe('BatchBuilder', () => {
}); });
} }
}); });
describe('getNewLineTerminator', () => {
it('returns expected', () => {
// arrange
const expected = '\r\n';
const sut = new BatchBuilderRevealer();
// act
const actual = sut.getNewLineTerminator();
// assert
expect(expected).to.equal(actual);
});
});
}); });

View File

@@ -11,6 +11,10 @@ describe('ShellBuilder', () => {
public writeStandardOut(text: string): string { public writeStandardOut(text: string): string {
return super.writeStandardOut(text); return super.writeStandardOut(text);
} }
public getNewLineTerminator(): string {
return super.getNewLineTerminator();
}
} }
describe('getCommentDelimiter', () => { describe('getCommentDelimiter', () => {
it('returns expected', () => { it('returns expected', () => {
@@ -52,4 +56,15 @@ describe('ShellBuilder', () => {
}); });
} }
}); });
describe('getNewLineTerminator', () => {
it('returns expected', () => {
// arrange
const expected = '\n';
const sut = new ShellBuilderRevealer();
// act
const actual = sut.getNewLineTerminator();
// assert
expect(expected).to.equal(actual);
});
});
}); });