test(ab-transport): guard libplctag confinement to its adapters (falsified by adding using libplctag to AbCipDriver.cs)
This commit is contained in:
+46
@@ -0,0 +1,46 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport.Tests;
|
||||
|
||||
public sealed class LibplctagConfinementGuardTests
|
||||
{
|
||||
private static readonly Regex Reference = new(
|
||||
@"\busing\s+libplctag\b|\blibplctag\.(Tag|Status|PlcType|Protocol|LibPlcTagException|NativeImport)\b",
|
||||
RegexOptions.Compiled);
|
||||
|
||||
[Fact]
|
||||
public void Only_the_libplctag_adapters_reference_libplctag()
|
||||
{
|
||||
var root = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
while (root is not null && !File.Exists(Path.Combine(root.FullName, "ZB.MOM.WW.OtOpcUa.slnx"))) root = root.Parent;
|
||||
root.ShouldNotBeNull("could not find the repo root");
|
||||
|
||||
var offenders = new List<string>();
|
||||
foreach (var file in Directory.EnumerateFiles(Path.Combine(root.FullName, "src"), "*.cs", SearchOption.AllDirectories))
|
||||
{
|
||||
if (file.Contains($"{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}") ||
|
||||
file.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}")) continue;
|
||||
var name = Path.GetFileName(file);
|
||||
var dir = Path.GetFileName(Path.GetDirectoryName(file)!);
|
||||
var allowed = name.StartsWith("Libplctag", StringComparison.Ordinal)
|
||||
&& dir is "ZB.MOM.WW.OtOpcUa.Driver.AbCip" or "ZB.MOM.WW.OtOpcUa.Driver.AbLegacy";
|
||||
if (allowed) continue;
|
||||
var lines = File.ReadAllLines(file);
|
||||
for (var i = 0; i < lines.Length; i++)
|
||||
{
|
||||
var code = lines[i].Split("//")[0];
|
||||
if (Reference.IsMatch(code)) offenders.Add($"{Path.GetRelativePath(root.FullName, file)}:{i + 1}: {lines[i].Trim()}");
|
||||
}
|
||||
}
|
||||
offenders.ShouldBeEmpty(string.Join(Environment.NewLine, offenders));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Prose_mentions_of_libplctag_do_not_trip_the_guard()
|
||||
{
|
||||
var prose = "via libplctag. Second of six driver CLIs";
|
||||
Reference.IsMatch(prose).ShouldBeFalse();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user