Skip to content

Commit 0ec32ae

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

4 files changed

Lines changed: 97 additions & 37 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/languages/traced.test.ts‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ test.serial(
289289
const env = getTestEnv();
290290
env.set(ActionsEnvVars.RUNNER_TEMP, tmpDir);
291291

292-
const statSync = sinon
293-
.stub(fs, "statSync")
292+
const lstatSync = sinon
293+
.stub(fs, "lstatSync")
294294
.returns({ isSymbolicLink: () => true } as fs.Stats);
295295
const readlinkSync = sinon
296296
.stub(fs, "readlinkSync")
@@ -317,8 +317,11 @@ test.serial(
317317
codeql,
318318
);
319319

320-
t.is(statSync.callCount, 1);
321-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
320+
t.is(lstatSync.callCount, 1);
321+
t.deepEqual(lstatSync.args[0], [
322+
XCODE_SELECT_LINK_PATH,
323+
{ throwIfNoEntry: false },
324+
]);
322325
t.is(readlinkSync.callCount, 1);
323326
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
324327

@@ -339,8 +342,8 @@ test.serial(
339342
const env = getTestEnv();
340343
env.set(ActionsEnvVars.RUNNER_TEMP, tmpDir);
341344

342-
const statSync = sinon
343-
.stub(fs, "statSync")
345+
const lstatSync = sinon
346+
.stub(fs, "lstatSync")
344347
.returns({ isSymbolicLink: () => true } as fs.Stats);
345348
const readlinkSync = sinon
346349
.stub(fs, "readlinkSync")
@@ -367,8 +370,11 @@ test.serial(
367370
codeql,
368371
);
369372

370-
t.is(statSync.callCount, 1);
371-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
373+
t.is(lstatSync.callCount, 1);
374+
t.deepEqual(lstatSync.args[0], [
375+
XCODE_SELECT_LINK_PATH,
376+
{ throwIfNoEntry: false },
377+
]);
372378
t.is(readlinkSync.callCount, 1);
373379
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
374380

@@ -409,8 +415,8 @@ test.serial(
409415
const env = getTestEnv();
410416
env.set(ActionsEnvVars.RUNNER_TEMP, tmpDir);
411417

412-
const statSync = sinon
413-
.stub(fs, "statSync")
418+
const lstatSync = sinon
419+
.stub(fs, "lstatSync")
414420
.returns({ isSymbolicLink: () => true } as fs.Stats);
415421
const readlinkSync = sinon
416422
.stub(fs, "readlinkSync")
@@ -442,8 +448,11 @@ test.serial(
442448
),
443449
);
444450

445-
t.is(statSync.callCount, 1);
446-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
451+
t.is(lstatSync.callCount, 1);
452+
t.deepEqual(lstatSync.args[0], [
453+
XCODE_SELECT_LINK_PATH,
454+
{ throwIfNoEntry: false },
455+
]);
447456
t.is(readlinkSync.callCount, 1);
448457
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
449458

‎src/platform.test.ts‎

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,31 @@ 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").returns(undefined);
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+
]);
85+
86+
checkExpectedLogMessages(t, logger.messages, [
87+
"Xcode is probably not installed.",
88+
]);
89+
});
90+
91+
test.serial("xcodeVersion returns undefined if lstatSync throws", (t) => {
92+
const logger = new RecordingLogger();
93+
94+
const lstatSync = sinon.stub(fs, "lstatSync").throws(new Error("ENOENT"));
95+
96+
t.is(xcodeVersion(logger), undefined);
97+
t.is(lstatSync.callCount, 1);
98+
t.deepEqual(lstatSync.args[0], [
99+
XCODE_SELECT_LINK_PATH,
100+
{ throwIfNoEntry: false },
101+
]);
82102

83103
checkExpectedLogMessages(t, logger.messages, [
84104
"Unable to determine Xcode version: ENOENT",
@@ -88,13 +108,16 @@ test.serial("xcodeVersion returns undefined if symlink doesn't exist", (t) => {
88108
test.serial("xcodeVersion returns undefined if file is not a symlink", (t) => {
89109
const logger = new RecordingLogger();
90110

91-
const statSync = sinon
92-
.stub(fs, "statSync")
111+
const lstatSync = sinon
112+
.stub(fs, "lstatSync")
93113
.returns({ isSymbolicLink: () => false } as fs.Stats);
94114

95115
t.is(xcodeVersion(logger), undefined);
96-
t.is(statSync.callCount, 1);
97-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
116+
t.is(lstatSync.callCount, 1);
117+
t.deepEqual(lstatSync.args[0], [
118+
XCODE_SELECT_LINK_PATH,
119+
{ throwIfNoEntry: false },
120+
]);
98121

99122
checkExpectedLogMessages(t, logger.messages, [
100123
"exists, but is not a symbolic link",
@@ -106,14 +129,17 @@ test.serial(
106129
(t) => {
107130
const logger = new RecordingLogger();
108131

109-
const statSync = sinon
110-
.stub(fs, "statSync")
132+
const lstatSync = sinon
133+
.stub(fs, "lstatSync")
111134
.returns({ isSymbolicLink: () => true } as fs.Stats);
112135
const readlinkSync = sinon.stub(fs, "readlinkSync").returns("");
113136

114137
t.is(xcodeVersion(logger), undefined);
115-
t.is(statSync.callCount, 1);
116-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
138+
t.is(lstatSync.callCount, 1);
139+
t.deepEqual(lstatSync.args[0], [
140+
XCODE_SELECT_LINK_PATH,
141+
{ throwIfNoEntry: false },
142+
]);
117143
t.is(readlinkSync.callCount, 1);
118144
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
119145

@@ -128,16 +154,19 @@ test.serial(
128154
(t) => {
129155
const logger = new RecordingLogger();
130156

131-
const statSync = sinon
132-
.stub(fs, "statSync")
157+
const lstatSync = sinon
158+
.stub(fs, "lstatSync")
133159
.returns({ isSymbolicLink: () => true } as fs.Stats);
134160
const readlinkSync = sinon
135161
.stub(fs, "readlinkSync")
136162
.returns("/Applications/Xcode.app/Contents/Developer");
137163

138164
t.is(xcodeVersion(logger), undefined);
139-
t.is(statSync.callCount, 1);
140-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
165+
t.is(lstatSync.callCount, 1);
166+
t.deepEqual(lstatSync.args[0], [
167+
XCODE_SELECT_LINK_PATH,
168+
{ throwIfNoEntry: false },
169+
]);
141170
t.is(readlinkSync.callCount, 1);
142171
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
143172

@@ -150,16 +179,19 @@ test.serial(
150179
test.serial("xcodeVersion returns undefined if match can't be parsed", (t) => {
151180
const logger = new RecordingLogger();
152181

153-
const statSync = sinon
154-
.stub(fs, "statSync")
182+
const lstatSync = sinon
183+
.stub(fs, "lstatSync")
155184
.returns({ isSymbolicLink: () => true } as fs.Stats);
156185
const readlinkSync = sinon
157186
.stub(fs, "readlinkSync")
158187
.returns("/Applications/Xcode_00.0.app/Contents/Developer");
159188

160189
t.is(xcodeVersion(logger), undefined);
161-
t.is(statSync.callCount, 1);
162-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
190+
t.is(lstatSync.callCount, 1);
191+
t.deepEqual(lstatSync.args[0], [
192+
XCODE_SELECT_LINK_PATH,
193+
{ throwIfNoEntry: false },
194+
]);
163195
t.is(readlinkSync.callCount, 1);
164196
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
165197

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

174-
const statSync = sinon
175-
.stub(fs, "statSync")
206+
const lstatSync = sinon
207+
.stub(fs, "lstatSync")
176208
.returns({ isSymbolicLink: () => true } as fs.Stats);
177209
const readlinkSync = sinon
178210
.stub(fs, "readlinkSync")
@@ -185,8 +217,11 @@ test.serial("xcodeVersion returns version from resolved path", (t) => {
185217
t.is(result.minor, 4);
186218
}
187219

188-
t.is(statSync.callCount, 1);
189-
t.deepEqual(statSync.args[0], [XCODE_SELECT_LINK_PATH]);
220+
t.is(lstatSync.callCount, 1);
221+
t.deepEqual(lstatSync.args[0], [
222+
XCODE_SELECT_LINK_PATH,
223+
{ throwIfNoEntry: false },
224+
]);
190225
t.is(readlinkSync.callCount, 1);
191226
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
192227
});

‎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)