Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(betterer 🐛): normalise issue text before creating issue #991

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/betterer/src/test/file-test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'assert';
import LinesAndColumns from 'lines-and-columns';

import { createHash } from '../../hasher';
import { isString, normalisedPath } from '../../utils';
import { isString, normalisedPath, normaliseNewlines } from '../../utils';
import { BettererFileIssue, BettererFileIssues, BettererFile } from './types';

const UNKNOWN_LOCATION = {
Expand Down Expand Up @@ -50,8 +50,10 @@ export class BettererFileΩ implements BettererFile {

const [line, column, length, message, overrideHash] = issue;
const start = lc.indexForLocation({ line, column }) || 0;
const hash = overrideHash || createHash(fileText.substr(start, length));
return { line, column, length, message, hash };
const issueText = fileText.substring(start, start + length);
const normalisedText = normaliseNewlines(issueText);
const hash = overrideHash || createHash(normalisedText);
return { line, column, length: normalisedText.length, message, hash };
}
}

Expand Down
17 changes: 17 additions & 0 deletions test/__snapshots__/file-test-line-endings.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`betterer should normalise line endings within issues 1`] = `
"// BETTERER RESULTS V2.
//
// If this file contains merge conflicts, use \`betterer merge\` to automatically resolve them:
// https://phenomnomnominal.github.io/betterer/docs/results-file/#merge
//
exports[\`test\`] = {
value: \`{
\\"src/index.js:1440504519\\": [
[0, 0, 30, \\"\\", \\"1440504519\\"]
]
}\`
};
"
`;
57 changes: 57 additions & 0 deletions test/file-test-line-endings.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { betterer } from '@betterer/betterer';

import { createFixture } from './fixture';

describe('betterer', () => {
it('should normalise line endings within issues', async () => {
const { paths, cleanup, testNames, resolve, readFile, writeFile } = await createFixture('file-test-line-endings', {
'.betterer.js': `
const { BettererFileTest } = require('@betterer/betterer');
const { eslint } = require('@betterer/eslint');
const { promises: fs } = require('fs');

module.exports = {
test: () => {
return new BettererFileTest(async (filePaths, fileTestResult) => {
await Promise.all(
filePaths.map(async filePath => {
const contents = await fs.readFile(filePath, 'utf-8');
const file = fileTestResult.addFile(filePath, contents);
file.addIssue(0, contents.length, '');
}),
);
}).include('**/*.js')
}
};
`,
'src/index.js': `
function test () {
return;
}
`
});

const configPaths = [paths.config];
const resultsPath = paths.results;
const indexPath = resolve('src/index.js');

const lfRun = await betterer({ configPaths, resultsPath, workers: false });
expect(testNames(lfRun.ran)).toEqual(['test']);

const lfResult = await readFile(resultsPath);
expect(lfResult).toMatchSnapshot();

const lf = await readFile(indexPath);
const crlf = lf.replace(/\n/g, '\r\n');
await writeFile(indexPath, crlf);

const crlfRun = await betterer({ configPaths, resultsPath, workers: false, ci: true });
expect(crlfRun.changed).toEqual([]);

const crlfResult = await readFile(resultsPath);

expect(crlfResult).toEqual(lfResult);

await cleanup();
});
});