From d1d95473539f2a317baa77cc8161436f8e5f270d Mon Sep 17 00:00:00 2001 From: xzyfer Date: Thu, 19 Jul 2018 18:46:18 +1000 Subject: [PATCH] Fix tests that have always been broke Four years ago there was a test that mutated `process.cwd` but didn't correctly reset it. The altered global state resulted in future tests having incorrect assetions. I only noticed this because for some reason it manifested in failures in only _some_ Node versions when N-API was being used. --- test/cli.js | 29 ++++++------------- .../include-files/expected-data-importer.css | 5 ++++ .../include-files/expected-file-importer.css | 2 ++ test/fixtures/source-map-embed/expected.css | 2 +- 4 files changed, 17 insertions(+), 21 deletions(-) create mode 100644 test/fixtures/include-files/expected-data-importer.css create mode 100644 test/fixtures/include-files/expected-file-importer.css diff --git a/test/cli.js b/test/cli.js index 78a80910c..e15fbb093 100644 --- a/test/cli.js +++ b/test/cli.js @@ -122,8 +122,6 @@ describe('cli', function() { describe('node-sass in.scss', function() { it('should compile a scss file', function(done) { - process.chdir(fixture('simple')); - var src = fixture('simple/index.scss'); var dest = fixture('simple/index.css'); var bin = spawn(cli, [src, dest]); @@ -131,14 +129,11 @@ describe('cli', function() { bin.once('close', function() { assert(fs.existsSync(dest)); fs.unlinkSync(dest); - process.chdir(__dirname); done(); }); }); it('should compile a scss file to custom destination', function(done) { - process.chdir(fixture('simple')); - var src = fixture('simple/index.scss'); var dest = fixture('simple/index-custom.css'); var bin = spawn(cli, [src, dest]); @@ -146,7 +141,6 @@ describe('cli', function() { bin.once('close', function() { assert(fs.existsSync(dest)); fs.unlinkSync(dest); - process.chdir(__dirname); done(); }); }); @@ -169,8 +163,6 @@ describe('cli', function() { }); it('should compile silently using the --quiet option', function(done) { - process.chdir(fixture('simple')); - var src = fixture('simple/index.scss'); var dest = fixture('simple/index.css'); var bin = spawn(cli, [src, dest, '--quiet']); @@ -183,14 +175,11 @@ describe('cli', function() { bin.once('close', function() { assert.equal(didEmit, false); fs.unlinkSync(dest); - process.chdir(__dirname); done(); }); }); it('should still report errors with the --quiet option', function(done) { - process.chdir(fixture('invalid')); - var src = fixture('invalid/index.scss'); var dest = fixture('invalid/index.css'); var bin = spawn(cli, [src, dest, '--quiet']); @@ -202,7 +191,6 @@ describe('cli', function() { bin.once('close', function() { assert.equal(didEmit, true); - process.chdir(__dirname); done(); }); }); @@ -644,7 +632,8 @@ describe('cli', function() { describe('importer', function() { var dest = fixture('include-files/index.css'); var src = fixture('include-files/index.scss'); - var expected = read(fixture('include-files/expected-importer.css'), 'utf8').trim().replace(/\r\n/g, '\n'); + var expectedData = read(fixture('include-files/expected-data-importer.css'), 'utf8').trim().replace(/\r\n/g, '\n'); + var expectedFile = read(fixture('include-files/expected-file-importer.css'), 'utf8').trim().replace(/\r\n/g, '\n'); it('should override imports and fire callback with file and contents', function(done) { var bin = spawn(cli, [ @@ -653,7 +642,7 @@ describe('cli', function() { ]); bin.once('close', function() { - assert.equal(read(dest, 'utf8').trim(), expected); + assert.equal(read(dest, 'utf8').trim(), expectedData); fs.unlinkSync(dest); done(); }); @@ -667,7 +656,7 @@ describe('cli', function() { bin.once('close', function() { if (fs.existsSync(dest)) { - assert.equal(read(dest, 'utf8').trim(), ''); + assert.equal(read(dest, 'utf8').trim(), expectedFile); fs.unlinkSync(dest); } @@ -682,7 +671,7 @@ describe('cli', function() { ]); bin.once('close', function() { - assert.equal(read(dest, 'utf8').trim(), expected); + assert.equal(read(dest, 'utf8').trim(), expectedData); fs.unlinkSync(dest); done(); }); @@ -695,7 +684,7 @@ describe('cli', function() { ]); bin.once('close', function() { - assert.equal(read(dest, 'utf8').trim(), expected); + assert.equal(read(dest, 'utf8').trim(), expectedData); fs.unlinkSync(dest); done(); }); @@ -709,7 +698,7 @@ describe('cli', function() { bin.once('close', function() { if (fs.existsSync(dest)) { - assert.equal(read(dest, 'utf8').trim(), ''); + assert.equal(read(dest, 'utf8').trim(), expectedFile); fs.unlinkSync(dest); } @@ -724,7 +713,7 @@ describe('cli', function() { ]); bin.once('close', function() { - assert.equal(read(dest, 'utf8').trim(), expected); + assert.equal(read(dest, 'utf8').trim(), expectedData); fs.unlinkSync(dest); done(); }); @@ -737,7 +726,7 @@ describe('cli', function() { ]); bin.once('close', function() { - assert.equal(read(dest, 'utf8').trim(), expected); + assert.equal(read(dest, 'utf8').trim(), expectedData); fs.unlinkSync(dest); done(); }); diff --git a/test/fixtures/include-files/expected-data-importer.css b/test/fixtures/include-files/expected-data-importer.css new file mode 100644 index 000000000..1925a6021 --- /dev/null +++ b/test/fixtures/include-files/expected-data-importer.css @@ -0,0 +1,5 @@ +div { + color: yellow; } + +div { + color: yellow; } diff --git a/test/fixtures/include-files/expected-file-importer.css b/test/fixtures/include-files/expected-file-importer.css new file mode 100644 index 000000000..326f694d5 --- /dev/null +++ b/test/fixtures/include-files/expected-file-importer.css @@ -0,0 +1,2 @@ +/* foo.scss */ +/* bar.scss */ diff --git a/test/fixtures/source-map-embed/expected.css b/test/fixtures/source-map-embed/expected.css index 56f2e59a3..a1e895f28 100644 --- a/test/fixtures/source-map-embed/expected.css +++ b/test/fixtures/source-map-embed/expected.css @@ -10,4 +10,4 @@ #navbar li a { font-weight: bold; } -/*# sourceMappingURL=data:application/json;base64,ewoJInZlcnNpb24iOiAzLAoJImZpbGUiOiAiZml4dHVyZXMvc291cmNlLW1hcC1lbWJlZC9pbmRleC5jc3MiLAoJInNvdXJjZXMiOiBbCgkJImZpeHR1cmVzL3NvdXJjZS1tYXAtZW1iZWQvaW5kZXguc2NzcyIKCV0sCgkibmFtZXMiOiBbXSwKCSJtYXBwaW5ncyI6ICJBQUFBLEFBQUEsT0FBTyxDQUFDO0VBQ04sS0FBSyxFQUFFLEdBQUc7RUFDVixNQUFNLEVBQUUsSUFBSSxHQUNiOztBQUVELEFBQUEsT0FBTyxDQUFDLEVBQUUsQ0FBQztFQUNULGVBQWUsRUFBRSxJQUFJLEdBQ3RCOztBQUVELEFBQUEsT0FBTyxDQUFDLEVBQUUsQ0FBQztFQUNULEtBQUssRUFBRSxJQUFJLEdBS1o7RUFORCxBQUdFLE9BSEssQ0FBQyxFQUFF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