Skip to content

Commit f583e03

Browse files
committed
Remove unnecessary lstatSync call
1 parent c6cc137 commit f583e03

4 files changed

Lines changed: 16 additions & 139 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 1 addition & 14 deletions
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: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,6 @@ test.serial(
386386
env.set(ActionsEnvVars.RUNNER_TEMP, tmpDir);
387387
const features = createFeatures([Feature.CheckUnsupportedTracedAnalysis]);
388388

389-
const lstatSync = sinon
390-
.stub(fs, "lstatSync")
391-
.returns({ isSymbolicLink: () => true } as fs.Stats);
392389
const readlinkSync = sinon
393390
.stub(fs, "readlinkSync")
394391
.returns("/Applications/Xcode_26.0.app/Contents/Developer");
@@ -418,11 +415,6 @@ test.serial(
418415
codeql,
419416
);
420417

421-
t.is(lstatSync.callCount, 1);
422-
t.deepEqual(lstatSync.args[0], [
423-
XCODE_SELECT_LINK_PATH,
424-
{ throwIfNoEntry: false },
425-
]);
426418
t.is(readlinkSync.callCount, 1);
427419
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
428420

@@ -444,9 +436,6 @@ test.serial(
444436
env.set(ActionsEnvVars.RUNNER_TEMP, tmpDir);
445437
const features = createFeatures([Feature.CheckUnsupportedTracedAnalysis]);
446438

447-
const lstatSync = sinon
448-
.stub(fs, "lstatSync")
449-
.returns({ isSymbolicLink: () => true } as fs.Stats);
450439
const readlinkSync = sinon
451440
.stub(fs, "readlinkSync")
452441
.returns("/Applications/Xcode_27.0.app/Contents/Developer");
@@ -476,11 +465,6 @@ test.serial(
476465
codeql,
477466
);
478467

479-
t.is(lstatSync.callCount, 1);
480-
t.deepEqual(lstatSync.args[0], [
481-
XCODE_SELECT_LINK_PATH,
482-
{ throwIfNoEntry: false },
483-
]);
484468
t.is(readlinkSync.callCount, 1);
485469
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
486470

@@ -521,9 +505,6 @@ test.serial(
521505
const env = getTestEnv();
522506
env.set(ActionsEnvVars.RUNNER_TEMP, tmpDir);
523507

524-
const lstatSync = sinon
525-
.stub(fs, "lstatSync")
526-
.returns({ isSymbolicLink: () => true } as fs.Stats);
527508
const readlinkSync = sinon
528509
.stub(fs, "readlinkSync")
529510
.returns("/Applications/Xcode_27.0.app/Contents/Developer");
@@ -560,11 +541,6 @@ test.serial(
560541
),
561542
);
562543

563-
t.is(lstatSync.callCount, 1);
564-
t.deepEqual(lstatSync.args[0], [
565-
XCODE_SELECT_LINK_PATH,
566-
{ throwIfNoEntry: false },
567-
]);
568544
t.is(readlinkSync.callCount, 1);
569545
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
570546

‎src/platform.test.ts‎

Lines changed: 12 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -74,53 +74,32 @@ test("darwinVersion 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 lstatSync = sinon.stub(fs, "lstatSync").returns(undefined);
78-
79-
t.is(xcodeVersion(logger), undefined);
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-
"'/private/var/db/xcode_select_link' does not exist",
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"));
77+
const readlinkSync = sinon
78+
.stub(fs, "readlinkSync")
79+
.throws(new Error("ENOENT: no such file or directory"));
9580

9681
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-
]);
82+
t.is(readlinkSync.callCount, 1);
83+
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
10284

10385
checkExpectedLogMessages(t, logger.messages, [
104-
"Unable to determine Xcode version: ENOENT",
86+
"Unable to determine Xcode version: ENOENT: no such file or directory",
10587
]);
10688
});
10789

10890
test.serial("xcodeVersion returns undefined if file is not a symlink", (t) => {
10991
const logger = new RecordingLogger();
11092

111-
const lstatSync = sinon
112-
.stub(fs, "lstatSync")
113-
.returns({ isSymbolicLink: () => false } as fs.Stats);
93+
const readlinkSync = sinon
94+
.stub(fs, "readlinkSync")
95+
.throws(new Error("EINVAL: invalid argument"));
11496

11597
t.is(xcodeVersion(logger), undefined);
116-
t.is(lstatSync.callCount, 1);
117-
t.deepEqual(lstatSync.args[0], [
118-
XCODE_SELECT_LINK_PATH,
119-
{ throwIfNoEntry: false },
120-
]);
98+
t.is(readlinkSync.callCount, 1);
99+
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
121100

122101
checkExpectedLogMessages(t, logger.messages, [
123-
"exists, but is not a symbolic link",
102+
"Unable to determine Xcode version: EINVAL: invalid argument",
124103
]);
125104
});
126105

@@ -129,17 +108,9 @@ test.serial(
129108
(t) => {
130109
const logger = new RecordingLogger();
131110

132-
const lstatSync = sinon
133-
.stub(fs, "lstatSync")
134-
.returns({ isSymbolicLink: () => true } as fs.Stats);
135111
const readlinkSync = sinon.stub(fs, "readlinkSync").returns("");
136112

137113
t.is(xcodeVersion(logger), undefined);
138-
t.is(lstatSync.callCount, 1);
139-
t.deepEqual(lstatSync.args[0], [
140-
XCODE_SELECT_LINK_PATH,
141-
{ throwIfNoEntry: false },
142-
]);
143114
t.is(readlinkSync.callCount, 1);
144115
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
145116

@@ -154,19 +125,11 @@ test.serial(
154125
(t) => {
155126
const logger = new RecordingLogger();
156127

157-
const lstatSync = sinon
158-
.stub(fs, "lstatSync")
159-
.returns({ isSymbolicLink: () => true } as fs.Stats);
160128
const readlinkSync = sinon
161129
.stub(fs, "readlinkSync")
162130
.returns("/Applications/Xcode.app/Contents/Developer");
163131

164132
t.is(xcodeVersion(logger), undefined);
165-
t.is(lstatSync.callCount, 1);
166-
t.deepEqual(lstatSync.args[0], [
167-
XCODE_SELECT_LINK_PATH,
168-
{ throwIfNoEntry: false },
169-
]);
170133
t.is(readlinkSync.callCount, 1);
171134
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
172135

@@ -179,19 +142,11 @@ test.serial(
179142
test.serial("xcodeVersion returns undefined if match can't be parsed", (t) => {
180143
const logger = new RecordingLogger();
181144

182-
const lstatSync = sinon
183-
.stub(fs, "lstatSync")
184-
.returns({ isSymbolicLink: () => true } as fs.Stats);
185145
const readlinkSync = sinon
186146
.stub(fs, "readlinkSync")
187147
.returns("/Applications/Xcode_00.0.app/Contents/Developer");
188148

189149
t.is(xcodeVersion(logger), undefined);
190-
t.is(lstatSync.callCount, 1);
191-
t.deepEqual(lstatSync.args[0], [
192-
XCODE_SELECT_LINK_PATH,
193-
{ throwIfNoEntry: false },
194-
]);
195150
t.is(readlinkSync.callCount, 1);
196151
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
197152

@@ -203,9 +158,6 @@ test.serial("xcodeVersion returns undefined if match can't be parsed", (t) => {
203158
test.serial("xcodeVersion returns version from resolved path", (t) => {
204159
const logger = new RecordingLogger();
205160

206-
const lstatSync = sinon
207-
.stub(fs, "lstatSync")
208-
.returns({ isSymbolicLink: () => true } as fs.Stats);
209161
const readlinkSync = sinon
210162
.stub(fs, "readlinkSync")
211163
.returns("/Applications/Xcode_16.4.app/Contents/Developer");
@@ -217,21 +169,13 @@ test.serial("xcodeVersion returns version from resolved path", (t) => {
217169
t.is(result.minor, 4);
218170
}
219171

220-
t.is(lstatSync.callCount, 1);
221-
t.deepEqual(lstatSync.args[0], [
222-
XCODE_SELECT_LINK_PATH,
223-
{ throwIfNoEntry: false },
224-
]);
225172
t.is(readlinkSync.callCount, 1);
226173
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
227174
});
228175

229176
test.serial("xcodeVersion returns major version from resolved path", (t) => {
230177
const logger = new RecordingLogger();
231178

232-
const lstatSync = sinon
233-
.stub(fs, "lstatSync")
234-
.returns({ isSymbolicLink: () => true } as fs.Stats);
235179
const readlinkSync = sinon
236180
.stub(fs, "readlinkSync")
237181
.returns("/Applications/Xcode_16.app/Contents/Developer");
@@ -243,11 +187,6 @@ test.serial("xcodeVersion returns major version from resolved path", (t) => {
243187
t.is(result.minor, 0);
244188
}
245189

246-
t.is(lstatSync.callCount, 1);
247-
t.deepEqual(lstatSync.args[0], [
248-
XCODE_SELECT_LINK_PATH,
249-
{ throwIfNoEntry: false },
250-
]);
251190
t.is(readlinkSync.callCount, 1);
252191
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
253192
});
@@ -257,9 +196,6 @@ test.serial(
257196
(t) => {
258197
const logger = new RecordingLogger();
259198

260-
const lstatSync = sinon
261-
.stub(fs, "lstatSync")
262-
.returns({ isSymbolicLink: () => true } as fs.Stats);
263199
const readlinkSync = sinon
264200
.stub(fs, "readlinkSync")
265201
.returns("/Applications/Xcode_16.5.9.app/Contents/Developer");
@@ -271,11 +207,6 @@ test.serial(
271207
t.is(result.minor, 5);
272208
}
273209

274-
t.is(lstatSync.callCount, 1);
275-
t.deepEqual(lstatSync.args[0], [
276-
XCODE_SELECT_LINK_PATH,
277-
{ throwIfNoEntry: false },
278-
]);
279210
t.is(readlinkSync.callCount, 1);
280211
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
281212
},

‎src/platform.ts‎

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,11 @@ export const XCODE_APP_FILENAME_PATTERN = new RegExp(
7575
*/
7676
export function xcodeVersion(logger: Logger): semver.SemVer | undefined {
7777
try {
78-
// Stat the expected symbolic link to check that it exists and is a symbolic link.
79-
// The `readlinkSync` call below returns an empty string in either case and so
80-
// this check allows us to distinguish between the two cases.
81-
const stats = fs.lstatSync(XCODE_SELECT_LINK_PATH, {
82-
throwIfNoEntry: false,
83-
});
84-
85-
if (stats === undefined) {
86-
logger.debug(`'${XCODE_SELECT_LINK_PATH}' does not exist.`);
87-
return undefined;
88-
}
89-
if (!stats.isSymbolicLink()) {
90-
logger.debug(
91-
`${XCODE_SELECT_LINK_PATH} exists, but is not a symbolic link.`,
92-
);
93-
return undefined;
94-
}
95-
96-
// Read what the symbolic link points to.
78+
// Read what the symbolic link points to. This will throw if `XCODE_SELECT_LINK_PATH` does not
79+
// exist (`ENOENT`) or is not a symbolic link (`EINVAL`).
9780
const xcodePath = fs.readlinkSync(XCODE_SELECT_LINK_PATH);
9881

99-
if (xcodePath === "") {
82+
if (xcodePath.trim() === "") {
10083
logger.debug(
10184
`Resolving ${XCODE_SELECT_LINK_PATH} unexpectedly returned nothing.`,
10285
);

0 commit comments

Comments
 (0)