2015-05-21 12:07:52 -04:00
|
|
|
let mockfs = require('mock-fs');
|
|
|
|
import fs = require('fs');
|
2016-01-22 13:51:16 -05:00
|
|
|
import {TreeDiffer, DiffResult} from './tree-differ';
|
2015-05-21 12:07:52 -04:00
|
|
|
import {MergeTrees} from './broccoli-merge-trees';
|
|
|
|
|
|
|
|
describe('MergeTrees', () => {
|
|
|
|
afterEach(() => mockfs.restore());
|
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
function mergeTrees(inputPaths: string[], cachePath: string, options: {}) {
|
2015-05-21 12:07:52 -04:00
|
|
|
return new MergeTrees(inputPaths, cachePath, options);
|
|
|
|
}
|
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
function MakeTreeDiffers(rootDirs: string[]): TreeDiffer[] {
|
|
|
|
return rootDirs.map((rootDir) => new TreeDiffer('MergeTrees', rootDir));
|
2015-05-21 12:07:52 -04:00
|
|
|
}
|
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
let diffTrees = (differs: TreeDiffer[]): DiffResult[] => differs.map(tree => tree.diffTree());
|
|
|
|
function read(path: string) { return fs.readFileSync(path, "utf-8"); }
|
2015-05-21 12:07:52 -04:00
|
|
|
|
2015-06-04 17:39:53 -04:00
|
|
|
it('should copy the file from the right-most inputTree with overwrite=true', () => {
|
2015-05-21 12:07:52 -04:00
|
|
|
let testDir: any = {
|
|
|
|
'tree1': {'foo.js': mockfs.file({content: 'tree1/foo.js content', mtime: new Date(1000)})},
|
|
|
|
'tree2': {'foo.js': mockfs.file({content: 'tree2/foo.js content', mtime: new Date(1000)})},
|
|
|
|
'tree3': {'foo.js': mockfs.file({content: 'tree3/foo.js content', mtime: new Date(1000)})}
|
|
|
|
};
|
|
|
|
mockfs(testDir);
|
|
|
|
let treeDiffer = MakeTreeDiffers(['tree1', 'tree2', 'tree3']);
|
2015-06-04 17:39:53 -04:00
|
|
|
let treeMerger = mergeTrees(['tree1', 'tree2', 'tree3'], 'dest', {overwrite: true});
|
2016-01-22 13:51:16 -05:00
|
|
|
treeMerger.rebuild(diffTrees(treeDiffer));
|
2015-05-21 12:07:52 -04:00
|
|
|
expect(read('dest/foo.js')).toBe('tree3/foo.js content');
|
|
|
|
|
|
|
|
delete testDir.tree2['foo.js'];
|
|
|
|
delete testDir.tree3['foo.js'];
|
|
|
|
mockfs(testDir);
|
2016-01-22 13:51:16 -05:00
|
|
|
treeMerger.rebuild(diffTrees(treeDiffer));
|
2015-05-21 12:07:52 -04:00
|
|
|
expect(read('dest/foo.js')).toBe('tree1/foo.js content');
|
|
|
|
|
|
|
|
testDir.tree2['foo.js'] = mockfs.file({content: 'tree2/foo.js content', mtime: new Date(1000)});
|
|
|
|
mockfs(testDir);
|
2016-01-22 13:51:16 -05:00
|
|
|
treeMerger.rebuild(diffTrees(treeDiffer));
|
2015-05-21 12:07:52 -04:00
|
|
|
expect(read('dest/foo.js')).toBe('tree2/foo.js content');
|
|
|
|
});
|
2015-06-04 17:39:53 -04:00
|
|
|
|
2015-05-31 20:24:21 -04:00
|
|
|
it('should throw if duplicates are found during the initial build', () => {
|
2015-06-04 17:39:53 -04:00
|
|
|
let testDir: any = {
|
|
|
|
'tree1': {'foo.js': mockfs.file({content: 'tree1/foo.js content', mtime: new Date(1000)})},
|
|
|
|
'tree2': {'foo.js': mockfs.file({content: 'tree2/foo.js content', mtime: new Date(1000)})},
|
|
|
|
'tree3': {'foo.js': mockfs.file({content: 'tree3/foo.js content', mtime: new Date(1000)})}
|
|
|
|
};
|
|
|
|
mockfs(testDir);
|
|
|
|
let treeDiffer = MakeTreeDiffers(['tree1', 'tree2', 'tree3']);
|
|
|
|
let treeMerger = mergeTrees(['tree1', 'tree2', 'tree3'], 'dest', {});
|
2016-01-22 13:51:16 -05:00
|
|
|
expect(() => treeMerger.rebuild(diffTrees(treeDiffer)))
|
2015-06-16 12:24:29 -04:00
|
|
|
.toThrowError(
|
|
|
|
'Duplicate path found while merging trees. Path: "foo.js".\n' +
|
|
|
|
'Either remove the duplicate or enable the "overwrite" option for this merge.');
|
2015-06-04 17:39:53 -04:00
|
|
|
|
2015-05-31 20:24:21 -04:00
|
|
|
testDir = {
|
|
|
|
'tree1': {'foo.js': mockfs.file({content: 'tree1/foo.js content', mtime: new Date(1000)})},
|
|
|
|
'tree2': {},
|
|
|
|
'tree3': {}
|
|
|
|
};
|
|
|
|
mockfs(testDir);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should throw if duplicates are found during rebuild', () => {
|
2016-01-22 13:51:16 -05:00
|
|
|
let testDir: any = {
|
2015-05-31 20:24:21 -04:00
|
|
|
'tree1': {'foo.js': mockfs.file({content: 'tree1/foo.js content', mtime: new Date(1000)})},
|
|
|
|
'tree2': {},
|
|
|
|
'tree3': {}
|
|
|
|
};
|
2015-06-04 17:39:53 -04:00
|
|
|
mockfs(testDir);
|
2015-05-31 20:24:21 -04:00
|
|
|
|
|
|
|
let treeDiffer = MakeTreeDiffers(['tree1', 'tree2', 'tree3']);
|
|
|
|
let treeMerger = mergeTrees(['tree1', 'tree2', 'tree3'], 'dest', {});
|
2016-01-22 13:51:16 -05:00
|
|
|
expect(() => treeMerger.rebuild(diffTrees(treeDiffer))).not.toThrow();
|
2015-06-04 17:39:53 -04:00
|
|
|
|
2015-05-31 20:24:21 -04:00
|
|
|
|
2015-06-04 17:39:53 -04:00
|
|
|
testDir.tree2['foo.js'] = mockfs.file({content: 'tree2/foo.js content', mtime: new Date(1000)});
|
|
|
|
mockfs(testDir);
|
2016-01-22 13:51:16 -05:00
|
|
|
expect(() => treeMerger.rebuild(diffTrees(treeDiffer)))
|
2015-06-16 12:24:29 -04:00
|
|
|
.toThrowError(
|
|
|
|
'Duplicate path found while merging trees. Path: "foo.js".\n' +
|
|
|
|
'Either remove the duplicate or enable the "overwrite" option for this merge.');
|
2015-06-04 17:39:53 -04:00
|
|
|
});
|
2015-05-21 12:07:52 -04:00
|
|
|
});
|