-
Notifications
You must be signed in to change notification settings - Fork 41
/
FileUtils.spec.ts
187 lines (157 loc) · 7.29 KB
/
FileUtils.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// tslint:disable: no-unused-expression
import { expect } from 'chai';
import * as fsExtra from 'fs-extra';
import * as path from 'path';
import * as rmfr from 'rmfr';
import * as sinonActual from 'sinon';
import { fileUtils } from './FileUtils';
import { SourceNode } from 'source-map';
let sinon = sinonActual.createSandbox();
let n = path.normalize;
const rootDir = path.normalize(path.dirname(__dirname));
describe('FileUtils', () => {
afterEach(() => {
sinon.restore();
});
describe('getAllRelativePaths', () => {
//basic test to get code coverage...we don't need to test the glob code too much here...
it('works', () => {
expect(fileUtils.getAllRelativePaths(path.join(__dirname, '..', 'images'))).to.contain(path.join('icon.png'));
});
});
describe('removeFileTruncation', () => {
it('does not replace when the `...` character is missing', () => {
expect(fileUtils.removeFileTruncation('project1/main.brs')).to.equal('project1/main.brs');
});
it('removes leading ... when present', () => {
expect(fileUtils.removeFileTruncation('...project1/main.brs')).to.equal('project1/main.brs');
});
});
describe('pathEndsWith', () => {
it('accepts same path', () => {
expect(fileUtils.pathEndsWith('project1/main.brs', 'project1/main.brs')).to.be.true;
});
it('rejects contained path not found at end', () => {
expect(fileUtils.pathEndsWith('project1/main.brs.map', 'project1/main.brs')).to.be.false;
});
it('rejects non-similar path', () => {
expect(fileUtils.pathEndsWith('project1/main.brs.map', 'project2/lib.brs')).to.be.false;
});
});
describe('findPartialFileInDirectory', () => {
beforeEach(() => {
sinon.stub(fileUtils, 'getAllRelativePaths').returns([
'source/main.brs',
'source/lib1/lib.brs',
'source/lib2/lib.brs'
]);
});
it('normalizes the paths', () => {
expect(fileUtils.findPartialFileInDirectory('...ource\\lib2//lib.brs', 'NOT_IMPORTANT_DUE_TO_MOCK'));
});
it('returns first result when multiple matches are found', () => {
let stub = sinon.stub(console, 'warn').returns(undefined);
expect(fileUtils.findPartialFileInDirectory('...lib.brs', 'SomeAppDir')).to.equal('source/lib1/lib.brs');
//a warning should be logged to the console about the fact that there are multiple matches
expect(stub.getCalls()).to.be.lengthOf(1);
});
it('returns undefined when no results found', () => {
expect(fileUtils.findPartialFileInDirectory('...promise.brs', 'SomeAppDir')).to.be.undefined;
});
});
describe('getComponentLibraryIndex', () => {
it('finds the index', () => {
expect(fileUtils.getComponentLibraryIndex('pkg:/source/main__lib0.brs', '__lib')).to.equal(0);
expect(fileUtils.getComponentLibraryIndex('pkg:/source/main__lib1.brs', '__lib')).to.equal(1);
expect(fileUtils.getComponentLibraryIndex('pkg:/source/main__lib12.brs', '__lib')).to.equal(12);
});
it('returns undefined no number was found', () => {
expect(fileUtils.getComponentLibraryIndex('pkg:/source/main__lib.brs', '__lib')).to.be.undefined;
});
it('returns undefined no postfix was found', () => {
expect(fileUtils.getComponentLibraryIndex('pkg:/source/main__notlib1.brs', '__lib')).to.be.undefined;
});
it('returns undefined when item after postfix is not a number', () => {
expect(fileUtils.getComponentLibraryIndex('pkg:/source/main__libcat.brs', '__lib')).to.be.undefined;
});
});
describe('findFirstRelativeFile', () => {
let paths = [] as string[];
let rootA = n(`${rootDir}/compLibA`);
let rootB = n(`${rootDir}/compLibB`);
let rootC = n(`${rootDir}/compLibC`);
let pathA = path.join(rootA, 'main.brs');
let pathB = path.join(rootB, 'main.brs');
let pathC = path.join(rootC, 'main.brs');
beforeEach(() => {
sinon.stub(fsExtra, 'pathExists').callsFake((filePath: string) => {
return paths.indexOf(filePath) > -1;
});
});
it('finds file from first folder', async () => {
paths = [pathA, pathB, pathC];
expect(await fileUtils.findFirstRelativeFile('main.brs', [rootA, rootB, rootC])).to.equal(pathA);
});
it('finds file from middle folder', async () => {
paths = [pathB, pathC];
expect(await fileUtils.findFirstRelativeFile('main.brs', [rootA, rootB, rootC])).to.equal(pathB);
});
it('finds file from last folder', async () => {
paths = [pathC];
expect(await fileUtils.findFirstRelativeFile('main.brs', [rootA, rootB, rootC])).to.equal(pathC);
});
});
describe('getSourceLocationFromSourceMap', () => {
let tempDirPath = n(`${rootDir}/.test_temp`);
let sourceDirPath = n(`${tempDirPath}/source`);
let outDirPath = n(`${tempDirPath}/out`);
let sourceFilePath = n(`${sourceDirPath}/file.brs`);
let outFilePath = n(`${outDirPath}/file.brs`);
let outFileMapPath = n(`${outFilePath}.map`);
beforeEach(async () => {
await rmfr(tempDirPath);
await fsExtra.mkdir(tempDirPath);
await fsExtra.mkdir(sourceDirPath);
await fsExtra.mkdir(outDirPath);
let sourceContents = `function main()\n print "hello"\n print "world"\nend function
`;
//create a source file
await fsExtra.writeFile(sourceFilePath, sourceContents);
});
afterEach(async () => {
await rmfr(tempDirPath);
});
async function createOutFiles(sourcePath) {
//transform the file (by adding extra newlines
let result = new SourceNode(null, null, sourcePath, [
new SourceNode(1, 0, sourcePath, 'function main()\n'),
'\n',
new SourceNode(2, 0, sourcePath, ' print "hello"\n'),
'\n',
new SourceNode(3, 0, sourcePath, ' print "world"\n'),
'\n',
new SourceNode(4, 0, sourcePath, 'end function\n')
]).toStringWithSourceMap();
await fsExtra.writeFile(outFilePath, result.code);
await fsExtra.writeFile(outFileMapPath, result.map);
}
it('supports absolute paths in source map', async () => {
await createOutFiles(sourceFilePath);
let location = await fileUtils.getSourceLocationFromSourcemap(outFilePath, 3);
expect(location).to.eql({
pathAbsolute: sourceFilePath,
lineNumber: 2,
columnIndex: 0
});
});
it('supports relative paths in source map', async () => {
await createOutFiles('../source/file.brs');
let location = await fileUtils.getSourceLocationFromSourcemap(outFilePath, 3);
expect(location).to.eql({
pathAbsolute: sourceFilePath,
lineNumber: 2,
columnIndex: 0
});
});
});
});