2015-12-15 01:39:29 +00:00
|
|
|
|
// 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.
|
|
|
|
|
|
2016-01-11 21:40:47 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2015-12-15 01:39:29 +00:00
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using FluentAssertions.Execution;
|
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Test.Utilities
|
|
|
|
|
{
|
|
|
|
|
public class CommandResultAssertions
|
|
|
|
|
{
|
|
|
|
|
private CommandResult _commandResult;
|
|
|
|
|
|
|
|
|
|
public CommandResultAssertions(CommandResult commandResult)
|
|
|
|
|
{
|
|
|
|
|
_commandResult = commandResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AndConstraint<CommandResultAssertions> ExitWith(int expectedExitCode)
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(_commandResult.ExitCode == expectedExitCode)
|
2016-01-24 09:04:39 +00:00
|
|
|
|
.FailWith(AppendDiagnosticsTo($"Expected command to exit with {expectedExitCode} but it did not."));
|
2015-12-15 01:39:29 +00:00
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AndConstraint<CommandResultAssertions> Pass()
|
2016-01-11 21:40:47 +00:00
|
|
|
|
{
|
2015-12-15 01:39:29 +00:00
|
|
|
|
Execute.Assertion.ForCondition(_commandResult.ExitCode == 0)
|
2016-01-24 09:04:39 +00:00
|
|
|
|
.FailWith(AppendDiagnosticsTo($"Expected command to pass but it did not."));
|
2015-12-15 01:39:29 +00:00
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AndConstraint<CommandResultAssertions> Fail()
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(_commandResult.ExitCode != 0)
|
2016-01-24 09:04:39 +00:00
|
|
|
|
.FailWith(AppendDiagnosticsTo($"Expected command to fail but it did not."));
|
2015-12-15 01:39:29 +00:00
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AndConstraint<CommandResultAssertions> HaveStdOut()
|
2016-01-11 21:40:47 +00:00
|
|
|
|
{
|
2015-12-15 01:39:29 +00:00
|
|
|
|
Execute.Assertion.ForCondition(!string.IsNullOrEmpty(_commandResult.StdOut))
|
2016-01-24 09:04:39 +00:00
|
|
|
|
.FailWith(AppendDiagnosticsTo("Command did not output anything to stdout"));
|
2015-12-15 01:39:29 +00:00
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 01:02:59 +00:00
|
|
|
|
public AndConstraint<CommandResultAssertions> HaveStdOut(string expectedOutput)
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(_commandResult.StdOut.Equals(expectedOutput, StringComparison.Ordinal))
|
2016-01-24 09:04:39 +00:00
|
|
|
|
.FailWith(AppendDiagnosticsTo($"Command did not output with Expected Output. Expected: {expectedOutput}"));
|
2015-12-31 01:02:59 +00:00
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 01:42:44 +00:00
|
|
|
|
public AndConstraint<CommandResultAssertions> HaveStdOutContaining(string pattern)
|
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
Execute.Assertion
|
|
|
|
|
.ForCondition(_commandResult.StdOut.Contains(pattern))
|
2016-03-02 01:42:44 +00:00
|
|
|
|
.FailWith(AppendDiagnosticsTo($"The command output did not contain expected result: {pattern}{Environment.NewLine}"));
|
2016-10-28 01:46:43 +00:00
|
|
|
|
|
2016-03-02 01:42:44 +00:00
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-26 20:41:45 +00:00
|
|
|
|
public AndConstraint<CommandResultAssertions> HaveStdOutContainingIgnoreCase(string pattern)
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(_commandResult.StdOut.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) >= 0)
|
|
|
|
|
.FailWith(AppendDiagnosticsTo($"The command output did not contain expected result (ignoring case): {pattern}{Environment.NewLine}"));
|
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 01:42:44 +00:00
|
|
|
|
public AndConstraint<CommandResultAssertions> HaveStdOutMatching(string pattern, RegexOptions options = RegexOptions.None)
|
2016-01-11 21:40:47 +00:00
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdOut, pattern, options).Success)
|
2016-01-24 09:04:39 +00:00
|
|
|
|
.FailWith(AppendDiagnosticsTo($"Matching the command output failed. Pattern: {pattern}{Environment.NewLine}"));
|
2016-01-11 21:40:47 +00:00
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-15 01:39:29 +00:00
|
|
|
|
public AndConstraint<CommandResultAssertions> HaveStdErr()
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(!string.IsNullOrEmpty(_commandResult.StdErr))
|
2016-01-24 09:04:39 +00:00
|
|
|
|
.FailWith(AppendDiagnosticsTo("Command did not output anything to stderr."));
|
2015-12-15 01:39:29 +00:00
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
2016-03-02 01:42:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AndConstraint<CommandResultAssertions> HaveStdErrContaining(string pattern)
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(_commandResult.StdErr.Contains(pattern))
|
|
|
|
|
.FailWith(AppendDiagnosticsTo($"The command error output did not contain expected result: {pattern}{Environment.NewLine}"));
|
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-30 03:19:35 +00:00
|
|
|
|
public AndConstraint<CommandResultAssertions> NotHaveStdErrContaining(string pattern)
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(!_commandResult.StdErr.Contains(pattern))
|
|
|
|
|
.FailWith(AppendDiagnosticsTo($"The command error output contained a result it should not have contained: {pattern}{Environment.NewLine}"));
|
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 01:42:44 +00:00
|
|
|
|
public AndConstraint<CommandResultAssertions> HaveStdErrMatching(string pattern, RegexOptions options = RegexOptions.None)
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdErr, pattern, options).Success)
|
|
|
|
|
.FailWith(AppendDiagnosticsTo($"Matching the command error output failed. Pattern: {pattern}{Environment.NewLine}"));
|
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
2015-12-15 01:39:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AndConstraint<CommandResultAssertions> NotHaveStdOut()
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(string.IsNullOrEmpty(_commandResult.StdOut))
|
2016-01-24 09:04:39 +00:00
|
|
|
|
.FailWith(AppendDiagnosticsTo($"Expected command to not output to stdout but it was not:"));
|
2015-12-15 01:39:29 +00:00
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AndConstraint<CommandResultAssertions> NotHaveStdErr()
|
|
|
|
|
{
|
|
|
|
|
Execute.Assertion.ForCondition(string.IsNullOrEmpty(_commandResult.StdErr))
|
2016-01-24 09:04:39 +00:00
|
|
|
|
.FailWith(AppendDiagnosticsTo("Expected command to not output to stderr but it was not:"));
|
2015-12-15 01:39:29 +00:00
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
|
|
|
|
}
|
2016-01-24 09:04:39 +00:00
|
|
|
|
|
|
|
|
|
private string AppendDiagnosticsTo(string s)
|
|
|
|
|
{
|
|
|
|
|
return s + $"{Environment.NewLine}" +
|
|
|
|
|
$"File Name: {_commandResult.StartInfo.FileName}{Environment.NewLine}" +
|
|
|
|
|
$"Arguments: {_commandResult.StartInfo.Arguments}{Environment.NewLine}" +
|
2016-10-28 01:46:43 +00:00
|
|
|
|
$"WorkingDir:: {_commandResult.StartInfo.WorkingDirectory}{Environment.NewLine}" +
|
2016-01-24 09:04:39 +00:00
|
|
|
|
$"Exit Code: {_commandResult.ExitCode}{Environment.NewLine}" +
|
|
|
|
|
$"StdOut:{Environment.NewLine}{_commandResult.StdOut}{Environment.NewLine}" +
|
|
|
|
|
$"StdErr:{Environment.NewLine}{_commandResult.StdErr}{Environment.NewLine}"; ;
|
2016-01-26 22:53:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-13 00:29:07 +00:00
|
|
|
|
public AndConstraint<CommandResultAssertions> HaveSkippedProjectCompilation(string skippedProject, string frameworkFullName)
|
2016-01-26 22:53:56 +00:00
|
|
|
|
{
|
2016-04-13 00:29:07 +00:00
|
|
|
|
_commandResult.StdOut.Should().Contain($"Project {skippedProject} ({frameworkFullName}) was previously compiled. Skipping compilation.");
|
2016-01-26 22:53:56 +00:00
|
|
|
|
|
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-13 00:29:07 +00:00
|
|
|
|
public AndConstraint<CommandResultAssertions> HaveCompiledProject(string compiledProject, string frameworkFullName)
|
2016-01-26 22:53:56 +00:00
|
|
|
|
{
|
2016-04-13 00:29:07 +00:00
|
|
|
|
_commandResult.StdOut.Should().Contain($"Project {compiledProject} ({frameworkFullName}) will be compiled");
|
2016-01-26 22:53:56 +00:00
|
|
|
|
|
|
|
|
|
return new AndConstraint<CommandResultAssertions>(this);
|
2016-01-24 09:04:39 +00:00
|
|
|
|
}
|
2015-12-15 01:39:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|