Skip to content

Commit

Permalink
fixing semmle warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
eddynaka committed Oct 26, 2020
1 parent ec4e414 commit c9f1ae6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
9 changes: 3 additions & 6 deletions src/Sarif.Multitool.Library/MergeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class MergeCommand : CommandBase

public MergeCommand(IFileSystem fileSystem = null)
{
_fileSystem = fileSystem ?? new FileSystem();
_fileSystem = fileSystem ?? FileSystem.Instance;
_ruleIdToRunsMap = new Dictionary<string, Run>();
_idToSarifLogMap = new Dictionary<string, SarifLog>();
}
Expand Down Expand Up @@ -147,8 +147,6 @@ private async Task<bool> MergeSarifLogsAsync()
Run emptyRun = run.DeepClone();
run.Results = cachedResults;

var idToRunMap = new Dictionary<string, Run>();

if (run.Results != null)
{
foreach (Result result in run.Results)
Expand All @@ -169,7 +167,6 @@ private async Task<bool> MergeSarifLogsAsync()

if (!_ruleIdToRunsMap.TryGetValue(key, out Run splitRun))
{
IEqualityComparer<Run> comparer = Microsoft.CodeAnalysis.Sarif.Run.ValueComparer;
splitRun = _ruleIdToRunsMap[key] = new Run()
{
Tool = emptyRun.Tool,
Expand Down Expand Up @@ -222,7 +219,7 @@ private async Task<bool> LoadSarifLogs()
private void ProcessInputSarifLog(string filePath)
{
SarifLog sarifLog = PrereleaseCompatibilityTransformer.UpdateToCurrentVersion(
File.ReadAllText(filePath),
_fileSystem.ReadAllText(filePath),
formatting: Formatting.None,
out string sarifText);

Expand Down Expand Up @@ -258,7 +255,7 @@ private async Task<bool> FindFilesAsync()
directory = @".\";
}

foreach (string file in Directory.EnumerateFiles(directory, filter, searchOption))
foreach (string file in _fileSystem.EnumerateFiles(directory, filter, searchOption))
{
Interlocked.Increment(ref _filesToProcessCount);
await _logLoadChannel.Writer.WriteAsync(file);
Expand Down
27 changes: 26 additions & 1 deletion src/Sarif/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public void SetAttributes(string path, FileAttributes fileAttributes)
/// <returns>
/// 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>
/// </returns>
public DirectoryInfo DirectoryCreate(string path)
{
return Directory.CreateDirectory(path);
Expand All @@ -279,5 +279,30 @@ public void FileDelete(string path)
{
File.Delete(path);
}

/// <summary>
/// Returns an enumerable collection of full file names that match a search pattern in a
/// specified path, and optionally searches subdirectories.
/// </summary>
/// <param name="path">
/// The relative or absolute path to the directory to search. This string is not case-sensitive.
/// </param>
/// <param name="searchPattern">
/// The search string to match against the names of files in path. This parameter can contain a
/// combination of valid literal path and wildcard (* and ?) characters, but it doesn't support
/// regular expressions.
/// </param>
/// <param name="searchOption">
/// One of the enumeration values that specifies whether the search operation should include only
/// the current directory or should include all subdirectories. The default value is TopDirectoryOnly.
/// </param>
/// <returns>
/// An enumerable collection of the full names (including paths) for the files in the directory
/// specified by path and that match the specified search pattern and search option.
/// </returns>
public IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOption)
{
return Directory.EnumerateFiles(path, searchPattern, searchOption);
}
}
}
22 changes: 22 additions & 0 deletions src/Sarif/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,5 +225,27 @@ public interface IFileSystem
/// The name of the file to be deleted. Wildcard characters are not supported.
/// </param>
void FileDelete(string path);

/// <summary>
/// Returns an enumerable collection of full file names that match a search pattern in a
/// specified path, and optionally searches subdirectories.
/// </summary>
/// <param name="path">
/// The relative or absolute path to the directory to search. This string is not case-sensitive.
/// </param>
/// <param name="searchPattern">
/// The search string to match against the names of files in path. This parameter can contain a
/// combination of valid literal path and wildcard (* and ?) characters, but it doesn't support
/// regular expressions.
/// </param>
/// <param name="searchOption">
/// One of the enumeration values that specifies whether the search operation should include only
/// the current directory or should include all subdirectories. The default value is TopDirectoryOnly.
/// </param>
/// <returns>
/// An enumerable collection of the full names (including paths) for the files in the directory
/// specified by path and that match the specified search pattern and search option.
/// </returns>
IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOption);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ protected override string ConstructTestOutputFromInputResource(string inputResou

// We mock the file system to fake out the read operations.
mockFileSystem.Setup(x => x.FileExists(outputFilePath)).Returns(false);
mockFileSystem.Setup(x => x.EnumerateFiles(InputFolderPath, "Inputs.NoInputFiles.sarif", SearchOption.TopDirectoryOnly)).Returns(new string[] { targetFileSpecifier });
mockFileSystem.Setup(x => x.DirectoryExists(InputFolderPath)).Returns(true);
mockFileSystem.Setup(x => x.GetFilesInDirectory(InputFolderPath, inputResourceName)).Returns(new string[0]); // <= The hard-coded return value in question.
// mockFileSystem.Setup(x => x.ReadAllText(targetFileSpecifier)).Returns(logFileContents);

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

0 comments on commit c9f1ae6

Please sign in to comment.