/* eslint-disable prefer-arrow-callback */
import {expect} from 'chai';
import fs from 'fs';
import {exec} from 'child_process';

describe('Smoke test for the executable script', function() {
  beforeEach(() => {
    fs.writeFileSync(
      'test/test-data.js',
      'var foo = 10;\n' +
      '[1, 2, 3].map(function(x) { return x*x });'
    );
  });

  afterEach(() => {
    fs.unlinkSync('test/test-data.js');
    if (fs.existsSync('test/output.js')) {
      fs.unlinkSync('test/output.js');
    }
  });

  describe('when valid input and output file given', function() {
    it('transforms input file to output file', done => {
      exec('node ./bin/index.js -t let,arrow test/test-data.js -o test/output.js', (error, stdout, stderr) => {
        expect(error).to.equal(null); // eslint-disable-line no-null/no-null
        expect(stderr).to.equal('');
        expect(stdout).to.equal('');

        expect(fs.readFileSync('test/output.js').toString()).to.equal(
          'const foo = 10;\n' +
          '[1, 2, 3].map(x => { return x*x });'
        );
        done();
      });
    });
  });

  describe('when no input/output files given', () => {
    it('reads STDIN and writes STDOUT', done => {
      exec('node ./bin/index.js -t let,arrow < test/test-data.js > test/output.js', (error, stdout, stderr) => {
        expect(error).to.equal(null); // eslint-disable-line no-null/no-null
        expect(stderr).to.equal('');
        expect(stdout).to.equal('');

        expect(fs.readFileSync('test/output.js').toString()).to.equal(
          'const foo = 10;\n' +
          '[1, 2, 3].map(x => { return x*x });'
        );
        done();
      });
    });
  });

  describe('when invalid transform name given', () => {
    it('exits with error message', done => {
      exec('node ./bin/index.js --transform blah test/test-data.js', (error, stdout, stderr) => {
        expect(error).not.to.equal(null); // eslint-disable-line no-null/no-null
        expect(stderr).to.equal('Unknown transform "blah".\n');
        expect(stdout).to.equal('');

        expect(fs.existsSync('test/output.js')).to.equal(false);
        done();
      });
    });
  });

  describe('when transform generates warnings', () => {
    beforeEach(() => {
      fs.writeFileSync(
        'test/test-data-warnings.js',
        'if (true) { var x = 10; }\n x = 12;\n'
      );
    });

    afterEach(() => {
      fs.unlinkSync('test/test-data-warnings.js');
    });

    it('logs warnings to STDERR', done => {
      exec('node ./bin/index.js --transform let test/test-data-warnings.js', (error, stdout, stderr) => {
        expect(error).to.equal(null); // eslint-disable-line no-null/no-null
        expect(stderr).to.equal('test/test-data-warnings.js:\n1:  warning  Unable to transform var  (let)\n');
        expect(stdout).to.equal('if (true) { var x = 10; }\n x = 12;\n');
        done();
      });
    });
  });
});

@@ -123,16 +123,153 @@
 cess';%0A%0A
+const INPUT_FILE = 'test/test-data.js';%0Aconst INPUT_WARNINGS_FILE = 'test/test-data-warnings.js';%0Aconst OUTPUT_FILE = 'test/output.js';%0A%0A
 describe
@@ -371,35 +371,26 @@
 (%0A      
-'test/test-data.js'
+INPUT_FILE
 ,%0A      
@@ -512,35 +512,26 @@
 inkSync(
-'test/test-data.js'
+INPUT_FILE
 );%0A    i
@@ -547,32 +547,27 @@
 stsSync(
-'test/output.js'
+OUTPUT_FILE
 )) %7B%0A   
@@ -583,32 +583,27 @@
 inkSync(
-'test/output.js'
+OUTPUT_FILE
 );%0A    %7D
@@ -736,33 +736,33 @@
 =%3E %7B%0A      exec(
-'
+%60
 node ./bin/index
@@ -782,44 +782,40 @@
 row 
-test/test-data.js -o test/output.js'
+$%7BINPUT_FILE%7D -o $%7BOUTPUT_FILE%7D%60
 , (e
@@ -1024,32 +1024,27 @@
 ileSync(
-'test/output.js'
+OUTPUT_FILE
 ).toStri
@@ -1299,33 +1299,33 @@
 =%3E %7B%0A      exec(
-'
+%60
 node ./bin/index
@@ -1347,43 +1347,39 @@
 w %3C 
-test/test-data.js %3E test/output.js'
+$%7BINPUT_FILE%7D %3E $%7BOUTPUT_FILE%7D%60
 , (e
@@ -1588,32 +1588,27 @@
 ileSync(
-'test/output.js'
+OUTPUT_FILE
 ).toStri
@@ -1859,33 +1859,33 @@
 =%3E %7B%0A      exec(
-'
+%60
 node ./bin/index
@@ -1905,34 +1905,30 @@
 rm blah 
-test/test-data.js'
+$%7BINPUT_FILE%7D%60
 , (error
@@ -2162,32 +2162,27 @@
 stsSync(
-'test/output.js'
+OUTPUT_FILE
 )).to.eq
@@ -2349,36 +2349,27 @@
     
-'test/test-data-warnings.js'
+INPUT_WARNINGS_FILE
 ,%0A  
@@ -2478,36 +2478,27 @@
 ync(
-'test/test-data-warnings.js'
+INPUT_WARNINGS_FILE
 );%0A 
@@ -2560,17 +2560,17 @@
    exec(
-'
+%60
 node ./b
@@ -2601,35 +2601,31 @@
 let 
-test/test-data-warnings.js'
+$%7BINPUT_WARNINGS_FILE%7D%60
 , (e
@@ -2764,35 +2764,31 @@
 ual(
-'test/test-data-warnings.js
+%60$%7BINPUT_WARNINGS_FILE%7D
 :%5Cn1
@@ -2831,17 +2831,17 @@
  (let)%5Cn
-'
+%60
 );%0A     

Use named constants for system test file names