/
file-utils.spec.ts
82 lines (76 loc) · 2.05 KB
/
file-utils.spec.ts
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
import { calculateFileChanges, WholeFileChange } from './file-utils';
import { DiffType, JsonChange, jsonDiff } from '../utils/json-diff';
const ignore = require('ignore');
describe('calculateFileChanges', () => {
it('should return a whole file change by default', () => {
const changes = calculateFileChanges(
['proj/index.ts'],
undefined,
(path, revision) => {
return revision === 'sha1' ? '' : 'const a = 0;';
}
);
expect(changes[0].getChanges()).toEqual([new WholeFileChange()]);
});
it('should return a json changes for json files', () => {
const changes = calculateFileChanges(
['package.json'],
{
base: 'sha1',
head: 'sha2'
},
(path, revision) => {
return revision === 'sha1'
? JSON.stringify({
dependencies: {
'happy-nrwl': '0.0.1',
'not-awesome-nrwl': '0.0.1'
}
})
: JSON.stringify({
dependencies: {
'happy-nrwl': '0.0.2',
'awesome-nrwl': '0.0.1'
}
});
}
);
expect(changes[0].getChanges()).toContainEqual({
type: DiffType.Modified,
path: ['dependencies', 'happy-nrwl'],
value: {
lhs: '0.0.1',
rhs: '0.0.2'
}
});
expect(changes[0].getChanges()).toContainEqual({
type: DiffType.Deleted,
path: ['dependencies', 'not-awesome-nrwl'],
value: {
lhs: '0.0.1',
rhs: undefined
}
});
expect(changes[0].getChanges()).toContainEqual({
type: DiffType.Added,
path: ['dependencies', 'awesome-nrwl'],
value: {
lhs: undefined,
rhs: '0.0.1'
}
});
});
it('should ignore *.md changes', () => {
const ig = ignore();
ig.add('*.md');
const changes = calculateFileChanges(
['proj/readme.md'],
undefined,
(path, revision) => {
return revision === 'sha1' ? '' : 'const a = 0;';
},
ig
);
expect(changes.length).toEqual(0);
});
});