Skip to content

Commit

Permalink
add comments and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pauld-msft committed Dec 18, 2024
1 parent e5475ce commit a1459bc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ protected async Task WithCleanupAsync(
var (postSuccess, latestFiles, latestDirs) = this.TryGetFilesAndDirectories(fileParentDirectory, this.CleanupPatterns, DefaultCleanDepth);
if (!postSuccess)
{

Check warning on line 84 in src/Microsoft.ComponentDetection.Contracts/FileComponentDetectorWithCleanup.cs

View check run for this annotation

Codecov / codecov/patch

src/Microsoft.ComponentDetection.Contracts/FileComponentDetectorWithCleanup.cs#L84

Added line #L84 was not covered by tests
// return early if we failed to get the latest files and directories, since we will not be able
// to determine what to clean up
return;

Check warning on line 87 in src/Microsoft.ComponentDetection.Contracts/FileComponentDetectorWithCleanup.cs

View check run for this annotation

Codecov / codecov/patch

src/Microsoft.ComponentDetection.Contracts/FileComponentDetectorWithCleanup.cs#L87

Added line #L87 was not covered by tests
}

Expand Down Expand Up @@ -159,8 +161,8 @@ private bool TryGetCleanupFileDirectory(ProcessRequest processRequest, out strin
}
catch (UnauthorizedAccessException e)
{
// ignore files and directories that we don't have access to
this.Logger.LogDebug(e, "Failed to get files and directories for {Root}", root);
// log and return false if we are unauthorized to get files and directories
this.Logger.LogDebug(e, "Unauthorized to get files and directories for {Root}", root);
return (false, new HashSet<string>(), new HashSet<string>());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ public async Task WithCleanupAsync_CleansUpCreatedFile()
var fileComponentDetector = new TestFileComponentDetector(["*.pyc"], this.loggerMock.Object, fileUtilityService, directoryUtilityService);
var createdFilePath = Path.Combine(this.testDirectory, "todelete.pyc");

// Add files/dirs to the mock file system

// Act
await fileComponentDetector.TestCleanupAsync(
(process, args, token) =>
Expand Down Expand Up @@ -198,6 +196,11 @@ await fileComponentDetector.TestCleanupAsync(
fileUtilityService.Exists(this.existingFilePath).Should().BeTrue();
directoryUtilityService.Exists(createdDirectory).Should().BeTrue();
fileUtilityService.Exists(createdFilePath).Should().BeTrue();

// Assert we don't even try to read items
this.directoryUtilityServiceMock.Verify(
d => d.GetFilesAndDirectories(It.IsAny<string>(), It.IsAny<List<string>>(), It.IsAny<int>()),
Times.Never);
}

[TestMethod]
Expand Down Expand Up @@ -292,6 +295,40 @@ await fileComponentDetector.TestCleanupAsync(
fileUtilityService.Exists(createdFilePath).Should().BeTrue();
}

[TestMethod]
public async Task WithCleanupAsync_NoCleanup_WhenUnauthorized()
{
// Arrange
var fileUtilityService = this.fileUtilityServiceMock.Object;
var directoryUtilityService = this.directoryUtilityServiceMock.Object;
CancellationToken cancellationToken = default;
var detectorArgs = new Dictionary<string, string>();
var cleanupCreatedFiles = true;
var fileComponentDetector = new TestFileComponentDetector(["*.pyc"], this.loggerMock.Object, fileUtilityService, directoryUtilityService);
var createdFilePath = Path.Combine(this.testDirectory, "todelete.pyc");
this.directoryUtilityServiceMock
.Setup(d => d.GetFilesAndDirectories(It.IsAny<string>(), It.IsAny<List<string>>(), It.IsAny<int>()))
.Throws(new UnauthorizedAccessException("Unauthorized"));

// Act
await fileComponentDetector.TestCleanupAsync(
(process, args, token) =>
{
// creates a single file
this.fileSystemMockFiles.Add(createdFilePath);
return Task.CompletedTask;
},
this.processRequest,
detectorArgs,
cleanupCreatedFiles,
cancellationToken).ConfigureAwait(false);

// Assert
directoryUtilityService.Exists(this.testDirectory).Should().BeTrue();
fileUtilityService.Exists(this.existingFilePath).Should().BeTrue();
fileUtilityService.Exists(createdFilePath).Should().BeTrue();
}

private bool IsPatternMatch(string path, string pattern)
{
return Regex.IsMatch(path, "^" + Regex.Escape(pattern).Replace("\\*", ".*") + "$");
Expand Down

0 comments on commit a1459bc

Please sign in to comment.