Skip to content

Commit

Permalink
test: test lib/glob-asset.js from temp directory
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Jul 19, 2018
1 parent 15aaf04 commit 13f3be4
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions test/glob-assets.test.js
@@ -1,31 +1,41 @@
import path from 'path';
import test from 'ava';
import {ensureDir} from 'fs-extra';
import {copy, ensureDir} from 'fs-extra';
import {isPlainObject, sortBy} from 'lodash';
import tempy from 'tempy';
import globAssets from '../lib/glob-assets';

const cwd = 'test/fixtures/files';
const sortAssets = assets => sortBy(assets, asset => (isPlainObject(asset) ? asset.path : asset));

const fixtures = 'test/fixtures/files';

test('Retrieve file from single path', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, ['upload.txt']);

t.deepEqual(globbedAssets, ['upload.txt']);
});

test('Retrieve multiple files from path', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, ['upload.txt', 'upload_other.txt']);

t.deepEqual(sortAssets(globbedAssets), sortAssets(['upload_other.txt', 'upload.txt']));
});

test('Include missing files as defined, using Object definition', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, ['upload.txt', {path: 'miss*.txt', label: 'Missing'}]);

t.deepEqual(sortAssets(globbedAssets), sortAssets(['upload.txt', {path: 'miss*.txt', label: 'Missing'}]));
});

test('Retrieve multiple files from Object', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, [
{path: 'upload.txt', name: 'upload_name', label: 'Upload label'},
'upload_other.txt',
Expand All @@ -38,6 +48,8 @@ test('Retrieve multiple files from Object', async t => {
});

test('Retrieve multiple files without duplicates', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, [
'upload_other.txt',
'upload.txt',
Expand All @@ -51,6 +63,8 @@ test('Retrieve multiple files without duplicates', async t => {
});

test('Favor Object over String values when removing duplicates', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, [
'upload_other.txt',
'upload.txt',
Expand All @@ -68,36 +82,48 @@ test('Favor Object over String values when removing duplicates', async t => {
});

test('Retrieve file from single glob', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, ['upload.*']);

t.deepEqual(globbedAssets, ['upload.txt']);
});

test('Retrieve multiple files from single glob', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, ['*.txt']);

t.deepEqual(sortAssets(globbedAssets), sortAssets(['upload_other.txt', 'upload.txt']));
});

test('Accept glob array with one value', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, [['*load.txt'], ['*_other.txt']]);

t.deepEqual(sortAssets(globbedAssets), sortAssets(['upload_other.txt', 'upload.txt']));
});

test('Include globs that resolve to no files as defined', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, [['upload.txt', '!upload.txt']]);

t.deepEqual(sortAssets(globbedAssets), sortAssets(['!upload.txt', 'upload.txt']));
});

test('Accept glob array with one value for missing files', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, [['*missing.txt'], ['*_other.txt']]);

t.deepEqual(sortAssets(globbedAssets), sortAssets(['upload_other.txt', '*missing.txt']));
});

test('Replace name by filename for Object that match multiple files', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, [{path: '*.txt', name: 'upload_name', label: 'Upload label'}]);

t.deepEqual(
Expand All @@ -110,36 +136,48 @@ test('Replace name by filename for Object that match multiple files', async t =>
});

test('Include dotfiles', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, ['.dot*']);

t.deepEqual(globbedAssets, ['.dotfile']);
});

test('Ingnore single negated glob', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, ['!*.txt']);

t.deepEqual(globbedAssets, []);
});

test('Ingnore single negated glob in Object', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, [{path: '!*.txt'}]);

t.deepEqual(globbedAssets, []);
});

test('Accept negated globs', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
const globbedAssets = await globAssets({cwd}, [['*.txt', '!**/*_other.txt']]);

t.deepEqual(globbedAssets, ['upload.txt']);
});

test('Expand directories', async t => {
const cwd = tempy.directory();
await copy(fixtures, path.resolve(cwd, '.'));
const globbedAssets = await globAssets({cwd}, [['.']]);

t.deepEqual(sortAssets(globbedAssets), sortAssets(['upload_other.txt', 'upload.txt', '.dotfile']));
t.deepEqual(sortAssets(globbedAssets), sortAssets(['dir', 'dir/upload_other.txt', 'dir/upload.txt', 'dir/.dotfile']));
});

test('Include empty directory as defined', async t => {
const cwd = tempy.directory();
await copy(fixtures, cwd);
await ensureDir(path.resolve(cwd, 'empty'));
const globbedAssets = await globAssets({cwd}, [['empty']]);

Expand Down

0 comments on commit 13f3be4

Please sign in to comment.