Skip to content

Commit bcaccd9

Browse files
committed
Use lstat instead of stat
1 parent 3ed54a4 commit bcaccd9

3 files changed

Lines changed: 59 additions & 25 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/platform.test.ts‎

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ test("macOSVersion returns semver if parsing succeeds", (t) => {
7474
test.serial("xcodeVersion returns undefined if symlink doesn't exist", (t) => {
7575
const logger = new RecordingLogger();
7676

77-
const statSync = sinon.stub(fs, "statSync").throws(new Error("ENOENT"));
77+
const lstatSync = sinon.stub(fs, "lstatSync").throws(new Error("ENOENT"));
7878

7979
t.is(xcodeVersion(logger), undefined);
80-
t.is(statSync.callCount, 1);
81-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
80+
t.is(lstatSync.callCount, 1);
81+
t.deepEqual(lstatSync.args[0], [
82+
XCODE_SELECT_LINK_PATH,
83+
{ throwIfNoEntry: false },
84+
]);
8285

8386
checkExpectedLogMessages(t, logger.messages, [
8487
"Unable to determine Xcode version: ENOENT",
@@ -88,13 +91,16 @@ test.serial("xcodeVersion returns undefined if symlink doesn't exist", (t) => {
8891
test.serial("xcodeVersion returns undefined if file is not a symlink", (t) => {
8992
const logger = new RecordingLogger();
9093

91-
const statSync = sinon
92-
.stub(fs, "statSync")
94+
const lstatSync = sinon
95+
.stub(fs, "lstatSync")
9396
.returns({ isSymbolicLink: () => false } as fs.Stats);
9497

9598
t.is(xcodeVersion(logger), undefined);
96-
t.is(statSync.callCount, 1);
97-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
99+
t.is(lstatSync.callCount, 1);
100+
t.deepEqual(lstatSync.args[0], [
101+
XCODE_SELECT_LINK_PATH,
102+
{ throwIfNoEntry: false },
103+
]);
98104

99105
checkExpectedLogMessages(t, logger.messages, [
100106
"exists, but is not a symbolic link",
@@ -106,14 +112,17 @@ test.serial(
106112
(t) => {
107113
const logger = new RecordingLogger();
108114

109-
const statSync = sinon
110-
.stub(fs, "statSync")
115+
const lstatSync = sinon
116+
.stub(fs, "lstatSync")
111117
.returns({ isSymbolicLink: () => true } as fs.Stats);
112118
const readlinkSync = sinon.stub(fs, "readlinkSync").returns("");
113119

114120
t.is(xcodeVersion(logger), undefined);
115-
t.is(statSync.callCount, 1);
116-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
121+
t.is(lstatSync.callCount, 1);
122+
t.deepEqual(lstatSync.args[0], [
123+
XCODE_SELECT_LINK_PATH,
124+
{ throwIfNoEntry: false },
125+
]);
117126
t.is(readlinkSync.callCount, 1);
118127
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
119128

@@ -128,16 +137,19 @@ test.serial(
128137
(t) => {
129138
const logger = new RecordingLogger();
130139

131-
const statSync = sinon
132-
.stub(fs, "statSync")
140+
const lstatSync = sinon
141+
.stub(fs, "lstatSync")
133142
.returns({ isSymbolicLink: () => true } as fs.Stats);
134143
const readlinkSync = sinon
135144
.stub(fs, "readlinkSync")
136145
.returns("/Applications/Xcode.app/Contents/Developer");
137146

138147
t.is(xcodeVersion(logger), undefined);
139-
t.is(statSync.callCount, 1);
140-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
148+
t.is(lstatSync.callCount, 1);
149+
t.deepEqual(lstatSync.args[0], [
150+
XCODE_SELECT_LINK_PATH,
151+
{ throwIfNoEntry: false },
152+
]);
141153
t.is(readlinkSync.callCount, 1);
142154
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
143155

@@ -150,16 +162,19 @@ test.serial(
150162
test.serial("xcodeVersion returns undefined if match can't be parsed", (t) => {
151163
const logger = new RecordingLogger();
152164

153-
const statSync = sinon
154-
.stub(fs, "statSync")
165+
const lstatSync = sinon
166+
.stub(fs, "lstatSync")
155167
.returns({ isSymbolicLink: () => true } as fs.Stats);
156168
const readlinkSync = sinon
157169
.stub(fs, "readlinkSync")
158170
.returns("/Applications/Xcode_00.0.app/Contents/Developer");
159171

160172
t.is(xcodeVersion(logger), undefined);
161-
t.is(statSync.callCount, 1);
162-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
173+
t.is(lstatSync.callCount, 1);
174+
t.deepEqual(lstatSync.args[0], [
175+
XCODE_SELECT_LINK_PATH,
176+
{ throwIfNoEntry: false },
177+
]);
163178
t.is(readlinkSync.callCount, 1);
164179
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
165180

@@ -171,8 +186,8 @@ test.serial("xcodeVersion returns undefined if match can't be parsed", (t) => {
171186
test.serial("xcodeVersion returns version from resolved path", (t) => {
172187
const logger = new RecordingLogger();
173188

174-
const statSync = sinon
175-
.stub(fs, "statSync")
189+
const lstatSync = sinon
190+
.stub(fs, "lstatSync")
176191
.returns({ isSymbolicLink: () => true } as fs.Stats);
177192
const readlinkSync = sinon
178193
.stub(fs, "readlinkSync")
@@ -185,8 +200,11 @@ test.serial("xcodeVersion returns version from resolved path", (t) => {
185200
t.is(result.minor, 4);
186201
}
187202

188-
t.is(statSync.callCount, 1);
189-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
203+
t.is(lstatSync.callCount, 1);
204+
t.deepEqual(lstatSync.args[0], [
205+
XCODE_SELECT_LINK_PATH,
206+
{ throwIfNoEntry: false },
207+
]);
190208
t.is(readlinkSync.callCount, 1);
191209
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
192210
});

‎src/platform.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,16 @@ export function xcodeVersion(logger: Logger): semver.SemVer | undefined {
7676
// Stat the expected symbolic link to check that it exists and is a symbolic link.
7777
// The `readlinkSync` call below returns an empty string in either case and so
7878
// this check allows us to distinguish between the two cases.
79-
const stats = fs.statSync(XCODE_SELECT_LINK_PATH);
79+
const stats = fs.lstatSync(XCODE_SELECT_LINK_PATH, {
80+
throwIfNoEntry: false,
81+
});
8082

83+
if (stats === undefined) {
84+
logger.debug(
85+
`'${XCODE_SELECT_LINK_PATH}' does not exist. Xcode is probably not installed.`,
86+
);
87+
return undefined;
88+
}
8189
if (!stats.isSymbolicLink()) {
8290
logger.warning(
8391
`${XCODE_SELECT_LINK_PATH} exists, but is not a symbolic link.`,

0 commit comments

Comments
 (0)