Skip to content

Commit 38eb26a

Browse files
committed
Improved testing of package test text output
1 parent e84ad6c commit 38eb26a

File tree

7 files changed

+34
-37
lines changed

7 files changed

+34
-37
lines changed

recipe/build-settings.cake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public static class BuildSettings
230230
FileName = "build-results/NUnitConsole.binlog",
231231
Imports = MSBuildBinaryLoggerImports.Embed
232232
}
233-
}.WithProperty("Version", BuildSettings.PackageVersion)
233+
}.WithProperty("Version", PackageVersion)
234234
};
235235

236236
// File Header Checks

recipe/output-checks.cake

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//////////////////////////////////////////////////////////////////////
2-
// SYNTAX FOR EXPRESSING A STRING CONSTRAINT
2+
// STATIC SYNTAX FOR EXPRESSING OUTPUT CHECKS
33
//////////////////////////////////////////////////////////////////////
44

55
//public static class StringConstraints
@@ -22,6 +22,11 @@
2222
// }
2323
//}
2424

25+
public static OutputContainsCheck Contains(string expectedText, int atleast = 1, int exactly = -1)
26+
=> new OutputContainsCheck(expectedText, atleast, exactly);
27+
28+
public static OutputDoesNotContain DoesNotContain(string text) => new OutputDoesNotContain(text);
29+
2530
// OutputCheck is used to check content of redirected package test output
2631
public abstract class OutputCheck
2732
{
@@ -43,9 +48,9 @@ public abstract class OutputCheck
4348
public string Message { get; protected set; }
4449
}
4550

46-
public class OutputContains : OutputCheck
51+
public class OutputContainsCheck : OutputCheck
4752
{
48-
public OutputContains(string expectedText, int atleast = 1, int exactly = -1) : base(expectedText, atleast, exactly) { }
53+
public OutputContainsCheck(string expectedText, int atleast = 1, int exactly = -1) : base(expectedText, atleast, exactly) { }
4954

5055
public override bool Matches(string output)
5156
{

recipe/package-checks.cake

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
//////////////////////////////////////////////////////////////////////
2-
// SYNTAX FOR EXPRESSING CHECKS
2+
// STATIC SYNTAX FOR EXPRESSING PACKAGE CHECKS
33
//////////////////////////////////////////////////////////////////////
44

5-
public static class Check
6-
{
7-
public static void That(DirectoryPath testDirPath, IList<PackageCheck> checks)
8-
{
9-
if (checks == null)
10-
throw new ArgumentNullException(nameof(checks));
11-
12-
bool allOK = true;
13-
14-
foreach (var check in checks)
15-
allOK &= check.ApplyTo(testDirPath);
16-
17-
if (!allOK) throw new Exception("Verification failed!");
18-
}
19-
}
20-
215
public static FileCheck HasFile(FilePath file) => HasFiles(new[] { file });
226
public static FileCheck HasFiles(params FilePath[] files) => new FileCheck(files);
237

recipe/package-definition.cake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public abstract class PackageDefinition
242242
string arguments = $"{packageTest.Arguments} {ExtraTestArguments} --work={testResultDir}";
243243
if (CommandLineOptions.TraceLevel.Value != "Off")
244244
arguments += $" --trace:{CommandLineOptions.TraceLevel.Value}";
245-
bool redirectOutput = packageTest.OutputCheck != null;
245+
bool redirectOutput = packageTest.ExpectedOutput != null;
246246

247247
int rc = runner.RunPackageTest(arguments, redirectOutput);
248248

recipe/package-test.cake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class PackageTest
1414
public string Arguments { get; set; }
1515
public int ExpectedReturnCode { get; set; } = 0;
1616
public ExpectedResult ExpectedResult { get; set; }
17-
public OutputCheck OutputCheck { get; set; }
17+
public OutputCheck[] ExpectedOutput { get; set; }
1818
public ExtensionSpecifier[] ExtensionsNeeded { get; set; } = new ExtensionSpecifier[0];
1919
public IPackageTestRunner[] TestRunners { get; set; } = new IPackageTestRunner[0];
2020

recipe/test-reports.cake

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class PackageTestReport
1313
Runner = runner;
1414

1515
var expectedResult = test.ExpectedResult;
16-
var expectedOutput = test.OutputCheck;
16+
var expectedOutput = test.ExpectedOutput;
1717

1818
if (expectedResult != null)
1919
{
@@ -57,10 +57,17 @@ public class PackageTestReport
5757
}
5858

5959
if (expectedOutput != null)
60-
{
61-
if (!expectedOutput.Matches(runner.Output))
62-
Errors.Add(expectedOutput.Message);
63-
}
60+
{
61+
var output = runner.Output;
62+
if (output is not null)
63+
foreach (var outputCheck in expectedOutput)
64+
{
65+
if (!outputCheck.Matches(output))
66+
Errors.Add(outputCheck.Message);
67+
}
68+
else
69+
Errors.Add("No output was produced");
70+
}
6471
}
6572

6673
public PackageTestReport(PackageTest test, int rc, IPackageTestRunner runner = null)
@@ -71,11 +78,12 @@ public class PackageTestReport
7178

7279
if (rc != test.ExpectedReturnCode)
7380
Errors.Add($" Expected: rc = {test.ExpectedReturnCode} But was: {rc}");
74-
else if (test.OutputCheck != null)
75-
{
76-
if (!test.OutputCheck.Matches(runner.Output))
77-
Errors.Add(test.OutputCheck.Message);
78-
}
81+
else if (test.ExpectedOutput != null)
82+
foreach (var outputCheck in test.ExpectedOutput)
83+
{
84+
if (!outputCheck.Matches(runner.Output))
85+
Errors.Add(outputCheck.Message);
86+
}
7987
}
8088

8189
public PackageTestReport(PackageTest test, Exception ex, IPackageTestRunner runner = null)

recipe/test-runners.cake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface IPackageTestRunner
2121
string PackageId { get; }
2222
string Version { get; }
2323

24-
IEnumerable<string> Output { get; }
24+
string Output { get; }
2525

2626
int RunPackageTest(string arguments, bool redirectOutput = false);
2727
}
@@ -44,7 +44,7 @@ public abstract class TestRunner
4444
public string PackageId { get; protected set; }
4545
public string Version { get; protected set; }
4646

47-
public IEnumerable<string> Output { get; protected set; }
47+
public string Output { get; private set; }
4848

4949
protected int RunPackageTest(FilePath executablePath, string arguments = null, bool redirectOutput = false)
5050
{
@@ -90,14 +90,14 @@ public abstract class TestRunner
9090
IEnumerable<string> output;
9191
// If Redirected Output was not requested, output will be null
9292
int rc = Context.StartProcess(executablePath, processSettings, out output);
93-
Output = output;
93+
Output = output != null ? string.Join("\r\n", output) : null;
9494
return rc;
9595
}
9696

9797
internal string OutputHandler(string output)
9898
{
9999
// Ensure that package test output displays and is also re-directed.
100-
// If the derive class doesn't need the output, it doesn't retrieve it.
100+
// If the derived class doesn't need the output, it doesn't retrieve it.
101101
Console.WriteLine(output);
102102
return output;
103103
}

0 commit comments

Comments
 (0)