fix(driver-s7-cli): resolve Low code-review findings (Driver.S7.Cli-004,005,006,007)
- Driver.S7.Cli-004: 'await using var driver' is the sole driver disposal path; dropped the redundant explicit await ShutdownAsync from each command's finally. - Driver.S7.Cli-005: deleted the stale empty tests/ZB.MOM.WW.OtOpcUa.Driver.S7.Cli.Tests/ directory (the real test project lives under tests/Drivers/Cli/). - Driver.S7.Cli-006: S7CommandBaseBuildOptionsTests cover the probe toggle, timeout mapping, host/port/CPU/rack/slot wiring, and tag list passthrough. - Driver.S7.Cli-007: re-added the SubscribeCommand handler comment explaining the CliFx IConsole.Output usage and that the poll-thread raises events. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.S7.Cli.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Driver.S7.Cli-004: every S7 CLI command must own one disposal mechanism for the
|
||||
/// <c>S7Driver</c>, not two. The chosen mechanism is <c>await using var driver = ...</c>
|
||||
/// — <c>S7Driver.DisposeAsync</c> already calls <c>ShutdownAsync</c>, so an additional
|
||||
/// explicit <c>driver.ShutdownAsync(...)</c> in a <c>finally</c> block runs shutdown
|
||||
/// twice (three times on subscribe). These tests guard against that regression by
|
||||
/// scanning the command source files.
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class CommandDisposalConventionsTests
|
||||
{
|
||||
private static readonly string CommandsDir = LocateCommandsDir();
|
||||
|
||||
[Theory]
|
||||
[InlineData("ProbeCommand.cs")]
|
||||
[InlineData("ReadCommand.cs")]
|
||||
[InlineData("WriteCommand.cs")]
|
||||
[InlineData("SubscribeCommand.cs")]
|
||||
public void Command_does_not_call_ShutdownAsync_explicitly(string commandFile)
|
||||
{
|
||||
var path = Path.Combine(CommandsDir, commandFile);
|
||||
File.Exists(path).ShouldBeTrue($"Expected {path} to exist.");
|
||||
var source = File.ReadAllText(path);
|
||||
|
||||
// The await-using statement is the single disposal mechanism. An explicit
|
||||
// driver.ShutdownAsync(...) call (typically inside a finally block) re-invokes
|
||||
// a shutdown path that DisposeAsync already runs and is the smell -004 flags.
|
||||
source.ShouldNotContain("driver.ShutdownAsync(");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("ProbeCommand.cs")]
|
||||
[InlineData("ReadCommand.cs")]
|
||||
[InlineData("WriteCommand.cs")]
|
||||
[InlineData("SubscribeCommand.cs")]
|
||||
public void Command_uses_await_using_for_S7Driver(string commandFile)
|
||||
{
|
||||
var path = Path.Combine(CommandsDir, commandFile);
|
||||
var source = File.ReadAllText(path);
|
||||
|
||||
source.ShouldContain("await using var driver = new S7Driver(");
|
||||
}
|
||||
|
||||
private static string LocateCommandsDir()
|
||||
{
|
||||
// Walk up from the test assembly bin/ folder to the repo root, then into the
|
||||
// source project's Commands/ directory. The test-host puts CWD somewhere under
|
||||
// bin/Debug/net10.0 so we resolve relative to AppContext.BaseDirectory.
|
||||
var dir = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "ZB.MOM.WW.OtOpcUa.slnx")))
|
||||
dir = dir.Parent;
|
||||
dir.ShouldNotBeNull("Could not find solution root (ZB.MOM.WW.OtOpcUa.slnx).");
|
||||
return Path.Combine(
|
||||
dir!.FullName, "src", "Drivers", "Cli", "ZB.MOM.WW.OtOpcUa.Driver.S7.Cli", "Commands");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Infrastructure;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Driver.S7.Cli;
|
||||
using S7NetCpuType = global::S7.Net.CpuType;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.S7.Cli.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Covers <see cref="S7CommandBase.BuildOptions"/> — the pure, deterministic mapping
|
||||
/// from the base's host/port/CPU/rack/slot/timeout flags onto an
|
||||
/// <c>S7DriverOptions</c>. The CLI is one-shot so the background connectivity probe
|
||||
/// must be disabled.
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class S7CommandBaseBuildOptionsTests
|
||||
{
|
||||
// Test-only S7CommandBase concrete subclass that exposes the protected BuildOptions
|
||||
// helper. The [Command] attribute is required by the CliFx analyzer
|
||||
// (CliFx_CommandMustBeAnnotated) — this command is never registered with the CLI app
|
||||
// but the analyzer rule fires for every ICommand implementor in the compilation.
|
||||
[Command("noop-test", Description = "Test-only probe of S7CommandBase.BuildOptions.")]
|
||||
private sealed class ProbeOnly : S7CommandBase
|
||||
{
|
||||
public override ValueTask ExecuteAsync(IConsole console) => default;
|
||||
public S7DriverOptions Invoke(IReadOnlyList<S7TagDefinition> tags) => BuildOptions(tags);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildOptions_disables_probe_for_one_shot_cli_runs()
|
||||
{
|
||||
var sut = new ProbeOnly
|
||||
{
|
||||
Host = "10.0.0.5",
|
||||
Port = 102,
|
||||
CpuType = S7NetCpuType.S71500,
|
||||
Rack = 0,
|
||||
Slot = 0,
|
||||
TimeoutMs = 5000,
|
||||
};
|
||||
|
||||
var options = sut.Invoke([]);
|
||||
|
||||
options.Probe.ShouldNotBeNull();
|
||||
options.Probe.Enabled.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildOptions_maps_TimeoutMs_to_Timeout_TimeSpan()
|
||||
{
|
||||
var sut = new ProbeOnly { Host = "h", TimeoutMs = 7500 };
|
||||
|
||||
var options = sut.Invoke([]);
|
||||
|
||||
options.Timeout.ShouldBe(TimeSpan.FromMilliseconds(7500));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildOptions_flows_host_port_cpu_rack_slot_through()
|
||||
{
|
||||
var sut = new ProbeOnly
|
||||
{
|
||||
Host = "plc.shop.local",
|
||||
Port = 4102,
|
||||
CpuType = S7NetCpuType.S7300,
|
||||
Rack = 1,
|
||||
Slot = 2,
|
||||
TimeoutMs = 3000,
|
||||
};
|
||||
|
||||
var options = sut.Invoke([]);
|
||||
|
||||
options.Host.ShouldBe("plc.shop.local");
|
||||
options.Port.ShouldBe(4102);
|
||||
options.CpuType.ShouldBe(S7NetCpuType.S7300);
|
||||
options.Rack.ShouldBe((short)1);
|
||||
options.Slot.ShouldBe((short)2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildOptions_forwards_tag_list_verbatim()
|
||||
{
|
||||
var sut = new ProbeOnly { Host = "h" };
|
||||
var tag = new S7TagDefinition("t", "MW0", S7DataType.Int16, Writable: false);
|
||||
|
||||
var options = sut.Invoke([tag]);
|
||||
|
||||
options.Tags.Count.ShouldBe(1);
|
||||
options.Tags[0].ShouldBeSameAs(tag);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.S7.Cli.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Driver.S7.Cli-007: the S7 subscribe command — a near-verbatim copy of the Modbus
|
||||
/// subscribe command — must keep the comment that explains why <c>OnDataChange</c>
|
||||
/// uses <c>console.Output.WriteLine</c> (synchronous, on a driver background thread)
|
||||
/// instead of <c>System.Console</c> or the async <c>WriteLineAsync</c>. The rationale
|
||||
/// is non-obvious to a reader and the Modbus copy carries it; the S7 copy must too.
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class SubscribeCommandConsoleHandlerCommentTests
|
||||
{
|
||||
[Fact]
|
||||
public void SubscribeCommand_explains_why_OnDataChange_uses_console_Output_synchronously()
|
||||
{
|
||||
var dir = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "ZB.MOM.WW.OtOpcUa.slnx")))
|
||||
dir = dir.Parent;
|
||||
dir.ShouldNotBeNull();
|
||||
var source = File.ReadAllText(Path.Combine(
|
||||
dir!.FullName, "src", "Drivers", "Cli", "ZB.MOM.WW.OtOpcUa.Driver.S7.Cli",
|
||||
"Commands", "SubscribeCommand.cs"));
|
||||
|
||||
// The comment must reference the CliFx console abstraction so future copy-pastes
|
||||
// do not lose the rationale.
|
||||
source.ShouldContain("CliFx console");
|
||||
source.ShouldContain("IConsole");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user