Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.

Commit 39e7173

Browse files
authored
Merge pull request #537 from viktomas/broken-symlink
Fixing Common.stat method to accept a broken symlink.
2 parents 6c7538b + 8bbb3d9 commit 39e7173

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@
4444
"devDependencies": {
4545
"@types/chai": "3.4.22-alpha",
4646
"@types/mocha": "2.2.21-alpha",
47+
"@types/mock-fs": "3.6.22-alpha",
4748
"chai": "3.5.0",
4849
"devtron": "1.2.1",
4950
"electron-builder": "5.7.0",
5051
"electron-prebuilt": "1.2.5",
5152
"mocha": "2.5.3",
53+
"mock-fs": "3.9.0",
5254
"npm-check-updates": "2.6.7",
5355
"spectron": "3.2.3",
5456
"ts-node": "0.9.3",

src/utils/Common.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ export function recursiveFilesIn(directoryPath: string): Promise<string[]> {
5858
);
5959
}
6060

61-
export function stat(filePath: string): Promise<fs.Stats> {
61+
export function lstat(filePath: string): Promise<fs.Stats> {
6262
return new Promise((resolve, reject) => {
63-
fs.stat(filePath, (error: NodeJS.ErrnoException, pathStat: fs.Stats) => {
63+
fs.lstat(filePath, (error: NodeJS.ErrnoException, pathStat: fs.Stats) => {
6464
if (error) {
6565
reject(error);
6666
} else {
@@ -72,7 +72,7 @@ export function stat(filePath: string): Promise<fs.Stats> {
7272

7373
export async function statsIn(directoryPath: FullPath): Promise<i.FileInfo[]> {
7474
return Promise.all((await filesIn(directoryPath)).map(async (fileName) => {
75-
return {name: fileName, stat: await stat(Path.join(directoryPath, fileName))};
75+
return {name: fileName, stat: await lstat(Path.join(directoryPath, fileName))};
7676
}));
7777
}
7878

@@ -86,7 +86,7 @@ export function exists(filePath: string): Promise<boolean> {
8686

8787
export async function isDirectory(directoryPath: string): Promise<boolean> {
8888
if (await exists(directoryPath)) {
89-
return (await stat(directoryPath)).isDirectory();
89+
return (await lstat(directoryPath)).isDirectory();
9090
} else {
9191
return false;
9292
}

test/utils/common_spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
import "mocha";
22
import {expect} from "chai";
3-
import {commonPrefix} from "../../src/utils/Common";
3+
import {commonPrefix, lstat, resolveFile} from "../../src/utils/Common";
4+
import * as mockFs from "mock-fs";
5+
46

57
describe("common utils", () => {
68
describe("commonPrefix", () => {
79
it("returns the whole string for the same strings", async() => {
810
expect(commonPrefix("abc", "abc")).to.eql("abc");
911
});
1012
});
13+
14+
describe("lstat", () => {
15+
it("returns stats even if the file is a borken symlink", async() => {
16+
mockFs({
17+
"/broken-symlink": mockFs.symlink({
18+
path: "non-existing-file"
19+
})
20+
});
21+
const stats = await lstat(resolveFile("/", "/broken-symlink"))
22+
expect(stats).to.exist;
23+
});
24+
});
1125
});

0 commit comments

Comments
 (0)