MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Collections.Generic;6using System.IO;7using System.Management.Automation;8using System.Management.Automation.Subsystem;9using System.Management.Automation.Subsystem.Feedback;10using System.Threading;11using Xunit;12 13namespace PSTests.Sequential14{15 public class MyFeedback : IFeedbackProvider16 {17 private readonly Guid _id;18 private readonly string _name, _description;19 private readonly bool _delay;20 21 public static readonly MyFeedback SlowFeedback;22 23 static MyFeedback()24 {25 SlowFeedback = new MyFeedback(26 Guid.NewGuid(),27 "Slow",28 "Description for #1 feedback provider.",29 delay: true);30 }31 32 private MyFeedback(Guid id, string name, string description, bool delay)33 {34 _id = id;35 _name = name;36 _description = description;37 _delay = delay;38 }39 40 public Guid Id => _id;41 42 public string Name => _name;43 44 public string Description => _description;45 46 public FeedbackItem GetFeedback(FeedbackContext context, CancellationToken token)47 {48 if (_delay)49 {50 // The delay is exaggerated to make the test reliable.51 // xUnit must spin up a lot tasks, which makes the test unreliable when the time difference between 'delay' and 'timeout' is small.52 Thread.Sleep(2500);53 }54 55 return new FeedbackItem(56 "slow-feedback-caption",57 new List<string> { $"{context.CommandLine}+{context.LastError.FullyQualifiedErrorId}" });58 }59 }60 61 public static class FeedbackProviderTests62 {63 [Fact]64 public static void GetFeedback()65 {66 var pwsh = PowerShell.Create();67 var settings = new PSInvocationSettings() { AddToHistory = true };68 69 // Setup the context for the test.70 // Change current working directory to the temp path.71 pwsh.AddCommand("Set-Location")72 .AddParameter("Path", Path.GetTempPath())73 .Invoke(input: null, settings);74 pwsh.Commands.Clear();75 76 // Create an empty file 'feedbacktest.ps1' under the temp path;77 pwsh.AddCommand("New-Item")78 .AddParameter("Path", "feedbacktest.ps1")79 .Invoke(input: null, settings);80 pwsh.Commands.Clear();81 82 // Run a command 'feedbacktest', so as to trigger the 'general' feedback.83 pwsh.AddScript("feedbacktest").Invoke(input: null, settings);84 pwsh.Commands.Clear();85 86 try87 {88 // Register the slow feedback provider.89 // The 'general' feedback provider is built-in and registered by default.90 SubsystemManager.RegisterSubsystem(SubsystemKind.FeedbackProvider, MyFeedback.SlowFeedback);91 92 // Expect the result from 'general' only because the 'slow' one cannot finish before the specified timeout.93 // The specified timeout is exaggerated to make the test reliable.94 // xUnit must spin up a lot tasks, which makes the test unreliable when the time difference between 'delay' and 'timeout' is small.95 var feedbacks = FeedbackHub.GetFeedback(pwsh.Runspace, millisecondsTimeout: 1500);96 string expectedCmd = Path.Combine(".", "feedbacktest");97 98 // Test the result from the 'general' feedback provider.99 Assert.Single(feedbacks);100 Assert.Equal("General Feedback", feedbacks[0].Name);101 Assert.Equal(expectedCmd, feedbacks[0].Item.RecommendedActions[0]);102 103 // Expect the result from both 'general' and the 'slow' feedback providers.104 // Same here -- the specified timeout is exaggerated to make the test reliable.105 // xUnit must spin up a lot tasks, which makes the test unreliable when the time difference between 'delay' and 'timeout' is small.106 feedbacks = FeedbackHub.GetFeedback(pwsh.Runspace, millisecondsTimeout: 4000);107 Assert.Equal(2, feedbacks.Count);108 109 FeedbackResult entry1 = feedbacks[0];110 Assert.Equal("General Feedback", entry1.Name);111 Assert.Equal(expectedCmd, entry1.Item.RecommendedActions[0]);112 113 FeedbackResult entry2 = feedbacks[1];114 Assert.Equal("Slow", entry2.Name);115 Assert.Equal("feedbacktest+CommandNotFoundException", entry2.Item.RecommendedActions[0]);116 }117 finally118 {119 SubsystemManager.UnregisterSubsystem<IFeedbackProvider>(MyFeedback.SlowFeedback.Id);120 }121 }122 }123}124 