Skip to content

Commit

Permalink
Ensure different OSes generate consistent snapshots
Browse files Browse the repository at this point in the history
Fixes #1786.
  • Loading branch information
sharkykh authored and novemberborn committed Aug 27, 2018
1 parent 0ecd0be commit 28fe4e8
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/snapshot-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const indentString = require('indent-string');
const makeDir = require('make-dir');
const md5Hex = require('md5-hex');
const convertSourceMap = require('convert-source-map');
const slash = require('slash');

const concordanceOptions = require('./concordance-options').snapshotManager;

Expand Down Expand Up @@ -128,7 +129,7 @@ function generateReport(relFile, snapFile, entries) {
const buffers = combined.buffers;
let byteLength = combined.byteLength;

const header = Buffer.from(`# Snapshot report for \`${relFile}\`
const header = Buffer.from(`# Snapshot report for \`${slash(relFile)}\`
The actual snapshot is saved in \`${snapFile}\`.
Expand Down Expand Up @@ -210,6 +211,7 @@ function encodeSnapshots(buffersByHash) {
byteOffset += bodyOffset;

const compressed = zlib.gzipSync(Buffer.concat(buffers, byteOffset));
compressed[9] = 0x03; // Override the GZip header containing the OS to always be Linux
const md5sum = crypto.createHash('md5').update(compressed).digest();
return Buffer.concat([
READABLE_PREFIX,
Expand Down
5 changes: 5 additions & 0 deletions test/fixture/snapshots/test-content/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ava": {
"files": "tests"
}
}
13 changes: 13 additions & 0 deletions test/fixture/snapshots/test-content/test.js.md.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Snapshot report for `tests/test.js`

The actual snapshot is saved in `test.js.snap`.

Generated by [AVA](https://ava.li).

## test title

> Snapshot 1

{
foo: 'bar',
}
Binary file not shown.
5 changes: 5 additions & 0 deletions test/fixture/snapshots/test-content/tests/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from '../../../../..';

test('test title', t => {
t.snapshot({foo: 'bar'});
});
37 changes: 37 additions & 0 deletions test/integration/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,40 @@ test('snapshots resolved location from "snapshotDir" in AVA config', t => {
t.end();
});
});

test(`snapshots are indentical on different platforms`, t => {
const fixtureDir = path.join(__dirname, '..', 'fixture', 'snapshots', 'test-content');
const reportPath = path.join(fixtureDir, 'tests', 'snapshots', 'test.js.md');
const snapPath = path.join(fixtureDir, 'tests', 'snapshots', 'test.js.snap');
const expectedReportPath = path.join(fixtureDir, 'test.js.md.expected');
const expectedSnapPath = path.join(fixtureDir, 'test.js.snap.expected');

const removeFile = filePath => {
try {
fs.unlinkSync(filePath);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
};

// Clear current snapshots
[reportPath, snapPath].forEach(fp => removeFile(fp));

// Test should pass, and a snapshot gets written
execCli(['--update-snapshots'], {dirname: fixtureDir}, err => {
t.ifError(err);
t.true(fs.existsSync(reportPath));
t.true(fs.existsSync(snapPath));

const reportContents = fs.readFileSync(reportPath);
const snapContents = fs.readFileSync(snapPath);
const expectedReportContents = fs.readFileSync(expectedReportPath);
const expectedSnapContents = fs.readFileSync(expectedSnapPath);

t.true(reportContents.equals(expectedReportContents), 'report file contents matches snapshot');
t.true(snapContents.equals(expectedSnapContents), 'snap file contents matches snapshot');
t.end();
});
});

0 comments on commit 28fe4e8

Please sign in to comment.