From a1459bc73f87d9c8639543c4ffd2e8c20b79f01b Mon Sep 17 00:00:00 2001 From: Paul Dorsch Date: Wed, 18 Dec 2024 15:56:34 -0500 Subject: [PATCH] add comments and unit tests --- .../FileComponentDetectorWithCleanup.cs | 6 ++- .../FileComponentDetectorWithCleanupTests.cs | 41 ++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.ComponentDetection.Contracts/FileComponentDetectorWithCleanup.cs b/src/Microsoft.ComponentDetection.Contracts/FileComponentDetectorWithCleanup.cs index 66fd832ea..0c230e4ca 100644 --- a/src/Microsoft.ComponentDetection.Contracts/FileComponentDetectorWithCleanup.cs +++ b/src/Microsoft.ComponentDetection.Contracts/FileComponentDetectorWithCleanup.cs @@ -82,6 +82,8 @@ protected async Task WithCleanupAsync( var (postSuccess, latestFiles, latestDirs) = this.TryGetFilesAndDirectories(fileParentDirectory, this.CleanupPatterns, DefaultCleanDepth); if (!postSuccess) { + // 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; } @@ -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(), new HashSet()); } } diff --git a/test/Microsoft.ComponentDetection.Contracts.Tests/FileComponentDetectorWithCleanupTests.cs b/test/Microsoft.ComponentDetection.Contracts.Tests/FileComponentDetectorWithCleanupTests.cs index 23e8165b2..116b8f62e 100644 --- a/test/Microsoft.ComponentDetection.Contracts.Tests/FileComponentDetectorWithCleanupTests.cs +++ b/test/Microsoft.ComponentDetection.Contracts.Tests/FileComponentDetectorWithCleanupTests.cs @@ -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) => @@ -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(), It.IsAny>(), It.IsAny()), + Times.Never); } [TestMethod] @@ -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(); + 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(), It.IsAny>(), It.IsAny())) + .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("\\*", ".*") + "$");