Skip to content

end to end test with bash script #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added msbuild.binlog
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
using System.Threading.Tasks;
using Xunit.Abstractions;
using static System.Environment;
using System.Runtime.InteropServices;
using Microsoft.DotNet.PlatformAbstractions;

namespace System.CommandLine.Suggest.Tests
{
public class DotnetSuggestEndToEndTests : IDisposable
{
private const string GetCompletionsResultPath = "get_completions_result.txt";
private readonly ITestOutputHelper _output;
private readonly FileInfo _endToEndTestApp;
private readonly FileInfo _dotnetSuggest;
Expand Down Expand Up @@ -155,5 +156,58 @@ await Process.ExecuteAsync(
.Should()
.Be($"--apple{NewLine}--banana{NewLine}--cherry{NewLine}--durian{NewLine}");
}

[ReleaseBuildOnlyFact]
public async Task dotnet_suggest_provides_completions_from_bash_shell_script()
{
if (RuntimeEnvironment.OperatingSystemPlatform == Platform.Windows)
{
return;
}

EnsureNoPreviousGetCompletionsResultPath();

// run once to trigger a call to dotnet-suggest register
await Process.ExecuteAsync(
_endToEndTestApp.FullName,
"-h",
stdOut: s => _output.WriteLine(s),
stdErr: s => _output.WriteLine(s),
environmentVariables: _environmentVariables);

var stdOut = new StringBuilder();
var stdErr = new StringBuilder();

var appendedTestAssetPath =
_endToEndTestApp.Directory.FullName + ":" + GetEnvironmentVariable("PATH");

await Process.ExecuteAsync(
command: "sh",
args: $"get-completions.sh {_endToEndTestApp.Name} --",
stdOut: s => _output.WriteLine(s),
stdErr: s => _output.WriteLine(s),
environmentVariables:
_environmentVariables.Concat(new[] {("PATH", appendedTestAssetPath)}).ToArray());

_output.WriteLine($"stdOut:{NewLine}{stdOut}{NewLine}");
_output.WriteLine($"stdErr:{NewLine}{stdErr}{NewLine}");

stdErr.ToString()
.Should()
.BeEmpty();

// stdOut cannot get echo's result. So it need to write to a file.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Help need. If get-completions.sh simply do an echo. stdOut cannot capture that. But testrunner output has it. I cannot make it work so I end up write to a file.

File.ReadAllText(GetCompletionsResultPath)
.Should()
.Contain("--apple --banana --cherry --durian");
}

private static void EnsureNoPreviousGetCompletionsResultPath()
{
if (File.Exists(GetCompletionsResultPath))
{
File.Delete(GetCompletionsResultPath);
}
}
}
}
19 changes: 8 additions & 11 deletions src/System.CommandLine.Suggest.Tests/dotnet-suggest.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<Content Remove="EndToEndTestApp/**" />
<EmbeddedResource Remove="EndToEndTestApp/**" />
<None Remove="EndToEndTestApp/**" />

<None Update="get-completions.sh">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand All @@ -32,11 +36,10 @@
</ItemGroup>

<Target Name="DotnetSuggestIntegrationTestAssets" BeforeTargets="Build" Condition="'$(Configuration)' == 'Release'">

<PropertyGroup>
<TestAssetsPath>
$([System.IO.Path]::GetFullPath('$(OutputPath)'))/TestAssets
</TestAssetsPath>
<TestAssetsPath>$([System.IO.Path]::GetFullPath('$(OutputPath)'))/TestAssets</TestAssetsPath>
<TestAssetsMsBuildProjectExtensionsPathDotnetSuggest>$([System.IO.Path]::GetFullPath('$(OutputPath)'))/TestAssetsMsBuildProjectExtensionsPath/dotnet-suggest/</TestAssetsMsBuildProjectExtensionsPathDotnetSuggest>
<TestAssetsMsBuildProjectExtensionsPathEndToEndTestApp>$([System.IO.Path]::GetFullPath('$(OutputPath)'))/TestAssetsMsBuildProjectExtensionsPath/EndToEndTestApp/</TestAssetsMsBuildProjectExtensionsPathEndToEndTestApp>
</PropertyGroup>

<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
Expand All @@ -51,12 +54,6 @@
<Rid>linux-x64</Rid>
</PropertyGroup>

<MSBuild BuildInParallel="False"
Projects="../System.CommandLine.Suggest/dotnet-suggest.csproj"
Targets="Restore"
Properties="UseAppHost=true;SelfContained=false;RuntimeIdentifier=$(Rid);ForceRestoreToEvaluateSeparately=1;Configuration=Release">
</MSBuild>

<MSBuild BuildInParallel="False"
Projects="../System.CommandLine.Suggest/dotnet-suggest.csproj"
Targets="Build;Publish"
Expand All @@ -65,7 +62,7 @@

<MSBuild BuildInParallel="False"
Projects="EndToEndTestApp/EndToEndTestApp.csproj"
Targets="Restore"
Targets="Restore"
Properties="UseAppHost=true;SelfContained=false;RuntimeIdentifier=$(Rid);ForceRestoreToEvaluateSeparately=1;Configuration=Release">
</MSBuild>

Expand Down
37 changes: 37 additions & 0 deletions src/System.CommandLine.Suggest.Tests/get-completions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

echo "hi"
get_completion_function_name_from_complete_output()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@omajid THANKS!!!!! IT WORKS!!!!!!!!!!!

{
local output=("$@")
local i
for ((i = 0; i < "${#output[@]}"; i++)); do
if [[ "${output[i]}" == "-F" ]]; then
echo "${output[((i+1))]}"
return 0
fi
done
return 1
}

get_completions()
{
COMP_WORDS=("$@") # completion to test
COMP_LINE="$(local IFS=" "; echo "${COMP_WORDS[@]}")"
COMP_CWORD=$((${#COMP_WORDS[@]} - 1)) # index into COMP_WORDS
COMP_POINT="${#COMP_LINE}" # index of cursor position

source dotnet-suggest-shim.bash

local cmd=${COMP_WORDS[0]}

local output=($(complete -p "$cmd"))
local func=$(get_completion_function_name_from_complete_output "${output[@]}")

#_dotnet_bash_complete
"$func"

echo "${COMPREPLY[@]}" > get_completions_result.txt
}

get_completions "$@"
8 changes: 1 addition & 7 deletions src/System.CommandLine.Suggest/dotnet-suggest-shim.bash
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@ _dotnet_bash_complete()
local suggestions=($(compgen -W "$completions"))

if [ "${#suggestions[@]}" == "1" ]; then
local number="${suggestions[0]/%\ */}"
COMPREPLY=("$number")
COMPREPLY=("${suggestions[0]}")
else
for i in "${!suggestions[@]}"; do
suggestions[$i]="$(printf '%*s' "-$COLUMNS" "${suggestions[$i]}")"
done

COMPREPLY=("${suggestions[@]}")
fi
}

complete -F _dotnet_bash_complete `dotnet-suggest list`
28 changes: 26 additions & 2 deletions src/System.CommandLine.Suggest/dotnet-suggest.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
<Rid>win-x64</Rid>
</PropertyGroup>
<PropertyGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'">
<Rid>osx-x64</Rid>
</PropertyGroup>
<PropertyGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">
<Rid>linux-x64</Rid>
</PropertyGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
Expand All @@ -9,12 +19,17 @@
<ToolCommandName>dotnet-suggest</ToolCommandName>
<PackAsToolShimRuntimeIdentifiers>win-x64;win-x86;osx-x64</PackAsToolShimRuntimeIdentifiers>
<PackagedShimOutputRootDirectory>$(OutputPath)</PackagedShimOutputRootDirectory>
<RuntimeIdentifier>$(Rid)</RuntimeIdentifier>
<SelfContained>false</SelfContained>

<!--warning about carried complete script. They are not used during nuget package-->
<NoWarn>NU5111</NoWarn>
</PropertyGroup>

<PropertyGroup Condition=" $(Build_BuildId) != '' " >
<PropertyGroup Condition=" $(Build_BuildId) != '' ">
<Version>1.0.2.$(Build_BuildId)</Version>
</PropertyGroup>

<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>
Expand All @@ -23,4 +38,13 @@
<ProjectReference Include="..\System.CommandLine\System.CommandLine.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="dotnet-suggest-shim.bash">
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to add auto copy-paste to bashrc in the future. So I let dotnet suggest carry them now to make it easier to test

<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="dotnet-suggest-shim.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>