in src/Analyzer.Cli.FunctionalTests/CommandLineParserTests.cs [232:323]
public void AnalyzeDirectory_ExecutionWithErrorAndWarning_PrintsExpectedMessages(bool usesVerboseMode, bool multipleErrors = false)
{
var directoryToAnalyze = GetFilePath("ToTestSummaryLogger");
var expectedLogSummary = "Execution summary:";
if (!usesVerboseMode)
{
expectedLogSummary += $"{Environment.NewLine}\tThe verbose mode (option -v or --verbose) can be used to obtain even more information about the execution.";
}
var warningMessage = "The parsing of the template output named badOutput failed";
var errorMessage1 = $"An exception occurred while analyzing template {Path.Combine(directoryToAnalyze, "ReportsError.json")}";
var errorMessage2 = $"An exception occurred while analyzing template {Path.Combine(directoryToAnalyze, "ReportsError2.json")}";
if (!multipleErrors)
{
expectedLogSummary += ($"{Environment.NewLine}{Environment.NewLine}\tSummary of the warnings:" +
$"{Environment.NewLine}\t\t1 instance of: {warningMessage}{Environment.NewLine}") +
$"{Environment.NewLine}\tSummary of the errors:" +
$"{Environment.NewLine}\t\t1 instance of: {errorMessage1}";
}
else
{
expectedLogSummary += ($"{Environment.NewLine}{Environment.NewLine}\tSummary of the warnings:" +
$"{Environment.NewLine}\t\t1 instance of: {warningMessage}{Environment.NewLine}") +
$"{Environment.NewLine}\tSummary of the errors:" +
$"{Environment.NewLine}\t\t1 instance of: {errorMessage1}" +
$"{Environment.NewLine}\t\t1 instance of: {errorMessage2}";
}
expectedLogSummary += ($"{Environment.NewLine}{Environment.NewLine}\t1 Warning" +
$"{Environment.NewLine}\t{(multipleErrors ? "2 Errors" : "1 Error")}{Environment.NewLine}");
var args = new string[] { "analyze-directory", directoryToAnalyze };
if (usesVerboseMode)
{
args = args.Append("--verbose").ToArray();
}
using StringWriter outputWriter = new();
Console.SetOut(outputWriter);
// Copy template producing an error to get multiple errors in run
string secondErrorTemplate = Path.Combine(directoryToAnalyze, "ReportsError2.json");
if (multipleErrors)
{
File.Copy(Path.Combine(directoryToAnalyze, "ReportsError.json"), secondErrorTemplate);
}
try
{
var result = _commandLineParser.InvokeCommandLineAPIAsync(args);
var cliConsoleOutput = outputWriter.ToString();
var indexOfLogSummary = cliConsoleOutput.IndexOf("Execution summary:");
Assert.IsTrue(indexOfLogSummary >= 0, $"Expected log message not found in CLI output. Found:{Environment.NewLine}{cliConsoleOutput}");
var errorLog = $"Error: {errorMessage1}";
var warningLog = $"Warning: {warningMessage}";
if (usesVerboseMode)
{
errorLog += $"{Environment.NewLine}Exception details:" +
$"{Environment.NewLine}Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzerException: Error while processing template.";
// output parse warning does not trigger exception, only log
}
var outputBeforeSummary = cliConsoleOutput[..indexOfLogSummary];
Assert.IsTrue(outputBeforeSummary.IndexOf(errorLog) > 0);
Assert.IsTrue(outputBeforeSummary.IndexOf(warningLog) > 0);
var logSummary = cliConsoleOutput[indexOfLogSummary..];
if (multipleErrors)
{
// on some platforms the exception messages can be in different order
var alternateExpectedLogSummary = expectedLogSummary
.Replace("ReportsError.json", "PLACEHOLDER")
.Replace("ReportsError2.json", "ReportsError.json")
.Replace("PLACEHOLDER", "ReportsError2.json");
Assert.IsTrue(expectedLogSummary.Equals(logSummary)
|| alternateExpectedLogSummary.Equals(logSummary));
}
else
{
Assert.AreEqual(expectedLogSummary, logSummary);
}
}
finally
{
File.Delete(secondErrorTemplate);
}
}