Skip to content

Add cross-platform unit tests with MSTest framework#21

Merged
Malcolmnixon merged 7 commits intomainfrom
copilot/add-unit-tests-for-dotnet-tool
Dec 25, 2025
Merged

Add cross-platform unit tests with MSTest framework#21
Malcolmnixon merged 7 commits intomainfrom
copilot/add-unit-tests-for-dotnet-tool

Conversation

Copy link
Contributor

Copilot AI commented Dec 25, 2025

Implementation Plan for Cross-Platform Unit Tests

  • Create test project structure
    • Create /test/DemaConsulting.DotnetToolWrapper.Tests directory
    • Create test project file with MSTest framework
    • Add project reference to source project
    • Create Runner.cs helper class
  • Implement unit tests
    • Test for missing DotnetToolWrapper.json file
    • Test for exit codes (0, 1, 42, 255)
    • Test for argument passing
    • Test for cross-platform compatibility
    • Test for unsupported target
    • Test for bad configuration
    • Test for GetOs, GetArchitecture, GetTarget methods
  • Update solution file
    • Add test project to solution
  • Update build workflow
    • Add Test job with matrix testing (windows, macOS, linux)
    • Keep Artifact Creation job on ubuntu-latest only
    • Add explicit permissions to workflow jobs for security
  • Update .gitignore
    • Add common .NET build artifacts
  • Run tests locally to verify functionality (All 11 tests passing on net8.0, net9.0, net10.0)
  • Address code review feedback
    • Updated Runner to capture both stdout and stderr
    • Added using statement to properly dispose Process
    • Split tests into separate files (ProgramTests.cs for unit tests, IntegrationTests.cs for DLL execution tests)
    • Use COMSPEC environment variable for complete Windows shell path instead of just "cmd.exe"
  • Security validation - All security issues resolved
  • Implementation complete
Original prompt

This section details on the original issue you should resolve

<issue_title>[Feature]: Cross-platform unit tests</issue_title>
<issue_description>### 🎯 Problem Statement

The project lacks unit tests.

💡 Proposed Solution

Add a unit test project under "/test/DemaConsulting.DotnetToolWrapper.Tests" folder with the following:

  • Tests written using the AAA (Arrange-Act-Assert) pattern
  • Using the MSTest framework
  • The test project dependent on the DotnetToolWrapper source project and copying its assemblies
  • A helper Runner.cs containing a Runner class
  • Tests that:
    1. Create a temporary 'DotnetToolWrapper.json' file
    2. Use the Runner class to run 'dotnet' to execute 'DemaConsulting.DotnetToolWrapper.dll' with test arguments
  • The tests should include such things as:
    • Verifying a missing DotnetToolWrapper.json results in the expected error
    • Verifying exit codes work (possibly by running the shell to exit with a test exit code)
    • Verifying arguments are passed (possibly by running the shell to echo the arguments)
    • Compatible with different operating systems (windows, macOS, linux)

The build.yaml should add a "Test" job to run the unit tests with matrix-test on windows, macOS, and linux; although the existing "Artifact Creation" job needs to only run on ubuntu-latest.

🔄 Alternatives Considered

No response

📝 Additional Context

The Runner class should be structured as follows:

/// <summary>
/// Program runner class
/// </summary>
internal static class Runner
{
    /// <summary>
    /// Run the specified program
    /// </summary>
    /// <param name="output">Program output</param>
    /// <param name="program">Program name</param>
    /// <param name="arguments">Program arguments</param>
    /// <returns>Program exit code</returns>
    /// <exception cref="InvalidOperationException">On program start error</exception>
    public static int Run(out string output, string program, params string[] arguments)
    {
        // Construct the start information
        var startInfo = new ProcessStartInfo(program)
        {
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        // Add the arguments
        foreach (var argument in arguments)
            startInfo.ArgumentList.Add(argument);

        // Start the process
        var process = Process.Start(startInfo) ??
                      throw new InvalidOperationException("Failed to start process");

        // Wait for the process to exit
        process.WaitForExit();

        // Save the output and return the exit code
        output = process.StandardOutput.ReadToEnd();
        return process.ExitCode;
    }
}

🤝 Contribution

  • I would be willing to implement this feature</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits December 25, 2025 00:39
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
Copilot AI changed the title [WIP] Add cross-platform unit tests for DotnetToolWrapper Add cross-platform unit tests with MSTest framework Dec 25, 2025
Copilot AI requested a review from Malcolmnixon December 25, 2025 00:46
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
/// <returns>Shell program name</returns>
private static string GetShellProgram()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "/bin/sh";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"cmd.exe" is not a complete path to the windows shell. Consider replacing this with using environment variables or some other more reliable way to get an absolute path to the shell program.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please try fixing this again - you encountered a failure last time because you got stuck in a loop forever failing to read CI workflow logs - do not try to read them when working on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 81f9cce. Changed GetShellProgram() to use the COMSPEC environment variable on Windows, which provides the full path to cmd.exe (typically C:\Windows\System32\cmd.exe), instead of just "cmd.exe".

Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
@Malcolmnixon Malcolmnixon marked this pull request as ready for review December 25, 2025 01:32
@Malcolmnixon Malcolmnixon merged commit a263750 into main Dec 25, 2025
8 checks passed
@Malcolmnixon Malcolmnixon deleted the copilot/add-unit-tests-for-dotnet-tool branch December 25, 2025 01:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Cross-platform unit tests

2 participants