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

Adding Delete in IFileSystem #2072

Merged
merged 5 commits into from
Sep 29, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Sarif.Multitool.Library/AbsoluteUriCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public int Run(AbsoluteUriOptions absoluteUriOptions)

if (!absoluteUriOptions.Inline)
{
_fileSystem.CreateDirectory(absoluteUriOptions.OutputDirectoryPath);
_fileSystem.DirectoryCreate(absoluteUriOptions.OutputDirectoryPath);
}

Formatting formatting = absoluteUriOptions.PrettyPrint
Expand Down
2 changes: 1 addition & 1 deletion src/Sarif.Multitool.Library/CommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void WriteSarifFile<T>(IFileSystem fileSystem, T sarifFile, string
Formatting = formatting
};

using (JsonTextWriter writer = new JsonTextWriter(new StreamWriter(fileSystem.Create(outputName))))
using (JsonTextWriter writer = new JsonTextWriter(new StreamWriter(fileSystem.FileCreate(outputName))))
{
serializer.Serialize(writer, sarifFile);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Sarif.Multitool.Library/MergeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public int Run(MergeOptions mergeOptions)
? Formatting.Indented
: Formatting.None;

_fileSystem.CreateDirectory(outputDirectory);
_fileSystem.DirectoryCreate(outputDirectory);

WriteSarifFile(_fileSystem, mergedLog, outputFilePath, formatting);
return 0;
Expand Down Expand Up @@ -124,7 +124,7 @@ public int Run(MergeOptions mergeOptions)
? Formatting.Indented
: Formatting.None;

_fileSystem.CreateDirectory(outputDirectory);
_fileSystem.DirectoryCreate(outputDirectory);

outputFilePath = Path.Combine(outputDirectory, GetOutputFileName(mergeOptions, ruleId));

Expand Down
2 changes: 1 addition & 1 deletion src/Sarif.Multitool.Library/PageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private void ExtractPage(PageOptions options, JsonMapNode root)
long lengthWritten = 0;
byte[] buffer = new byte[64 * 1024];

using (Stream output = _fileSystem.Create(options.OutputFilePath))
using (Stream output = _fileSystem.FileCreate(options.OutputFilePath))
using (Stream source = _fileSystem.OpenRead(options.InputFilePath))
{
// Copy everything up to 'runs' (includes the '[')
Expand Down
2 changes: 1 addition & 1 deletion src/Sarif.Multitool.Library/RebaseUriCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public int Run(RebaseUriOptions rebaseOptions)

if (!rebaseOptions.Inline)
{
_fileSystem.CreateDirectory(rebaseOptions.OutputDirectoryPath);
_fileSystem.DirectoryCreate(rebaseOptions.OutputDirectoryPath);
}

Formatting formatting = rebaseOptions.PrettyPrint
Expand Down
4 changes: 2 additions & 2 deletions src/Sarif.Multitool.Library/ResultMatchSetCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public int Run(ResultMatchSetOptions options)
// Remove previous results.
if (_fileSystem.DirectoryExists(options.OutputFolderPath) && options.Force)
{
_fileSystem.DeleteDirectory(options.OutputFolderPath, true);
_fileSystem.DirectoryDelete(options.OutputFolderPath, true);
}

// Create output folder.
_fileSystem.CreateDirectory(options.OutputFolderPath);
_fileSystem.DirectoryCreate(options.OutputFolderPath);

string previousFileName = "";
string previousGroup = "";
Expand Down
17 changes: 14 additions & 3 deletions src/Sarif/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public Stream OpenRead(string path)
/// </summary>
/// <param name="path">File System path of file to open</param>
/// <returns>Stream to write file</returns>
public Stream Create(string path)
public Stream FileCreate(string path)
{
return File.Create(path);
}
Expand Down Expand Up @@ -251,7 +251,7 @@ public void SetAttributes(string path, FileAttributes fileAttributes)
/// An object that represents the directory at the specified path. This object is
/// returned regardless of whether a directory at the specified path already exists.
/// </returns>
public DirectoryInfo CreateDirectory(string path)
public DirectoryInfo DirectoryCreate(string path)
{
return Directory.CreateDirectory(path);
}
Expand All @@ -262,9 +262,20 @@ public DirectoryInfo CreateDirectory(string path)
/// <param name="path">
/// The name of the empty directory to remove. This directory must be writable and empty.
/// </param>
public void DeleteDirectory(string path, bool recursive = false)
public void DirectoryDelete(string path, bool recursive = false)
{
Directory.Delete(path, recursive);
}

/// <summary>
/// Deletes the specified file.
/// </summary>
/// <param name="path">
/// The name of the file to be deleted. Wildcard characters are not supported.
/// </param>
public void FileDelete(string path)
{
File.Delete(path);
}
}
}
14 changes: 11 additions & 3 deletions src/Sarif/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public interface IFileSystem
/// </summary>
/// <param name="path">File System path of file to open</param>
/// <returns>Stream to write file</returns>
Stream Create(string path);
Stream FileCreate(string path);

/// <summary>
/// Sets the specified <see cref="FileAttributes"/> of the file on the specified path.
Expand All @@ -208,14 +208,22 @@ public interface IFileSystem
/// An object that represents the directory at the specified path. This object is
/// returned regardless of whether a directory at the specified path already exists.
/// </returns>
DirectoryInfo CreateDirectory(string path);
DirectoryInfo DirectoryCreate(string path);

/// <summary>
/// Deletes an empty directory from a specified path.
/// </summary>
/// <param name="path">
/// The name of the empty directory to remove. This directory must be writable and empty.
/// </param>
void DeleteDirectory(string path, bool recursive = false);
void DirectoryDelete(string path, bool recursive = false);

/// <summary>
/// Deletes the specified file.
/// </summary>
/// <param name="path">
/// The name of the file to be deleted. Wildcard characters are not supported.
/// </param>
void FileDelete(string path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ protected override string ConstructTestOutputFromInputResource(string inputResou
mockFileSystem.Setup(x => x.GetFilesInDirectory(InputFolderPath, inputResourceName)).Returns(new string[0]); // <= The hard-coded return value in question.

// But we really do want to create the output file, so tell the mock to execute the actual write operations.
mockFileSystem.Setup(x => x.CreateDirectory(OutputFolderPath)).Returns((string path) => { return Directory.CreateDirectory(path); });
mockFileSystem.Setup(x => x.Create(outputFilePath)).Returns((string path) => { return File.Create(path); });
mockFileSystem.Setup(x => x.DirectoryCreate(OutputFolderPath)).Returns((string path) => { return Directory.CreateDirectory(path); });
mockFileSystem.Setup(x => x.FileCreate(outputFilePath)).Returns((string path) => { return File.Create(path); });

IFileSystem fileSystem = mockFileSystem.Object;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private static Mock<IFileSystem> ArrangeMockFileSystem(string sarifLog, string l
var mockFileSystem = new Mock<IFileSystem>();
mockFileSystem.Setup(x => x.ReadAllText(logFilePath)).Returns(sarifLog);
mockFileSystem.Setup(x => x.OpenRead(logFilePath)).Returns(() => new MemoryStream(Encoding.UTF8.GetBytes(sarifLog)));
mockFileSystem.Setup(x => x.Create(logFilePath)).Returns(() => new MemoryStreamToStringBuilder(transformedContents));
mockFileSystem.Setup(x => x.FileCreate(logFilePath)).Returns(() => new MemoryStreamToStringBuilder(transformedContents));
mockFileSystem.Setup(x => x.WriteAllText(logFilePath, It.IsAny<string>())).Callback<string, string>((path, contents) => { transformedContents.Append(contents); });
mockFileSystem.Setup(x => x.DirectoryExists(It.IsAny<string>())).Returns(true);
mockFileSystem.Setup(x => x.GetFilesInDirectory(It.IsAny<string>(), It.IsAny<string>())).Returns(new string[] { logFilePath });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private static string RunTransformationCore(string logFileContents, SarifVersion
var mockFileSystem = new Mock<IFileSystem>();
mockFileSystem.Setup(x => x.ReadAllText(LogFilePath)).Returns(logFileContents);
mockFileSystem.Setup(x => x.OpenRead(LogFilePath)).Returns(() => new MemoryStream(Encoding.UTF8.GetBytes(logFileContents)));
mockFileSystem.Setup(x => x.Create(LogFilePath)).Returns(() => new MemoryStreamToStringBuilder(transformedContents));
mockFileSystem.Setup(x => x.FileCreate(LogFilePath)).Returns(() => new MemoryStreamToStringBuilder(transformedContents));
mockFileSystem.Setup(x => x.WriteAllText(LogFilePath, It.IsAny<string>())).Callback<string, string>((path, contents) => { transformedContents.Append(contents); });

var transformCommand = new TransformCommand(mockFileSystem.Object);
Expand Down