Skip to content

Commit

Permalink
Added in test that writes to a file and then ensures that the file wa…
Browse files Browse the repository at this point in the history
…s actually written to. Also ensures multiple arguments works properly. Still need to do cleanup and potentially add more tests.
  • Loading branch information
kkeirstead committed Aug 18, 2021
1 parent 1014578 commit 1dc9038
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class Program
{
public static int Main(string[] args)
{
File.WriteAllText("C:\\Users\\kkeirstead\\FileOutputs\\Test.txt", args[0]); // Testing purposes only
//File.WriteAllText("C:\\Users\\kkeirstead\\FileOutputs\\Test.txt", args[0]); // Testing purposes only

string testType = args[0];
const int DelayMs = 3000; // Should be greater than the token delay in the test.
Expand All @@ -28,8 +28,18 @@ public static int Main(string[] args)
Thread.Sleep(DelayMs);
return 0;

case "TextFileOutput":
//File.WriteAllText("C:\\Users\\kkeirstead\\FileOutputs\\Test2.txt", args[1]); // Testing purposes only
//File.WriteAllText("C:\\Users\\kkeirstead\\FileOutputs\\Test3.txt", args[2]); // Testing purposes only

string textFilePath = args[1];
string textFileMessage = args[2];

File.WriteAllText(textFilePath, textFileMessage);
return 0;

default:
return -100;
return -100; // Arbitrary nonzero exit code
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Diagnostics.Monitoring.TestCommon;
using System.Threading;
using System;
using System.IO;

namespace Microsoft.Diagnostics.Monitoring.Tool.UnitTests
{
Expand All @@ -20,7 +21,6 @@ public sealed class ExecuteActionTests
// This should be identical to the error message found in Strings.resx (except without the process's exit code)
private const string NonzeroExitCodeMessage = "The process exited with exit code";

// This should be identical to the error message found in Strings.resx (except without the process's exit code)
private const string TaskCanceledMessage = "A task was canceled";

[Fact]
Expand Down Expand Up @@ -59,7 +59,7 @@ public async Task ExecuteAction_NonzeroExitCode()
CancellationToken cancellationToken = cancellationTokenSource.Token;

InvalidOperationException invalidOperationException = await Assert.ThrowsAsync<InvalidOperationException>(
() => action.ExecuteAsync(options, null, cancellationToken));
() => action.ExecuteAsync(options, null, cancellationToken));
Assert.Contains(NonzeroExitCodeMessage, invalidOperationException.Message);
}

Expand All @@ -79,8 +79,44 @@ public async Task ExecuteAction_TokenCancellation()
CancellationToken cancellationToken = cancellationTokenSource.Token;

TaskCanceledException invalidOperationException = await Assert.ThrowsAsync<TaskCanceledException>(
() => action.ExecuteAsync(options, null, cancellationToken));
() => action.ExecuteAsync(options, null, cancellationToken));
Assert.Contains(TaskCanceledMessage, invalidOperationException.Message);
}

[Fact]
public async Task ExecuteAction_TextFileOutput()
{
ExecuteAction action = new();

ExecuteOptions options = new();

DirectoryInfo outputDirectory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "ExecuteAction", Guid.NewGuid().ToString()));
string textFileOutputPath = outputDirectory.FullName;

textFileOutputPath += "\\file.txt";

const string testMessage = "TestMessage";

options.Path = DotNetHost.HostExePath;
options.Arguments = Assembly.GetExecutingAssembly().Location.Replace(
Assembly.GetExecutingAssembly().GetName().Name,
"Microsoft.Diagnostics.Monitoring.ExecuteApp") + " TextFileOutput " + textFileOutputPath + " " + testMessage;

using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TokenTimeoutMs);
CancellationToken cancellationToken = cancellationTokenSource.Token;

CollectionRuleActionResult result = await action.ExecuteAsync(options, null, cancellationToken);

Assert.Equal("0", result.OutputValues["ExitCode"]);
Assert.Equal(testMessage, File.ReadAllText(textFileOutputPath));

try
{
outputDirectory?.Delete(recursive: true);
}
catch
{
}
}
}
}

0 comments on commit 1dc9038

Please sign in to comment.