-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
123 lines (117 loc) · 4.71 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const test = require('tape');
const mongorcmgr = require('./mongorc-manager');
function runCountHelper(fn) {
let runCount = 0;
return function () {
runCount++;
fn.apply(fn, [...arguments, runCount]);
}
}
test('file is created when not existing', t => {
t.plan(3);
const fileManager = {
writeFromArray: (file, array) => {
t.equal(file, mongorcmgr.mongorcPath, 'writes the right file');
t.equal(array[0], `//${mongorcmgr.WATERMARK}//`, 'writes the right content');
},
fileExistsRW: file => {
t.equal(file, mongorcmgr.mongorcPath, 'checks the right file');
return false;
}
};
mongorcmgr._checkMongorc(fileManager);
});
test('file is backed up when already existing and not managed', t => {
t.plan(11);
const fileManager = {
fileExistsRW: file => {
t.equal(file, mongorcmgr.mongorcPath, 'checks the right file');
return true;
},
rename: (origin, dest) => {
t.equal(origin, mongorcmgr.mongorcPath, 'renames the right file');
t.equal(dest, mongorcmgr.mongorcBackupPath, 'backs up to the right file');
},
writeFromArray: runCountHelper((file, array, runCount) => {
t.equal(file, mongorcmgr.mongorcPath, 'writes the right file');
if (runCount === 1) {
t.equal(array[0], `//${mongorcmgr.WATERMARK}//`, 'writes the right content the .mongorc.js is initialized');
return;
}
if (runCount === 2) {
t.equal(array[0], `//${mongorcmgr.WATERMARK}//`, 'writes the right watermark header');
t.equal(array[1], `load('${mongorcmgr.mongorcBackupPath}');`, 'writes the right linked script');
return;
}
}),
readAsArray: file => {
t.equal(file, mongorcmgr.mongorcPath, 'reads the right file');
return ['function foo() {'];
}
};
const lines = mongorcmgr._checkMongorc(fileManager);
t.equal(lines[0], `//${mongorcmgr.WATERMARK}//`, 'lines contains the right watermark header');
t.equal(lines[1], `load('${mongorcmgr.mongorcBackupPath}');`, 'lines contains the right linked script');
});
test('list linked libraries', t => {
t.plan(4);
const fileManager = {
fileExistsRW: file => {
return true;
},
readAsArray: file => {
t.equal(file, mongorcmgr.mongorcPath, 'reads the right file');
return [`//${mongorcmgr.WATERMARK}//`, `load('/foo/bar/monkey.js');`, `load('/foo/bar/dave.js');`];
}
};
mongorcmgr.listLinkedLibraries(fileManager, {
log: runCountHelper((m, runCount) => {
if (runCount === 1) {
t.pass('header gets printed out');
}
if (runCount === 2) {
t.ok(m.includes('/foo/bar/monkey.js'), 'the first linked library gets printed out');
}
if (runCount === 3) {
t.ok(m.includes('/foo/bar/dave.js'), 'the second linked library gets printed out');
}
})
});
});
test('links library', t => {
t.plan(5);
const fileManager = {
fileExistsRW: file => {
return true;
},
readAsArray: file => {
t.equal(file, mongorcmgr.mongorcPath, 'reads the right file');
return [`//${mongorcmgr.WATERMARK}//`, `load('/foo/bar/monkey.js');`];
},
writeFromArray: (file, array) => {
t.equal(file, mongorcmgr.mongorcPath, 'writes the right file');
t.equal(array[0], `//${mongorcmgr.WATERMARK}//`, 'writes the right watermark header');
t.equal(array[1], `load('/foo/bar/monkey.js');`, 'writes back the library that was already linked');
t.equal(array[2], `load('/foo/bar/dave.js');`, 'writes the new library that was just linked');
}
};
mongorcmgr.linkLibrary('/foo/bar/dave.js', fileManager);
});
test('unlinks library', t => {
t.plan(4);
const fileManager = {
fileExistsRW: file => {
return true;
},
readAsArray: file => {
t.equal(file, mongorcmgr.mongorcPath, 'reads the right file');
return [`//${mongorcmgr.WATERMARK}//`, `load('/foo/bar/monkey.js');`, `load('/foo/bar/dave.js');`];
},
writeFromArray: (file, array) => {
t.equal(file, mongorcmgr.mongorcPath, 'writes the right file');
t.equal(array[0], `//${mongorcmgr.WATERMARK}//`, 'writes the right watermark header');
t.equal(array[1], `load('/foo/bar/dave.js');`, 'writes the new library that was just linked');
}
};
mongorcmgr.unlinkLibrary('/foo/bar/monkey.js', fileManager);
});