Skip to content

Commit

Permalink
Make the tests pass on Unix. (#500)
Browse files Browse the repository at this point in the history
Co-authored-by: Kharitonov, Mark <mark.kharitonov@ceridian.com>
  • Loading branch information
MarkKharitonov and Kharitonov, Mark committed Oct 14, 2022
1 parent d66e56b commit 39e6587
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 58 deletions.
40 changes: 28 additions & 12 deletions tests/RazorLight.Precompile.Tests/FileSystemCachingStrategyTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NUnit.Framework;
using RazorLight.Caching;
using System.Runtime.InteropServices;

namespace RazorLight.Precompile.Tests
{
Expand All @@ -11,19 +12,34 @@ public class FileSystemCachingStrategyTests
SimpleFileCachingStrategy.Instance,
};

private static readonly string[] s_firstSepOptions = { "", "/", "\\" };
private static readonly string[] s_secondSepOptions = { "/", "\\" };
private static readonly string[] s_firstSepOptionsWindows = { "", "/", "\\" };
private static readonly string[] s_secondSepOptionsWindows = { "/", "\\" };
private static readonly string[] s_firstSepOptionsUnix = { "", "/" };
private static readonly string[] s_secondSepOptionsUnix = { "/" };

private static readonly IEnumerable<string[]> s_sepCombinations = GetSeparatorCombinations();

private static IEnumerable<string[]> GetSeparatorCombinations()
{
foreach (var s11 in s_firstSepOptions)
string[] firstSepOptions, secondSepOptions;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
firstSepOptions = s_firstSepOptionsWindows;
secondSepOptions = s_secondSepOptionsWindows;
}
else
{
firstSepOptions = s_firstSepOptionsUnix;
secondSepOptions = s_secondSepOptionsUnix;
}

foreach (var s11 in firstSepOptions)
{
foreach (var s12 in s_firstSepOptions)
foreach (var s12 in firstSepOptions)
{
foreach (var s21 in s_secondSepOptions)
foreach (var s21 in secondSepOptions)
{
foreach (var s22 in s_secondSepOptions)
foreach (var s22 in secondSepOptions)
{
if (s11 != s12 || s21 != s22)
{
Expand All @@ -38,9 +54,9 @@ private static IEnumerable<string[]> GetSeparatorCombinations()
[TestCaseSource(nameof(s_testCases))]
public void DifferentKey(IFileSystemCachingStrategy s)
{
var templateFilePath = "Samples\\folder\\MessageItem.cshtml";
var o1 = s.GetCachedFileInfo("folder\\MessageItem.cshtml", templateFilePath, "X:\\");
var o2 = s.GetCachedFileInfo("MessageItem.cshtml", templateFilePath, "X:\\");
var templateFilePath = "Samples/folder/MessageItem.cshtml";
var o1 = s.GetCachedFileInfo("folder/MessageItem.cshtml", templateFilePath, "X:/");
var o2 = s.GetCachedFileInfo("MessageItem.cshtml", templateFilePath, "X:/");
Assert.AreNotEqual(o1.AssemblyFilePath, o2.AssemblyFilePath);
}

Expand All @@ -65,12 +81,12 @@ public void EquivalentKeySimpleFileCachingStrategy(string[] sepCombination)

private static (string, string) GetAsmFilePaths(IFileSystemCachingStrategy s, string[] sepCombination)
{
var templateFilePath = "Samples\\folder\\MessageItem.cshtml";
var templateFilePath = "Samples/folder/MessageItem.cshtml";
string key1 = $"{sepCombination[0]}folder{sepCombination[1]}MessageItem.cshtml";
string key2 = $"{sepCombination[2]}folder{sepCombination[3]}MessageItem.cshtml";
Assert.AreNotEqual(key1, key2);
var asmFilePath1 = s.GetCachedFileInfo(key1, templateFilePath, "X:\\").AssemblyFilePath;
var asmFilePath2 = s.GetCachedFileInfo(key2, templateFilePath, "X:\\").AssemblyFilePath;
var asmFilePath1 = s.GetCachedFileInfo(key1, templateFilePath, "X:/").AssemblyFilePath;
var asmFilePath2 = s.GetCachedFileInfo(key2, templateFilePath, "X:/").AssemblyFilePath;
return (asmFilePath1, asmFilePath2);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/RazorLight.Precompile.Tests/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static class Helper
public static string RunCommandTrimNewline(params string[] args)
{
var sb = RunCommand(args);
sb.Replace("\r\n", "");
sb.Replace(Environment.NewLine, "");
return sb.ToString();
}

Expand Down
34 changes: 17 additions & 17 deletions tests/RazorLight.Precompile.Tests/PrecompileTestCases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,23 @@ public static void CleanupDlls(string dir)

public static readonly TestCaseData[] TestCases = new TestCaseData[]
{
new("Samples\\WorkItemFields.json", AllDefaults) { TestName = AllDefaults.Name },
new("Samples\\FullMessage.cshtml", AllDefaults) { TestName = AllDefaults.Name },
new("Samples\\folder\\MessageItem.cshtml", AllDefaults) { TestName = AllDefaults.Name },
new("Samples\\WorkItemComment.cshtml", AllDefaults) { TestName = AllDefaults.Name },
new("Samples\\FullMessage.cshtml", WithCache) { TestName = WithCache.Name },
new("Samples\\folder\\MessageItem.cshtml", WithCache) { TestName = WithCache.Name },
new("Samples\\FullMessage.cshtml", WithStrategyFileHash) { TestName = WithStrategyFileHash.Name },
new("Samples\\folder\\MessageItem.cshtml", WithStrategyFileHash) { TestName = WithStrategyFileHash.Name },
new("Samples\\FullMessage.cshtml", WithStrategySimple) { TestName = WithStrategySimple.Name },
new("Samples\\folder\\MessageItem.cshtml", WithStrategySimple) { TestName = WithStrategySimple.Name },
new("Samples\\FullMessage.cshtml", WithCacheAndStrategyFileHash) { TestName = WithCacheAndStrategyFileHash.Name },
new("Samples\\folder\\MessageItem.cshtml", WithCacheAndStrategySimple) { TestName = WithCacheAndStrategySimple.Name },
new("SamplesWithBaseDir\\FullMessage.cshtml", WithBase) { TestName = WithBase.Name },
new("SamplesWithBaseDir\\MessageItem.cshtml", WithBase) { TestName = WithBase.Name },
new("SamplesWithBaseDir\\FullMessage.cshtml", WithCacheAndBase) { TestName = WithCacheAndBase.Name },
new("SamplesWithBaseDir\\MessageItem.cshtml", WithCacheAndBase) { TestName = WithCacheAndBase.Name },
new("SamplesWithBaseDir\\FullMessage.cshtml", WithCacheAndBaseAndStrategySimple) { TestName = WithCacheAndBaseAndStrategySimple.Name },
new("Samples/WorkItemFields.json", AllDefaults) { TestName = AllDefaults.Name },
new("Samples/FullMessage.cshtml", AllDefaults) { TestName = AllDefaults.Name },
new("Samples/folder/MessageItem.cshtml", AllDefaults) { TestName = AllDefaults.Name },
new("Samples/WorkItemComment.cshtml", AllDefaults) { TestName = AllDefaults.Name },
new("Samples/FullMessage.cshtml", WithCache) { TestName = WithCache.Name },
new("Samples/folder/MessageItem.cshtml", WithCache) { TestName = WithCache.Name },
new("Samples/FullMessage.cshtml", WithStrategyFileHash) { TestName = WithStrategyFileHash.Name },
new("Samples/folder/MessageItem.cshtml", WithStrategyFileHash) { TestName = WithStrategyFileHash.Name },
new("Samples/FullMessage.cshtml", WithStrategySimple) { TestName = WithStrategySimple.Name },
new("Samples/folder/MessageItem.cshtml", WithStrategySimple) { TestName = WithStrategySimple.Name },
new("Samples/FullMessage.cshtml", WithCacheAndStrategyFileHash) { TestName = WithCacheAndStrategyFileHash.Name },
new("Samples/folder/MessageItem.cshtml", WithCacheAndStrategySimple) { TestName = WithCacheAndStrategySimple.Name },
new("SamplesWithBaseDir/FullMessage.cshtml", WithBase) { TestName = WithBase.Name },
new("SamplesWithBaseDir/MessageItem.cshtml", WithBase) { TestName = WithBase.Name },
new("SamplesWithBaseDir/FullMessage.cshtml", WithCacheAndBase) { TestName = WithCacheAndBase.Name },
new("SamplesWithBaseDir/MessageItem.cshtml", WithCacheAndBase) { TestName = WithCacheAndBase.Name },
new("SamplesWithBaseDir/FullMessage.cshtml", WithCacheAndBaseAndStrategySimple) { TestName = WithCacheAndBaseAndStrategySimple.Name },
};
}
}
2 changes: 1 addition & 1 deletion tests/RazorLight.Precompile.Tests/PrecompileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace RazorLight.Precompile.Tests
{
public class PrecompileTests
public class PrecompileTests : TestWithCulture
{
[TestCaseSource(typeof(PrecompileTestCases), nameof(PrecompileTestCases.TestCases))]
public void PrecompileFromScratch(string templateFilePath, TestScenario scenario)
Expand Down
26 changes: 11 additions & 15 deletions tests/RazorLight.Precompile.Tests/Render1Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RazorLight.Precompile.Tests
{
public class Render1Tests
public class Render1Tests : TestWithCulture
{
private static TestCaseData T(string templateFilePath, string? jsonQuery, string expected) =>
new(templateFilePath, jsonQuery, expected) { TestName = "{m}({0},{1})" };
Expand All @@ -22,9 +22,8 @@ public class Render1Tests
""resolution:UNRESOLVED"",
""status:OPEN""
]
}
"),
T("folder\\MessageItem.cshtml", "[0]", @"Issue Id: 123
}" + Environment.NewLine),
T("folder/MessageItem.cshtml", "[0]", @"Issue Id: 123
<br />
<a href=""https://www.youtube.com/watch?v=Rhc5jXWu55c&amp;t=4815s"">src/_sqlutil.async.cs:251</a>
<br />
Expand All @@ -39,9 +38,8 @@ public class Render1Tests
First Found Date: 9/9/2021 3:35:49 PM
<br />
<span>Found no annotations.</span>
"),
T("folder\\MessageItem.cshtml", "[1]", @"Issue Id: 987
" + Environment.NewLine),
T("folder/MessageItem.cshtml", "[1]", @"Issue Id: 987
<br />
<a href=""https://www.youtube.com/watch?v=Rhc5jXWu55c&amp;t=4815s"">src/sqlconnectioncontext.cs:35</a>
<br />
Expand All @@ -62,21 +60,19 @@ public class Render1Tests
<tr><td>COMMENT</td><td>3/31/2021 6:42:26 PM</td><td>John Smith</td><td>Mitigation statements reviewed. Recommend for closure and approval</td></tr>
<tr><td>APPDESIGN</td><td>10/22/2020 6:58:56 PM</td><td>Li Jet</td><td>Some explanation</td></tr>
</table>
"),
" + Environment.NewLine),
T("WorkItemComment.cshtml", "[1].annotations[0]", @"Action: APPROVED
Created: 3/31/2021 6:44:12 PM
User name: Michael Jackson
Approved per rationale provided and John Smith&#x27; review and approval on 3/30.
")
" + Environment.NewLine)
};

[TestCaseSource(nameof(s_testCases))]
public void Render(string templateFilePath, string jsonQuery, string expected)
{
templateFilePath = "Samples\\" + templateFilePath;
templateFilePath = "Samples/" + templateFilePath;

string precompiledFilePath = Helper.RunCommandTrimNewline("precompile", "-t", templateFilePath);

Expand All @@ -86,7 +82,7 @@ public void Render(string templateFilePath, string jsonQuery, string expected)
"-p",
precompiledFilePath,
"-m",
"Samples\\FindingsWithSourceCodeInfo.json"
"Samples/FindingsWithSourceCodeInfo.json"
};
if (jsonQuery != null)
{
Expand All @@ -100,15 +96,15 @@ public void Render(string templateFilePath, string jsonQuery, string expected)
[TestCaseSource(nameof(s_testCases))]
public void PrecompileAndRender(string templateFilePath, string jsonQuery, string expected)
{
templateFilePath = "Samples\\" + templateFilePath;
templateFilePath = "Samples/" + templateFilePath;

var commandLineArgs = new List<string>
{
"precompile",
"-t",
templateFilePath,
"-m",
"Samples\\FindingsWithSourceCodeInfo.json"
"Samples/FindingsWithSourceCodeInfo.json"
};
if (jsonQuery != null)
{
Expand Down
19 changes: 9 additions & 10 deletions tests/RazorLight.Precompile.Tests/Render2Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace RazorLight.Precompile.Tests
{
public class Render2Tests
public class Render2Tests : TestWithCulture
{
private static TestCaseData T(string templateFilePath, string templateFilePath2, IFileSystemCachingStrategy s, string expected) =>
new(templateFilePath, templateFilePath2, s, expected) { TestName = "{m}({0},{1},{2})" };

private const string EXPECTED = @"Count of issues with the source information: 2
private static readonly string s_expected = @"Count of issues with the source information: 2
<p>
Issue Id: 123
Expand Down Expand Up @@ -50,13 +50,12 @@ public class Render2Tests
<tr><td>APPDESIGN</td><td>10/22/2020 6:58:56 PM</td><td>Li Jet</td><td>Some explanation</td></tr>
</table>
</p>
";
" + Environment.NewLine;

private static readonly TestCaseData[] s_testCases = new TestCaseData[]
{
T("FullMessage.cshtml", "folder\\MessageItem.cshtml", FileHashCachingStrategy.Instance, EXPECTED),
T("FullMessage.cshtml", "folder\\MessageItem.cshtml", SimpleFileCachingStrategy.Instance, EXPECTED),
T("FullMessage.cshtml", "folder/MessageItem.cshtml", FileHashCachingStrategy.Instance, s_expected),
T("FullMessage.cshtml", "folder/MessageItem.cshtml", SimpleFileCachingStrategy.Instance, s_expected),
};

[SetUp]
Expand Down Expand Up @@ -86,7 +85,7 @@ public void RenderGlobRecursive(string key, string key2, IFileSystemCachingStrat
{
Precompile(key, key2, s);

Run(key, expected, "**\\*.dll");
Run(key, expected, "**/*.dll");
}

[TestCaseSource(nameof(s_testCases))]
Expand All @@ -111,7 +110,7 @@ public void RenderGlobNonRecursive(string key, string key2, IFileSystemCachingSt
{
Precompile(key, key2, s);

var exc = Assert.Throws<RazorLightException>(() => Run(key, expected, "Samples\\*.dll"));
var exc = Assert.Throws<RazorLightException>(() => Run(key, expected, "Samples/*.dll"));
Assert.AreEqual("No precompiled template found for the key /folder/MessageItem.cshtml", exc.Message);
}

Expand All @@ -128,7 +127,7 @@ private static void Run(string key, string expected, string precompiledFilePath,
"-p",
precompiledFilePath,
"-m",
"Samples\\FindingsWithSourceCodeInfo.json",
"Samples/FindingsWithSourceCodeInfo.json",
"-k",
key
};
Expand All @@ -151,7 +150,7 @@ public void PrecompileAndRender(string templateFilePath, string _, IFileSystemCa
"-s",
s.Name,
"-m",
"Samples\\FindingsWithSourceCodeInfo.json"
"Samples/FindingsWithSourceCodeInfo.json"
};

var actual = Helper.RunCommand(commandLineArgs.ToArray()).ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Count of issues with the source information: @Model.Count
@foreach (var item in Model)
{
<p>
@{await IncludeAsync("folder\\MessageItem.cshtml", item);}
@{await IncludeAsync("folder/MessageItem.cshtml", item);}
</p>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Count of issues with the source information: @Model.Count
@foreach (var item in Model)
{
<p>
@{await IncludeAsync("SamplesWithBaseDir\\MessageItem.cshtml", item);}
@{await IncludeAsync("SamplesWithBaseDir/MessageItem.cshtml", item);}
</p>
}
12 changes: 12 additions & 0 deletions tests/RazorLight.Precompile.Tests/TestWithCulture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Globalization;

namespace RazorLight.Precompile.Tests
{
public class TestWithCulture
{
public TestWithCulture()
{
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
}
}
}

0 comments on commit 39e6587

Please sign in to comment.