Skip to content

Commit c87ce5b

Browse files
Copilotjkotas
andcommitted
Refactor tests to use shared test data and fix comment about path normalization
Co-authored-by: jkotas <[email protected]>
1 parent 8e479ba commit c87ce5b

File tree

3 files changed

+38
-102
lines changed

3 files changed

+38
-102
lines changed

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/GetFiles.cs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -193,44 +193,39 @@ public void DirectoryWithTrailingSeparators(string trailing)
193193
FSAssert.EqualWhenOrdered(new string[] { rootFile, nestedFile }, files);
194194
}
195195

196-
[Fact]
197-
public void EnumerateFilesWithLeadingSpacesDots()
198-
{
199-
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
200-
File.Create(Path.Combine(testDir.FullName, " leading")).Dispose();
201-
File.Create(Path.Combine(testDir.FullName, ".leading")).Dispose();
202-
File.Create(Path.Combine(testDir.FullName, "..leading")).Dispose();
203-
204-
string[] files = GetEntries(testDir.FullName);
205-
Assert.Equal(3, files.Length);
206-
Assert.Contains(files, f => Path.GetFileName(f) == " leading");
207-
Assert.Contains(files, f => Path.GetFileName(f) == ".leading");
208-
Assert.Contains(files, f => Path.GetFileName(f) == "..leading");
209-
}
210-
211-
[Fact]
212-
public void EnumerateFilesWithDashes()
196+
[Theory, MemberData(nameof(TestData.ValidFileNames), MemberType = typeof(TestData))]
197+
public void EnumerateFilesWithProblematicNames(string fileName)
213198
{
214199
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
215-
File.Create(Path.Combine(testDir.FullName, "-")).Dispose();
216-
File.Create(Path.Combine(testDir.FullName, "--")).Dispose();
200+
File.Create(Path.Combine(testDir.FullName, fileName)).Dispose();
217201

218202
string[] files = GetEntries(testDir.FullName);
219-
Assert.Equal(2, files.Length);
220-
Assert.Contains(files, f => Path.GetFileName(f) == "-");
221-
Assert.Contains(files, f => Path.GetFileName(f) == "--");
203+
Assert.Single(files);
204+
Assert.Contains(files, f => Path.GetFileName(f) == fileName);
222205
}
223206

224-
[Fact]
225-
[PlatformSpecific(TestPlatforms.AnyUnix)]
226-
public void UnixEnumerateFilesWithTabs()
207+
[ConditionalTheory(nameof(UsingNewNormalization))]
208+
[MemberData(nameof(TestData.WindowsTrailingProblematicFileNames), MemberType = typeof(TestData))]
209+
[PlatformSpecific(TestPlatforms.Windows)]
210+
public void WindowsEnumerateFilesWithTrailingSpacePeriod(string fileName)
227211
{
212+
// Files with trailing spaces/periods must be created with \\?\ on Windows
213+
// but enumeration can find them. This tests the scenario from issue #113120.
228214
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
229-
File.Create(Path.Combine(testDir.FullName, "file\tname")).Dispose();
215+
string filePath = Path.Combine(testDir.FullName, fileName);
216+
File.Create(@"\\?\" + filePath).Dispose();
230217

231218
string[] files = GetEntries(testDir.FullName);
232219
Assert.Single(files);
233-
Assert.Contains(files, f => Path.GetFileName(f) == "file\tname");
220+
Assert.Contains(files, f => Path.GetFileName(f) == fileName);
221+
222+
// The problematic scenario: enumeration returns the filename with trailing space/period,
223+
// but File.Exists and File.OpenRead will fail on the returned path because
224+
// normalization strips the trailing characters.
225+
// This is tracked in issue #113120.
226+
string returnedPath = files[0];
227+
// TODO: This should work but currently fails - tracked in #113120
228+
// Assert.True(File.Exists(returnedPath));
234229
}
235230
}
236231
}

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/Copy.cs

Lines changed: 6 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -423,65 +423,12 @@ public void DestinationFileIsTruncatedWhenItsLargerThanSourceFile()
423423
Assert.Equal(content, File.ReadAllBytes(destPath));
424424
}
425425

426-
[Theory]
427-
[InlineData(" leading", "destination")]
428-
[InlineData("source", " leading")]
429-
[InlineData(" leading", " leadingdest")]
430-
public void CopyWithLeadingSpaces(string sourceFileName, string destFileName)
431-
{
432-
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
433-
string sourcePath = Path.Combine(testDir.FullName, sourceFileName);
434-
string destPath = Path.Combine(testDir.FullName, destFileName);
435-
436-
File.Create(sourcePath).Dispose();
437-
Copy(sourcePath, destPath);
438-
439-
Assert.True(File.Exists(sourcePath));
440-
Assert.True(File.Exists(destPath));
441-
}
442-
443-
[Theory]
444-
[InlineData(".leading", "destination")]
445-
[InlineData("source", ".leading")]
446-
public void CopyWithLeadingDots(string sourceFileName, string destFileName)
447-
{
448-
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
449-
string sourcePath = Path.Combine(testDir.FullName, sourceFileName);
450-
string destPath = Path.Combine(testDir.FullName, destFileName);
451-
452-
File.Create(sourcePath).Dispose();
453-
Copy(sourcePath, destPath);
454-
455-
Assert.True(File.Exists(sourcePath));
456-
Assert.True(File.Exists(destPath));
457-
}
458-
459-
[Theory]
460-
[InlineData("-", "destination")]
461-
[InlineData("source", "-")]
462-
[InlineData("--", "destination")]
463-
public void CopyWithDashPrefixedNames(string sourceFileName, string destFileName)
464-
{
465-
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
466-
string sourcePath = Path.Combine(testDir.FullName, sourceFileName);
467-
string destPath = Path.Combine(testDir.FullName, destFileName);
468-
469-
File.Create(sourcePath).Dispose();
470-
Copy(sourcePath, destPath);
471-
472-
Assert.True(File.Exists(sourcePath));
473-
Assert.True(File.Exists(destPath));
474-
}
475-
476-
[Theory]
477-
[InlineData("file\tname", "destination")]
478-
[InlineData("source", "file\tname")]
479-
[PlatformSpecific(TestPlatforms.AnyUnix)]
480-
public void UnixCopyWithEmbeddedControlCharacters(string sourceFileName, string destFileName)
426+
[Theory, MemberData(nameof(TestData.ValidFileNames), MemberType = typeof(TestData))]
427+
public void CopyWithProblematicNames(string fileName)
481428
{
482429
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
483-
string sourcePath = Path.Combine(testDir.FullName, sourceFileName);
484-
string destPath = Path.Combine(testDir.FullName, destFileName);
430+
string sourcePath = Path.Combine(testDir.FullName, fileName);
431+
string destPath = Path.Combine(testDir.FullName, fileName + "_copy");
485432

486433
File.Create(sourcePath).Dispose();
487434
Copy(sourcePath, destPath);
@@ -502,8 +449,8 @@ public void WindowsCopyWithTrailingSpacePeriod_ViaExtendedSyntax(string sourceFi
502449
// because path normalization strips these characters. While enumeration APIs like
503450
// Directory.GetFiles can discover these files, direct string-based APIs (File.OpenRead,
504451
// File.Exists) will fail because the trailing characters get stripped during normalization.
505-
// Only FileInfo/DirectoryInfo objects from enumeration use EnsureExtendedPrefixIfNeeded
506-
// internally and can access these files. See TrimmedPaths.cs for comprehensive tests.
452+
// FileInfo/DirectoryInfo objects from enumeration work because they skip path normalization
453+
// and use the original path directly. See TrimmedPaths.cs for comprehensive tests.
507454
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
508455
string sourcePath = Path.Combine(testDir.FullName, sourceFileName);
509456
string destPath = Path.Combine(testDir.FullName, destFileName);
@@ -521,22 +468,6 @@ public void WindowsCopyWithTrailingSpacePeriod_ViaExtendedSyntax(string sourceFi
521468
Assert.True(File.Exists(sourceToCopy));
522469
Assert.True(File.Exists(destToCopy));
523470
}
524-
525-
[Theory]
526-
[InlineData("name with spaces", "dest with spaces")]
527-
[InlineData("name.with.periods", "dest.with.periods")]
528-
public void CopyEmbeddedSpacesPeriods(string sourceFileName, string destFileName)
529-
{
530-
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
531-
string sourcePath = Path.Combine(testDir.FullName, sourceFileName);
532-
string destPath = Path.Combine(testDir.FullName, destFileName);
533-
534-
File.Create(sourcePath).Dispose();
535-
Copy(sourcePath, destPath);
536-
537-
Assert.True(File.Exists(sourcePath));
538-
Assert.True(File.Exists(destPath));
539-
}
540471
}
541472

542473
/// <summary>

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/TestData.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,23 @@ public static TheoryData<string> ValidFileNames
122122
"name with spaces.txt"
123123
};
124124

125+
// On Unix, control characters are also valid in filenames
126+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
127+
{
128+
data.Add("file\tname"); // tab
129+
data.Add("file\rname"); // carriage return
130+
data.Add("file\vname"); // vertical tab
131+
data.Add("file\fname"); // form feed
132+
}
133+
125134
return data;
126135
}
127136
}
128137

129138
/// <summary>
130139
/// Filenames with control characters that are only valid on Unix.
131140
/// Windows reserves control characters 0-31 as invalid.
141+
/// Use ValidFileNames instead which includes these on Unix platforms.
132142
/// </summary>
133143
public static TheoryData<string> UnixOnlyFileNames
134144
{

0 commit comments

Comments
 (0)