From 426de54fce6c0a5d5c2b267051296001542acca0 Mon Sep 17 00:00:00 2001 From: Alexander Ogilvie Date: Thu, 11 Jul 2019 11:58:48 +0200 Subject: [PATCH 1/2] fix: Mode 'verify' and tests do not run on Windows (#19) Made 'verify' mode stable for Windows by always converting line separators to LF before comparing content (Windows / git might create / convert to CRLF). --- index.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 6c7a989..15af2d9 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,7 @@ const getNoDeclarationFileError = ({ filename }) => ); const getTypeMismatchError = ({ filename, expected, actual }) => { - const diff = new LineDiff(actual, expected).toString(); + const diff = new LineDiff(enforceLFLineSeparators(actual), expected).toString(); return new Error( `Generated type declaration file is outdated. Run webpack and commit the updated type declaration for '${filename}'\n\n${diff}` @@ -36,6 +36,19 @@ const filenameToTypingsFilename = filename => { return path.join(dirName, `${baseName}.d.ts`); }; +const enforceLFLineSeparators = text => { + if (text) { + // replace all CRLFs (Windows) by LFs (Unix) + return text.replace(/\r\n/g, "\n"); + } else { + return text; + } +}; + +const compareText = (contentA, contentB) => { + return enforceLFLineSeparators(contentA) === enforceLFLineSeparators(contentB); +}; + const validModes = ['emit', 'verify']; const isFileNotFound = err => err && err.code === 'ENOENT'; @@ -91,7 +104,7 @@ module.exports = function(content, ...rest) { return failed(err); } - if (cssModuleDefinition !== fileContents) { + if (!compareText(cssModuleDefinition, fileContents)) { return failed( getTypeMismatchError({ filename: cssModuleInterfaceFilename, @@ -105,7 +118,7 @@ module.exports = function(content, ...rest) { }); } else { read((_, fileContents) => { - if (cssModuleDefinition !== fileContents) { + if (!compareText(cssModuleDefinition, fileContents)) { write(cssModuleDefinition, err => { if (err) { failed(err); From 3e6db5993c93fc5cbbfc670351c5db5bdc7f34da Mon Sep 17 00:00:00 2001 From: Alexander Ogilvie Date: Thu, 11 Jul 2019 11:59:29 +0200 Subject: [PATCH 2/2] fix: Mode 'verify' and tests do not run on Windows (#19) Fixed tests for Windows. --- .../verify-invalid-declaration.test.js | 5 ++++- .../verify-missing-declaration.test.js | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/test/verify-invalid-declaration/verify-invalid-declaration.test.js b/test/verify-invalid-declaration/verify-invalid-declaration.test.js index 80062e7..a83cfe3 100644 --- a/test/verify-invalid-declaration/verify-invalid-declaration.test.js +++ b/test/verify-invalid-declaration/verify-invalid-declaration.test.js @@ -9,6 +9,9 @@ test('Can error on invalid declaration', async () => { mode: 'verify' }); } catch (err) { - expect(getErrorMessage(err.errors[0])).toMatchSnapshot(); + // make test robust for Windows by replacing backslashes in the file path with slashes + let errorMessage = getErrorMessage(err.errors[0]).replace(/(?<=Error:.*)\\/g, "/"); + + expect(errorMessage).toMatchSnapshot(); } }); diff --git a/test/verify-missing-declaration/verify-missing-declaration.test.js b/test/verify-missing-declaration/verify-missing-declaration.test.js index 80062e7..a83cfe3 100644 --- a/test/verify-missing-declaration/verify-missing-declaration.test.js +++ b/test/verify-missing-declaration/verify-missing-declaration.test.js @@ -9,6 +9,9 @@ test('Can error on invalid declaration', async () => { mode: 'verify' }); } catch (err) { - expect(getErrorMessage(err.errors[0])).toMatchSnapshot(); + // make test robust for Windows by replacing backslashes in the file path with slashes + let errorMessage = getErrorMessage(err.errors[0]).replace(/(?<=Error:.*)\\/g, "/"); + + expect(errorMessage).toMatchSnapshot(); } });