Skip to content

Commit

Permalink
Fix .mockignore behavior to use and override parent folders settings (C…
Browse files Browse the repository at this point in the history
…loses #75)
  • Loading branch information
natenho committed Mar 29, 2021
1 parent 020b83e commit 40c989c
Showing 1 changed file with 47 additions and 26 deletions.
73 changes: 47 additions & 26 deletions src/Mockaco/Templating/Providers/TemplateFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@ public sealed class TemplateFileProvider : ITemplateProvider, IDisposable
private const string DefaultTemplateSearchPattern = "*.json";

private readonly ILogger<TemplateFileProvider> _logger;

private readonly IMemoryCache _memoryCache;
private PhysicalFileProvider _fileProvider;
private CancellationTokenSource _resetCacheToken = new CancellationTokenSource();

public TemplateFileProvider(IOptionsMonitor<TemplateFileProviderOptions> options, IWebHostEnvironment webHostEnvironment, IMemoryCache memoryCache, ILogger<TemplateFileProvider> logger)
{
{
_memoryCache = memoryCache;
_logger = logger;

SetMockRootPath(options.CurrentValue.Path);
KeepWatchingForFileChanges();

options.OnChange(options => {
options.OnChange(options =>
{
SetMockRootPath(options.Path);
ResetCacheAndNotifyChange();
});
Expand All @@ -46,22 +47,22 @@ private void SetMockRootPath(string path)
{
try
{
if(_fileProvider?.Root.Equals(path) == true)
if (_fileProvider?.Root.Equals(path) == true)
{
return;
}

var fullPath = Path.IsPathRooted(path)
? path
var fullPath = Path.IsPathRooted(path)
? path
: Path.Combine(Directory.GetCurrentDirectory(), path);

var fileProvider = new PhysicalFileProvider(fullPath, ExclusionFilters.Hidden | ExclusionFilters.System);

_fileProvider?.Dispose();
_fileProvider = fileProvider;

_logger.LogInformation("Mock path: {fullPath}", fullPath);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error setting mock root path");
Expand All @@ -80,14 +81,14 @@ private void ResetCacheAndNotifyChange()

private void KeepWatchingForFileChanges()
{
if(_fileProvider == null)
if (_fileProvider == null)
{
return;
}

var jsonChangeToken = _fileProvider.Watch($"**/{DefaultTemplateSearchPattern}");
jsonChangeToken.RegisterChangeCallback(TemplateFileModified, default);

var mockIgnoreChangeToken = _fileProvider.Watch($"**/*{MockIgnoreFileName}");
mockIgnoreChangeToken.RegisterChangeCallback(TemplateFileModified, default);
}
Expand All @@ -102,7 +103,7 @@ private void TemplateFileModified(object state)
public IEnumerable<IRawTemplate> GetTemplates()
{
return _memoryCache.GetOrCreate(
nameof(TemplateFileProvider),
nameof(TemplateFileProvider) + nameof(GetTemplates),
e =>
{
e.RegisterPostEvictionCallback(PostEvictionCallback);
Expand All @@ -119,7 +120,7 @@ private void PostEvictionCallback(object key, object value, EvictionReason reaso

private IEnumerable<IRawTemplate> LoadTemplatesFromDirectory()
{
if(_fileProvider == null)
if (_fileProvider == null)
{
yield break;
}
Expand All @@ -133,7 +134,7 @@ private IEnumerable<IRawTemplate> LoadTemplatesFromDirectory()

if (ShouldIgnoreFile(file))
{
_logger.LogDebug("{relativeFilePath} ignored using a {MockIgnoreFileName} file", relativeFilePath, MockIgnoreFileName);
_logger.LogDebug("{relativeFilePath} ignored by a {MockIgnoreFileName} file", relativeFilePath, MockIgnoreFileName);
continue;
}

Expand All @@ -157,24 +158,45 @@ private IEnumerable<IRawTemplate> LoadTemplatesFromDirectory()

private bool ShouldIgnoreFile(FileInfo file)
{
var mockIgnorePath = Path.Combine(file.DirectoryName, MockIgnoreFileName);
var ignores = GetIgnoreList(file.Directory);

return ignores.IsIgnored(file);
}

private IgnoreList GetIgnoreList(DirectoryInfo directoryInfo)
{
var relativePath = Path.GetRelativePath(_fileProvider.Root, directoryInfo.FullName);
var pathSegments = relativePath.Split(Path.DirectorySeparatorChar);

var ignoreList = new IgnoreList();

var currentCombinedPath = _fileProvider.Root;

if (!File.Exists(mockIgnorePath))
TryIncludeIgnoreListFrom(currentCombinedPath, ignoreList);

foreach (var pathSegment in pathSegments)
{
return false;
currentCombinedPath = Path.Combine(currentCombinedPath, pathSegment);

TryIncludeIgnoreListFrom(currentCombinedPath, ignoreList);
}

var ignores = WrapWithFileExceptionHandling(mockIgnorePath, () =>
{
return new IgnoreList(mockIgnorePath);
});
return ignoreList;
}

private void TryIncludeIgnoreListFrom(string currentCombinedPath, IgnoreList ignoreList)
{
var mockIgnorePath = Path.Combine(currentCombinedPath, MockIgnoreFileName);

if (ignores == default)
if (File.Exists(mockIgnorePath))
{
return false;
}
WrapWithFileExceptionHandling(mockIgnorePath, () =>
{
ignoreList.AddRules(mockIgnorePath);
return ignores.IsIgnored(file);
return ignoreList;
});
}
}

private T WrapWithFileExceptionHandling<T>(string path, Func<T> action)
Expand All @@ -184,8 +206,7 @@ private T WrapWithFileExceptionHandling<T>(string path, Func<T> action)
try
{
return Policy.Handle<IOException>()
.WaitAndRetry(sleepDurations: new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(3) },
onRetry: (ex, _) => log(ex))
.WaitAndRetry(sleepDurations: new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(3) })
.Execute(action);
}
catch (Exception ex)
Expand Down

0 comments on commit 40c989c

Please sign in to comment.