Advertisement
pszczyg

Unit testing PSCmdlet-derived classes

May 6th, 2021
1,547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1.  [Test]
  2.         public void Exemplary_PSCmdlet_Test() {
  3.  
  4.             var fileInteractorMock = new Mock<IFileInteractor>();
  5.             fileInteractorMock.Setup(m => m.CreateDirectory(null)).Throws<ArgumentNullException>();
  6.  
  7.             var logger = new SimpleLogger();
  8.  
  9.             var fileManager = new InvocationFileManager(logger, fileInteractorMock.Object);
  10.  
  11.             var parser = new InvokeCvApiParameterParser(fileInteractorMock.Object, fileManager, logger);
  12.  
  13.             var testBLContextProvider = new Mock<IContextProvider>();
  14.  
  15.             var cmdlet = new InvokeCVApiCommand(testBLContextProvider.Object, fileManager, logger, parser) {
  16.                 FunctionFilePath = "fakePath",
  17.                 OutputFolder = "fakeOutputFolder"
  18.             };
  19.  
  20.             var psEmulator = new PowershellEmulator();
  21.             cmdlet.CommandRuntime = psEmulator;
  22.             cmdlet.ProcessInternal();
  23.             var results = psEmulator.OutputObjects;
  24.             //do whatever you want with output
  25.         }
  26.  
  27.         internal class PowershellEmulator : ICommandRuntime {
  28.             public List<object> OutputObjects { get; } = new List<object>();
  29.             public PSHost Host { get; }
  30.             public PSTransactionContext CurrentPSTransaction { get; }
  31.  
  32.             public bool ShouldContinue(string query, string caption, ref bool yesToAll, ref bool noToAll) {
  33.                 return true;
  34.             }
  35.  
  36.             public bool ShouldContinue(string query, string caption) {
  37.                 return true;
  38.             }
  39.  
  40.             public bool TransactionAvailable() {
  41.                 return true;
  42.             }
  43.  
  44.             public bool ShouldProcess(string verboseDescription, string verboseWarning, string caption,
  45.                 out ShouldProcessReason shouldProcessReason) {
  46.                 shouldProcessReason = ShouldProcessReason.None;
  47.                 return true;
  48.             }
  49.  
  50.             public bool ShouldProcess(string verboseDescription, string verboseWarning, string caption) {
  51.                 return true;
  52.             }
  53.  
  54.             public bool ShouldProcess(string target, string action) {
  55.                 return true;
  56.             }
  57.  
  58.             public bool ShouldProcess(string target) {
  59.                 return true;
  60.             }
  61.  
  62.             public void ThrowTerminatingError(ErrorRecord errorRecord) {
  63.                 throw new InvalidOperationException("Error in pipeline", errorRecord.Exception);
  64.             }
  65.  
  66.             public void WriteCommandDetail(string text) {
  67.                 WriteObject(text);
  68.             }
  69.  
  70.             public void WriteDebug(string text) {
  71.                 WriteObject(text);
  72.             }
  73.  
  74.             public void WriteError(ErrorRecord errorRecord) {
  75.                 throw new InvalidOperationException("Error in pipeline", errorRecord.Exception);
  76.             }
  77.  
  78.             public void WriteObject(object sendToPipeline, bool enumerateCollection) {
  79.                 WriteObject(sendToPipeline);
  80.             }
  81.  
  82.             public void WriteObject(object sendToPipeline) {
  83.                 OutputObjects.Add(sendToPipeline);
  84.             }
  85.  
  86.             public void WriteProgress(long sourceId, ProgressRecord progressRecord) {
  87.                 WriteObject(progressRecord);
  88.             }
  89.  
  90.             public void WriteProgress(ProgressRecord progressRecord) {
  91.                 WriteObject(progressRecord);
  92.             }
  93.  
  94.             public void WriteVerbose(string text) {
  95.                 WriteObject(text);
  96.             }
  97.  
  98.             public void WriteWarning(string text) {
  99.                 WriteObject(text);
  100.             }
  101.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement