forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkdir-cache.test.js
More file actions
134 lines (109 loc) · 4.25 KB
/
Copy pathworkdir-cache.test.js
File metadata and controls
134 lines (109 loc) · 4.25 KB
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
124
125
126
127
128
129
130
131
132
133
134
import path from 'path';
import temp from 'temp';
import fs from 'fs-extra';
import {cloneRepository} from '../helpers';
import WorkdirCache from '../../lib/models/workdir-cache';
describe('WorkdirCache', function() {
let cache;
beforeEach(function() {
cache = new WorkdirCache(5);
});
it('finds a workdir that is the given path', async function() {
const sameDir = await cloneRepository('three-files');
const workDir = await cache.find(sameDir);
assert.equal(sameDir, workDir);
});
it("finds a workdir that's a parent of the given path", async function() {
const expectedDir = await cloneRepository('three-files');
const givenDir = path.join(expectedDir, 'subdir-1');
const actualDir = await cache.find(givenDir);
assert.equal(actualDir, expectedDir);
});
it('finds a workdir from a gitdir file', async function() {
const repoDir = await cloneRepository('three-files');
const expectedDir = await fs.realpath(temp.mkdirSync());
fs.writeFileSync(path.join(expectedDir, '.git'), `gitdir: ${path.join(repoDir, '.git')}`, 'utf8');
const actualDir = await cache.find(expectedDir);
assert.equal(actualDir, expectedDir);
});
it('returns null when a path is not in a git repository', async function() {
const nonWorkdirPath = temp.mkdirSync();
assert.isNull(await cache.find(nonWorkdirPath));
});
it('returns null when a path does not exist', async function() {
const nope = path.join(
__dirname,
'does', 'not', 'exist', 'no', 'seriously', 'why', 'did', 'you', 'name', 'a', 'directory', 'this',
);
assert.isNull(await cache.find(nope));
});
it('understands a file path', async function() {
const expectedDir = await cloneRepository('three-files');
const givenFile = path.join(expectedDir, 'subdir-1', 'b.txt');
const actualDir = await cache.find(givenFile);
assert.equal(actualDir, expectedDir);
});
it('caches previously discovered results', async function() {
const expectedDir = await cloneRepository('three-files');
const givenDir = path.join(expectedDir, 'subdir-1');
// Prime the cache
await cache.find(givenDir);
assert.isTrue(cache.known.has(givenDir));
sinon.spy(cache, 'revParse');
const actualDir = await cache.find(givenDir);
assert.equal(actualDir, expectedDir);
assert.isFalse(cache.revParse.called);
});
it('removes all cached entries', async function() {
const [dir0, dir1] = await Promise.all([
cloneRepository('three-files'),
cloneRepository('three-files'),
]);
const pathsToCheck = [
dir0,
path.join(dir0, 'a.txt'),
path.join(dir0, 'subdir-1'),
path.join(dir0, 'subdir-1', 'b.txt'),
dir1,
];
const expectedWorkdirs = [
dir0, dir0, dir0, dir0, dir1,
];
// Prime the cache
const initial = await Promise.all(
pathsToCheck.map(input => cache.find(input)),
);
assert.deepEqual(initial, expectedWorkdirs);
// Re-lookup and hit the cache
sinon.spy(cache, 'revParse');
const relookup = await Promise.all(
pathsToCheck.map(input => cache.find(input)),
);
assert.deepEqual(relookup, expectedWorkdirs);
assert.equal(cache.revParse.callCount, 0);
// Clear the cache
await cache.invalidate();
// Re-lookup and miss the cache
const after = await Promise.all(
pathsToCheck.map(input => cache.find(input)),
);
assert.deepEqual(after, expectedWorkdirs);
assert.isTrue(cache.revParse.calledWith(dir0));
assert.isTrue(cache.revParse.calledWith(path.join(dir0, 'a.txt')));
assert.isTrue(cache.revParse.calledWith(path.join(dir0, 'subdir-1')));
assert.isTrue(cache.revParse.calledWith(path.join(dir0, 'subdir-1', 'b.txt')));
assert.isTrue(cache.revParse.calledWith(dir1));
});
it('clears the cache when the maximum size is exceeded', async function() {
const dirs = await Promise.all(
Array(6).fill(null, 0, 6).map(() => cloneRepository('three-files')),
);
await Promise.all(dirs.slice(0, 5).map(dir => cache.find(dir)));
await cache.find(dirs[5]);
const expectedDir = dirs[2];
sinon.spy(cache, 'revParse');
const actualDir = await cache.find(expectedDir);
assert.strictEqual(actualDir, expectedDir);
assert.isTrue(cache.revParse.called);
});
});