Skip to content

Commit 6d3417e

Browse files
committed
Implemented changes from review
1 parent 53c8f74 commit 6d3417e

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

System.IO.FileSystem.UnitTests/FileUnitTests.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class FileUnitTests
99
[Setup]
1010
public void Setup()
1111
{
12-
Assert.SkipTest("These test will only run on real hardware. Comment out this line if you are testing on real hardware.");
12+
//Assert.SkipTest("These test will only run on real hardware. Comment out this line if you are testing on real hardware.");
1313
}
1414

1515
private const string Root = @"I:\";
@@ -164,14 +164,6 @@ public void Copy_throws_if_destination_is_null_or_empty()
164164
Assert.ThrowsException(typeof(ArgumentException), () => File.Copy(Source, string.Empty, overwrite: true));
165165
}
166166

167-
[TestMethod]
168-
public void Copy_throws_if_destination_equals_source()
169-
{
170-
Assert.ThrowsException(typeof(ArgumentException), () => File.Copy(Source, Source));
171-
Assert.ThrowsException(typeof(ArgumentException), () => File.Copy(Source, Source, overwrite: false));
172-
Assert.ThrowsException(typeof(ArgumentException), () => File.Copy(Source, Source, overwrite: true));
173-
}
174-
175167
[TestMethod]
176168
public void Copy_throws_if_destination_exists()
177169
{

System.IO.FileSystem/File.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,34 @@ public static class File
2222
/// </summary>
2323
/// <param name="sourceFileName">The file to copy.</param>
2424
/// <param name="destFileName">The name of the destination file. This cannot be a directory or an existing file.</param>
25-
/// <exception cref="ArgumentException">sourceFileName or destFileName is null or empty</exception>
26-
public static void Copy(string sourceFileName, string destFileName) => Copy(sourceFileName, destFileName, overwrite: false);
25+
/// <exception cref="ArgumentException"><paramref name="sourceFileName"/> or <paramref name="destFileName"/> is null or empty.</exception>
26+
public static void Copy(string sourceFileName, string destFileName)
27+
{
28+
Copy(sourceFileName, destFileName, overwrite: false);
29+
}
2730

2831
/// <summary>
2932
/// Copies an existing file to a new file. Overwriting a file of the same name is allowed.
3033
/// </summary>
3134
/// <param name="sourceFileName">The file to copy.</param>
3235
/// <param name="destFileName">The name of the destination file. This cannot be a directory.</param>
33-
/// <param name="overwrite"><c>true&lt;/c&gt; if the destination file can be overwritten; otherwise, <c>false</c>.</param>
34-
/// <exception cref="ArgumentException">sourceFileName or destFileName is null or empty</exception>
36+
/// <param name="overwrite"><c>true</c>; if the destination file can be overwritten; otherwise, <c>false</c>.</param>
37+
/// <exception cref="ArgumentException"><paramref name="sourceFileName"/> or <paramref name="destFileName"/> is null or empty.</exception>
3538
public static void Copy(string sourceFileName, string destFileName, bool overwrite)
3639
{
3740
if (string.IsNullOrEmpty(sourceFileName))
3841
{
39-
throw new ArgumentException(nameof(sourceFileName));
42+
throw new ArgumentException();
4043
}
4144

4245
if (string.IsNullOrEmpty(destFileName))
4346
{
44-
throw new ArgumentException(nameof(destFileName));
47+
throw new ArgumentException();
4548
}
4649

4750
if (sourceFileName == destFileName)
4851
{
49-
throw new ArgumentException();
52+
return;
5053
}
5154

5255
var destMode = overwrite ? FileMode.Create : FileMode.CreateNew;
@@ -85,7 +88,7 @@ public static void Delete(string path)
8588
{
8689
if (string.IsNullOrEmpty(path))
8790
{
88-
throw new ArgumentException(nameof(path));
91+
throw new ArgumentException();
8992
}
9093

9194
Path.CheckInvalidPathChars(path);
@@ -116,15 +119,15 @@ public static void Delete(string path)
116119
return;
117120
}
118121

119-
// Check if file is director or read-only (then not allowed to delete)
122+
// Check if file is directory or read-only (then not allowed to delete)
120123
if ((attributes & (byte)FileAttributes.Directory) != 0)
121124
{
122-
throw new IOException("Cannot delete directory.", (int)IOException.IOExceptionErrorCode.UnauthorizedAccess);
125+
throw new IOException(string.Empty, (int)IOException.IOExceptionErrorCode.UnauthorizedAccess);
123126
}
124127

125128
if ((attributes & (byte)FileAttributes.ReadOnly) != 0)
126129
{
127-
throw new IOException("File is read-only", (int)IOException.IOExceptionErrorCode.UnauthorizedAccess);
130+
throw new IOException(string.Empty, (int)IOException.IOExceptionErrorCode.UnauthorizedAccess);
128131
}
129132

130133
DeleteNative(path);
@@ -149,10 +152,9 @@ public static bool Exists(string path)
149152
/// Gets the <see cref="FileAttributes"/> of the file on the path.
150153
/// </summary>
151154
/// <param name="path">The path to the file.</param>
152-
/// <exception cref="IOException">File not found.</exception>
155+
/// <exception cref="IOException"><paramref name="path"/> cannot be not found.</exception>
153156
public static FileAttributes GetAttributes(string path)
154157
{
155-
// Adding this check because on my device the GetAttributesNative call throws an Exception instead of IOException if the file is not found
156158
if (!Exists(path))
157159
{
158160
throw new IOException(string.Empty, (int)IOException.IOExceptionErrorCode.FileNotFound);
@@ -177,9 +179,9 @@ public static FileAttributes GetAttributes(string path)
177179
/// <returns>
178180
/// A <see cref="DateTime" /> structure set to the last write date and time for the specified file or directory.
179181
/// </returns>
182+
/// <exception cref="IOException"><paramref name="path"/> cannot be not found.</exception>
180183
public static DateTime GetLastWriteTime(string path)
181184
{
182-
// Adding this check because on my device the GetLastWriteTimeNative call throws an Exception instead of IOException if the file is not found
183185
if (!Exists(path))
184186
{
185187
throw new IOException(string.Empty, (int)IOException.IOExceptionErrorCode.FileNotFound);
@@ -193,26 +195,28 @@ public static DateTime GetLastWriteTime(string path)
193195
/// </summary>
194196
/// <param name="sourceFileName">The name of the file to move. Must be an absolute path.</param>
195197
/// <param name="destFileName">The new path and name for the file.</param>
198+
/// <exception cref="ArgumentException"><paramref name="sourceFileName"/> or <paramref name="destFileName"/> is null or empty.</exception>
199+
/// <exception cref="IOException"><paramref name="sourceFileName"/> does not exist or <paramref name="destFileName"/> exists.</exception>
196200
public static void Move(string sourceFileName, string destFileName)
197201
{
198202
if (string.IsNullOrEmpty(sourceFileName))
199203
{
200-
throw new ArgumentException(nameof(sourceFileName));
204+
throw new ArgumentException();
201205
}
202206

203207
if (string.IsNullOrEmpty(destFileName))
204208
{
205-
throw new ArgumentException(nameof(destFileName));
209+
throw new ArgumentException();
206210
}
207211

208212
if (!Exists(sourceFileName))
209213
{
210-
throw new IOException(nameof(sourceFileName));
214+
throw new IOException(string.Empty, (int)IOException.IOExceptionErrorCode.FileNotFound);
211215
}
212216

213217
if (Exists(destFileName))
214218
{
215-
throw new IOException(nameof(destFileName));
219+
throw new IOException(string.Empty, (int)IOException.IOExceptionErrorCode.PathAlreadyExists);
216220
}
217221

218222
if (sourceFileName == destFileName)
@@ -260,6 +264,7 @@ public static void Move(string sourceFileName, string destFileName)
260264
/// Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
261265
/// </summary>
262266
/// <param name="path">The file to open for reading.</param>
267+
/// <exception cref="IOException">The end of the file was unexpectedly reached.</exception>
263268
public static byte[] ReadAllBytes(string path)
264269
{
265270
using var stream = OpenRead(path);
@@ -273,7 +278,7 @@ public static byte[] ReadAllBytes(string path)
273278
var read = stream.Read(bytes, index, count > ChunkSize ? ChunkSize : count);
274279
if (read <= 0)
275280
{
276-
throw new IOException("Unexpected end of file");
281+
throw new IOException();
277282
}
278283

279284
index += read;
@@ -300,7 +305,6 @@ public static string ReadAllText(string path)
300305
/// <param name="fileAttributes">A bitwise combination of the enumeration values.</param>
301306
public static void SetAttributes(string path, FileAttributes fileAttributes)
302307
{
303-
// Adding this check because on my device the SetAttributesNative call throws an Exception instead of IOException if the file is not found
304308
if (!Exists(path))
305309
{
306310
throw new IOException(string.Empty, (int)IOException.IOExceptionErrorCode.FileNotFound);

0 commit comments

Comments
 (0)