Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code cleanup #63

Merged
merged 1 commit into from Mar 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions System.IO.Abstractions/Converters.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace System.IO.Abstractions
Expand All @@ -17,6 +18,7 @@ internal static FileSystemInfoBase[] WrapFileSystemInfos(this IEnumerable<FileSy
return (DirectoryInfoBase) item;

throw new NotImplementedException(string.Format(
CultureInfo.InvariantCulture,
"The type {0} is not recognized by the System.IO.Abstractions library.",
item.GetType().AssemblyQualifiedName
));
Expand Down
7 changes: 2 additions & 5 deletions System.IO.Abstractions/FileSystemWatcherWrapper.cs
@@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace System.IO.Abstractions
{
[Serializable]
public class FileSystemWatcherWrapper : FileSystemWatcherBase
{
[NonSerialized]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the type FileSystemWatcher itself is not serializable, because FileSystemWrapper is serializable the field should be marked with NonSerialized

private readonly FileSystemWatcher watcher;

public FileSystemWatcherWrapper()
Expand Down
2 changes: 1 addition & 1 deletion TestHelpers.Tests/FileSystemTests.cs
Expand Up @@ -14,7 +14,7 @@ public void Is_Serializable()
var serializer = new Runtime.Serialization.Formatters.Binary.BinaryFormatter();
serializer.Serialize(memoryStream, fileSystem);

Assert.That(memoryStream.Length > 0, "Length didnt increase after serialization task.");
Assert.That(memoryStream.Length > 0, "Length didn't increase after serialization task.");
}
}
}
2 changes: 1 addition & 1 deletion TestHelpers.Tests/MockFileSystemTests.cs
Expand Up @@ -90,7 +90,7 @@ public void Is_Serializable()
var serializer = new Runtime.Serialization.Formatters.Binary.BinaryFormatter();
serializer.Serialize(memoryStream, fileSystem);

Assert.That(memoryStream.Length > 0, "Length didnt increase after serialization task.");
Assert.That(memoryStream.Length > 0, "Length didn't increase after serialization task.");
}
}
}
7 changes: 4 additions & 3 deletions TestHelpers.Tests/MockFileTests.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using NUnit.Framework;
Expand Down Expand Up @@ -73,10 +74,10 @@ public void MockFile_AppendAllText_ShouldFailIfNotExistButDirectoryAlsoNotExist(
// Assert
Exception ex;
ex = Assert.Throws<DirectoryNotFoundException>(() => fileSystem.File.AppendAllText(path, "some text"));
Assert.That(ex.Message, Is.EqualTo(String.Format("Could not find a part of the path '{0}'.", path)));
Assert.That(ex.Message, Is.EqualTo(String.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path)));

ex = Assert.Throws<DirectoryNotFoundException>(() => fileSystem.File.AppendAllText(path, "some text", Encoding.Unicode));
Assert.That(ex.Message, Is.EqualTo(String.Format("Could not find a part of the path '{0}'.", path)));
Assert.That(ex.Message, Is.EqualTo(String.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path)));
}

[Test]
Expand Down Expand Up @@ -1012,7 +1013,7 @@ public void Mockfile_Create_ThrowsWhenPathIsReadOnly()
mockFile.SetAttributes(path, FileAttributes.ReadOnly);

var exception = Assert.Throws<UnauthorizedAccessException>(() => mockFile.Create(path).Close());
Assert.That(exception.Message, Is.EqualTo(string.Format("Access to the path '{0}' is denied.", path)));
Assert.That(exception.Message, Is.EqualTo(string.Format(CultureInfo.InvariantCulture, "Access to the path '{0}' is denied.", path)));
}

[Test]
Expand Down
11 changes: 6 additions & 5 deletions TestingHelpers/MockDirectory.cs
Expand Up @@ -50,7 +50,7 @@ public override void Delete(string path, bool recursive)
path = EnsurePathEndsWithDirectorySeparator(mockFileDataAccessor.Path.GetFullPath(path));
var affectedPaths = mockFileDataAccessor
.AllPaths
.Where(p => p.StartsWith(path, StringComparison.InvariantCultureIgnoreCase))
.Where(p => p.StartsWith(path, StringComparison.OrdinalIgnoreCase))
.ToList();

if (!affectedPaths.Any())
Expand All @@ -69,7 +69,7 @@ public override bool Exists(string path)
path = EnsurePathEndsWithDirectorySeparator(path);

path = mockFileDataAccessor.Path.GetFullPath(path);
return mockFileDataAccessor.AllDirectories.Any(p => p.Equals(path, StringComparison.InvariantCultureIgnoreCase));
return mockFileDataAccessor.AllDirectories.Any(p => p.Equals(path, StringComparison.OrdinalIgnoreCase));
}

public override DirectorySecurity GetAccessControl(string path)
Expand Down Expand Up @@ -113,7 +113,7 @@ public override string[] GetDirectories(string path, string searchPattern, Searc

if (!Exists(path))
{
throw new DirectoryNotFoundException(string.Format("Could not find a part of the path '{0}'.", path));
throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path));
}

var dirs = GetFilesInternal(mockFileDataAccessor.AllDirectories, path, searchPattern, searchOption);
Expand Down Expand Up @@ -141,7 +141,7 @@ public override string[] GetFiles(string path, string searchPattern, SearchOptio
{
if (!Exists(path))
{
throw new DirectoryNotFoundException(string.Format("Could not find a part of the path '{0}'.", path));
throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path));
}

return GetFilesInternal(mockFileDataAccessor.AllFiles, path, searchPattern, searchOption);
Expand All @@ -162,6 +162,7 @@ private string[] GetFilesInternal(IEnumerable<string> files, string path, string
.Replace(@"\?", @"[^<>:""/\\|?*]?");

var pathPattern = string.Format(
CultureInfo.InvariantCulture,
@"(?i:^{0}{1}{2}$)",
Regex.Escape(path),
searchOption == SearchOption.AllDirectories ? allDirectoriesPattern : string.Empty,
Expand Down Expand Up @@ -290,7 +291,7 @@ public override void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)

static string EnsurePathEndsWithDirectorySeparator(string path)
{
if (!path.EndsWith(Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture)))
if (!path.EndsWith(Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase))
path += Path.DirectorySeparatorChar;
return path;
}
Expand Down
4 changes: 2 additions & 2 deletions TestingHelpers/MockDirectoryInfo.cs
Expand Up @@ -11,8 +11,8 @@ public class MockDirectoryInfo : DirectoryInfoBase
readonly IMockFileDataAccessor mockFileDataAccessor;
readonly string directoryPath;

private string EnsurePathEndsWithDirectorySeparator(string directoryPath) {
if (!directoryPath.EndsWith(Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture)))
private static string EnsurePathEndsWithDirectorySeparator(string directoryPath) {
if (!directoryPath.EndsWith(Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase))
directoryPath += Path.DirectorySeparatorChar;
return directoryPath;
}
Expand Down
17 changes: 9 additions & 8 deletions TestingHelpers/MockFile.cs
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Globalization;
using System.Security.AccessControl;
using System.Text;

Expand All @@ -23,7 +24,7 @@ public override void AppendAllText(string path, string contents)
var dir = mockFileDataAccessor.Path.GetDirectoryName(path);
if (!mockFileDataAccessor.Directory.Exists(dir))
{
throw new DirectoryNotFoundException(String.Format("Could not find a part of the path '{0}'.", path));
throw new DirectoryNotFoundException(String.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path));
}
mockFileDataAccessor.AddFile(path, new MockFileData(contents));
}
Expand All @@ -42,7 +43,7 @@ public override void AppendAllText(string path, string contents, Encoding encodi
var dir = mockFileDataAccessor.Path.GetDirectoryName(path);
if (!mockFileDataAccessor.Directory.Exists(dir))
{
throw new DirectoryNotFoundException(String.Format("Could not find a part of the path '{0}'.", path));
throw new DirectoryNotFoundException(String.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path));
}
mockFileDataAccessor.AddFile(path, new MockFileData(encoding.GetBytes(contents)));
}
Expand Down Expand Up @@ -73,7 +74,7 @@ public override StreamWriter AppendText(string path)
public override void Copy(string sourceFileName, string destFileName)
{
if (mockFileDataAccessor.FileExists(destFileName))
throw new IOException(string.Format("The file {0} already exists.", destFileName));
throw new IOException(string.Format(CultureInfo.InvariantCulture, "The file {0} already exists.", destFileName));

mockFileDataAccessor.AddFile(destFileName, mockFileDataAccessor.GetFile(sourceFileName));
}
Expand Down Expand Up @@ -195,7 +196,7 @@ public override DateTime GetLastWriteTimeUtc(string path)
var sourceFile = mockFileDataAccessor.GetFile(sourceFileName);

if (sourceFile == null)
throw new FileNotFoundException(string.Format("The file \"{0}\" could not be found.", sourceFileName), sourceFileName);
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "The file \"{0}\" could not be found.", sourceFileName), sourceFileName);

mockFileDataAccessor.AddFile(destFileName, new MockFileData(sourceFile.Contents));
mockFileDataAccessor.RemoveFile(sourceFileName);
Expand All @@ -218,7 +219,7 @@ public override Stream Open(string path, FileMode mode)
bool exists = mockFileDataAccessor.FileExists(path);

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

if ((mode == FileMode.Open || mode == FileMode.Truncate) && !exists)
throw new FileNotFoundException(path);
Expand Down Expand Up @@ -278,7 +279,7 @@ public override byte[] ReadAllBytes(string path)
public override string[] ReadAllLines(string path)
{
if (!mockFileDataAccessor.FileExists(path))
throw new FileNotFoundException(string.Format("Can't find {0}", path));
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Can't find {0}", path));
return mockFileDataAccessor
.GetFile(path)
.TextContents
Expand All @@ -288,7 +289,7 @@ public override string[] ReadAllLines(string path)
public override string[] ReadAllLines(string path, Encoding encoding)
{
if (!mockFileDataAccessor.FileExists(path))
throw new FileNotFoundException(string.Format("Can't find {0}", path));
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Can't find {0}", path));
return encoding
.GetString(mockFileDataAccessor.GetFile(path).Contents)
.SplitLines();
Expand All @@ -297,7 +298,7 @@ public override string[] ReadAllLines(string path, Encoding encoding)
public override string ReadAllText(string path)
{
if (!mockFileDataAccessor.FileExists(path))
throw new FileNotFoundException(string.Format("Can't find {0}", path));
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Can't find {0}", path));
return mockFileDataAccessor.GetFile(path).TextContents;
}

Expand Down
3 changes: 2 additions & 1 deletion TestingHelpers/MockFileData.cs
Expand Up @@ -6,7 +6,8 @@ namespace System.IO.Abstractions.TestingHelpers
public class MockFileData
{
static readonly Encoding defaultEncoding = Encoding.UTF8;
public static readonly MockFileData NullObject = new MockFileData("") {

public static readonly MockFileData NullObject = new MockFileData(string.Empty) {
LastWriteTime = new DateTime(1601, 01, 01, 00, 00, 00, DateTimeKind.Utc),
LastAccessTime = new DateTime(1601, 01, 01, 00, 00, 00, DateTimeKind.Utc),
CreationTime = new DateTime(1601, 01, 01, 00, 00, 00, DateTimeKind.Utc),
Expand Down
7 changes: 4 additions & 3 deletions TestingHelpers/MockFileSystem.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace System.IO.Abstractions.TestingHelpers
Expand All @@ -17,7 +18,7 @@ public class MockFileSystem : IFileSystem, IMockFileDataAccessor

public MockFileSystem(IDictionary<string, MockFileData> files, string currentDirectory = @"C:\Foo\Bar")
{
this.files = new Dictionary<string, MockFileData>(StringComparer.InvariantCultureIgnoreCase);
this.files = new Dictionary<string, MockFileData>(StringComparer.OrdinalIgnoreCase);
pathField = new MockPath(this);
file = new MockFile(this);
directory = new MockDirectory(this, file, FixPath(currentDirectory));
Expand Down Expand Up @@ -72,7 +73,7 @@ public void AddFile(string path, MockFileData mockFile)
{
var fixedPath = FixPath(path);
if (FileExists(fixedPath) && (files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
throw new UnauthorizedAccessException(string.Format("Access to the path '{0}' is denied.", path));
throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, "Access to the path '{0}' is denied.", path));

var directoryPath = Path.GetDirectoryName(fixedPath);

Expand All @@ -93,7 +94,7 @@ public void AddDirectory(string path)
{
if (FileExists(path) &&
(files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
throw new UnauthorizedAccessException(string.Format("Access to the path '{0}' is denied.", path));
throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, "Access to the path '{0}' is denied.", path));

files[fixedPath] = new MockDirectoryData();
}
Expand Down