|
| 1 | +// Copyright (c) DEMA Consulting |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in all |
| 11 | +// copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | + |
| 21 | +using DemaConsulting.BuildMark.RepoConnectors; |
| 22 | + |
| 23 | +namespace DemaConsulting.BuildMark.Tests; |
| 24 | + |
| 25 | +/// <summary> |
| 26 | +/// Tests for the Validation class. |
| 27 | +/// </summary> |
| 28 | +[TestClass] |
| 29 | +public class ValidationTests |
| 30 | +{ |
| 31 | + /// <summary> |
| 32 | + /// Test that Validation.Run writes TRX results file when specified. |
| 33 | + /// </summary> |
| 34 | + [TestMethod] |
| 35 | + public void Validation_Run_WithTrxResultsFile_WritesTrxFile() |
| 36 | + { |
| 37 | + // Arrange |
| 38 | + var tempDir = Path.Combine(Path.GetTempPath(), $"buildmark_test_{Guid.NewGuid()}"); |
| 39 | + Directory.CreateDirectory(tempDir); |
| 40 | + |
| 41 | + try |
| 42 | + { |
| 43 | + var trxFile = Path.Combine(tempDir, "results.trx"); |
| 44 | + var args = new[] { "--validate", "--results", trxFile }; |
| 45 | + |
| 46 | + StringWriter? outputWriter = null; |
| 47 | + StringWriter? errorWriter = null; |
| 48 | + |
| 49 | + try |
| 50 | + { |
| 51 | + // Capture console output |
| 52 | + outputWriter = new StringWriter(); |
| 53 | + errorWriter = new StringWriter(); |
| 54 | + Console.SetOut(outputWriter); |
| 55 | + Console.SetError(errorWriter); |
| 56 | + |
| 57 | + // Act |
| 58 | + using var context = Context.Create(args, () => new MockRepoConnector()); |
| 59 | + Validation.Run(context); |
| 60 | + |
| 61 | + // Assert - Verify TRX file was created |
| 62 | + Assert.IsTrue(File.Exists(trxFile), "TRX file should be created"); |
| 63 | + |
| 64 | + // Verify TRX file contains expected content |
| 65 | + var trxContent = File.ReadAllText(trxFile); |
| 66 | + Assert.Contains("TestRun", trxContent); |
| 67 | + Assert.Contains("BuildMark Self-Validation", trxContent); |
| 68 | + } |
| 69 | + finally |
| 70 | + { |
| 71 | + // Restore console output |
| 72 | + var standardOutput = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }; |
| 73 | + Console.SetOut(standardOutput); |
| 74 | + var standardError = new StreamWriter(Console.OpenStandardError()) { AutoFlush = true }; |
| 75 | + Console.SetError(standardError); |
| 76 | + |
| 77 | + outputWriter?.Dispose(); |
| 78 | + errorWriter?.Dispose(); |
| 79 | + } |
| 80 | + } |
| 81 | + finally |
| 82 | + { |
| 83 | + // Cleanup |
| 84 | + if (Directory.Exists(tempDir)) |
| 85 | + { |
| 86 | + Directory.Delete(tempDir, true); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Test that Validation.Run writes JUnit XML results file when specified. |
| 93 | + /// </summary> |
| 94 | + [TestMethod] |
| 95 | + public void Validation_Run_WithXmlResultsFile_WritesJUnitFile() |
| 96 | + { |
| 97 | + // Arrange |
| 98 | + var tempDir = Path.Combine(Path.GetTempPath(), $"buildmark_test_{Guid.NewGuid()}"); |
| 99 | + Directory.CreateDirectory(tempDir); |
| 100 | + |
| 101 | + try |
| 102 | + { |
| 103 | + var xmlFile = Path.Combine(tempDir, "results.xml"); |
| 104 | + var args = new[] { "--validate", "--results", xmlFile }; |
| 105 | + |
| 106 | + StringWriter? outputWriter = null; |
| 107 | + StringWriter? errorWriter = null; |
| 108 | + |
| 109 | + try |
| 110 | + { |
| 111 | + // Capture console output |
| 112 | + outputWriter = new StringWriter(); |
| 113 | + errorWriter = new StringWriter(); |
| 114 | + Console.SetOut(outputWriter); |
| 115 | + Console.SetError(errorWriter); |
| 116 | + |
| 117 | + // Act |
| 118 | + using var context = Context.Create(args, () => new MockRepoConnector()); |
| 119 | + Validation.Run(context); |
| 120 | + |
| 121 | + // Assert - Verify XML file was created |
| 122 | + Assert.IsTrue(File.Exists(xmlFile), "XML file should be created"); |
| 123 | + |
| 124 | + // Verify XML file contains expected content |
| 125 | + var xmlContent = File.ReadAllText(xmlFile); |
| 126 | + Assert.Contains("testsuites", xmlContent); |
| 127 | + Assert.Contains("BuildMark Self-Validation", xmlContent); |
| 128 | + } |
| 129 | + finally |
| 130 | + { |
| 131 | + // Restore console output |
| 132 | + var standardOutput = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }; |
| 133 | + Console.SetOut(standardOutput); |
| 134 | + var standardError = new StreamWriter(Console.OpenStandardError()) { AutoFlush = true }; |
| 135 | + Console.SetError(standardError); |
| 136 | + |
| 137 | + outputWriter?.Dispose(); |
| 138 | + errorWriter?.Dispose(); |
| 139 | + } |
| 140 | + } |
| 141 | + finally |
| 142 | + { |
| 143 | + // Cleanup |
| 144 | + if (Directory.Exists(tempDir)) |
| 145 | + { |
| 146 | + Directory.Delete(tempDir, true); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Test that Validation.Run handles unsupported results file extension. |
| 153 | + /// </summary> |
| 154 | + [TestMethod] |
| 155 | + public void Validation_Run_WithUnsupportedResultsFileExtension_ShowsError() |
| 156 | + { |
| 157 | + // Arrange |
| 158 | + var tempDir = Path.Combine(Path.GetTempPath(), $"buildmark_test_{Guid.NewGuid()}"); |
| 159 | + Directory.CreateDirectory(tempDir); |
| 160 | + |
| 161 | + try |
| 162 | + { |
| 163 | + var unsupportedFile = Path.Combine(tempDir, "results.json"); |
| 164 | + var args = new[] { "--validate", "--results", unsupportedFile }; |
| 165 | + |
| 166 | + StringWriter? outputWriter = null; |
| 167 | + |
| 168 | + try |
| 169 | + { |
| 170 | + // Capture console output |
| 171 | + outputWriter = new StringWriter(); |
| 172 | + Console.SetOut(outputWriter); |
| 173 | + |
| 174 | + // Act |
| 175 | + using var context = Context.Create(args, () => new MockRepoConnector()); |
| 176 | + Validation.Run(context); |
| 177 | + |
| 178 | + // Assert - Verify error message in output (WriteError writes to Console.WriteLine) |
| 179 | + var output = outputWriter.ToString(); |
| 180 | + Assert.Contains("Unsupported results file format", output); |
| 181 | + } |
| 182 | + finally |
| 183 | + { |
| 184 | + // Restore console output |
| 185 | + var standardOutput = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }; |
| 186 | + Console.SetOut(standardOutput); |
| 187 | + |
| 188 | + outputWriter?.Dispose(); |
| 189 | + } |
| 190 | + } |
| 191 | + finally |
| 192 | + { |
| 193 | + // Cleanup |
| 194 | + if (Directory.Exists(tempDir)) |
| 195 | + { |
| 196 | + Directory.Delete(tempDir, true); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + /// <summary> |
| 202 | + /// Test that Validation.Run handles write failure for results file. |
| 203 | + /// </summary> |
| 204 | + [TestMethod] |
| 205 | + public void Validation_Run_WithInvalidResultsFilePath_ShowsError() |
| 206 | + { |
| 207 | + // Arrange |
| 208 | + var invalidPath = Path.Combine("/invalid_path_that_does_not_exist_12345678", "results.trx"); |
| 209 | + var args = new[] { "--validate", "--results", invalidPath }; |
| 210 | + |
| 211 | + StringWriter? outputWriter = null; |
| 212 | + |
| 213 | + try |
| 214 | + { |
| 215 | + // Capture console output |
| 216 | + outputWriter = new StringWriter(); |
| 217 | + Console.SetOut(outputWriter); |
| 218 | + |
| 219 | + // Act |
| 220 | + using var context = Context.Create(args, () => new MockRepoConnector()); |
| 221 | + Validation.Run(context); |
| 222 | + |
| 223 | + // Assert - Verify error message in output (WriteError writes to Console.WriteLine) |
| 224 | + var output = outputWriter.ToString(); |
| 225 | + Assert.Contains("Failed to write results file", output); |
| 226 | + } |
| 227 | + finally |
| 228 | + { |
| 229 | + // Restore console output |
| 230 | + var standardOutput = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }; |
| 231 | + Console.SetOut(standardOutput); |
| 232 | + |
| 233 | + outputWriter?.Dispose(); |
| 234 | + } |
| 235 | + } |
| 236 | +} |
0 commit comments