| |
| |
|
|
| using System; |
| using System.Collections.Generic; |
| using System.IO; |
| using System.Management.Automation; |
| using System.Management.Automation.Subsystem; |
| using System.Management.Automation.Subsystem.Feedback; |
| using System.Threading; |
| using Xunit; |
|
|
| namespace PSTests.Sequential |
| { |
| public class MyFeedback : IFeedbackProvider |
| { |
| private readonly Guid _id; |
| private readonly string _name, _description; |
| private readonly bool _delay; |
|
|
| public static readonly MyFeedback SlowFeedback; |
|
|
| static MyFeedback() |
| { |
| SlowFeedback = new MyFeedback( |
| Guid.NewGuid(), |
| "Slow", |
| "Description for #1 feedback provider.", |
| delay: true); |
| } |
|
|
| private MyFeedback(Guid id, string name, string description, bool delay) |
| { |
| _id = id; |
| _name = name; |
| _description = description; |
| _delay = delay; |
| } |
|
|
| public Guid Id => _id; |
|
|
| public string Name => _name; |
|
|
| public string Description => _description; |
|
|
| public FeedbackItem GetFeedback(FeedbackContext context, CancellationToken token) |
| { |
| if (_delay) |
| { |
| |
| |
| Thread.Sleep(2500); |
| } |
|
|
| return new FeedbackItem( |
| "slow-feedback-caption", |
| new List<string> { $"{context.CommandLine}+{context.LastError.FullyQualifiedErrorId}" }); |
| } |
| } |
|
|
| public static class FeedbackProviderTests |
| { |
| [Fact] |
| public static void GetFeedback() |
| { |
| var pwsh = PowerShell.Create(); |
| var settings = new PSInvocationSettings() { AddToHistory = true }; |
|
|
| |
| |
| pwsh.AddCommand("Set-Location") |
| .AddParameter("Path", Path.GetTempPath()) |
| .Invoke(input: null, settings); |
| pwsh.Commands.Clear(); |
|
|
| |
| pwsh.AddCommand("New-Item") |
| .AddParameter("Path", "feedbacktest.ps1") |
| .Invoke(input: null, settings); |
| pwsh.Commands.Clear(); |
|
|
| |
| pwsh.AddScript("feedbacktest").Invoke(input: null, settings); |
| pwsh.Commands.Clear(); |
|
|
| try |
| { |
| |
| |
| SubsystemManager.RegisterSubsystem(SubsystemKind.FeedbackProvider, MyFeedback.SlowFeedback); |
|
|
| |
| |
| |
| var feedbacks = FeedbackHub.GetFeedback(pwsh.Runspace, millisecondsTimeout: 1500); |
| string expectedCmd = Path.Combine(".", "feedbacktest"); |
|
|
| |
| Assert.Single(feedbacks); |
| Assert.Equal("General Feedback", feedbacks[0].Name); |
| Assert.Equal(expectedCmd, feedbacks[0].Item.RecommendedActions[0]); |
|
|
| |
| |
| |
| feedbacks = FeedbackHub.GetFeedback(pwsh.Runspace, millisecondsTimeout: 4000); |
| Assert.Equal(2, feedbacks.Count); |
|
|
| FeedbackResult entry1 = feedbacks[0]; |
| Assert.Equal("General Feedback", entry1.Name); |
| Assert.Equal(expectedCmd, entry1.Item.RecommendedActions[0]); |
|
|
| FeedbackResult entry2 = feedbacks[1]; |
| Assert.Equal("Slow", entry2.Name); |
| Assert.Equal("feedbacktest+CommandNotFoundException", entry2.Item.RecommendedActions[0]); |
| } |
| finally |
| { |
| SubsystemManager.UnregisterSubsystem<IFeedbackProvider>(MyFeedback.SlowFeedback.Id); |
| } |
| } |
| } |
| } |
|
|