Skip to content

Commit

Permalink
Implemented changes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
CoryCharlton committed Nov 8, 2023
1 parent 53c8f74 commit 6d3417e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
10 changes: 1 addition & 9 deletions System.IO.FileSystem.UnitTests/FileUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class FileUnitTests
[Setup]
public void Setup()
{
Assert.SkipTest("These test will only run on real hardware. Comment out this line if you are testing on real hardware.");
//Assert.SkipTest("These test will only run on real hardware. Comment out this line if you are testing on real hardware.");
}

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

[TestMethod]
public void Copy_throws_if_destination_equals_source()
{
Assert.ThrowsException(typeof(ArgumentException), () => File.Copy(Source, Source));
Assert.ThrowsException(typeof(ArgumentException), () => File.Copy(Source, Source, overwrite: false));
Assert.ThrowsException(typeof(ArgumentException), () => File.Copy(Source, Source, overwrite: true));
}

[TestMethod]
public void Copy_throws_if_destination_exists()
{
Expand Down
44 changes: 24 additions & 20 deletions System.IO.FileSystem/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,34 @@ public static class File
/// </summary>
/// <param name="sourceFileName">The file to copy.</param>
/// <param name="destFileName">The name of the destination file. This cannot be a directory or an existing file.</param>
/// <exception cref="ArgumentException">sourceFileName or destFileName is null or empty</exception>
public static void Copy(string sourceFileName, string destFileName) => Copy(sourceFileName, destFileName, overwrite: false);
/// <exception cref="ArgumentException"><paramref name="sourceFileName"/> or <paramref name="destFileName"/> is null or empty.</exception>
public static void Copy(string sourceFileName, string destFileName)
{
Copy(sourceFileName, destFileName, overwrite: false);
}

/// <summary>
/// Copies an existing file to a new file. Overwriting a file of the same name is allowed.
/// </summary>
/// <param name="sourceFileName">The file to copy.</param>
/// <param name="destFileName">The name of the destination file. This cannot be a directory.</param>
/// <param name="overwrite"><c>true&lt;/c&gt; if the destination file can be overwritten; otherwise, <c>false</c>.</param>
/// <exception cref="ArgumentException">sourceFileName or destFileName is null or empty</exception>
/// <param name="overwrite"><c>true</c>; if the destination file can be overwritten; otherwise, <c>false</c>.</param>
/// <exception cref="ArgumentException"><paramref name="sourceFileName"/> or <paramref name="destFileName"/> is null or empty.</exception>
public static void Copy(string sourceFileName, string destFileName, bool overwrite)
{
if (string.IsNullOrEmpty(sourceFileName))
{
throw new ArgumentException(nameof(sourceFileName));
throw new ArgumentException();
}

if (string.IsNullOrEmpty(destFileName))
{
throw new ArgumentException(nameof(destFileName));
throw new ArgumentException();
}

if (sourceFileName == destFileName)
{
throw new ArgumentException();
return;
}

var destMode = overwrite ? FileMode.Create : FileMode.CreateNew;
Expand Down Expand Up @@ -85,7 +88,7 @@ public static void Delete(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException(nameof(path));
throw new ArgumentException();
}

Path.CheckInvalidPathChars(path);
Expand Down Expand Up @@ -116,15 +119,15 @@ public static void Delete(string path)
return;
}

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

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

DeleteNative(path);
Expand All @@ -149,10 +152,9 @@ public static bool Exists(string path)
/// Gets the <see cref="FileAttributes"/> of the file on the path.
/// </summary>
/// <param name="path">The path to the file.</param>
/// <exception cref="IOException">File not found.</exception>
/// <exception cref="IOException"><paramref name="path"/> cannot be not found.</exception>
public static FileAttributes GetAttributes(string path)
{
// Adding this check because on my device the GetAttributesNative call throws an Exception instead of IOException if the file is not found
if (!Exists(path))
{
throw new IOException(string.Empty, (int)IOException.IOExceptionErrorCode.FileNotFound);
Expand All @@ -177,9 +179,9 @@ public static FileAttributes GetAttributes(string path)
/// <returns>
/// A <see cref="DateTime" /> structure set to the last write date and time for the specified file or directory.
/// </returns>
/// <exception cref="IOException"><paramref name="path"/> cannot be not found.</exception>
public static DateTime GetLastWriteTime(string path)
{
// Adding this check because on my device the GetLastWriteTimeNative call throws an Exception instead of IOException if the file is not found
if (!Exists(path))
{
throw new IOException(string.Empty, (int)IOException.IOExceptionErrorCode.FileNotFound);
Expand All @@ -193,26 +195,28 @@ public static DateTime GetLastWriteTime(string path)
/// </summary>
/// <param name="sourceFileName">The name of the file to move. Must be an absolute path.</param>
/// <param name="destFileName">The new path and name for the file.</param>
/// <exception cref="ArgumentException"><paramref name="sourceFileName"/> or <paramref name="destFileName"/> is null or empty.</exception>
/// <exception cref="IOException"><paramref name="sourceFileName"/> does not exist or <paramref name="destFileName"/> exists.</exception>
public static void Move(string sourceFileName, string destFileName)
{
if (string.IsNullOrEmpty(sourceFileName))
{
throw new ArgumentException(nameof(sourceFileName));
throw new ArgumentException();
}

if (string.IsNullOrEmpty(destFileName))
{
throw new ArgumentException(nameof(destFileName));
throw new ArgumentException();
}

if (!Exists(sourceFileName))
{
throw new IOException(nameof(sourceFileName));
throw new IOException(string.Empty, (int)IOException.IOExceptionErrorCode.FileNotFound);
}

if (Exists(destFileName))
{
throw new IOException(nameof(destFileName));
throw new IOException(string.Empty, (int)IOException.IOExceptionErrorCode.PathAlreadyExists);
}

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

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

0 comments on commit 6d3417e

Please sign in to comment.