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