Skip to content

Commit

Permalink
Escape curly braces in file names when logging (#1000)
Browse files Browse the repository at this point in the history
Escape curly braces in file names when logging
  • Loading branch information
heaths authored Nov 26, 2020
1 parent ff76158 commit b63ab2a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/coverlet.core/Coverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public CoveragePrepareResult PrepareModules()

Array.ForEach(_excludeFilters ?? Array.Empty<string>(), filter => _logger.LogVerbose($"Excluded module filter '{filter}'"));
Array.ForEach(_includeFilters ?? Array.Empty<string>(), filter => _logger.LogVerbose($"Included module filter '{filter}'"));
Array.ForEach(_excludedSourceFiles ?? Array.Empty<string>(), filter => _logger.LogVerbose($"Excluded source files filter '{filter}'"));
Array.ForEach(_excludedSourceFiles ?? Array.Empty<string>(), filter => _logger.LogVerbose($"Excluded source files filter '{FileSystem.EscapeFileName(filter)}'"));

_excludeFilters = _excludeFilters?.Where(f => _instrumentationHelper.IsValidFilterExpression(f)).ToArray();
_includeFilters = _includeFilters?.Where(f => _instrumentationHelper.IsValidFilterExpression(f)).ToArray();
Expand Down
6 changes: 6 additions & 0 deletions src/coverlet.core/Helpers/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,11 @@ public string[] ReadAllLines(string path)
{
return File.ReadAllLines(path);
}

// Escape format characters in file names
internal static string EscapeFileName(string fileName)
{
return fileName?.Replace("{", "{{").Replace("}", "}}");
}
}
}
2 changes: 1 addition & 1 deletion src/coverlet.core/Helpers/SourceRootTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public string ResolveFilePath(string originalFileName)
if (_fileSystem.Exists(pathToCheck = Path.GetFullPath(originalFileName.Replace(mapping.Key, srm.OriginalPath))))
{
(_resolutionCacheFiles ??= new Dictionary<string, string>()).Add(originalFileName, pathToCheck);
_logger.LogVerbose($"Mapping resolved: '{originalFileName}' -> '{pathToCheck}'");
_logger.LogVerbose($"Mapping resolved: '{FileSystem.EscapeFileName(originalFileName)}' -> '{FileSystem.EscapeFileName(pathToCheck)}'");
return pathToCheck;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/coverlet.core/Instrumentation/Instrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Coverlet.Core.Instrumentation.Reachability;
using Coverlet.Core.Abstractions;
using Coverlet.Core.Attributes;
using Coverlet.Core.Helpers;
using Coverlet.Core.Symbols;
using Microsoft.Extensions.FileSystemGlobbing;
using Mono.Cecil;
Expand Down Expand Up @@ -110,7 +111,7 @@ public bool CanInstrument()
}
else
{
_logger.LogVerbose($"Unable to instrument module: {_module}, embedded pdb without local source files, [{firstNotFoundDocument}]");
_logger.LogVerbose($"Unable to instrument module: {_module}, embedded pdb without local source files, [{FileSystem.EscapeFileName(firstNotFoundDocument)}]");
return false;
}
}
Expand All @@ -122,7 +123,7 @@ public bool CanInstrument()
}
else
{
_logger.LogVerbose($"Unable to instrument module: {_module}, pdb without local source files, [{firstNotFoundDocument}]");
_logger.LogVerbose($"Unable to instrument module: {_module}, pdb without local source files, [{FileSystem.EscapeFileName(firstNotFoundDocument)}]");
return false;
}
}
Expand Down Expand Up @@ -159,7 +160,7 @@ public InstrumenterResult Instrument()
{
foreach (string sourceFile in _excludedSourceFiles)
{
_logger.LogVerbose($"Excluded source file: '{sourceFile}'");
_logger.LogVerbose($"Excluded source file: '{FileSystem.EscapeFileName(sourceFile)}'");
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/coverlet.core.tests/Helpers/FileSystemTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Coverlet.Core.Helpers;
using Xunit;

namespace Coverlet.Core.Helpers.Tests
{
public class FileSystemTests
{
[Theory]
[InlineData(null, null)]
[InlineData("", "")]
[InlineData("filename.cs", "filename.cs")]
[InlineData("filename{T}.cs", "filename{{T}}.cs")]
public void TestEscapeFileName(string fileName, string expected)
{
var actual = FileSystem.EscapeFileName(fileName);

Assert.Equal(expected, actual);
}
}
}

0 comments on commit b63ab2a

Please sign in to comment.