Skip to content

Commit f444ab9

Browse files
committed
Initial basic snapshot tests for bash and pwsh
1 parent 5d2fcbc commit f444ab9

12 files changed

+440
-1
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace System.CommandLine.StaticCompletions.Tests;
5+
6+
using System.CommandLine.StaticCompletions.Shells;
7+
using Xunit;
8+
9+
public class BashShellProviderTests
10+
{
11+
private readonly VerifySettings _settings;
12+
public BashShellProviderTests()
13+
{
14+
_settings = new VerifySettings();
15+
_settings.UseDirectory("snapshots/bash");
16+
17+
18+
}
19+
[Fact]
20+
public async Task GenericCompletions()
21+
{
22+
var provider = new BashShellProvider();
23+
var completions = provider.GenerateCompletions(new("mycommand"));
24+
await Verify(target: completions, extension: "sh", settings: _settings);
25+
}
26+
27+
[Fact]
28+
public async Task SimpleOptionCompletion()
29+
{
30+
var provider = new BashShellProvider();
31+
var completions = provider.GenerateCompletions(new("mycommand") {
32+
new CliOption<string>("--name")
33+
});
34+
await Verify(target: completions, extension: "sh", settings: _settings);
35+
}
36+
37+
[Fact]
38+
public async Task SubcommandAndOptionInTopLevelList()
39+
{
40+
var provider = new BashShellProvider();
41+
var completions = provider.GenerateCompletions(
42+
new("mycommand") {
43+
new CliOption<string>("--name"),
44+
new CliCommand("subcommand")
45+
}
46+
);
47+
await Verify(target: completions, extension: "sh", settings: _settings);
48+
}
49+
50+
[Fact]
51+
public async Task NestedSubcommandCompletion()
52+
{
53+
var provider = new BashShellProvider();
54+
var completions = provider.GenerateCompletions(new("mycommand") {
55+
new CliCommand("subcommand") {
56+
new CliCommand("nested")
57+
}
58+
});
59+
await Verify(target: completions, extension: "sh", settings: _settings);
60+
}
61+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace System.CommandLine.StaticCompletions.Tests;
5+
6+
using System.CommandLine.StaticCompletions.Shells;
7+
using EmptyFiles;
8+
using Xunit;
9+
10+
public class PowershellProviderTests
11+
{
12+
private readonly VerifySettings _settings;
13+
public PowershellProviderTests()
14+
{
15+
_settings = new VerifySettings();
16+
_settings.UseDirectory("snapshots/pwsh");
17+
FileExtensions.AddTextExtension("ps1");
18+
19+
20+
}
21+
[Fact]
22+
public async Task GenericCompletions()
23+
{
24+
var provider = new PowershellShellProvider();
25+
var completions = provider.GenerateCompletions(new("mycommand"));
26+
await Verify(target: completions, extension: "ps1", settings: _settings);
27+
}
28+
29+
[Fact]
30+
public async Task SimpleOptionCompletion()
31+
{
32+
var provider = new PowershellShellProvider();
33+
var completions = provider.GenerateCompletions(new("mycommand") {
34+
new CliOption<string>("--name")
35+
});
36+
await Verify(target: completions, extension: "ps1", settings: _settings);
37+
}
38+
39+
[Fact]
40+
public async Task SubcommandAndOptionInTopLevelList()
41+
{
42+
var provider = new PowershellShellProvider();
43+
var completions = provider.GenerateCompletions(
44+
new("mycommand") {
45+
new CliOption<string>("--name"),
46+
new CliCommand("subcommand")
47+
}
48+
);
49+
await Verify(target: completions, extension: "ps1", settings: _settings);
50+
}
51+
52+
[Fact]
53+
public async Task NestedSubcommandCompletion()
54+
{
55+
var provider = new PowershellShellProvider();
56+
var completions = provider.GenerateCompletions(new("mycommand") {
57+
new CliCommand("subcommand") {
58+
new CliCommand("nested")
59+
}
60+
});
61+
await Verify(target: completions, extension: "ps1", settings: _settings);
62+
}
63+
}

test/System.CommandLine.StaticCompletions.Tests/System.CommandLine.StaticCompletions.Tests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
I don't want that. This should be able to stand alone. -->
1414
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.24528.1"/>
1515
<PackageReference Include="FluentAssertions" Version="6.12.0" />
16-
<PackageReference Include="XUnit" Version="2.4.2" />
16+
<PackageReference Include="XUnit" Version="2.9.0" />
1717
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
1818
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0-preview-24610-01" />
1919
<PackageReference Include="Microsoft.TestPlatform" Version="17.13.0-preview-24610-01" />
20+
<PackageReference Include="Verify.Xunit" Version="25.0.2" />
21+
<PackageReference Include="Verify.DiffPlex" Version="3.0.0" />
2022
</ItemGroup>
2123

2224
<ItemGroup>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
namespace System.CommandLine.StaticCompletions.Tests;
3+
4+
using System.Runtime.CompilerServices;
5+
6+
public static class VerifyConfiguration
7+
{
8+
[ModuleInitializer]
9+
public static void Initialize()
10+
{
11+
VerifyDiffPlex.Initialize();
12+
Verifier.UseProjectRelativeDirectory("snapshots");
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#! /bin/bash
2+
_mycommand() {
3+
4+
cur="${COMP_WORDS[COMP_CWORD]}"
5+
prev="${COMP_WORDS[COMP_CWORD-1]}"
6+
COMPREPLY=()
7+
8+
opts=""
9+
10+
if [[ $COMP_CWORD == "1" ]]; then
11+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
12+
return
13+
fi
14+
15+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
16+
}
17+
18+
19+
20+
complete -F _mycommand mycommand
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#! /bin/bash
2+
_mycommand() {
3+
4+
cur="${COMP_WORDS[COMP_CWORD]}"
5+
prev="${COMP_WORDS[COMP_CWORD-1]}"
6+
COMPREPLY=()
7+
8+
opts="subcommand"
9+
10+
if [[ $COMP_CWORD == "1" ]]; then
11+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
12+
return
13+
fi
14+
15+
case ${COMP_WORDS[1]} in
16+
(subcommand)
17+
_mycommand_subcommand 2
18+
return
19+
;;
20+
21+
esac
22+
23+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
24+
}
25+
26+
_mycommand_subcommand() {
27+
28+
cur="${COMP_WORDS[COMP_CWORD]}"
29+
prev="${COMP_WORDS[COMP_CWORD-1]}"
30+
COMPREPLY=()
31+
32+
opts="nested"
33+
34+
if [[ $COMP_CWORD == "$1" ]]; then
35+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
36+
return
37+
fi
38+
39+
case ${COMP_WORDS[$1]} in
40+
(nested)
41+
_mycommand_subcommand_nested $(($1+1))
42+
return
43+
;;
44+
45+
esac
46+
47+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
48+
}
49+
50+
_mycommand_subcommand_nested() {
51+
52+
cur="${COMP_WORDS[COMP_CWORD]}"
53+
prev="${COMP_WORDS[COMP_CWORD-1]}"
54+
COMPREPLY=()
55+
56+
opts=""
57+
58+
if [[ $COMP_CWORD == "$1" ]]; then
59+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
60+
return
61+
fi
62+
63+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
64+
}
65+
66+
67+
68+
complete -F _mycommand mycommand
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#! /bin/bash
2+
_mycommand() {
3+
4+
cur="${COMP_WORDS[COMP_CWORD]}"
5+
prev="${COMP_WORDS[COMP_CWORD-1]}"
6+
COMPREPLY=()
7+
8+
opts="--name"
9+
10+
if [[ $COMP_CWORD == "1" ]]; then
11+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
12+
return
13+
fi
14+
15+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
16+
}
17+
18+
19+
20+
complete -F _mycommand mycommand
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#! /bin/bash
2+
_mycommand() {
3+
4+
cur="${COMP_WORDS[COMP_CWORD]}"
5+
prev="${COMP_WORDS[COMP_CWORD-1]}"
6+
COMPREPLY=()
7+
8+
opts="subcommand --name"
9+
10+
if [[ $COMP_CWORD == "1" ]]; then
11+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
12+
return
13+
fi
14+
15+
case ${COMP_WORDS[1]} in
16+
(subcommand)
17+
_mycommand_subcommand 2
18+
return
19+
;;
20+
21+
esac
22+
23+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
24+
}
25+
26+
_mycommand_subcommand() {
27+
28+
cur="${COMP_WORDS[COMP_CWORD]}"
29+
prev="${COMP_WORDS[COMP_CWORD-1]}"
30+
COMPREPLY=()
31+
32+
opts=""
33+
34+
if [[ $COMP_CWORD == "$1" ]]; then
35+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
36+
return
37+
fi
38+
39+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
40+
}
41+
42+
43+
44+
complete -F _mycommand mycommand
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using namespace System.Management.Automation
2+
using namespace System.Management.Automation.Language
3+
4+
Register-ArgumentCompleter -Native -CommandName 'mycommand' -ScriptBlock {
5+
param($wordToComplete, $commandAst, $cursorPosition)
6+
7+
$commandElements = $commandAst.CommandElements
8+
$command = @(
9+
'mycommand'
10+
for ($i = 1; $i -lt $commandElements.Count; $i++) {
11+
$element = $commandElements[$i]
12+
if ($element -isnot [StringConstantExpressionAst] -or
13+
$element.StringConstantType -ne [StringConstantType]::BareWord -or
14+
$element.Value.StartsWith('-') -or
15+
$element.Value -eq $wordToComplete) {
16+
break
17+
}
18+
$element.Value
19+
}) -join ';'
20+
21+
$completions = @()
22+
switch ($command) {
23+
'mycommand' {
24+
$staticCompletions = @(
25+
)
26+
$completions += $staticCompletions
27+
break
28+
}
29+
}
30+
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } | Sort-Object -Property ListItemText
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using namespace System.Management.Automation
2+
using namespace System.Management.Automation.Language
3+
4+
Register-ArgumentCompleter -Native -CommandName 'mycommand' -ScriptBlock {
5+
param($wordToComplete, $commandAst, $cursorPosition)
6+
7+
$commandElements = $commandAst.CommandElements
8+
$command = @(
9+
'mycommand'
10+
for ($i = 1; $i -lt $commandElements.Count; $i++) {
11+
$element = $commandElements[$i]
12+
if ($element -isnot [StringConstantExpressionAst] -or
13+
$element.StringConstantType -ne [StringConstantType]::BareWord -or
14+
$element.Value.StartsWith('-') -or
15+
$element.Value -eq $wordToComplete) {
16+
break
17+
}
18+
$element.Value
19+
}) -join ';'
20+
21+
$completions = @()
22+
switch ($command) {
23+
'mycommand' {
24+
$staticCompletions = @(
25+
[CompletionResult]::new('subcommand', 'subcommand', [CompletionResultType]::ParameterValue, "subcommand")
26+
)
27+
$completions += $staticCompletions
28+
break
29+
}
30+
'mycommand;subcommand' {
31+
$staticCompletions = @(
32+
[CompletionResult]::new('nested', 'nested', [CompletionResultType]::ParameterValue, "nested")
33+
)
34+
$completions += $staticCompletions
35+
break
36+
}
37+
'mycommand;subcommand;nested' {
38+
$staticCompletions = @(
39+
)
40+
$completions += $staticCompletions
41+
break
42+
}
43+
}
44+
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } | Sort-Object -Property ListItemText
45+
}

0 commit comments

Comments
 (0)