diff --git a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport.Tests/LibplctagConfinementGuardTests.cs b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport.Tests/LibplctagConfinementGuardTests.cs new file mode 100644 index 00000000..dae8c0b0 --- /dev/null +++ b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport.Tests/LibplctagConfinementGuardTests.cs @@ -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(); + 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(); + } +}