Skip to content

Commit

Permalink
fix: Support scanning files with BOMs
Browse files Browse the repository at this point in the history
  • Loading branch information
francescomari committed Apr 12, 2022
1 parent 958b5a6 commit 3f00da9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/cli/commands/test/iac/local-execution/file-loader.ts
Expand Up @@ -27,9 +27,9 @@ export async function tryLoadFileData(
): Promise<IacFileData> {
const fileType = getFileType(pathToScan);

const fileContent = (
await fs.readFile(pathToScan, DEFAULT_ENCODING)
).toString();
const fileContent = removeBOM(
await fs.readFile(pathToScan, DEFAULT_ENCODING),
);

return {
filePath: pathToScan,
Expand All @@ -38,6 +38,13 @@ export async function tryLoadFileData(
};
}

function removeBOM(s: string): string {
if (s.length > 0 && s.charCodeAt(0) === 0xfeff) {
return s.slice(1);
}
return s;
}

export class NoFilesToScanError extends CustomError {
constructor(message?: string) {
super(message || 'Could not find any valid IaC files');
Expand Down
30 changes: 29 additions & 1 deletion test/jest/unit/iac/file-loader.spec.ts
@@ -1,4 +1,4 @@
const mockFs = require('mock-fs');
import * as mockFs from 'mock-fs';
import {
FailedToLoadFileError,
loadContentForFiles,
Expand Down Expand Up @@ -41,6 +41,8 @@ describe('loadContentForFiles', () => {
});

describe('errors', () => {
afterEach(jest.restoreAllMocks);

it('throws an error when an error occurs when loading files', async () => {
jest.spyOn(fileLoader, 'tryLoadFileData').mockImplementation(() => {
throw FailedToLoadFileError;
Expand All @@ -52,3 +54,29 @@ describe('loadContentForFiles', () => {
});
});
});

describe('tryLoadFileData', () => {
// In UTF-8, a BOM is useless because there is no need to signal a specific
// byte ordering. Some operating systems and editors, though, still prepend a
// BOM to files encoded in UTF-8. The BOM for UTF-8 is encoded by the bytes
// contained in the following buffer. When reading a file encoded as UTF-8
// with BOM, Node.js generates a string encoded in UTF-16 with BOM. The BOM
// for UTF-16 is encoded by the unicode code point U+FEFF. For more details,
// see https://en.wikipedia.org/wiki/Byte_order_mark.

const UTF8_BOM = Buffer.from([0xef, 0xbb, 0xbf]);

afterEach(mockFs.restore);

it('should load a file without BOM', async () => {
mockFs({ file: Buffer.from('data') });
const result = await fileLoader.tryLoadFileData('file');
expect(result.fileContent).toBe('data');
});

it('should load a file with BOM', async () => {
mockFs({ file: Buffer.concat([UTF8_BOM, Buffer.from('data')]) });
const result = await fileLoader.tryLoadFileData('file');
expect(result.fileContent).toBe('data');
});
});

0 comments on commit 3f00da9

Please sign in to comment.