2015-06-09 01:08:13 -04:00
|
|
|
let mockfs = require('mock-fs');
|
|
|
|
import fs = require('fs');
|
2015-06-23 12:09:32 -04:00
|
|
|
import path = require('path');
|
2015-06-09 01:08:13 -04:00
|
|
|
import {TreeDiffer} from './tree-differ';
|
|
|
|
import {DiffingFlatten} from './broccoli-flatten';
|
|
|
|
|
|
|
|
describe('Flatten', () => {
|
|
|
|
afterEach(() => mockfs.restore());
|
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
let flatten = (inputPaths: string) => new DiffingFlatten(inputPaths, 'output', null);
|
2016-05-26 13:45:37 -04:00
|
|
|
let read = (path: string) => fs.readFileSync(path, {encoding: 'utf-8'});
|
2016-01-22 13:51:16 -05:00
|
|
|
let rm = (path: string) => fs.unlinkSync(path);
|
|
|
|
let write =
|
2016-05-26 13:45:37 -04:00
|
|
|
(path: string, content: string) => { fs.writeFileSync(path, content, {encoding: 'utf-8'}); }
|
2015-06-09 01:08:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
it('should flatten files and be incremental', () => {
|
|
|
|
let testDir = {
|
|
|
|
'input': {
|
|
|
|
'dir1': {
|
|
|
|
'file-1.txt': mockfs.file({content: 'file-1.txt content', mtime: new Date(1000)}),
|
|
|
|
'file-2.txt': mockfs.file({content: 'file-2.txt content', mtime: new Date(1000)}),
|
|
|
|
'subdir-1': {
|
|
|
|
'file-1.1.txt': mockfs.file({content: 'file-1.1.txt content', mtime: new Date(1000)})
|
|
|
|
},
|
|
|
|
'empty-dir': {}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'output': {}
|
|
|
|
};
|
|
|
|
mockfs(testDir);
|
|
|
|
|
|
|
|
let differ = new TreeDiffer('testLabel', 'input');
|
|
|
|
let flattenedTree = flatten('input');
|
|
|
|
flattenedTree.rebuild(differ.diffTree());
|
|
|
|
|
|
|
|
expect(fs.readdirSync('output')).toEqual(['file-1.1.txt', 'file-1.txt', 'file-2.txt']);
|
|
|
|
// fails due to a mock-fs bug related to reading symlinks?
|
2015-06-12 10:50:45 -04:00
|
|
|
// expect(read('output/file-1.1.txt')).toBe('file-1.1.txt content');
|
2015-06-09 01:08:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
// delete a file
|
|
|
|
rm('input/dir1/file-1.txt');
|
|
|
|
// add a new one
|
|
|
|
write('input/dir1/file-3.txt', 'file-3.txt content');
|
|
|
|
|
|
|
|
flattenedTree.rebuild(differ.diffTree());
|
|
|
|
|
|
|
|
expect(fs.readdirSync('output')).toEqual(['file-1.1.txt', 'file-2.txt', 'file-3.txt']);
|
|
|
|
});
|
2015-05-31 20:24:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
it('should throw an exception if duplicates are found', () => {
|
|
|
|
let testDir = {
|
|
|
|
'input': {
|
|
|
|
'dir1': {
|
|
|
|
'file-1.txt': mockfs.file({content: 'file-1.txt content', mtime: new Date(1000)}),
|
2015-06-12 10:50:45 -04:00
|
|
|
'subdir-1':
|
|
|
|
{'file-1.txt': mockfs.file({content: 'file-1.1.txt content', mtime: new Date(1000)})},
|
2015-05-31 20:24:21 -04:00
|
|
|
'empty-dir': {}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'output': {}
|
|
|
|
};
|
|
|
|
mockfs(testDir);
|
|
|
|
|
|
|
|
let differ = new TreeDiffer('testLabel', 'input');
|
|
|
|
let flattenedTree = flatten('input');
|
2015-06-12 10:50:45 -04:00
|
|
|
expect(() => flattenedTree.rebuild(differ.diffTree()))
|
2016-05-26 13:45:37 -04:00
|
|
|
.toThrowError(
|
|
|
|
'Duplicate file \'file-1.txt\' found in path \'dir1' + path.sep + 'subdir-1' +
|
|
|
|
path.sep + 'file-1.txt\'');
|
2015-05-31 20:24:21 -04:00
|
|
|
});
|
2015-06-09 01:08:13 -04:00
|
|
|
});
|