Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b86274b

Browse files
author
Ian Hays
committed
-Updated with responses to PR feedback
-Added OSX checks for Case Sensitivity tests -Fixed a bug within several Enumerable tests that was causing a test directory to be left over. -Increased code coverage slightly for Directory. -Added tests to ensure whitespace file names are acceptable on Unix machines and marked those tests with an ActiveIssue referencing the newly created #2205
1 parent 9e20082 commit b86274b

16 files changed

+1047
-1309
lines changed

src/System.IO.FileSystem/tests/Directory/Directory_EnumerableAPIs.cs

Lines changed: 0 additions & 744 deletions
This file was deleted.

src/System.IO.FileSystem/tests/Directory/EnumerableAPIs.cs

Lines changed: 603 additions & 0 deletions
Large diffs are not rendered by default.

src/System.IO.FileSystem/tests/Directory/GetFileSystemEntries_str.cs

Lines changed: 107 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,21 @@ public void NullFileName()
3232
[Fact]
3333
public void EmptyFileName()
3434
{
35-
Assert.Throws<ArgumentException>(() => GetEntries(""));
35+
Assert.Throws<ArgumentException>(() => GetEntries(String.Empty));
3636
}
3737

3838
[Fact]
39-
public void NonexistentDirectory()
39+
public void InvalidFileNames()
4040
{
41-
Assert.Throws<DirectoryNotFoundException>(() => GetEntries(GetTestFilePath()));
41+
DirectoryInfo testDir = new DirectoryInfo(TestDirectory);
42+
Assert.Throws<DirectoryNotFoundException>(() => GetEntries(","));
43+
Assert.Throws<DirectoryNotFoundException>(() => GetEntries("DoesNotExist"));
4244
}
4345

4446
[Fact]
45-
public void FileNameWithSpaces()
47+
public void NonexistentFile()
4648
{
47-
String strTempDir = " ";
48-
Assert.Throws<ArgumentException>(() => GetEntries(strTempDir));
49+
Assert.Throws<DirectoryNotFoundException>(() => GetEntries(GetTestFilePath()));
4950
}
5051

5152
[Fact]
@@ -68,7 +69,7 @@ public void CurrentDirectory()
6869
[Fact]
6970
public void ValidDirectoryEmpty()
7071
{
71-
String testDir = Path.Combine(TestDirectory, GetTestFilePath());
72+
String testDir = Path.Combine(TestDirectory, GetTestFileName());
7273
new DirectoryInfo(testDir).Create();
7374
String[] strArr = GetEntries(testDir);
7475
Assert.NotNull(strArr);
@@ -78,7 +79,7 @@ public void ValidDirectoryEmpty()
7879
[Fact]
7980
public void ValidDirectoryNotEmpty()
8081
{
81-
using (FileStream fs1 = new FileInfo(Path.Combine(TestDirectory, GetTestFilePath())).Create())
82+
using (File.Create(Path.Combine(TestDirectory, GetTestFileName())))
8283
{
8384
String[] strArr = GetEntries(TestDirectory);
8485
Assert.NotNull(strArr);
@@ -93,47 +94,73 @@ public void NonexistentPath()
9394
}
9495

9596
[Fact]
96-
public void SubDirectories()
97+
public void GetEntriesThenDelete()
9798
{
98-
DirectoryInfo testDir = new DirectoryInfo(TestDirectory);
99-
testDir.CreateSubdirectory("TestDir1");
100-
testDir.CreateSubdirectory("TestDir2");
101-
testDir.CreateSubdirectory("TestDir3");
102-
using (new FileInfo(Path.Combine(TestDirectory, "TestFile1")).Create())
103-
using (new FileInfo(Path.Combine(TestDirectory, "TestFile2")).Create())
104-
using (new FileInfo(Path.Combine(TestDirectory, "Test1File2")).Create())
105-
using (new FileInfo(Path.Combine(TestDirectory, "Test1Dir2")).Create())
99+
String testDirPath = Path.Combine(TestDirectory, GetTestFilePath());
100+
DirectoryInfo testDirInfo = new DirectoryInfo(testDirPath);
101+
testDirInfo.Create();
102+
String testDir1 = GetTestFileName();
103+
String testDir2 = GetTestFileName();
104+
String testFile1 = GetTestFileName();
105+
String testFile2 = GetTestFileName();
106+
String testFile3 = GetTestFileName();
107+
String testFile4 = GetTestFileName();
108+
String testFile5 = GetTestFileName();
109+
testDirInfo.CreateSubdirectory(testDir1);
110+
testDirInfo.CreateSubdirectory(testDir2);
111+
using (File.Create(Path.Combine(testDirPath, testFile1)))
112+
using (File.Create(Path.Combine(testDirPath, testFile2)))
113+
using (File.Create(Path.Combine(testDirPath, testFile3)))
106114
{
107-
String[] strArr = GetEntries(TestDirectory);
108-
Assert.Equal(7, strArr.Length);
109-
Assert.Contains(Path.Combine(TestDirectory, "TestDir1"), strArr);
110-
Assert.Contains(Path.Combine(TestDirectory, "TestDir2"), strArr);
111-
Assert.Contains(Path.Combine(TestDirectory, "TestDir3"), strArr);
112-
Assert.Contains(Path.Combine(TestDirectory, "Test1File2"), strArr);
113-
Assert.Contains(Path.Combine(TestDirectory, "Test1Dir2"), strArr);
114-
Assert.Contains(Path.Combine(TestDirectory, "TestFile1"), strArr);
115-
Assert.Contains(Path.Combine(TestDirectory, "TestFile2"), strArr);
115+
String[] results;
116+
using (File.Create(Path.Combine(testDirPath, testFile4)))
117+
using (File.Create(Path.Combine(testDirPath, testFile5)))
118+
{
119+
results = GetEntries(testDirPath);
120+
Assert.NotNull(results);
121+
Assert.NotEmpty(results);
122+
Assert.Contains(Path.Combine(testDirPath, testFile1), results);
123+
Assert.Contains(Path.Combine(testDirPath, testFile2), results);
124+
Assert.Contains(Path.Combine(testDirPath, testFile3), results);
125+
Assert.Contains(Path.Combine(testDirPath, testFile4), results);
126+
Assert.Contains(Path.Combine(testDirPath, testFile5), results);
127+
Assert.Contains(Path.Combine(testDirPath, testDir1), results);
128+
Assert.Contains(Path.Combine(testDirPath, testDir2), results);
129+
}
130+
131+
File.Delete(Path.Combine(testDirPath, testFile4));
132+
File.Delete(Path.Combine(testDirPath, testFile5));
133+
FailSafeDirectoryOperations.DeleteDirectory(testDir1, true);
134+
135+
results = GetEntries(testDirPath);
136+
Assert.NotNull(results);
137+
Assert.NotEmpty(results);
138+
Assert.Contains(Path.Combine(testDirPath, testFile1), results);
139+
Assert.Contains(Path.Combine(testDirPath, testFile2), results);
140+
Assert.Contains(Path.Combine(testDirPath, testFile3), results);
141+
Assert.Contains(Path.Combine(testDirPath, testDir2), results);
116142
}
117143
}
118144

119145
[Fact]
120146
public void IgnoreSubDirectoryFiles()
121147
{
122-
String testDir1Str = GetTestFileName();
123-
String testDir11Str = GetTestFileName();
124-
DirectoryInfo testDir = new DirectoryInfo(TestDirectory);
125-
DirectoryInfo testDir1 = testDir.CreateSubdirectory(testDir1Str);
126-
testDir1.CreateSubdirectory(testDir11Str);
127-
using (new FileInfo(Path.Combine(TestDirectory, testDir1Str, testDir11Str, GetTestFileName())).Create())
128-
using (new FileInfo(Path.Combine(TestDirectory, testDir1Str, GetTestFileName())).Create())
148+
String testDir = GetTestFileName();
149+
String testFile1 = Path.Combine(TestDirectory, GetTestFileName());
150+
String testFile2 = Path.Combine(TestDirectory, testDir, GetTestFileName());
151+
Directory.CreateDirectory(Path.Combine(TestDirectory, testDir));
152+
using (File.Create(testFile1))
153+
using (File.Create(testFile2))
129154
{
130-
Assert.Equal(2, GetEntries(Path.Combine(TestDirectory, testDir1Str)).Length);
155+
String[] results = GetEntries(TestDirectory);
156+
Assert.Contains(testFile1, results);
157+
Assert.DoesNotContain(testFile2, results);
131158
}
132159
}
133160

134161
#endregion
135162

136-
#region WindowsOnly
163+
#region PlatformSpecific
137164

138165
[Fact]
139166
[PlatformSpecific(PlatformID.Windows)]
@@ -143,9 +170,51 @@ public void WildCharactersFileName()
143170
Assert.Throws<ArgumentException>(() => GetEntries(strTempDir));
144171
}
145172

146-
#endregion
147-
}
148-
173+
[Fact]
174+
[PlatformSpecific(PlatformID.Windows)]
175+
public void WindowsFileNameWithSpaces()
176+
{
177+
Assert.Throws<ArgumentException>(() => GetEntries("\n"));
178+
Assert.Throws<ArgumentException>(() => GetEntries(" "));
179+
Assert.Throws<ArgumentException>(() => GetEntries(" "));
180+
Assert.Throws<ArgumentException>(() => GetEntries(""));
181+
Assert.Throws<ArgumentException>(() => GetEntries(">"));
182+
Assert.Throws<ArgumentException>(() => GetEntries("\0"));
183+
}
149184

185+
[Fact]
186+
[PlatformSpecific(PlatformID.AnyUnix)]
187+
[ActiveIssue(2205)]
188+
public void UnixFileNameWithSpaces()
189+
{
190+
String testDirPath = Path.Combine(TestDirectory, GetTestFileName());
191+
using (File.Create(Path.Combine(testDirPath, " ")))
192+
using (File.Create(Path.Combine(testDirPath, " ")))
193+
using (File.Create(Path.Combine(testDirPath, "\n")))
194+
{
195+
String[] results = GetEntries(testDirPath);
196+
Assert.Contains(Path.Combine(testDirPath, " "), results);
197+
Assert.Contains(Path.Combine(testDirPath, " "), results);
198+
Assert.Contains(Path.Combine(testDirPath, "\n"), results);
199+
}
200+
}
150201

202+
[Fact]
203+
[PlatformSpecific(PlatformID.AnyUnix)]
204+
[ActiveIssue(2205)]
205+
public void UnixDirectoryNameWithSpaces()
206+
{
207+
String testDirPath = Path.Combine(TestDirectory, GetTestFileName());
208+
DirectoryInfo testDir = new DirectoryInfo(testDirPath);
209+
testDir.CreateSubdirectory(" ");
210+
testDir.CreateSubdirectory("\n");
211+
testDir.CreateSubdirectory(" ");
212+
213+
String[] results = GetEntries(testDirPath);
214+
Assert.Contains(Path.Combine(testDirPath, " "), results);
215+
Assert.Contains(Path.Combine(testDirPath, " "), results);
216+
Assert.Contains(Path.Combine(testDirPath, "\n"), results);
217+
}
218+
#endregion
219+
}
151220

src/System.IO.FileSystem/tests/Directory/GetFileSystemEntries_str_str.cs

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Directory_GetFileSystemEntries_str_str : Directory_GetFileSystemEnt
1414
/// </summary>
1515
public override String[] GetEntries(String dirName)
1616
{
17-
return Directory.GetFileSystemEntries(dirName, "*");
17+
return System.IO.Directory.GetFileSystemEntries(dirName, "*");
1818
}
1919

2020
/// <summary>
@@ -23,7 +23,7 @@ public override String[] GetEntries(String dirName)
2323
/// </summary>
2424
public virtual String[] GetEntries(String dirName, String searchPattern)
2525
{
26-
return Directory.GetFileSystemEntries(dirName, searchPattern);
26+
return System.IO.Directory.GetFileSystemEntries(dirName, searchPattern);
2727
}
2828

2929
#endregion
@@ -48,7 +48,7 @@ public void EmptySearchPattern()
4848
public void ValidSearchPattern()
4949
{
5050
String strTempDir = "a..b abc..d";
51-
GetEntries(TestDirectory, strTempDir); //Should not throw
51+
Assert.Empty(GetEntries(TestDirectory, strTempDir)); //Should not throw
5252
}
5353

5454
[Fact]
@@ -58,24 +58,17 @@ public void InvalidSearchPattern()
5858
Assert.Throws<ArgumentException>(() => GetEntries(TestDirectory, strTempDir));
5959
}
6060

61-
[Fact]
62-
public void SearchPatternWithSpaces()
63-
{
64-
Assert.Empty(GetEntries(TestDirectory, " "));
65-
Assert.Empty(GetEntries(TestDirectory, "\n"));
66-
}
67-
6861
[Fact]
6962
public void SearchPatternWithTrailingStar()
7063
{
7164
DirectoryInfo testDir = new DirectoryInfo(TestDirectory);
7265
testDir.CreateSubdirectory("TestDir1");
7366
testDir.CreateSubdirectory("TestDir2");
7467
testDir.CreateSubdirectory("TestDir3");
75-
using (new FileInfo(Path.Combine(TestDirectory, "TestFile1")).Create())
76-
using (new FileInfo(Path.Combine(TestDirectory, "TestFile2")).Create())
77-
using (new FileInfo(Path.Combine(TestDirectory, "Test1File2")).Create())
78-
using (new FileInfo(Path.Combine(TestDirectory, "Test1Dir2")).Create())
68+
using (File.Create(Path.Combine(TestDirectory, "TestFile1")))
69+
using (File.Create(Path.Combine(TestDirectory, "TestFile2")))
70+
using (File.Create(Path.Combine(TestDirectory, "Test1File2")))
71+
using (File.Create(Path.Combine(TestDirectory, "Test1Dir2")))
7972
{
8073
String[] strArr = GetEntries(TestDirectory, "Test1*");
8174
Assert.Equal(2, strArr.Length);
@@ -91,10 +84,10 @@ public void SearchPatternWithLeadingStar()
9184
testDir.CreateSubdirectory("TestDir1");
9285
testDir.CreateSubdirectory("TestDir2");
9386
testDir.CreateSubdirectory("TestDir3");
94-
using (new FileInfo(Path.Combine(TestDirectory, "TestFile1")).Create())
95-
using (new FileInfo(Path.Combine(TestDirectory, "TestFile2")).Create())
96-
using (new FileInfo(Path.Combine(TestDirectory, "Test1File2")).Create())
97-
using (new FileInfo(Path.Combine(TestDirectory, "Test1Dir2")).Create())
87+
using (File.Create(Path.Combine(TestDirectory, "TestFile1")))
88+
using (File.Create(Path.Combine(TestDirectory, "TestFile2")))
89+
using (File.Create(Path.Combine(TestDirectory, "Test1File2")))
90+
using (File.Create(Path.Combine(TestDirectory, "Test1Dir2")))
9891
{
9992
String[] strArr = GetEntries(TestDirectory, "*2");
10093
Assert.Equal(4, strArr.Length);
@@ -114,12 +107,12 @@ public void SearchPatternWithLeadingStar()
114107
public void SearchPatternCaseSensitivity()
115108
{
116109
DirectoryInfo testDir = new DirectoryInfo(TestDirectory);
117-
using (new FileInfo(Path.Combine(TestDirectory, "AAABB")).Create())
110+
using (File.Create(Path.Combine(TestDirectory, "AAABB")))
118111
{
119-
Directory.CreateDirectory(Path.Combine(TestDirectory, "aaabbcc"));
112+
System.IO.Directory.CreateDirectory(Path.Combine(TestDirectory, "aaabbcc"));
120113
String[] strArr = GetEntries(TestDirectory, "*BB*");
121114
Assert.Contains(Path.Combine(TestDirectory, "AAABB"), strArr);
122-
if (Interop.IsWindows)
115+
if (Interop.IsWindows || Interop.IsOSX)
123116
Assert.Contains(Path.Combine(TestDirectory, "aaabbcc"), strArr);
124117
else
125118
Assert.DoesNotContain(Path.Combine(TestDirectory, "aaabbcc"), strArr);
@@ -133,7 +126,7 @@ public void SearchPatternNonFullPath()
133126
String testDir1Str = GetTestFileName();
134127
DirectoryInfo testDir = new DirectoryInfo(TestDirectory);
135128
testDir.CreateSubdirectory(testDir1Str);
136-
using (new FileInfo(Path.Combine(TestDirectory, testDir1Str, GetTestFileName())).Create())
129+
using (File.Create(Path.Combine(TestDirectory, testDir1Str, GetTestFileName())))
137130
{
138131
Assert.Equal(1, GetEntries(TestDirectory, Path.Combine(testDir1Str, "*")).Length);
139132
}
@@ -142,16 +135,16 @@ public void SearchPatternNonFullPath()
142135
[Fact]
143136
public void SearchPatternIgnoreSubDirectories()
144137
{
145-
String testDir1Str = GetTestFileName();
146-
String testDir11Str = GetTestFileName();
147-
DirectoryInfo testDir = new DirectoryInfo(TestDirectory);
148-
DirectoryInfo testDir1 = testDir.CreateSubdirectory(testDir1Str);
149-
testDir1.CreateSubdirectory(testDir11Str);
150-
151-
using (new FileInfo(Path.Combine(TestDirectory, testDir1Str, testDir11Str, GetTestFileName())).Create())
152-
using (new FileInfo(Path.Combine(TestDirectory, testDir1Str, GetTestFileName())).Create())
138+
String testDir = GetTestFileName();
139+
String testFile1 = Path.Combine(TestDirectory, GetTestFileName());
140+
String testFile2 = Path.Combine(TestDirectory, testDir, GetTestFileName());
141+
Directory.CreateDirectory(Path.Combine(TestDirectory, testDir));
142+
using (File.Create(testFile1))
143+
using (File.Create(testFile2))
153144
{
154-
Assert.Equal(2, GetEntries(TestDirectory, Path.Combine(testDir1Str, "*")).Length);
145+
String[] results = GetEntries(Directory.GetCurrentDirectory(), Path.Combine(new DirectoryInfo(TestDirectory).Name, "*"));
146+
Assert.Contains(testFile1, results);
147+
Assert.DoesNotContain(testFile2, results);
155148
}
156149
}
157150

@@ -163,12 +156,41 @@ public void SearchPatternIgnoreSubDirectories()
163156
[PlatformSpecific(PlatformID.Windows)]
164157
public void WildCharactersSearchPattern()
165158
{
166-
String strTempDir = Path.Combine("dls;d", "442349-0", "v443094(*)(+*$#$*") + new string(Path.DirectorySeparatorChar, 3);
159+
String strTempDir = Path.Combine("dls;d", "442349-0", "v443094(*)(+*$#$*") +
160+
new string(Path.DirectorySeparatorChar, 3);
167161
Assert.Throws<ArgumentException>(() => GetEntries(TestDirectory, strTempDir));
168162
}
169163

164+
[Fact]
165+
[PlatformSpecific(PlatformID.Windows)]
166+
public void SearchPatternWithSpaces()
167+
{
168+
Assert.Empty(GetEntries(TestDirectory, " "));
169+
Assert.Empty(GetEntries(TestDirectory, "\n"));
170+
Assert.Empty(GetEntries(TestDirectory, " "));
171+
Assert.Empty(GetEntries(TestDirectory, ""));
172+
Assert.Throws<ArgumentException>(() => GetEntries(TestDirectory, "\0"));
173+
Assert.Throws<ArgumentException>(() => GetEntries(TestDirectory, ">"));
174+
}
175+
176+
[Fact]
177+
[PlatformSpecific(PlatformID.AnyUnix)]
178+
[ActiveIssue(2205)]
179+
public void UnixFilePathWithSpaces()
180+
{
181+
Assert.Throws<ArgumentException>(() => GetEntries("\0"));
182+
using (File.Create(Path.Combine(TestDirectory, " ")))
183+
using (File.Create(Path.Combine(TestDirectory, " ")))
184+
using (File.Create(Path.Combine(TestDirectory, "\n")))
185+
{
186+
String[] results = GetEntries(TestDirectory);
187+
Assert.Contains(Path.Combine(TestDirectory, " "), results);
188+
Assert.Contains(Path.Combine(TestDirectory, " "), results);
189+
Assert.Contains(Path.Combine(TestDirectory, "\n"), results);
190+
}
191+
}
192+
170193
#endregion
171194
}
172195

173196

174-

0 commit comments

Comments
 (0)