-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathfile-system.ts
More file actions
416 lines (371 loc) · 12 KB
/
file-system.ts
File metadata and controls
416 lines (371 loc) · 12 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import { Yok } from "../../yok";
import { mkdtempSync } from "fs";
import { tmpdir } from "os";
import * as path from "path";
import * as hostInfoLib from "../../host-info";
import { assert, use } from "chai";
import "chai-as-promised";
import chaiAsPromised from "chai-as-promised";
import * as fileSystemFile from "../../file-system";
import * as childProcessLib from "../../child-process";
import { CommonLoggerStub } from "./stubs";
import { IInjector } from "../../definitions/yok";
import * as _ from "lodash";
import { IFileSystem } from "../../declarations";
use(chaiAsPromised);
const sampleZipFileTest = path.join(
__dirname,
"../resources/sampleZipFileTest.zip",
);
const unzippedFileName = "sampleZipFileTest.txt";
const sampleZipFileTestIncorrectName = path.join(
__dirname,
"../resources/sampleZipfileTest.zip",
);
function isOsCaseSensitive(testInjector: IInjector): boolean {
const hostInfo = testInjector.resolve("hostInfo");
return hostInfo.isLinux;
}
function createWriteJsonTestCases(): {
exists: boolean;
text: string;
testCondition: string;
expectedIndentation: string;
}[] {
return [
{
exists: true,
text: `{\n\t"a" : 5 }`,
testCondition: "when the indentation is tab",
expectedIndentation: "\t",
},
{
exists: true,
text: `{\n "a" : 5 }`,
testCondition: "when the indentation is space",
expectedIndentation: " ",
},
{
exists: true,
text: `{\n "a" : 5 }`,
testCondition: "when the indentation is two spaces",
expectedIndentation: " ",
},
{
exists: false,
text: `{\n "a" : 5 }`,
testCondition: "when the file does not exist",
expectedIndentation: "\t",
},
{
exists: true,
text: `"just-string"`,
testCondition: "when the the content is string",
expectedIndentation: "\t",
},
{
exists: true,
text: `{ "a" : 5 }`,
testCondition: "when the content does not have new line after the {",
expectedIndentation: " ",
},
{
exists: true,
text: `{"a" : 5 }`,
testCondition: "when the content is not correctly formatted",
expectedIndentation: "\t",
},
{
exists: true,
text: `{\r\n "a" : 5 }`,
testCondition: "when the new line is in Windows format",
expectedIndentation: " ",
},
{
exists: true,
text: `{\r\n\t"a" : 5 }`,
testCondition: "when the new line is in Windows format",
expectedIndentation: "\t",
},
];
}
function createTestInjector(): IInjector {
const testInjector = new Yok();
testInjector.register("fs", fileSystemFile.FileSystem);
testInjector.register("errors", {
fail: (...args: any[]) => {
throw new Error(args[0]);
},
});
testInjector.register("logger", CommonLoggerStub);
testInjector.register("childProcess", childProcessLib.ChildProcess);
testInjector.register("staticConfig", {
disableAnalytics: true,
});
testInjector.register("hostInfo", hostInfoLib.HostInfo);
testInjector.register("injector", testInjector);
return testInjector;
}
describe("FileSystem", () => {
describe("unzip", () => {
describe("overwriting files tests", () => {
let testInjector: IInjector;
let tempDir: string;
let fs: IFileSystem;
let file: string;
const msg = "data";
beforeEach(() => {
testInjector = createTestInjector();
tempDir = mkdtempSync(path.join(tmpdir(), "projectToUnzip-"));
fs = testInjector.resolve("fs");
file = path.join(tempDir, unzippedFileName);
fs.writeFile(file, msg);
});
it("does not overwrite files when overwriteExisitingFiles is false", async () => {
await fs.unzip(
sampleZipFileTest,
tempDir,
{ overwriteExisitingFiles: false },
[unzippedFileName],
);
const data = fs.readFile(file);
assert.strictEqual(
msg,
data.toString(),
"When overwriteExistingFiles is false, we should not ovewrite files.",
);
});
it("overwrites files when overwriteExisitingFiles is true", async () => {
await fs.unzip(
sampleZipFileTest,
tempDir,
{ overwriteExisitingFiles: true },
[unzippedFileName],
);
const data = fs.readFile(file);
assert.notEqual(
msg,
data.toString(),
"We must overwrite files when overwriteExisitingFiles is true.",
);
});
it("overwrites files when overwriteExisitingFiles is not set", async () => {
await fs.unzip(sampleZipFileTest, tempDir, {}, [unzippedFileName]);
const data = fs.readFile(file);
assert.notEqual(
msg,
data.toString(),
"We must overwrite files when overwriteExisitingFiles is not set.",
);
});
it("overwrites files when options is not set", async () => {
await fs.unzip(sampleZipFileTest, tempDir, undefined, [
unzippedFileName,
]);
const data = fs.readFile(file);
assert.notEqual(
msg,
data.toString(),
"We must overwrite files when options is not defined.",
);
});
});
// NOTE: This tests will never fail on Windows/Mac as file system is case insensitive
describe("case sensitive tests", () => {
const commandUnzipFailedMessage = "Command unzip failed with exit code 9";
it("is case sensitive when options is not defined", async () => {
const testInjector = createTestInjector();
const tempDir = mkdtempSync(path.join(tmpdir(), "projectToUnzip-"));
const fs: IFileSystem = testInjector.resolve("fs");
if (isOsCaseSensitive(testInjector)) {
await assert.isRejected(
fs.unzip(sampleZipFileTestIncorrectName, tempDir, undefined, [
unzippedFileName,
]),
commandUnzipFailedMessage,
);
}
});
it("is case sensitive when caseSensitive option is not defined", async () => {
const testInjector = createTestInjector();
const tempDir = mkdtempSync(path.join(tmpdir(), "projectToUnzip-"));
const fs: IFileSystem = testInjector.resolve("fs");
if (isOsCaseSensitive(testInjector)) {
await assert.isRejected(
fs.unzip(sampleZipFileTestIncorrectName, tempDir, {}, [
unzippedFileName,
]),
commandUnzipFailedMessage,
);
}
});
it("is case sensitive when caseSensitive option is true", async () => {
const testInjector = createTestInjector();
const tempDir = mkdtempSync(path.join(tmpdir(), "projectToUnzip-"));
const fs: IFileSystem = testInjector.resolve("fs");
if (isOsCaseSensitive(testInjector)) {
await assert.isRejected(
fs.unzip(
sampleZipFileTestIncorrectName,
tempDir,
{ caseSensitive: true },
[unzippedFileName],
),
commandUnzipFailedMessage,
);
}
});
it("is case insensitive when caseSensitive option is false", async () => {
const testInjector = createTestInjector();
const tempDir = mkdtempSync(path.join(tmpdir(), "projectToUnzip-"));
const fs: IFileSystem = testInjector.resolve("fs");
const file = path.join(tempDir, unzippedFileName);
await fs.unzip(
sampleZipFileTestIncorrectName,
tempDir,
{ caseSensitive: false },
[unzippedFileName],
);
// This will throw error in case file is not extracted
fs.readFile(file);
});
});
});
describe("renameIfExists", () => {
it("returns true when file is renamed", () => {
const testInjector = createTestInjector();
const tempDir = mkdtempSync(path.join(tmpdir(), "renameIfExists-"));
const testFileName = path.join(tempDir, "testRenameIfExistsMethod");
const newFileName = path.join(tempDir, "newfilename");
const fs: IFileSystem = testInjector.resolve("fs");
fs.writeFile(testFileName, "data");
const result = fs.renameIfExists(testFileName, newFileName);
assert.isTrue(result, "On successfull rename, result must be true.");
assert.isTrue(fs.exists(newFileName), "Renamed file should exists.");
assert.isFalse(
fs.exists(testFileName),
"Original file should not exist.",
);
});
it("returns false when file does not exist", () => {
const testInjector = createTestInjector();
const fs: IFileSystem = testInjector.resolve("fs");
const newName = "tempDir2";
const result = fs.renameIfExists("tempDir", newName);
assert.isFalse(result, "When file does not exist, result must be false.");
assert.isFalse(fs.exists(newName), "New file should not exist.");
});
});
describe("copyFile", () => {
let testInjector: IInjector;
let tempDir: string;
let testFileName: string;
let newFileName: string;
const fileContent = "data";
let fs: IFileSystem;
beforeEach(() => {
testInjector = createTestInjector();
tempDir = mkdtempSync(path.join(tmpdir(), "copyFile-"));
testFileName = path.join(tempDir, "testCopyFile");
newFileName = path.join(tempDir, "newfilename");
fs = testInjector.resolve("fs");
fs.writeFile(testFileName, fileContent);
});
it("correctly copies file to the same directory", () => {
fs.copyFile(testFileName, newFileName);
assert.isTrue(fs.exists(newFileName), "Renamed file should exists.");
assert.isTrue(fs.exists(testFileName), "Original file should exist.");
assert.deepStrictEqual(
fs.getFsStats(testFileName).size,
fs.getFsStats(testFileName).size,
"Original file and copied file must have the same size.",
);
});
it("copies file to non-existent directory", () => {
const newFileNameInSubDir = path.join(tempDir, "subDir", "newfilename");
assert.isFalse(fs.exists(newFileNameInSubDir));
fs.copyFile(testFileName, newFileNameInSubDir);
assert.isTrue(
fs.exists(newFileNameInSubDir),
"Renamed file should exists.",
);
assert.isTrue(fs.exists(testFileName), "Original file should exist.");
assert.deepStrictEqual(
fs.getFsStats(testFileName).size,
fs.getFsStats(testFileName).size,
"Original file and copied file must have the same size.",
);
});
it("produces correct file when source and target file are the same", () => {
const originalSize = fs.getFsStats(testFileName).size;
fs.copyFile(testFileName, testFileName);
assert.isTrue(fs.exists(testFileName), "Original file should exist.");
assert.deepStrictEqual(
fs.getFsStats(testFileName).size,
originalSize,
"Original file and copied file must have the same size.",
);
assert.deepStrictEqual(
fs.readText(testFileName),
fileContent,
"File content should not be changed.",
);
});
});
describe("removeEmptyParents", () => {
let testInjector: IInjector;
let fs: IFileSystem;
const notEmptyRootDirectory = path.join("not-empty");
let removedDirectories: string[];
beforeEach(() => {
testInjector = createTestInjector();
fs = testInjector.resolve("fs");
removedDirectories = [];
fs.deleteDirectory = (directory: string) => {
removedDirectories.push(path.basename(directory));
};
});
it("should remove all empty parents.", () => {
const emptyDirectories = ["first", "second", "third"];
let directory = notEmptyRootDirectory;
_.each(emptyDirectories, (dir: string) => {
directory = path.join(directory, dir);
});
// We need to add the fourth directory because this directory does not actually exist and the method will start deleting its parents.
directory = path.join(directory, "fourth");
const originalIsEmptyDir = fs.isEmptyDir;
fs.isEmptyDir = (dirName: string) => dirName !== notEmptyRootDirectory;
fs.deleteEmptyParents(directory);
fs.isEmptyDir = originalIsEmptyDir;
assert.deepStrictEqual(emptyDirectories, _.reverse(removedDirectories));
});
});
describe("writeJson", () => {
const testCases = createWriteJsonTestCases();
let testInjector: IInjector;
let fs: IFileSystem;
beforeEach(() => {
testInjector = createTestInjector();
fs = testInjector.resolve("fs");
});
_.each(testCases, (testCase) => {
it(`should use the correct indentation ${testCase.testCondition}.`, () => {
fs.readText = () => testCase.text;
fs.exists = () => testCase.exists;
fs.writeFile = () => null;
let actualIndentation: string;
const originalJsonStringify = JSON.stringify;
(<any>JSON).stringify = (
value: any,
replacer: any[],
space: string | number,
) => {
actualIndentation = <string>space;
};
fs.writeJson("", testCase.text);
JSON.stringify = originalJsonStringify;
assert.deepStrictEqual(actualIndentation, testCase.expectedIndentation);
});
});
});
});