var fs = require('fs');

describe('CLI output', function () {
    var coloredOutput = /\u001b\[/; // eslint-disable-line no-control-regex

    describe('TTY', function () {
        // @todo: Change to assert colored output after https://github.com/shelljs/shelljs/pull/524 is released
        it('should produce colored output without any options', function (done) {
            exec('node ./bin/newman.js run test/fixtures/run/single-get-request.json', function (code, stdout, stderr) {
                expect(code).be(0);
                expect(stderr).to.be.empty();
                expect(stdout).to.match(coloredOutput);

                done(code);
            });
        });

        it('should produce colored output with --color', function (done) {
            // eslint-disable-next-line max-len
            exec('node ./bin/newman.js run test/fixtures/run/single-get-request.json --color', function (code, stdout, stderr) {
                expect(code).be(0);
                expect(stderr).to.be.empty();
                expect(stdout).to.match(coloredOutput);

                done(code);
            });
        });

        it('should not produce colored output with --no-color', function (done) {
            // eslint-disable-next-line max-len
            exec('node ./bin/newman.js run test/fixtures/run/single-get-request.json --no-color', function (code, stdout, stderr) {
                expect(code).be(0);
                expect(stderr).to.be.empty();
                expect(stdout).to.not.match(coloredOutput);

                done(code);
            });
        });
    });

    describe('noTTY', function () {
        var encoding = 'utf-8',
            outFile = 'out/color-tty-test.txt';

        beforeEach(function (done) {
            fs.stat('out', function (err) {
                if (err) {
                    return fs.mkdir('out', done);
                }

                done();
            });
        });

        afterEach(function (done) {
            fs.stat(outFile, function (err) {
                if (err) {
                    return done();
                }

                fs.unlink(outFile, done);
            });
        });

        it('should produce colored output without any options', function (done) {
            exec(`node ./bin/newman.js run test/fixtures/run/single-get-request.json > ${outFile}`,
                function (code, stdout, stderr) {
                    expect(code).be(0);
                    expect(stderr).to.be.empty();
                    expect(stdout).to.be.empty();

                    fs.readFile(outFile, encoding, function (err, data) {
                        if (err) { return done(err); }

                        expect(data).to.match(coloredOutput);
                        done();
                    });
                });
        });

        it('should produce colored output with --color', function (done) {
            exec(`node ./bin/newman.js run test/fixtures/run/single-get-request.json --color > ${outFile}`,
                function (code, stdout, stderr) {
                    expect(code).be(0);
                    expect(stderr).to.be.empty();
                    expect(stdout).to.be.empty();

                    fs.readFile(outFile, encoding, function (err, data) {
                        if (err) { return done(err); }

                        expect(data).to.match(coloredOutput);
                        done();
                    });
                });
        });

        it('should not produce colored output with --no-color', function (done) {
            exec(`node ./bin/newman.js run test/fixtures/run/single-get-request.json --no-color > ${outFile}`,
                function (code, stdout, stderr) {
                    expect(code).be(0);
                    expect(stderr).to.be.empty();
                    expect(stdout).to.be.empty();

                    fs.readFile(outFile, encoding, function (err, data) {
                        if (err) { return done(err); }

                        expect(data).to.not.match(coloredOutput);
                        done();
                    });
                });
        });
    });
});

@@ -1,12 +1,52 @@
+/* global describe, it, exec, expect */%0A
 var fs = req
@@ -540,36 +540,72 @@
      expect(code
-).be
+, 'should have exit code of 0').to.equal
 (0);%0A           
@@ -627,34 +627,32 @@
 err).to.be.empty
-()
 ;%0A              
@@ -1022,36 +1022,72 @@
      expect(code
-).be
+, 'should have exit code of 0').to.equal
 (0);%0A           
@@ -1109,34 +1109,32 @@
 err).to.be.empty
-()
 ;%0A              
@@ -1514,36 +1514,72 @@
      expect(code
-).be
+, 'should have exit code of 0').to.equal
 (0);%0A           
@@ -1601,34 +1601,32 @@
 err).to.be.empty
-()
 ;%0A              
@@ -2573,36 +2573,72 @@
      expect(code
-).be
+, 'should have exit code of 0').to.equal
 (0);%0A           
@@ -2664,34 +2664,32 @@
 err).to.be.empty
-()
 ;%0A              
@@ -2712,34 +2712,32 @@
 out).to.be.empty
-()
 ;%0A%0A             
@@ -3260,36 +3260,72 @@
      expect(code
-).be
+, 'should have exit code of 0').to.equal
 (0);%0A           
@@ -3351,34 +3351,32 @@
 err).to.be.empty
-()
 ;%0A              
@@ -3399,34 +3399,32 @@
 out).to.be.empty
-()
 ;%0A%0A             
@@ -3969,12 +3969,48 @@
 code
-).be
+, 'should have exit code of 0').to.equal
 (0);
@@ -4056,18 +4056,16 @@
 be.empty
-()
 ;%0A      
@@ -4104,18 +4104,16 @@
 be.empty
-()
 ;%0A%0A     

Update color-tty tests to chai