|
1 | 1 | using System.Diagnostics; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using System.Security.AccessControl; |
2 | 4 | using System.Text; |
3 | 5 | using CliWrap; |
4 | 6 | using CliWrap.Buffered; |
@@ -89,6 +91,72 @@ public async Task Format_Should_Format_Subdirectory(string subdirectory) |
89 | 91 | (await ReadAllTextAsync("Subdirectory/BasicFile.cs")).Should().Be(formattedContent); |
90 | 92 | } |
91 | 93 |
|
| 94 | + [Test] |
| 95 | + public async Task Format_Should_Handle_UnauthorizedAccessException_In_Subdirectory() |
| 96 | + { |
| 97 | + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 98 | + { |
| 99 | + // on linux you can't read a subdirectory if you don't have access to the parent directory |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + var formattedContent = "public class ClassName { }\n"; |
| 104 | + var unformattedContent = "public class ClassName {\n\n}"; |
| 105 | + |
| 106 | + await WriteFileAsync( |
| 107 | + "UnauthorizedSubdirectory/Subdirectory/BasicFile.cs", |
| 108 | + unformattedContent |
| 109 | + ); |
| 110 | + |
| 111 | + var directory = new DirectoryInfo( |
| 112 | + Path.Combine(testFileDirectory, "UnauthorizedSubdirectory") |
| 113 | + ); |
| 114 | + |
| 115 | + void ChangeDirectoryPermissions(bool allowAccess) |
| 116 | + { |
| 117 | + var accessControl = directory.GetAccessControl(); |
| 118 | + if (allowAccess) |
| 119 | + { |
| 120 | + accessControl.RemoveAccessRule( |
| 121 | + new FileSystemAccessRule( |
| 122 | + "Everyone", |
| 123 | + FileSystemRights.FullControl, |
| 124 | + AccessControlType.Deny |
| 125 | + ) |
| 126 | + ); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + accessControl.AddAccessRule( |
| 131 | + new FileSystemAccessRule( |
| 132 | + "Everyone", |
| 133 | + FileSystemRights.FullControl, |
| 134 | + AccessControlType.Deny |
| 135 | + ) |
| 136 | + ); |
| 137 | + } |
| 138 | + directory.SetAccessControl(accessControl); |
| 139 | + } |
| 140 | + |
| 141 | + try |
| 142 | + { |
| 143 | + ChangeDirectoryPermissions(allowAccess: false); |
| 144 | + |
| 145 | + var formatResult = await new CsharpierProcess() |
| 146 | + .WithArguments("format UnauthorizedSubdirectory/Subdirectory") |
| 147 | + .ExecuteAsync(); |
| 148 | + |
| 149 | + formatResult.ErrorOutput.Should().BeEmpty(); |
| 150 | + (await ReadAllTextAsync("UnauthorizedSubdirectory/Subdirectory/BasicFile.cs")) |
| 151 | + .Should() |
| 152 | + .Be(formattedContent); |
| 153 | + } |
| 154 | + finally |
| 155 | + { |
| 156 | + ChangeDirectoryPermissions(allowAccess: true); |
| 157 | + } |
| 158 | + } |
| 159 | + |
92 | 160 | [Test] |
93 | 161 | public async Task Format_Should_Respect_Ignore_File_With_Subdirectory_When_DirectorOrFile_Is_Dot() |
94 | 162 | { |
@@ -283,6 +351,39 @@ public async Task Format_Should_Format_Piped_File_With_Config() |
283 | 351 | result.ExitCode.Should().Be(0); |
284 | 352 | } |
285 | 353 |
|
| 354 | + [Test] |
| 355 | + public async Task Format_Should_Format_Piped_File_With_Config_And_Path() |
| 356 | + { |
| 357 | + await WriteFileAsync("Stdin/.csharpierrc", "printWidth: 10"); |
| 358 | + |
| 359 | + var formattedContent1 = "var x =\n _________________longName;\n"; |
| 360 | + var unformattedContent1 = "var x = _________________longName;\n"; |
| 361 | + |
| 362 | + var result = await new CsharpierProcess() |
| 363 | + .WithArguments("format --stdin-path ./Stdin/Test.cs") |
| 364 | + .WithPipedInput(unformattedContent1) |
| 365 | + .ExecuteAsync(); |
| 366 | + |
| 367 | + result.Output.Should().Be(formattedContent1); |
| 368 | + result.ExitCode.Should().Be(0); |
| 369 | + } |
| 370 | + |
| 371 | + [Test] |
| 372 | + public async Task Format_Should_Not_Format_Piped_File_With_Gitignore_And_Path() |
| 373 | + { |
| 374 | + await WriteFileAsync("Stdin/.gitignore", "*"); |
| 375 | + |
| 376 | + var unformattedContent1 = "var x = _________________longName;\n"; |
| 377 | + |
| 378 | + var result = await new CsharpierProcess() |
| 379 | + .WithArguments("format --stdin-path ./Stdin/Test.cs") |
| 380 | + .WithPipedInput(unformattedContent1) |
| 381 | + .ExecuteAsync(); |
| 382 | + |
| 383 | + result.Output.Should().Be(unformattedContent1); |
| 384 | + result.ExitCode.Should().Be(0); |
| 385 | + } |
| 386 | + |
286 | 387 | [Test] |
287 | 388 | public async Task Format_Should_Format_Piped_File_With_EditorConfig() |
288 | 389 | { |
|
0 commit comments