Skip to content
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
1 change: 1 addition & 0 deletions Trax.Dashboard.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
</Folder>
<Folder Name="/tests/">
<Project Path="tests/Trax.Dashboard.Tests.Integration/Trax.Dashboard.Tests.Integration.csproj" />
<Project Path="tests/Trax.Dashboard.Tests.Meta/Trax.Dashboard.Tests.Meta.csproj" />
</Folder>
</Solution>
9 changes: 9 additions & 0 deletions tests/Trax.Dashboard.Tests.Meta/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Text.RegularExpressions;
global using System.Xml.Linq;
global using FluentAssertions;
global using NUnit.Framework;
global using Trax.Dashboard.Tests.Meta.Infrastructure;
28 changes: 28 additions & 0 deletions tests/Trax.Dashboard.Tests.Meta/Infrastructure/RepoRoot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Trax.Dashboard.Tests.Meta.Infrastructure;

internal static class RepoRoot
{
private static readonly Lazy<string> Cached = new(Resolve);

public static string Path => Cached.Value;

public static string Combine(params string[] segments) =>
System.IO.Path.Combine(new[] { Path }.Concat(segments).ToArray());

public static string Relative(string absolute) =>
System.IO.Path.GetRelativePath(Path, absolute);

private static string Resolve()
{
var dir = new DirectoryInfo(AppContext.BaseDirectory);
while (dir is not null)
{
if (dir.EnumerateFiles("*.slnx").Any())
return dir.FullName;
dir = dir.Parent;
}
throw new InvalidOperationException(
$"Could not locate repository root: no .slnx found walking up from '{AppContext.BaseDirectory}'."
);
}
}
43 changes: 43 additions & 0 deletions tests/Trax.Dashboard.Tests.Meta/Infrastructure/SourceFiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Trax.Dashboard.Tests.Meta.Infrastructure;

internal static class SourceFiles
{
public static IEnumerable<string> CSharp(params string[] subdirs) => Enumerate("*.cs", subdirs);

public static IEnumerable<string> Projects(params string[] subdirs) =>
Enumerate("*.csproj", subdirs);

public static IEnumerable<string> Markdown(params string[] subdirs) =>
Enumerate("*.md", subdirs);

private static IEnumerable<string> Enumerate(string pattern, string[] subdirs)
{
var roots =
subdirs.Length == 0
? new[] { RepoRoot.Path }
: subdirs.Select(s => Path.Combine(RepoRoot.Path, s)).ToArray();

foreach (var root in roots)
{
if (!Directory.Exists(root))
continue;
foreach (
var file in Directory.EnumerateFiles(root, pattern, SearchOption.AllDirectories)
)
{
if (IsExcluded(file))
continue;
yield return file;
}
}
}

private static bool IsExcluded(string path)
{
var s = Path.DirectorySeparatorChar;
return path.Contains($"{s}bin{s}", StringComparison.Ordinal)
|| path.Contains($"{s}obj{s}", StringComparison.Ordinal)
|| path.Contains($"{s}node_modules{s}", StringComparison.Ordinal)
|| path.Contains($"{s}.git{s}", StringComparison.Ordinal);
}
}
47 changes: 47 additions & 0 deletions tests/Trax.Dashboard.Tests.Meta/Infrastructure/SourceText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace Trax.Dashboard.Tests.Meta.Infrastructure;

internal static class SourceText
{
private static readonly Regex BlockComment = new(
"/\\*.*?\\*/",
RegexOptions.Singleline | RegexOptions.Compiled
);
private static readonly Regex LineComment = new("//[^\\r\\n]*", RegexOptions.Compiled);
private static readonly Regex VerbatimString = new(
"@\"(?:[^\"]|\"\")*\"",
RegexOptions.Compiled
);
private static readonly Regex InterpolatedVerbatim = new(
"\\$@\"(?:[^\"]|\"\")*\"",
RegexOptions.Compiled
);
private static readonly Regex RegularString = new(
"\"(?:\\\\.|[^\"\\\\])*\"",
RegexOptions.Compiled
);

public static string StripCommentsAndStrings(string source)
{
var s = BlockComment.Replace(source, " ");
s = LineComment.Replace(s, " ");
s = InterpolatedVerbatim.Replace(s, "\"\"");
s = VerbatimString.Replace(s, "\"\"");
s = RegularString.Replace(s, "\"\"");
return s;
}

public static IReadOnlyList<(int LineNumber, string Line)> MatchingLines(
string source,
Regex pattern
)
{
var hits = new List<(int, string)>();
var lines = source.Replace("\r\n", "\n").Split('\n');
for (var i = 0; i < lines.Length; i++)
{
if (pattern.IsMatch(lines[i]))
hits.Add((i + 1, lines[i]));
}
return hits;
}
}
Loading
Loading