test.js 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
'use strict';

var expect = require('expect.js');
var commandExists = require('..');
var commandExistsSync = commandExists.sync;
var resolve = require('path').resolve;
var isUsingWindows = process.platform == 'win32'

describe('commandExists', function(){
    describe('async - callback', function() {
        it('it should find a command named ls or xcopy', function(done){
            var commandToUse = 'ls'
            if (isUsingWindows) {
                commandToUse = 'xcopy'
            }

            commandExists(commandToUse, function(err, exists) {
                expect(err).to.be(null);
                expect(exists).to.be(true);
                done();
            });
        });

        it('it should not find a command named fdsafdsafdsafdsafdsa', function(done){
            commandExists('fdsafdsafdsafdsafdsa', function(err, exists) {
                expect(err).to.be(null);
                expect(exists).to.be(false);
                done();
            });
        });
    });

    describe('async - promise', function() {
        it('it should find a command named ls or xcopy', function(done){
            var commandToUse = 'ls'
            if (isUsingWindows) {
                commandToUse = 'xcopy'
            }

            commandExists(commandToUse)
            .then(function(command) {
                expect(command).to.be(commandToUse);
                done();
            });
        });

        it('it should not find a command named fdsafdsafdsafdsafdsa', function(done){
            commandExists('fdsafdsafdsafdsafdsa')
            .then(function() {
                // We should not execute this line.
                expect(true).to.be(false);
            })
            .catch(function() {
                done();
            });
        });
    });

    describe('sync', function() {
        it('it should find a command named ls or xcopy', function(){
            var commandToUse = 'ls'
            if (isUsingWindows) {
                commandToUse = 'xcopy'
            }
            expect(commandExistsSync(commandToUse)).to.be(true);
        });

        it('it should not find a command named fdsafdsafdsafdsafdsa', function(){
            expect(commandExistsSync('fdsafdsafdsafdsafdsa')).to.be(false);
        });

        it('it should not find a command named ls or xcopy prefixed with some nonsense', function(){
            var commandToUse = 'fdsafdsa ls'
            if (isUsingWindows) {
                commandToUse = 'fdsafdsaf xcopy'
            }
            expect(commandExistsSync(commandToUse)).to.be(false);
        });

        it('it should not execute some nefarious code', function(){
            expect(commandExistsSync('ls; touch /tmp/foo0')).to.be(false);
        });

        it('it should not execute some nefarious code', function(){
            expect(commandExistsSync('ls touch /tmp/foo0')).to.be(false);
        });
    });

    describe('local file', function() {
        it('it should report false if there is a non-executable file with that name', function(done) {
            var commandToUse = 'test/non-executable-script.js'
            commandExists(commandToUse)
                .then(function(command){
                    // We should not execute this line.
                    expect(true).to.be(false);
                }).catch(function(err){
                    expect(err).to.be(null);
                    done();
                });
        });


        if (!isUsingWindows) {
            it('it should report true if there is an executable file with that name', function(done) {
                var commandToUse = 'test/executable-script.js'
                commandExists(commandToUse)
                    .then(function(command){
                        // We should not execute this line.
                        expect(command).to.be(commandToUse);
                        done();
                    });
            });
        }

        if (isUsingWindows) {
            it('it should report true if there is an executable file with that name', function(done) {
                var commandToUse = 'test\\executable-script.cmd'
                commandExists(commandToUse)
                    .then(function(command){
                        expect(command).to.be(commandToUse);
                        done();
                    });
            });

            it('it should report false if there is a double quotation mark in the file path', function() {
                var commandToUse = 'test\\"executable-script.cmd'
                expect(commandExists.sync(commandToUse)).to.be(false);
            });
        }
    });

    describe('absolute path', function() {
        it('it should report true if there is a command with that name in absolute path', function(done) {
            var commandToUse = resolve('test/executable-script.js');
            commandExists(commandToUse)
            .then(function(command){
                expect(command).to.be(commandToUse);
                done();
            });
        });
        
        it('it should report false if there is not a command with that name in absolute path', function() {
            var commandToUse = resolve('executable-script.js');
            expect(commandExists.sync(commandToUse)).to.be(false);
        });
    });
});