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

Mode 'verify' and tests do not run on Windows #20

Merged
merged 3 commits into from
Sep 20, 2019
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
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand All @@ -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';
Expand Down Expand Up @@ -91,7 +104,7 @@ module.exports = function(content, ...rest) {
return failed(err);
}

if (cssModuleDefinition !== fileContents) {
if (!compareText(cssModuleDefinition, fileContents)) {
return failed(
getTypeMismatchError({
filename: cssModuleInterfaceFilename,
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});