Skip to content

Commit fd2b529

Browse files
Narrow symlink-setup exception handling in plugin tests
Catch only the privilege exceptions (IOException/UnauthorizedAccessException) when creating symbolic links and remove the File.Exists/Directory.Exists guards, so genuine test-setup failures surface instead of being masked. The early return is retained only for environments (e.g. Windows build agents) that don't permit symbolic link creation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f6f87a1 commit fd2b529

3 files changed

Lines changed: 20 additions & 76 deletions

File tree

dotnet/src/Plugins/Plugins.UnitTests/Core/FileIOPluginTests.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,9 @@ public async Task ItCannotReadThroughSymlinkOutsideAllowedFoldersAsync()
164164
{
165165
File.CreateSymbolicLink(symlinkPath, outsideFile);
166166
}
167-
catch (IOException)
168-
{
169-
return;
170-
}
171-
catch (UnauthorizedAccessException)
172-
{
173-
return;
174-
}
175-
176-
if (!File.Exists(symlinkPath))
167+
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
177168
{
169+
// Skip: this environment does not permit symbolic link creation (e.g., Windows without the required privilege).
178170
return;
179171
}
180172

@@ -209,17 +201,9 @@ public async Task ItCannotWriteThroughSymlinkOutsideAllowedFoldersAsync()
209201
{
210202
File.CreateSymbolicLink(symlinkPath, outsideFile);
211203
}
212-
catch (IOException)
213-
{
214-
return;
215-
}
216-
catch (UnauthorizedAccessException)
217-
{
218-
return;
219-
}
220-
221-
if (!File.Exists(symlinkPath))
204+
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
222205
{
206+
// Skip: this environment does not permit symbolic link creation (e.g., Windows without the required privilege).
223207
return;
224208
}
225209

@@ -257,12 +241,9 @@ public async Task ItCannotWriteThroughDanglingSymlinkOutsideAllowedFoldersAsync(
257241
{
258242
File.CreateSymbolicLink(symlinkPath, outsideFile);
259243
}
260-
catch (IOException)
261-
{
262-
return;
263-
}
264-
catch (UnauthorizedAccessException)
244+
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
265245
{
246+
// Skip: this environment does not permit symbolic link creation (e.g., Windows without the required privilege).
266247
return;
267248
}
268249

dotnet/src/Plugins/Plugins.UnitTests/Core/PathUtilitiesTests.cs

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,9 @@ public void GetSafeFullPathResolvesFileSymlink()
7272
{
7373
File.CreateSymbolicLink(symlinkPath, targetFile);
7474
}
75-
catch (IOException)
76-
{
77-
// Symlink creation requires elevated privileges on Windows
78-
return;
79-
}
80-
81-
if (!File.Exists(symlinkPath))
75+
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
8276
{
77+
// Skip: this environment does not permit symbolic link creation (e.g., Windows without the required privilege).
8378
return;
8479
}
8580

@@ -109,14 +104,9 @@ public void GetSafeFullPathResolvesSymlinkPointingOutsideDirectory()
109104
{
110105
File.CreateSymbolicLink(symlinkInAllowedDir, sensitiveFile);
111106
}
112-
catch (IOException)
113-
{
114-
// Symlink creation requires elevated privileges on Windows
115-
return;
116-
}
117-
118-
if (!File.Exists(symlinkInAllowedDir))
107+
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
119108
{
109+
// Skip: this environment does not permit symbolic link creation (e.g., Windows without the required privilege).
120110
return;
121111
}
122112

@@ -149,14 +139,9 @@ public void GetSafeFullPathResolvesDirectorySymlink()
149139
{
150140
Directory.CreateSymbolicLink(symlinkDir, outsideDir);
151141
}
152-
catch (IOException)
153-
{
154-
// Symlink creation requires elevated privileges on Windows
155-
return;
156-
}
157-
158-
if (!Directory.Exists(symlinkDir))
142+
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
159143
{
144+
// Skip: this environment does not permit symbolic link creation (e.g., Windows without the required privilege).
160145
return;
161146
}
162147

@@ -188,14 +173,9 @@ public void GetSafeFullPathResolvesNestedDirectorySymlink()
188173
{
189174
Directory.CreateSymbolicLink(symlinkDir, outsideDir);
190175
}
191-
catch (IOException)
192-
{
193-
// Symlink creation requires elevated privileges on Windows
194-
return;
195-
}
196-
197-
if (!Directory.Exists(symlinkDir))
176+
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
198177
{
178+
// Skip: this environment does not permit symbolic link creation (e.g., Windows without the required privilege).
199179
return;
200180
}
201181

@@ -224,14 +204,9 @@ public void GetSafeFullPathResolvesChainedSymlinks()
224204
File.CreateSymbolicLink(link2, realTarget);
225205
File.CreateSymbolicLink(link1, link2);
226206
}
227-
catch (IOException)
228-
{
229-
// Symlink creation requires elevated privileges on Windows
230-
return;
231-
}
232-
233-
if (!File.Exists(link1))
207+
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
234208
{
209+
// Skip: this environment does not permit symbolic link creation (e.g., Windows without the required privilege).
235210
return;
236211
}
237212

@@ -253,13 +228,9 @@ public void GetSafeFullPathResolvesDanglingSymlinkTarget()
253228
{
254229
File.CreateSymbolicLink(symlinkPath, missingTarget);
255230
}
256-
catch (IOException)
257-
{
258-
// Symlink creation requires elevated privileges on Windows
259-
return;
260-
}
261-
catch (UnauthorizedAccessException)
231+
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
262232
{
233+
// Skip: this environment does not permit symbolic link creation (e.g., Windows without the required privilege).
263234
return;
264235
}
265236

dotnet/src/Plugins/Plugins.UnitTests/Core/SessionsPythonPluginTests.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -493,17 +493,9 @@ public async Task ItShouldDenyDownloadThroughSymlinkOutsideAllowedDirectoriesAsy
493493
{
494494
File.CreateSymbolicLink(symlinkPath, outsideFile);
495495
}
496-
catch (IOException)
497-
{
498-
return;
499-
}
500-
catch (UnauthorizedAccessException)
501-
{
502-
return;
503-
}
504-
505-
if (!File.Exists(symlinkPath))
496+
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
506497
{
498+
// Skip: this environment does not permit symbolic link creation (e.g., Windows without the required privilege).
507499
return;
508500
}
509501

0 commit comments

Comments
 (0)