Skip to content

Commit

Permalink
chore: add some missing curlies
Browse files Browse the repository at this point in the history
  • Loading branch information
fgreinacher committed Sep 11, 2020
1 parent 2b3386b commit e715094
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public override void Delete()

public override void Refresh()
{
// Nothing to do here. Mock file system is always up-to-date.
}

public override FileAttributes Attributes
Expand Down
9 changes: 7 additions & 2 deletions src/System.IO.Abstractions.TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,19 @@ private Stream OpenInternal(
bool exists = mockFileDataAccessor.FileExists(path);

if (mode == FileMode.CreateNew && exists)
{
throw new IOException(string.Format(CultureInfo.InvariantCulture, "The file '{0}' already exists.", path));
}

if ((mode == FileMode.Open || mode == FileMode.Truncate) && !exists)
{
throw CommonExceptions.FileNotFound(path);
}

if (!exists || mode == FileMode.CreateNew)
{
return Create(path);
}

if (mode == FileMode.Create || mode == FileMode.Truncate)
{
Expand All @@ -424,8 +430,7 @@ public override StreamReader OpenText(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

return new StreamReader(
OpenRead(path));
return new StreamReader(OpenRead(path));
}

public override Stream OpenWrite(string path) => OpenWriteInternal(path, FileOptions.None);
Expand Down
2 changes: 2 additions & 0 deletions src/System.IO.Abstractions.TestingHelpers/MockFileData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ public FileSecurity AccessControl
internal void CheckFileAccess(string path, FileAccess access)
{
if (!AllowedFileShare.HasFlag((FileShare)access))
{
throw CommonExceptions.ProcessCannotAccessFileInUse(path);
}
}
}
}
46 changes: 38 additions & 8 deletions src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,45 @@ public override void Delete()

public override void Refresh()
{
// Nothing to do here. Mock file system is always up-to-date.
}

public override FileAttributes Attributes
{
get
{
if (MockFileData == null) throw CommonExceptions.FileNotFound(path);
if (MockFileData == null)
{
throw CommonExceptions.FileNotFound(path);
}
return MockFileData.Attributes;
}
set { MockFileData.Attributes = value; }
set
{
if (MockFileData == null)
{
throw CommonExceptions.FileNotFound(path);
}
MockFileData.Attributes = value;
}
}

public override DateTime CreationTime
{
get
{
if (MockFileData == null) throw CommonExceptions.FileNotFound(path);
if (MockFileData == null)
{
throw CommonExceptions.FileNotFound(path);
}
return MockFileData.CreationTime.DateTime;
}
set
{
if (MockFileData == null) throw CommonExceptions.FileNotFound(path);
if (MockFileData == null)
{
throw CommonExceptions.FileNotFound(path);
}
MockFileData.CreationTime = value;
}
}
Expand Down Expand Up @@ -238,7 +255,7 @@ public override Stream Open(FileMode mode, FileAccess access, FileShare share)

public override StreamReader OpenText() => mockFileSystem.File.OpenText(path);

public override Stream OpenWrite() => mockFileSystem.File.OpenWrite(path);
public override Stream OpenWrite() => mockFileSystem.File.OpenWrite(path);

public override IFileInfo Replace(string destinationFileName, string destinationBackupFileName)
{
Expand Down Expand Up @@ -278,24 +295,37 @@ public override bool IsReadOnly
{
get
{
if (MockFileData == null) throw CommonExceptions.FileNotFound(path);
if (MockFileData == null)
{
throw CommonExceptions.FileNotFound(path);
}
return (MockFileData.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
}
set
{
if (MockFileData == null) throw CommonExceptions.FileNotFound(path);
if (MockFileData == null)
{
throw CommonExceptions.FileNotFound(path);
}
if (value)
{
MockFileData.Attributes |= FileAttributes.ReadOnly;
}
else
{
MockFileData.Attributes &= ~FileAttributes.ReadOnly;
}
}
}

public override long Length
{
get
{
if (MockFileData == null || MockFileData.IsDirectory) throw CommonExceptions.FileNotFound(path);
if (MockFileData == null || MockFileData.IsDirectory)
{
throw CommonExceptions.FileNotFound(path);
}
return MockFileData.Contents.Length;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ public void AddDirectory(string path)
{
if (FileExists(fixedPath) &&
(GetFile(fixedPath).Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
throw CommonExceptions.AccessDenied(fixedPath);

}
var lastIndex = 0;
var isUnc =
StringOperations.StartsWith(fixedPath, @"\\") ||
Expand All @@ -176,7 +177,9 @@ public void AddDirectory(string path)
lastIndex = StringOperations.IndexOf(fixedPath, separator, 2);

if (lastIndex < 0)
{
throw CommonExceptions.InvalidUncPath(nameof(path));
}

/*
* Although CreateDirectory(@"\\server\share\") is not going to work in real code, we allow it here for the purposes of setting up test doubles.
Expand Down
2 changes: 2 additions & 0 deletions src/System.IO.Abstractions/DirectoryInfoBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ internal DirectoryInfoBase() { }
public static implicit operator DirectoryInfoBase(DirectoryInfo directoryInfo)
{
if (directoryInfo == null)
{
return null;
}
return new DirectoryInfoWrapper(new FileSystem(), directoryInfo);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/System.IO.Abstractions/FileSystemWatcherBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static implicit operator FileSystemWatcherBase(FileSystemWatcher watcher)
{
if (watcher == null)
{
throw new ArgumentNullException(nameof(watcher));
return null;
}

return new FileSystemWatcherWrapper(watcher);
Expand Down

0 comments on commit e715094

Please sign in to comment.