Skip to content

Commit f2575d8

Browse files
vitorbalnzakas
authored andcommitted
Fix: ensure consistent linebreaks when testing transforms (#6)
1 parent f8f5c23 commit f2575d8

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

tests/lib/new-rule-format/new-rule-format.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,35 @@
99

1010
var jscodeshift = require("jscodeshift");
1111
var fs = require("fs");
12+
var os = require("os");
1213
var path = require("path");
1314
var expect = require("chai").expect;
1415

1516
var newRuleFormatTransform = require("../../../lib/new-rule-format/new-rule-format");
1617

18+
/**
19+
* Returns a new string with all the EOL markers from the string passed in
20+
* replaced with the Operating System specific EOL marker.
21+
* Useful for guaranteeing two transform outputs have the same EOL marker format.
22+
*
23+
* @param {string} input - the string which will have its EOL markers replaced
24+
* @returns {string} a new string with all EOL markers replaced
25+
* @private
26+
*/
27+
function normalizeLineEndngs(input) {
28+
return input.replace(/(\r\n|\n|\r)/gm, os.EOL);
29+
}
30+
1731
/**
1832
* Run a transform against a fixture file and compare results with expected output.
1933
* The fixture file and expected output file should be named in the format
2034
* `prefix.input.js` and `prefix.output.js` and should be located in the
2135
* `tests/fixtures/` folder.
2236
*
2337
* @param {Function} transform - transform to test against
24-
* @param {String} transformFixturePrefix - prefix of fixture files
38+
* @param {string} transformFixturePrefix - prefix of fixture files
2539
* @returns {void}
40+
* @private
2641
*/
2742
function testTransformWithFixture(transform, transformFixturePrefix) {
2843
var fixtureDir = path.join(__dirname, "../../fixtures/lib/new-rule-format");
@@ -42,9 +57,9 @@ function testTransformWithFixture(transform, transformFixturePrefix) {
4257
);
4358

4459
expect(
45-
(output || "").trim()
60+
normalizeLineEndngs((output || "").trim())
4661
).to.equal(
47-
expectedOutput.trim()
62+
normalizeLineEndngs(expectedOutput.trim())
4863
);
4964
});
5065
}
@@ -57,8 +72,9 @@ function testTransformWithFixture(transform, transformFixturePrefix) {
5772
* `tests/fixtures/` folder.
5873
*
5974
* @param {Function} transform - transform to test against
60-
* @param {String[]} fixtures - list of fixture prefixes
75+
* @param {string[]} fixtures - list of fixture prefixes
6176
* @returns {void}
77+
* @private
6278
*/
6379
function testTransformWithFixtures(transform, fixtures) {
6480
return fixtures.forEach(function(fixture) {

0 commit comments

Comments
 (0)