|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +namespace Microsoft.Azure.Functions.PowerShellWorker.Test |
| 7 | +{ |
| 8 | + using System; |
| 9 | + using System.Linq; |
| 10 | + using System.Management.Automation; |
| 11 | + using System.Text.RegularExpressions; |
| 12 | + |
| 13 | + using Xunit; |
| 14 | + |
| 15 | + using Microsoft.Azure.Functions.PowerShellWorker.PowerShell; |
| 16 | + |
| 17 | + public class ErrorRecordFormatterTests |
| 18 | + { |
| 19 | + private static readonly ErrorRecordFormatter s_errorRecordFormatter = new ErrorRecordFormatter(); |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void FormattedStringContainsBasicErrorRecordData() |
| 23 | + { |
| 24 | + var exception = new RuntimeException("My exception"); |
| 25 | + var errorRecord = new ErrorRecord(exception, "error id", ErrorCategory.InvalidOperation, null); |
| 26 | + |
| 27 | + var result = s_errorRecordFormatter.Format(errorRecord); |
| 28 | + |
| 29 | + Assert.StartsWith(exception.Message, result); |
| 30 | + var resultLines = result.Split(Environment.NewLine); |
| 31 | + Assert.Contains(resultLines, line => Regex.IsMatch(line, @"\bCategoryInfo\b[:\s]*?\bInvalidOperation\b")); |
| 32 | + Assert.Contains(resultLines, line => Regex.IsMatch(line, @"\bFullyQualifiedErrorId\b[:\s]*?\berror id\b")); |
| 33 | + Assert.Contains(resultLines, line => line == "System.Management.Automation.RuntimeException: My exception"); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void FormattedStringContainsInnerExceptions() |
| 38 | + { |
| 39 | + var exception1 = new Exception("My exception 1"); |
| 40 | + var exception2 = new Exception("My exception 2", exception1); |
| 41 | + var exception3 = new Exception("My exception 3", exception2); |
| 42 | + var errorRecord = new ErrorRecord(exception3, "error id", ErrorCategory.InvalidOperation, null); |
| 43 | + |
| 44 | + var result = s_errorRecordFormatter.Format(errorRecord); |
| 45 | + |
| 46 | + var resultLines = result.Split(Environment.NewLine); |
| 47 | + Assert.Contains(resultLines, line => line == "Inner exception: System.Exception: My exception 1"); |
| 48 | + Assert.Contains(resultLines, line => line == "Inner exception: System.Exception: My exception 2"); |
| 49 | + Assert.Equal(2, resultLines.Count(line => line.Contains("Inner exception:"))); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void FormattedStringContainsAggregateExceptionMembers() |
| 54 | + { |
| 55 | + var exception1 = new Exception("My exception 1"); |
| 56 | + var exception2 = new Exception("My exception 2"); |
| 57 | + var exception3 = new AggregateException("My exception 3", exception1, exception2); |
| 58 | + var exception4 = new Exception("My exception 4", exception3); |
| 59 | + var exception5 = new Exception("My exception 5"); |
| 60 | + var exception6 = new AggregateException("My exception 6", exception4, exception5); |
| 61 | + var exception7 = new Exception("My exception 7", exception6); |
| 62 | + var errorRecord = new ErrorRecord(exception7, "error id", ErrorCategory.InvalidOperation, null); |
| 63 | + |
| 64 | + var result = s_errorRecordFormatter.Format(errorRecord); |
| 65 | + |
| 66 | + var resultLines = result.Split(Environment.NewLine); |
| 67 | + Assert.Contains(resultLines, line => line == "Inner exception: System.Exception: My exception 1"); |
| 68 | + Assert.Contains(resultLines, line => line == "Inner exception: System.Exception: My exception 2"); |
| 69 | + Assert.Contains(resultLines, line => line.StartsWith("Inner exception: System.AggregateException: My exception 3")); |
| 70 | + Assert.Contains(resultLines, line => line == "Inner exception: System.Exception: My exception 4"); |
| 71 | + Assert.Contains(resultLines, line => line == "Inner exception: System.Exception: My exception 5"); |
| 72 | + Assert.Contains(resultLines, line => line.StartsWith("Inner exception: System.AggregateException: My exception 6")); |
| 73 | + Assert.Contains(resultLines, line => line == "System.Exception: My exception 7"); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void FormattedStringIsTruncatedIfTooLong() |
| 78 | + { |
| 79 | + var exception1 = new Exception("My exception 1"); |
| 80 | + var exception2 = new Exception("My exception 2", exception1); |
| 81 | + var exception3 = new Exception("My exception 3", exception2); |
| 82 | + var errorRecord = new ErrorRecord(exception3, "error id", ErrorCategory.InvalidOperation, null); |
| 83 | + var fullResult = s_errorRecordFormatter.Format(errorRecord); |
| 84 | + |
| 85 | + var maxSize = fullResult.Length / 2; |
| 86 | + var truncatedResult = new ErrorRecordFormatter().Format(errorRecord, maxSize); |
| 87 | + |
| 88 | + const string ExpectedPostfix = "..."; |
| 89 | + Assert.InRange(truncatedResult.Length, ExpectedPostfix.Length + 1, maxSize); |
| 90 | + Assert.EndsWith(ExpectedPostfix, truncatedResult); |
| 91 | + Assert.StartsWith(fullResult.Substring(0, truncatedResult.Length - ExpectedPostfix.Length), truncatedResult); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments