-
Notifications
You must be signed in to change notification settings - Fork 0
Increase code coverage for PathHelpers and Validation #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
test/DemaConsulting.BuildMark.Tests/PathHelpersTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Copyright (c) DEMA Consulting | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in all | ||
| // copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| // SOFTWARE. | ||
|
|
||
| namespace DemaConsulting.BuildMark.Tests; | ||
|
|
||
| /// <summary> | ||
| /// Tests for the PathHelpers class. | ||
| /// </summary> | ||
| [TestClass] | ||
| public class PathHelpersTests | ||
| { | ||
| /// <summary> | ||
| /// Test that SafePathCombine correctly combines valid paths. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void PathHelpers_SafePathCombine_ValidPaths_CombinesCorrectly() | ||
| { | ||
| // Arrange | ||
| var basePath = "/home/user/project"; | ||
| var relativePath = "subfolder/file.txt"; | ||
|
|
||
| // Act | ||
| var result = PathHelpers.SafePathCombine(basePath, relativePath); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual(Path.Combine(basePath, relativePath), result); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Test that SafePathCombine throws ArgumentException for path traversal with double dots. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void PathHelpers_SafePathCombine_PathTraversalWithDoubleDots_ThrowsArgumentException() | ||
| { | ||
| // Arrange | ||
| var basePath = "/home/user/project"; | ||
| var relativePath = "../etc/passwd"; | ||
|
|
||
| // Act & Assert | ||
| var exception = Assert.Throws<ArgumentException>(() => | ||
| PathHelpers.SafePathCombine(basePath, relativePath)); | ||
| Assert.Contains("Invalid path component", exception.Message); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Test that SafePathCombine throws ArgumentException for path with double dots in middle. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void PathHelpers_SafePathCombine_DoubleDotsInMiddle_ThrowsArgumentException() | ||
| { | ||
| // Arrange | ||
| var basePath = "/home/user/project"; | ||
| var relativePath = "subfolder/../../../etc/passwd"; | ||
|
|
||
| // Act & Assert | ||
| var exception = Assert.Throws<ArgumentException>(() => | ||
| PathHelpers.SafePathCombine(basePath, relativePath)); | ||
| Assert.Contains("Invalid path component", exception.Message); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Test that SafePathCombine throws ArgumentException for absolute paths. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void PathHelpers_SafePathCombine_AbsolutePath_ThrowsArgumentException() | ||
| { | ||
| // Test Unix absolute path | ||
| var unixBasePath = "/home/user/project"; | ||
| var unixRelativePath = "/etc/passwd"; | ||
| var unixException = Assert.Throws<ArgumentException>(() => | ||
| PathHelpers.SafePathCombine(unixBasePath, unixRelativePath)); | ||
| Assert.Contains("Invalid path component", unixException.Message); | ||
|
|
||
| // Test Windows absolute path (only on Windows since Windows paths may not be rooted on Unix) | ||
| if (OperatingSystem.IsWindows()) | ||
| { | ||
| var windowsBasePath = "C:\\Users\\project"; | ||
| var windowsRelativePath = "C:\\Windows\\System32\\file.txt"; | ||
| var windowsException = Assert.Throws<ArgumentException>(() => | ||
| PathHelpers.SafePathCombine(windowsBasePath, windowsRelativePath)); | ||
| Assert.Contains("Invalid path component", windowsException.Message); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| // Copyright (c) DEMA Consulting | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in all | ||
| // copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| // SOFTWARE. | ||
|
|
||
| using DemaConsulting.BuildMark.RepoConnectors; | ||
|
|
||
| namespace DemaConsulting.BuildMark.Tests; | ||
|
|
||
| /// <summary> | ||
| /// Tests for the Validation class. | ||
| /// </summary> | ||
| [TestClass] | ||
| public class ValidationTests | ||
| { | ||
| /// <summary> | ||
| /// Test that Validation.Run writes TRX results file when specified. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void Validation_Run_WithTrxResultsFile_WritesTrxFile() | ||
| { | ||
| // Arrange | ||
| var tempDir = Path.Combine(Path.GetTempPath(), $"buildmark_test_{Guid.NewGuid()}"); | ||
| Directory.CreateDirectory(tempDir); | ||
|
|
||
| try | ||
| { | ||
| var trxFile = Path.Combine(tempDir, "results.trx"); | ||
| var args = new[] { "--validate", "--results", trxFile }; | ||
|
|
||
| StringWriter? outputWriter = null; | ||
| StringWriter? errorWriter = null; | ||
|
|
||
| try | ||
| { | ||
| // Capture console output | ||
| outputWriter = new StringWriter(); | ||
| errorWriter = new StringWriter(); | ||
| Console.SetOut(outputWriter); | ||
| Console.SetError(errorWriter); | ||
|
|
||
| // Act | ||
| using var context = Context.Create(args, () => new MockRepoConnector()); | ||
| Validation.Run(context); | ||
|
|
||
| // Assert - Verify TRX file was created | ||
| Assert.IsTrue(File.Exists(trxFile), "TRX file should be created"); | ||
|
|
||
| // Verify TRX file contains expected content | ||
| var trxContent = File.ReadAllText(trxFile); | ||
| Assert.Contains("TestRun", trxContent); | ||
| Assert.Contains("BuildMark Self-Validation", trxContent); | ||
| } | ||
| finally | ||
| { | ||
| // Restore console output | ||
| var standardOutput = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }; | ||
| Console.SetOut(standardOutput); | ||
| var standardError = new StreamWriter(Console.OpenStandardError()) { AutoFlush = true }; | ||
| Console.SetError(standardError); | ||
|
|
||
| outputWriter?.Dispose(); | ||
| errorWriter?.Dispose(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| // Cleanup | ||
| if (Directory.Exists(tempDir)) | ||
| { | ||
| Directory.Delete(tempDir, true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Test that Validation.Run writes JUnit XML results file when specified. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void Validation_Run_WithXmlResultsFile_WritesJUnitFile() | ||
| { | ||
| // Arrange | ||
| var tempDir = Path.Combine(Path.GetTempPath(), $"buildmark_test_{Guid.NewGuid()}"); | ||
| Directory.CreateDirectory(tempDir); | ||
|
|
||
| try | ||
| { | ||
| var xmlFile = Path.Combine(tempDir, "results.xml"); | ||
| var args = new[] { "--validate", "--results", xmlFile }; | ||
|
|
||
| StringWriter? outputWriter = null; | ||
| StringWriter? errorWriter = null; | ||
|
|
||
| try | ||
| { | ||
| // Capture console output | ||
| outputWriter = new StringWriter(); | ||
| errorWriter = new StringWriter(); | ||
| Console.SetOut(outputWriter); | ||
| Console.SetError(errorWriter); | ||
|
|
||
| // Act | ||
| using var context = Context.Create(args, () => new MockRepoConnector()); | ||
| Validation.Run(context); | ||
|
|
||
| // Assert - Verify XML file was created | ||
| Assert.IsTrue(File.Exists(xmlFile), "XML file should be created"); | ||
|
|
||
| // Verify XML file contains expected content | ||
| var xmlContent = File.ReadAllText(xmlFile); | ||
| Assert.Contains("testsuites", xmlContent); | ||
| Assert.Contains("BuildMark Self-Validation", xmlContent); | ||
| } | ||
| finally | ||
| { | ||
| // Restore console output | ||
| var standardOutput = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }; | ||
| Console.SetOut(standardOutput); | ||
| var standardError = new StreamWriter(Console.OpenStandardError()) { AutoFlush = true }; | ||
| Console.SetError(standardError); | ||
|
|
||
| outputWriter?.Dispose(); | ||
| errorWriter?.Dispose(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| // Cleanup | ||
| if (Directory.Exists(tempDir)) | ||
| { | ||
| Directory.Delete(tempDir, true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Test that Validation.Run handles unsupported results file extension. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void Validation_Run_WithUnsupportedResultsFileExtension_ShowsError() | ||
| { | ||
| // Arrange | ||
| var tempDir = Path.Combine(Path.GetTempPath(), $"buildmark_test_{Guid.NewGuid()}"); | ||
| Directory.CreateDirectory(tempDir); | ||
|
|
||
| try | ||
| { | ||
| var unsupportedFile = Path.Combine(tempDir, "results.json"); | ||
| var args = new[] { "--validate", "--results", unsupportedFile }; | ||
|
|
||
| StringWriter? outputWriter = null; | ||
|
|
||
| try | ||
| { | ||
| // Capture console output | ||
| outputWriter = new StringWriter(); | ||
| Console.SetOut(outputWriter); | ||
|
|
||
| // Act | ||
| using var context = Context.Create(args, () => new MockRepoConnector()); | ||
| Validation.Run(context); | ||
|
|
||
| // Assert - Verify error message in output (WriteError writes to Console.WriteLine) | ||
| var output = outputWriter.ToString(); | ||
| Assert.Contains("Unsupported results file format", output); | ||
| } | ||
| finally | ||
| { | ||
| // Restore console output | ||
| var standardOutput = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }; | ||
| Console.SetOut(standardOutput); | ||
|
|
||
| outputWriter?.Dispose(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| // Cleanup | ||
| if (Directory.Exists(tempDir)) | ||
| { | ||
| Directory.Delete(tempDir, true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Test that Validation.Run handles write failure for results file. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void Validation_Run_WithInvalidResultsFilePath_ShowsError() | ||
| { | ||
| // Arrange | ||
| var invalidPath = Path.Combine("/invalid_path_that_does_not_exist_12345678", "results.trx"); | ||
| var args = new[] { "--validate", "--results", invalidPath }; | ||
|
|
||
| StringWriter? outputWriter = null; | ||
|
|
||
| try | ||
| { | ||
| // Capture console output | ||
| outputWriter = new StringWriter(); | ||
| Console.SetOut(outputWriter); | ||
|
|
||
| // Act | ||
| using var context = Context.Create(args, () => new MockRepoConnector()); | ||
| Validation.Run(context); | ||
|
|
||
| // Assert - Verify error message in output (WriteError writes to Console.WriteLine) | ||
| var output = outputWriter.ToString(); | ||
| Assert.Contains("Failed to write results file", output); | ||
| } | ||
| finally | ||
| { | ||
| // Restore console output | ||
| var standardOutput = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }; | ||
| Console.SetOut(standardOutput); | ||
|
|
||
| outputWriter?.Dispose(); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please examine https://github.com/demaconsulting/SarifMark/blob/main/test/DemaConsulting.SarifMark.Tests/PathHelpersTests.cs which tests SafePathCombine but doesn't need to do the inconclusive tests for different operating systems. Please use its approach instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored in commit 25322e8 to use the cleaner Assert.Throws pattern from SarifMark. Combined Unix/Windows absolute path tests into a single method with conditional Windows check, eliminating the need for inconclusive tests.