fix(cli): resolve CLI-001 — honor SCADALINK_FORMAT and config-file format precedence
This commit is contained in:
54
tests/ScadaLink.CLI.Tests/FormatResolutionTests.cs
Normal file
54
tests/ScadaLink.CLI.Tests/FormatResolutionTests.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System.CommandLine;
|
||||
using ScadaLink.CLI;
|
||||
using ScadaLink.CLI.Commands;
|
||||
|
||||
namespace ScadaLink.CLI.Tests;
|
||||
|
||||
public class FormatResolutionTests
|
||||
{
|
||||
private static (Option<string> formatOption, RootCommand root) BuildHarness()
|
||||
{
|
||||
var formatOption = new Option<string>("--format") { Recursive = true };
|
||||
var root = new RootCommand();
|
||||
root.Add(formatOption);
|
||||
return (formatOption, root);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResolveFormat_ExplicitFlag_OverridesConfig()
|
||||
{
|
||||
var (formatOption, root) = BuildHarness();
|
||||
var result = root.Parse(new[] { "--format", "table" });
|
||||
var config = new CliConfig { DefaultFormat = "json" };
|
||||
|
||||
var format = CommandHelpers.ResolveFormat(result, formatOption, config);
|
||||
|
||||
Assert.Equal("table", format);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResolveFormat_FlagAbsent_UsesConfigDefaultFormat()
|
||||
{
|
||||
// Regression for CLI-001: when --format is not supplied, the config-file /
|
||||
// env-var DefaultFormat must be honoured instead of always falling back to "json".
|
||||
var (formatOption, root) = BuildHarness();
|
||||
var result = root.Parse(Array.Empty<string>());
|
||||
var config = new CliConfig { DefaultFormat = "table" };
|
||||
|
||||
var format = CommandHelpers.ResolveFormat(result, formatOption, config);
|
||||
|
||||
Assert.Equal("table", format);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResolveFormat_FlagAbsent_AndNoConfig_DefaultsToJson()
|
||||
{
|
||||
var (formatOption, root) = BuildHarness();
|
||||
var result = root.Parse(Array.Empty<string>());
|
||||
var config = new CliConfig { DefaultFormat = "json" };
|
||||
|
||||
var format = CommandHelpers.ResolveFormat(result, formatOption, config);
|
||||
|
||||
Assert.Equal("json", format);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user