- 
                Notifications
    
You must be signed in to change notification settings  - Fork 412
 
SuggestionStore::GetCompletions to check exit code of invoked applica… #2160
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
          
     Draft
      
      
            baradgur
  wants to merge
  6
  commits into
  dotnet:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
baradgur:issue-2137
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +247
        
        
          −85
        
        
          
        
      
    
  
  
     Draft
                    Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      d3e8b47
              
                SuggestionStore::GetCompletions to check exit code of invoked applica…
              
              
                baradgur 1c23816
              
                SuggestionStore::GetCompletions to check exit code of invoked applica…
              
              
                baradgur 65a92f5
              
                added tests to SuggestionStore::GetCompletions
              
              
                baradgur d8adba7
              
                use async read for standard output for SuggestionStore::GetCompletions
              
              
                baradgur 8b8e3b7
              
                stopping async read in SuggestionStore::GetCompletions gracefully
              
              
                baradgur 8895dfd
              
                Merge remote-tracking branch 'remotes/origin/main' into issue-2137
              
              
                baradgur File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            45 changes: 45 additions & 0 deletions
          
          45 
        
  src/System.CommandLine.Suggest.Tests/SuggestionStoreTests.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
| 
     | 
||
| using System.CommandLine.Tests.Utility; | ||
| using FluentAssertions; | ||
| using Xunit.Abstractions; | ||
| using static System.Environment; | ||
| 
     | 
||
| namespace System.CommandLine.Suggest.Tests | ||
| { | ||
| public class SuggestionStoreTests : TestsWithTestApps | ||
| { | ||
| public SuggestionStoreTests(ITestOutputHelper output) : base(output) | ||
| { | ||
| } | ||
| 
     | 
||
| [ReleaseBuildOnlyFact] | ||
| public void GetCompletions_obtains_suggestions_successfully() | ||
| { | ||
| var store = new SuggestionStore(); | ||
| var completions = store.GetCompletions(EndToEndTestApp.FullName, "[suggest:1] \"a\"", TimeSpan.FromSeconds(1)); | ||
| completions.Should().Be($"--apple{NewLine}--banana{NewLine}--durian{NewLine}"); | ||
| } | ||
| 
     | 
||
| [ReleaseBuildOnlyFact] | ||
| public void GetCompletions_fails_to_obtain_suggestions_because_app_takes_too_long() | ||
| { | ||
| var store = new SuggestionStore(); | ||
| var appHangingTimeSpanArgument = TimeSpan.FromMilliseconds(2000).ToString(); | ||
| var completions = store | ||
| .GetCompletions(WaitAndFailTestApp.FullName, appHangingTimeSpanArgument, TimeSpan.FromMilliseconds(1000)); | ||
| completions.Should().BeEmpty(); | ||
| } | ||
| 
     | 
||
| [ReleaseBuildOnlyFact] | ||
| public void GetCompletions_fails_to_obtain_suggestions_because_app_exited_with_nonzero_code() | ||
| { | ||
| var store = new SuggestionStore(); | ||
| var appHangingTimeSpanArgument = TimeSpan.FromMilliseconds(0).ToString(); | ||
| var completions = store | ||
| .GetCompletions(WaitAndFailTestApp.FullName, appHangingTimeSpanArgument, TimeSpan.FromMilliseconds(1000)); | ||
| completions.Should().BeEmpty(); | ||
| } | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| using System.IO; | ||
| using System.Linq; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
| 
     | 
||
| namespace System.CommandLine.Suggest.Tests; | ||
| 
     | 
||
| [Collection("TestsWithTestApps")] | ||
| public class TestsWithTestApps : IDisposable | ||
| { | ||
| protected readonly ITestOutputHelper Output; | ||
| protected readonly FileInfo EndToEndTestApp; | ||
| protected readonly FileInfo WaitAndFailTestApp; | ||
| protected readonly FileInfo DotnetSuggest; | ||
| protected readonly (string, string)[] EnvironmentVariables; | ||
| private readonly DirectoryInfo _dotnetHostDir = DotnetMuxer.Path.Directory; | ||
| private static string _testRoot; | ||
| 
     | 
||
| protected TestsWithTestApps(ITestOutputHelper output) | ||
| { | ||
| Output = output; | ||
| 
     | 
||
| // delete sentinel files for TestApps in order to trigger registration when it's run | ||
| var sentinelsDir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "system-commandline-sentinel-files")); | ||
| 
     | 
||
| if (sentinelsDir.Exists) | ||
| { | ||
| var sentinels = sentinelsDir | ||
| .EnumerateFiles() | ||
| .Where(f => f.Name.Contains("EndToEndTestApp") || f.Name.Contains("WaitAndFailTestApp")); | ||
| 
     | 
||
| foreach (var sentinel in sentinels) | ||
| { | ||
| sentinel.Delete(); | ||
| } | ||
| } | ||
| 
     | 
||
| var currentDirectory = Path.Combine( | ||
| Directory.GetCurrentDirectory(), | ||
| "TestAssets"); | ||
| 
     | 
||
| EndToEndTestApp = new DirectoryInfo(currentDirectory) | ||
| .GetFiles("EndToEndTestApp".ExecutableName()) | ||
| .SingleOrDefault(); | ||
| 
     | 
||
| WaitAndFailTestApp = new DirectoryInfo(currentDirectory) | ||
| .GetFiles("WaitAndFailTestApp".ExecutableName()) | ||
| .SingleOrDefault(); | ||
| 
     | 
||
| DotnetSuggest = new DirectoryInfo(currentDirectory) | ||
| .GetFiles("dotnet-suggest".ExecutableName()) | ||
| .SingleOrDefault(); | ||
| 
     | 
||
| PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome(); | ||
| 
     | 
||
| EnvironmentVariables = new[] { | ||
| ("DOTNET_ROOT", _dotnetHostDir.FullName), | ||
| ("INTERNAL_TEST_DOTNET_SUGGEST_HOME", _testRoot)}; | ||
| } | ||
| 
     | 
||
| public void Dispose() | ||
| { | ||
| if (_testRoot != null && Directory.Exists(_testRoot)) | ||
| { | ||
| Directory.Delete(_testRoot, recursive: true); | ||
| } | ||
| } | ||
| 
     | 
||
| private static void PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome() | ||
| { | ||
| _testRoot = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
| Directory.CreateDirectory(_testRoot); | ||
| } | ||
| } | ||
        
          
          
            25 changes: 25 additions & 0 deletions
          
          25 
        
  src/System.CommandLine.Suggest.Tests/WaitAndFailTestApp/Program.cs
  
  
      
      
   
        
      
      
    | 
                       There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure whether that solution with the faulted console app was the right way, honestly.  | 
            
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| 
     | 
||
| namespace WaitAndFailTestApp; | ||
| 
     | 
||
| public class Program | ||
| { | ||
| private static TimeSpan defaultWait = TimeSpan.FromMilliseconds(3000); | ||
| 
     | 
||
| //we should not be able to receive any suggestion from this test app, | ||
| //so we are not constructing it using CliConfiguration | ||
| 
     | 
||
| static async Task Main(string[] args) | ||
| { | ||
| var waitPeriod = args.Length > 0 && int.TryParse(args[0], out var millisecondsToWaitParsed) | ||
| ? TimeSpan.FromMilliseconds(millisecondsToWaitParsed) | ||
| : defaultWait; | ||
| 
     | 
||
| await Task.Delay(waitPeriod); | ||
| Environment.ExitCode = 1; | ||
| 
     | 
||
| Console.WriteLine("this 'suggestion' is provided too late and/or with invalid app exit code"); | ||
| } | ||
| } | ||
| 
     | 
        
          
          
            12 changes: 12 additions & 0 deletions
          
          12 
        
  src/System.CommandLine.Suggest.Tests/WaitAndFailTestApp/WaitAndFailTestApp.csproj
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| 
     | 
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\System.CommandLine\System.CommandLine.csproj" /> | ||
| </ItemGroup> | ||
| 
     | 
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
| </PropertyGroup> | ||
| 
     | 
||
| </Project> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this attribute is inherited, so both SuggestionStoreTests and DotnetSuggestEndToEndTests will run in the same collection, meaning - not in parallel. This allows the gracefull cleanup of TestsApp files.