Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,26 @@ public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, Concu
var scan = new ActionBlock<DirectoryInfo>(
di =>
{
var enumerator = new FileSystemEnumerable<FileSystemInfo>(di.FullName, this.Transform, new EnumerationOptions()
try
{
RecurseSubdirectories = true,
IgnoreInaccessible = true,
ReturnSpecialDirectories = false,
})
{
ShouldRecursePredicate = shouldRecurse,
};

foreach (var fileSystemInfo in enumerator)
var enumerator = new FileSystemEnumerable<FileSystemInfo>(di.FullName, this.Transform, new EnumerationOptions()
{
RecurseSubdirectories = true,
IgnoreInaccessible = true,
ReturnSpecialDirectories = false,
})
{
ShouldRecursePredicate = shouldRecurse,
};

foreach (var fileSystemInfo in enumerator)
{
observer.OnNext(fileSystemInfo);
}
Comment thread
mpysson marked this conversation as resolved.
}
catch (DirectoryNotFoundException)
{
observer.OnNext(fileSystemInfo);
this.logger.LogDebug("Directory disappeared during enumeration: {DirectoryFullName}", di.FullName);
}
},
new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount });
Expand Down Expand Up @@ -184,6 +191,12 @@ public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, Concu

s.OnNext(info);
},
error =>
{
sw.Stop();
this.logger.LogError(error, "Directory enumeration failed for {RootFullName}", root.FullName);
s.OnError(error);
},
() =>
{
sw.Stop();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#nullable disable
namespace Microsoft.ComponentDetection.Common.Tests;

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using AwesomeAssertions;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

[TestClass]
[TestCategory("Governance/All")]
[TestCategory("Governance/ComponentDetection")]
public class FastDirectoryWalkerFactoryTests
{
private string temporaryDirectory;

[TestInitialize]
public void TestInitialize()
{
this.temporaryDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(this.temporaryDirectory);
}

[TestCleanup]
public void TestCleanup()
{
if (Directory.Exists(this.temporaryDirectory))
{
Directory.Delete(this.temporaryDirectory, true);
}
}
Comment thread
mpysson marked this conversation as resolved.

[TestMethod]
public async Task GetDirectoryScanner_CompletesWhenDiscoveredDirectoryDisappears()
{
var disappearingDirectory = Directory.CreateDirectory(Path.Combine(this.temporaryDirectory, "disappearing"));
await File.WriteAllTextAsync(Path.Combine(disappearingDirectory.FullName, "component.txt"), "content");

var directoryWasDeleted = false;
bool DeleteDiscoveredDirectory(ReadOnlySpan<char> directoryName, ReadOnlySpan<char> parentPath)
{
_ = directoryName;
_ = parentPath;
Directory.Delete(disappearingDirectory.FullName, true);
directoryWasDeleted = true;
return false;
}

var walker = new FastDirectoryWalkerFactory(
Mock.Of<IPathUtilityService>(),
Mock.Of<ILogger<FastDirectoryWalkerFactory>>());

var completion = new TaskCompletionSource<IReadOnlyList<FileSystemInfo>>(TaskCreationOptions.RunContinuationsAsynchronously);
using var subscription = walker.GetDirectoryScanner(
new DirectoryInfo(this.temporaryDirectory),
new ConcurrentDictionary<string, bool>(),
DeleteDiscoveredDirectory).Subscribe(new RecordingObserver(completion));

var results = await completion.Task.WaitAsync(TimeSpan.FromSeconds(5));

directoryWasDeleted.Should().BeTrue();
results.Should().BeEmpty();
}

[TestMethod]
public async Task GetDirectoryScanner_PropagatesUnexpectedEnumerationError()
{
var parentDirectory = Directory.CreateDirectory(Path.Combine(this.temporaryDirectory, "parent"));
Directory.CreateDirectory(Path.Combine(parentDirectory.FullName, "nested"));
var expectedException = new InvalidOperationException("Unexpected enumeration error");

bool ThrowForNestedDirectory(ReadOnlySpan<char> directoryName, ReadOnlySpan<char> parentPath)
{
_ = parentPath;

if (directoryName.SequenceEqual("nested"))
{
throw expectedException;
}

return false;
}

var logger = new Mock<ILogger<FastDirectoryWalkerFactory>>();
var walker = new FastDirectoryWalkerFactory(
Mock.Of<IPathUtilityService>(),
logger.Object);

var completion = new TaskCompletionSource<IReadOnlyList<FileSystemInfo>>(TaskCreationOptions.RunContinuationsAsynchronously);
using var subscription = walker.GetDirectoryScanner(
new DirectoryInfo(this.temporaryDirectory),
new ConcurrentDictionary<string, bool>(),
ThrowForNestedDirectory).Subscribe(new RecordingObserver(completion));

Func<Task> waitForCompletion = async () => await completion.Task.WaitAsync(TimeSpan.FromSeconds(5));
var assertion = await waitForCompletion.Should().ThrowAsync<InvalidOperationException>();
assertion.Which.Should().BeSameAs(expectedException);
logger.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((value, _) => value.ToString().Contains("Directory enumeration failed")),
expectedException,
It.IsAny<Func<It.IsAnyType, Exception, string>>()),
Times.Once);
}

private sealed class RecordingObserver(TaskCompletionSource<IReadOnlyList<FileSystemInfo>> completion) : IObserver<FileSystemInfo>
{
private readonly List<FileSystemInfo> results = [];

public void OnCompleted() => completion.SetResult(this.results);

public void OnError(Exception error) => completion.SetException(error);

Comment thread
mpysson marked this conversation as resolved.
public void OnNext(FileSystemInfo value) => this.results.Add(value);
}
}
Loading