Skip to content

Commit 1813b56

Browse files
committed
added tests to SuggestionStore::GetCompletions
Fix dotnet#2147
1 parent 976e4c0 commit 1813b56

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System.IO;
2+
using System.Linq;
3+
using Xunit;
4+
using Xunit.Abstractions;
5+
6+
namespace System.CommandLine.Suggest.Tests;
7+
8+
[Collection("TestsWithTestApps")]
9+
public class TestsWithTestApps : IDisposable
10+
{
11+
protected readonly ITestOutputHelper Output;
12+
protected readonly FileInfo EndToEndTestApp;
13+
protected readonly FileInfo WaitAndFailTestApp;
14+
protected readonly FileInfo DotnetSuggest;
15+
protected readonly (string, string)[] EnvironmentVariables;
16+
private readonly DirectoryInfo _dotnetHostDir = DotnetMuxer.Path.Directory;
17+
private static string _testRoot;
18+
19+
protected TestsWithTestApps(ITestOutputHelper output)
20+
{
21+
Output = output;
22+
23+
// delete sentinel files for TestApps in order to trigger registration when it's run
24+
var sentinelsDir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "system-commandline-sentinel-files"));
25+
26+
if (sentinelsDir.Exists)
27+
{
28+
var sentinels = sentinelsDir
29+
.EnumerateFiles()
30+
.Where(f => f.Name.Contains("EndToEndTestApp") || f.Name.Contains("WaitAndFailTestApp"));
31+
32+
foreach (var sentinel in sentinels)
33+
{
34+
sentinel.Delete();
35+
}
36+
}
37+
38+
var currentDirectory = Path.Combine(
39+
Directory.GetCurrentDirectory(),
40+
"TestAssets");
41+
42+
EndToEndTestApp = new DirectoryInfo(currentDirectory)
43+
.GetFiles("EndToEndTestApp".ExecutableName())
44+
.SingleOrDefault();
45+
46+
WaitAndFailTestApp = new DirectoryInfo(currentDirectory)
47+
.GetFiles("WaitAndFailTestApp".ExecutableName())
48+
.SingleOrDefault();
49+
50+
DotnetSuggest = new DirectoryInfo(currentDirectory)
51+
.GetFiles("dotnet-suggest".ExecutableName())
52+
.SingleOrDefault();
53+
54+
PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome();
55+
56+
EnvironmentVariables = new[] {
57+
("DOTNET_ROOT", _dotnetHostDir.FullName),
58+
("INTERNAL_TEST_DOTNET_SUGGEST_HOME", _testRoot)};
59+
}
60+
61+
public void Dispose()
62+
{
63+
if (_testRoot != null && Directory.Exists(_testRoot))
64+
{
65+
Directory.Delete(_testRoot, recursive: true);
66+
}
67+
}
68+
69+
private static void PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome()
70+
{
71+
_testRoot = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
72+
Directory.CreateDirectory(_testRoot);
73+
}
74+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace WaitAndFailTestApp;
5+
6+
public class Program
7+
{
8+
private static TimeSpan defaultWait = TimeSpan.FromMilliseconds(3000);
9+
10+
//we should not be able to receive any suggestion from this test app,
11+
//so we are not constructing it using CliConfiguration
12+
13+
static async Task Main(string[] args)
14+
{
15+
var waitPeriod = args.Length > 0 && int.TryParse(args[0], out var millisecondsToWaitParsed)
16+
? TimeSpan.FromMilliseconds(millisecondsToWaitParsed)
17+
: defaultWait;
18+
19+
await Task.Delay(waitPeriod);
20+
Environment.ExitCode = 1;
21+
22+
Console.WriteLine("this 'suggestion' is provided too late and/or with invalid app exit code");
23+
}
24+
}
25+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\System.CommandLine\System.CommandLine.csproj" />
5+
</ItemGroup>
6+
7+
<PropertyGroup>
8+
<OutputType>Exe</OutputType>
9+
<TargetFramework>net7.0</TargetFramework>
10+
</PropertyGroup>
11+
12+
</Project>

src/System.CommandLine.Suggest.Tests/dotnet-suggest.Tests.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
<Content Remove="EndToEndTestApp/**" />
1414
<EmbeddedResource Remove="EndToEndTestApp/**" />
1515
<None Remove="EndToEndTestApp/**" />
16+
17+
<Compile Remove="WaitAndFailTestApp/**" />
18+
<Content Remove="WaitAndFailTestApp/**" />
19+
<EmbeddedResource Remove="WaitAndFailTestApp/**" />
20+
<None Remove="WaitAndFailTestApp/**" />
1621
</ItemGroup>
1722

1823
<ItemGroup>
@@ -67,6 +72,12 @@
6772

6873
<MSBuild BuildInParallel="False" Projects="EndToEndTestApp/EndToEndTestApp.csproj" Targets="Build;Publish" Properties="UseAppHost=true;SelfContained=false;RuntimeIdentifier=$(Rid);PublishDir=$(TestAssetsPath);Configuration=Release">
6974
</MSBuild>
75+
76+
<MSBuild BuildInParallel="False" Projects="WaitAndFailTestApp/WaitAndFailTestApp.csproj" Targets="Restore" Properties="UseAppHost=true;SelfContained=false;RuntimeIdentifier=$(Rid);ForceRestoreToEvaluateSeparately=1;Configuration=Release">
77+
</MSBuild>
78+
79+
<MSBuild BuildInParallel="False" Projects="WaitAndFailTestApp/WaitAndFailTestApp.csproj" Targets="Build;Publish" Properties="UseAppHost=true;SelfContained=false;RuntimeIdentifier=$(Rid);PublishDir=$(TestAssetsPath);Configuration=Release">
80+
</MSBuild>
7081

7182
</Target>
7283

0 commit comments

Comments
 (0)