Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape curly braces in file names when logging #1000

Merged
merged 1 commit into from
Nov 26, 2020
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
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);
}
}
}