-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCompleteCommandFixture.cs
More file actions
139 lines (121 loc) · 4.04 KB
/
CompleteCommandFixture.cs
File metadata and controls
139 lines (121 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.IO;
using System.Threading.Tasks;
using NSubstitute;
using NUnit.Framework;
using Octopus.CommandLine;
using Octopus.CommandLine.Commands;
using Serilog;
using Shouldly;
namespace Tests.Commands;
[TestFixture]
public class CompleteCommandFixture
{
CompleteCommand completeCommand;
ICommandOutputProvider commandOutputProvider;
ILogger logger;
TextWriter originalOutput;
StringWriter output;
ICommandLocator commandLocator;
[SetUp]
public void SetUp()
{
originalOutput = Console.Out;
output = new StringWriter();
Console.SetOut(output);
commandLocator = Substitute.For<ICommandLocator>();
logger = new LoggerConfiguration().WriteTo.TextWriter(output).CreateLogger();
commandOutputProvider = new CommandOutputProvider("TestApp", "1.0.0", new DefaultCommandOutputJsonSerializer(), logger);
commandLocator.List()
.Returns([
new CommandAttribute("test"),
new CommandAttribute("help")
]);
var helpCommand = new HelpCommand(new Lazy<ICommandLocator>(() => commandLocator), commandOutputProvider);
var testCommand = new TestCommand(commandOutputProvider);
commandLocator.Find("help").Returns(helpCommand);
commandLocator.Find("test").Returns(testCommand);
completeCommand = new CompleteCommand(new Lazy<ICommandLocator>(() => commandLocator), commandOutputProvider);
}
[Test]
public async Task ShouldReturnSubCommandSuggestions()
{
await completeCommand.Execute(["he"]);
output.ToString()
.ShouldSatisfyAllConditions(
actual => actual.ShouldContain("help"),
actual => actual.ShouldNotContain("test")
);
}
[Test]
public async Task ShouldReturnParameterSuggestions()
{
await completeCommand.Execute(["test", "--ap"]);
output.ToString()
.ShouldContain("--apiKey");
}
[Test]
public async Task ShouldReturnCommonOptionsWhenSingleEmptyParameter()
{
await completeCommand.Execute(["--"]);
output.ToString()
.ShouldContain("--helpOutputFormat");
}
[Test]
public async Task ShouldReturnOptionSuggestions()
{
await completeCommand.Execute(["--helpOut"]);
output.ToString()
.ShouldSatisfyAllConditions(
actual => actual.ShouldContain("--helpOutputFormat"),
actual => actual.ShouldNotContain("--help\n")
);
}
[Test]
public async Task ShouldReturnAllSubCommandsWhenEmptyArguments()
{
await completeCommand.Execute([""]);
output.ToString()
.ShouldSatisfyAllConditions(
actual => actual.ShouldContain("help"),
actual => actual.ShouldContain("test")
);
}
[Test]
public async Task ShouldStopSubCommandCompletionAfterOptionSuggestion()
{
await completeCommand.Execute(["test", "--api", "API-KEY", "--u"]);
output.ToString()
.ShouldContain("--url");
}
[Test]
[TestCase("--help")]
[TestCase("foo --help")]
public async Task SupportsHelpOption(string commandLine)
{
await completeCommand.Execute(commandLine.Split(' '));
output.ToString()
.ShouldContain("Where <command> is the current command line to filter auto-completions");
}
[TearDown]
public void TearDown()
{
Console.SetOut(originalOutput);
}
}
[Command("test", Description = "test command")]
public class TestCommand : CommandBase
{
public TestCommand(ICommandOutputProvider commandOutputProvider) : base(commandOutputProvider)
{
var options = Options.For("Test group");
options.Add<string>("apiKey", "api key", v => ApiKey = v);
options.Add<string>("url", "url", v => Url = v);
}
public string Url { get; set; }
public string ApiKey { get; set; }
public override Task Execute(string[] commandLineArguments)
{
return Task.Run(() => 0);
}
}