|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var utils = require('../../lib/utils'); |
| 4 | +var fs = require('fs'); |
| 5 | +var path = require('path'); |
| 6 | +var os = require('os'); |
| 7 | +var mkdirp = require('mkdirp'); |
| 8 | +var rimraf = require('rimraf'); |
| 9 | + |
| 10 | +describe('file utils', function () { |
| 11 | + var tmpDir = path.join(os.tmpDir(), 'mocha-file-lookup'); |
| 12 | + var existsSync = fs.existsSync; |
| 13 | + var tmpFile = path.join.bind(path, tmpDir); |
| 14 | + var symlinkSupported = false; |
| 15 | + |
| 16 | + (function testSymlinkSupport () { |
| 17 | + makeTempDir(); |
| 18 | + |
| 19 | + fs.writeFileSync(tmpFile('mocha-utils.js'), 'yippy skippy ying yang yow'); |
| 20 | + try { |
| 21 | + fs.symlinkSync(tmpFile('mocha-utils.js'), tmpFile('mocha-utils-link.js')); |
| 22 | + symlinkSupported = true; |
| 23 | + } catch (ignored) { |
| 24 | + // ignored |
| 25 | + } finally { |
| 26 | + removeTempDir(); |
| 27 | + } |
| 28 | + }()); |
| 29 | + |
| 30 | + beforeEach(function () { |
| 31 | + makeTempDir(); |
| 32 | + |
| 33 | + fs.writeFileSync(tmpFile('mocha-utils.js'), 'yippy skippy ying yang yow'); |
| 34 | + if (symlinkSupported) { |
| 35 | + fs.symlinkSync(tmpFile('mocha-utils.js'), tmpFile('mocha-utils-link.js')); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + describe('.lookupFiles', function () { |
| 40 | + (symlinkSupported ? it : it.skip)('should not return broken symlink file path', function () { |
| 41 | + expect(utils.lookupFiles(tmpDir, ['js'], false)) |
| 42 | + .to |
| 43 | + .contain(tmpFile('mocha-utils-link.js')) |
| 44 | + .and |
| 45 | + .contain(tmpFile('mocha-utils.js')) |
| 46 | + .and |
| 47 | + .have |
| 48 | + .length(2); |
| 49 | + expect(existsSync(tmpFile('mocha-utils-link.js'))) |
| 50 | + .to |
| 51 | + .be(true); |
| 52 | + fs.renameSync(tmpFile('mocha-utils.js'), tmpFile('bob')); |
| 53 | + expect(existsSync(tmpFile('mocha-utils-link.js'))) |
| 54 | + .to |
| 55 | + .be(false); |
| 56 | + expect(utils.lookupFiles(tmpDir, ['js'], false)) |
| 57 | + .to |
| 58 | + .eql([]); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should accept a glob "path" value', function () { |
| 62 | + var res = utils.lookupFiles(tmpFile('mocha-utils*'), ['js'], false) |
| 63 | + .map(path.normalize.bind(path)); |
| 64 | + |
| 65 | + var expectedLength = 0; |
| 66 | + var ex = expect(res) |
| 67 | + .to |
| 68 | + .contain(tmpFile('mocha-utils.js')); |
| 69 | + expectedLength++; |
| 70 | + |
| 71 | + if (symlinkSupported) { |
| 72 | + ex = ex.and |
| 73 | + .contain(tmpFile('mocha-utils-link.js')); |
| 74 | + expectedLength++; |
| 75 | + } |
| 76 | + |
| 77 | + ex.and |
| 78 | + .have |
| 79 | + .length(expectedLength); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('.files', function () { |
| 84 | + (symlinkSupported ? it : it.skip)('should return broken symlink file path', function () { |
| 85 | + expect(utils.files(tmpDir, ['js'])) |
| 86 | + .to.contain(tmpFile('mocha-utils-link.js')) |
| 87 | + .and.contain(tmpFile('mocha-utils.js')) |
| 88 | + .and.have.length(2); |
| 89 | + |
| 90 | + expect(existsSync(tmpFile('mocha-utils-link.js'))) |
| 91 | + .to.be(true); |
| 92 | + |
| 93 | + fs.renameSync(tmpFile('mocha-utils.js'), tmpFile('bob')); |
| 94 | + |
| 95 | + expect(existsSync(tmpFile('mocha-utils-link.js'))) |
| 96 | + .to.be(false); |
| 97 | + |
| 98 | + expect(utils.files(tmpDir, ['js'])) |
| 99 | + .to.eql([tmpFile('mocha-utils-link.js')]); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + afterEach(removeTempDir); |
| 104 | + |
| 105 | + function makeTempDir () { |
| 106 | + mkdirp.sync(tmpDir); |
| 107 | + } |
| 108 | + |
| 109 | + function removeTempDir () { |
| 110 | + rimraf.sync(tmpDir); |
| 111 | + } |
| 112 | +}); |
0 commit comments