Skip to content

Commit

Permalink
WSL Support (#165)
Browse files Browse the repository at this point in the history
Co-authored-by: Normen Scheiber <nscheibe@live.com>
  • Loading branch information
joslat and nscheibe authored Nov 15, 2022
1 parent 6f809ee commit 9966944
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Snapshooter.Benchmarks;
namespace Snapshooter.Benchmarks.Base64;

public class Base64Parser
{
Expand All @@ -15,10 +15,10 @@ public bool IsBase64FromBase64String(string base64String)
}
}

public static bool IsBase64TryFromBase64String(string base64String)
public bool IsBase64TryFromBase64String(string base64String)
{
Span<byte> buffer = new Span<byte>(new byte[base64String.Length]);
bool isBase64 = Convert.TryFromBase64String(base64String, buffer, out int bytesParsed);
var buffer = new Span<byte>(new byte[base64String.Length]);
var isBase64 = Convert.TryFromBase64String(base64String, buffer, out var bytesParsed);

return isBase64;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Mathematics;

namespace Snapshooter.Benchmarks;
namespace Snapshooter.Benchmarks.Base64;

[RPlotExporter, MemoryDiagnoser]
[Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.FastestToSlowest)]
[RankColumn(NumeralSystem.Arabic)]
public class Base64ParserBenchmarks
{
{
private static string SmallBase64String = "VGhpcyBpcyBhIHNtYWxsIGJhc2UgNjQgc3RyaW5nIGZvciBiZW5jaG1hcmsgdGVzdGluZw==";
private static string SmallNoneBase64String = "Thisisasmallbase64stringforbenchmarktesting";

private static readonly Base64Parser Base64Parser = new Base64Parser();
private static readonly Base64Parser _base64Parser = new Base64Parser();

[Benchmark(Baseline = true)]
public void ClassicSmallNoneBase64Parse()
{
Base64Parser.IsBase64FromBase64String(SmallNoneBase64String);
_base64Parser.IsBase64FromBase64String(SmallNoneBase64String);
}

[Benchmark]
public void ClassicSmallBase64Parse()
{
Base64Parser.IsBase64FromBase64String(SmallBase64String);
_base64Parser.IsBase64FromBase64String(SmallBase64String);
}

[Benchmark]
public void NewSmallNoneBase64Parse()
{
Base64Parser.IsBase64TryFromBase64String(SmallNoneBase64String);
_base64Parser.IsBase64TryFromBase64String(SmallNoneBase64String);
}

[Benchmark]
public void NewSmallBase64Parse()
{
Base64Parser.IsBase64TryFromBase64String(SmallBase64String);
_base64Parser.IsBase64TryFromBase64String(SmallBase64String);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Snapshooter.Benchmarks.DirectoryName
{
public class DirectoryNameResolver
{
public string? GetDirectoryName_PathString(string filenameFullPath)
{
return Path.GetDirectoryName(filenameFullPath);
}

public ReadOnlySpan<char> GetDirectoryName_PathAsSpan(string filenameFullPath)
{
return Path.GetDirectoryName(filenameFullPath.AsSpan());
}

public string GetDirectoryName_Split(string filenameFullPath)
{
var directorySections = filenameFullPath.Split('\\');
IEnumerable<string>? minusLastSection = directorySections.Take(directorySections.Length - 1);
var directoryName = string.Join("\\", minusLastSection);

return directoryName;
}

public string GetDirectoryName_IndexOfString(string filenameFullPath)
{
string d = filenameFullPath.Remove(filenameFullPath.LastIndexOf('\\'));

return d;
}

public ReadOnlySpan<char> GetDirectoryName_IndexOfSpan(string filenameFullPath)
{
return filenameFullPath.AsSpan(0, filenameFullPath.LastIndexOf('\\'));
}

public string GetDirectoryName_IndexOfSpanToString(string filenameFullPath)
{
return filenameFullPath.AsSpan(0, filenameFullPath.LastIndexOf('\\')).ToString();
}

public string GetDirectoryName_IndexOfString_Slash(string filenameFullPath)
{
int index = filenameFullPath.LastIndexOf('\\');

if(index < 0)
{
index = filenameFullPath.LastIndexOf('/');
}

return filenameFullPath.Remove(index);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Mathematics;

namespace Snapshooter.Benchmarks.DirectoryName;

[RPlotExporter, MemoryDiagnoser]
[Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.FastestToSlowest)]
[RankColumn(NumeralSystem.Arabic)]
public class DirectoryNameResolverBenchmarks
{
private static string absoluteFileNameBackSlash = "C:\\somewhere\\snapshot\\testing\\snapshotfile.snap";
private static string absoluteFileNameSlash = "C:/somewhere/snapshot/testing/snapshotfile.snap";

private static readonly DirectoryNameResolver _directoryNameResolver = new DirectoryNameResolver();

[Benchmark(Baseline = true)]
public void GetDirectoryName_PathString()
{
_directoryNameResolver.GetDirectoryName_PathString(absoluteFileNameBackSlash);
}

[Benchmark]
public void GetDirectoryName_PathAsSpan()
{
_directoryNameResolver.GetDirectoryName_PathAsSpan(absoluteFileNameBackSlash);
}

[Benchmark]
public void GetDirectoryName_Split()
{
_directoryNameResolver.GetDirectoryName_Split(absoluteFileNameBackSlash);
}

[Benchmark]
public void GetDirectoryName_IndexOfString()
{
_directoryNameResolver.GetDirectoryName_IndexOfString(absoluteFileNameBackSlash);
}

[Benchmark]
public void GetDirectoryName_IndexOfSpan()
{
_directoryNameResolver.GetDirectoryName_IndexOfSpan(absoluteFileNameBackSlash);
}

[Benchmark]
public void GetDirectoryName_IndexOfSpanToString()
{
_directoryNameResolver.GetDirectoryName_IndexOfSpanToString(absoluteFileNameBackSlash);
}

[Benchmark]
public void GetDirectoryName_PathString_Slash()
{
_directoryNameResolver.GetDirectoryName_PathString(absoluteFileNameSlash);
}

[Benchmark]
public void GetDirectoryName_IndexOfString_Slash()
{
_directoryNameResolver.GetDirectoryName_IndexOfString_Slash(absoluteFileNameSlash);
}

[Benchmark]
public void GetDirectoryName_IndexOfString_BackSlash()
{
_directoryNameResolver.GetDirectoryName_IndexOfString_Slash(absoluteFileNameBackSlash);
}
}

6 changes: 3 additions & 3 deletions benchmark/Snapshooter.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using BenchmarkDotNet.Running;
using Snapshooter.Benchmarks;

Base64ParserBenchmarks base64ParserBenchmarks = new Base64ParserBenchmarks();
using Snapshooter.Benchmarks.Base64;
using Snapshooter.Benchmarks.DirectoryName;

BenchmarkRunner.Run<Base64ParserBenchmarks>();
BenchmarkRunner.Run<DirectoryNameResolverBenchmarks>();
4 changes: 2 additions & 2 deletions src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public SnapshotFullName ReadSnapshotFullName()
{
snapshotFullName = new SnapshotFullName(
GetMethodSnapshotName(method),
Path.GetDirectoryName(stackFrame.GetFileName()));
stackFrame.GetFileName().GetDirectoryName());

break;
}
Expand All @@ -45,7 +45,7 @@ public SnapshotFullName ReadSnapshotFullName()
{
snapshotFullName = new SnapshotFullName(
GetMethodSnapshotName(asyncMethod),
Path.GetDirectoryName(stackFrame.GetFileName()));
stackFrame.GetFileName().GetDirectoryName());

break;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Snapshooter.NUnit/NUnitSnapshotFullNameReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using NUnit.Framework.Interfaces;
using Snapshooter.Core;
using Snapshooter.Exceptions;
using Snapshooter.Extensions;

namespace Snapshooter.NUnit
{
Expand All @@ -35,7 +36,7 @@ public SnapshotFullName ReadSnapshotFullName()
{
snapshotFullName = new SnapshotFullName(
GetCurrentSnapshotName(),
Path.GetDirectoryName(stackFrame.GetFileName()));
stackFrame.GetFileName().GetDirectoryName());

break;
}
Expand All @@ -45,7 +46,7 @@ public SnapshotFullName ReadSnapshotFullName()
{
snapshotFullName = new SnapshotFullName(
GetCurrentSnapshotName(),
Path.GetDirectoryName(stackFrame.GetFileName()));
stackFrame.GetFileName().GetDirectoryName());

break;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Snapshooter.Xunit/XunitSnapshotFullNameReader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
Expand All @@ -26,14 +25,15 @@ public SnapshotFullName ReadSnapshotFullName()
{
SnapshotFullName snapshotFullName = null;
StackFrame[] stackFrames = new StackTrace(true).GetFrames();

foreach (StackFrame stackFrame in stackFrames)
{
MethodBase method = stackFrame.GetMethod();
if (IsXunitTestMethod(method))
{
snapshotFullName = new SnapshotFullName(
method.ToName(),
Path.GetDirectoryName(stackFrame.GetFileName()));
stackFrame.GetFileName().GetDirectoryName());

break;
}
Expand All @@ -43,12 +43,12 @@ public SnapshotFullName ReadSnapshotFullName()
{
snapshotFullName = new SnapshotFullName(
asyncMethod.ToName(),
Path.GetDirectoryName(stackFrame.GetFileName()));
stackFrame.GetFileName().GetDirectoryName());

break;
}
}

if (snapshotFullName == null)
{
throw new SnapshotTestException(
Expand All @@ -61,7 +61,7 @@ public SnapshotFullName ReadSnapshotFullName()
}

snapshotFullName = LiveUnitTestingDirectoryResolver
.CheckForSession(snapshotFullName);
.CheckForSession(snapshotFullName);

return snapshotFullName;
}
Expand Down
52 changes: 51 additions & 1 deletion src/Snapshooter/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand All @@ -11,7 +13,7 @@ namespace Snapshooter.Extensions
/// <summary>
/// Some string extensions to support the snapshot testing.
/// </summary>
internal static class StringExtension
public static class StringExtension
{
/// <summary>
/// Ensures that the given string ends with a line ending '\n'.
Expand Down Expand Up @@ -140,5 +142,53 @@ public static bool TryFindFieldsByName(this string fieldsPath, out string fieldN
fieldName = fieldsPath;
return false;
}

/// <summary>
/// Gets the directory path of a filename. The Path.GetDirectoryName
/// returns an empty string if it runs on wsl ubuntu if a windows file path
/// is given. This method has a fallback if this is the case. If
/// Path.GetDirectoryName returns an empty string, then this method tries
/// to get the folder path manually.
/// and thus with c:\\dir1\\dir\\filename.cs (as example)
/// </summary>
/// <param name="filenameFullPath">The filenameFullPath string.</param>
/// <returns>The folder path.</returns>
public static string GetDirectoryName(this string filenameFullPath)
{
var folderPath = Path.GetDirectoryName(filenameFullPath);

if (string.IsNullOrEmpty(folderPath))
{
folderPath = filenameFullPath.RemoveFilename();
}

return folderPath;
}

/// <summary>
/// Removes the last part of a file path after the folder separator
/// if any. This is the filename and its extension, if any.
/// This is meant as a fallback for the Path.GetDirectoryName when executed
/// in WSL but with a directory from xUnit (run in Windows subsystem)
/// and thus with c:\\dir1\\dir\\filename.cs (as example)
/// </summary>
/// <param name="filenameFullPath">The filenameFullPath string.</param>
/// <returns>The text string without the filename.</returns>
public static string RemoveFilename(this string filenameFullPath)
{
int index = filenameFullPath.LastIndexOf('\\');

if (index < 0)
{
index = filenameFullPath.LastIndexOf('/');

if(index < 0)
{
return string.Empty;
}
}

return filenameFullPath.Remove(index);
}
}
}
19 changes: 19 additions & 0 deletions test/Snapshooter.Tests/Extensions/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Snapshooter.Extensions;
using Xunit;

namespace Snapshooter.Tests.Extensions
{
public class StringExtensionsTests
{
[Theory]
[InlineData("C:\\normal\\path\\with\\filename", "C:\\normal\\path\\with")]
[InlineData("C:/normal/path/with/slash/filename", "C:/normal/path/with/slash")]
[InlineData("/normal/path/with/slash/filename", "/normal/path/with/slash")]
[InlineData("filename", "")]
public void RemoveFilename_RemoveFilenameFromDifferentPath_Success(
string absoluteFilename, string result)
{
Assert.Equal(result, absoluteFilename.RemoveFilename());
}
}
}

0 comments on commit 9966944

Please sign in to comment.