|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/* eslint-env mocha */ |
| 4 | +const chai = require('chai') |
| 5 | +const dirtyChai = require('dirty-chai') |
| 6 | +const chaiAsPromised = require('chai-as-promised') |
| 7 | +const globSource = require('../../src/files/glob-source') |
| 8 | +const all = require('async-iterator-all') |
| 9 | +const path = require('path') |
| 10 | +const isNode = require('is-node') |
| 11 | + |
| 12 | +chai.use(dirtyChai) |
| 13 | +chai.use(chaiAsPromised) |
| 14 | +const expect = chai.expect |
| 15 | + |
| 16 | +describe('glob-source', () => { |
| 17 | + it('single file, relative path', async function () { |
| 18 | + if (!isNode) { |
| 19 | + return this.skip() |
| 20 | + } |
| 21 | + |
| 22 | + const result = await all(globSource(path.relative(process.cwd(), path.join(__dirname, '..', 'fixtures', 'file-0.html')))) |
| 23 | + |
| 24 | + expect(result.length).to.equal(1) |
| 25 | + expect(result[0].path).to.equal('file-0.html') |
| 26 | + }) |
| 27 | + |
| 28 | + it('directory, relative path', async function () { |
| 29 | + if (!isNode) { |
| 30 | + return this.skip() |
| 31 | + } |
| 32 | + |
| 33 | + const result = await all(globSource(path.relative(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir')), { |
| 34 | + recursive: true |
| 35 | + })) |
| 36 | + |
| 37 | + expect(result.length).to.equal(3) |
| 38 | + expect(result[0].path).to.equal('/dir/file-1.txt') |
| 39 | + expect(result[1].path).to.equal('/dir/file-2.js') |
| 40 | + expect(result[2].path).to.equal('/dir/file-3.css') |
| 41 | + }) |
| 42 | + |
| 43 | + it('single file, absolute path', async function () { |
| 44 | + if (!isNode) { |
| 45 | + return this.skip() |
| 46 | + } |
| 47 | + |
| 48 | + const result = await all(globSource(path.resolve(process.cwd(), path.join(__dirname, '..', 'fixtures', 'file-0.html')))) |
| 49 | + |
| 50 | + expect(result.length).to.equal(1) |
| 51 | + expect(result[0].path).to.equal('file-0.html') |
| 52 | + }) |
| 53 | + |
| 54 | + it('directory, relative path', async function () { |
| 55 | + if (!isNode) { |
| 56 | + return this.skip() |
| 57 | + } |
| 58 | + |
| 59 | + const result = await all(globSource(path.resolve(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir')), { |
| 60 | + recursive: true |
| 61 | + })) |
| 62 | + |
| 63 | + expect(result.length).to.equal(3) |
| 64 | + expect(result[0].path).to.equal('/dir/file-1.txt') |
| 65 | + expect(result[1].path).to.equal('/dir/file-2.js') |
| 66 | + expect(result[2].path).to.equal('/dir/file-3.css') |
| 67 | + }) |
| 68 | + |
| 69 | + it('directory, hidden files', async function () { |
| 70 | + if (!isNode) { |
| 71 | + return this.skip() |
| 72 | + } |
| 73 | + |
| 74 | + const result = await all(globSource(path.resolve(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir')), { |
| 75 | + recursive: true, |
| 76 | + hidden: true |
| 77 | + })) |
| 78 | + |
| 79 | + expect(result.length).to.equal(4) |
| 80 | + expect(result[0].path).to.equal('/dir/.hidden.txt') |
| 81 | + expect(result[1].path).to.equal('/dir/file-1.txt') |
| 82 | + expect(result[2].path).to.equal('/dir/file-2.js') |
| 83 | + expect(result[3].path).to.equal('/dir/file-3.css') |
| 84 | + }) |
| 85 | + |
| 86 | + it('directory, ignore files', async function () { |
| 87 | + if (!isNode) { |
| 88 | + return this.skip() |
| 89 | + } |
| 90 | + |
| 91 | + const result = await all(globSource(path.resolve(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir')), { |
| 92 | + recursive: true, |
| 93 | + ignore: ['**/file-1.txt'] |
| 94 | + })) |
| 95 | + |
| 96 | + expect(result.length).to.equal(2) |
| 97 | + expect(result[0].path).to.equal('/dir/file-2.js') |
| 98 | + expect(result[1].path).to.equal('/dir/file-3.css') |
| 99 | + }) |
| 100 | + |
| 101 | + it('require recusive flag for directory', async function () { |
| 102 | + if (!isNode) { |
| 103 | + return this.skip() |
| 104 | + } |
| 105 | + |
| 106 | + await expect(all(globSource(path.resolve(process.cwd(), path.join(__dirname, '..', 'fixtures', 'dir'))))).to.be.rejectedWith(/recursive option not set/) |
| 107 | + }) |
| 108 | +}) |
0 commit comments