Skip to content

refactor(tests): extract shared console capture helper - #162

Open
701tkdgns wants to merge 1 commit into
Unity-Technologies:mainfrom
701tkdgns:refactor/issue-139-console-capture
Open

701tkdgns wants to merge 1 commit into
Unity-Technologies:mainfrom
701tkdgns:refactor/issue-139-console-capture

Conversation

@701tkdgns

Copy link
Copy Markdown

Summay

Part of #139

Extract duplicated console-capture logic into a shared CommandTestHelper.

Changes

  • Add CommandTestHelper.RunAsync() to capture exit codes, stdout, and stderr.
  • Refactor AnalyzeOverwriteTests and AnalyzeExitCodeTests to use the shared helper.
  • Preserve existing test assertions and return values.

Testing

  • Release build Succeeded.
  • All 5 targeted NUnit tests passed.
  • Full test suite: 866 passed, 0 failed, 10 skipped.

Scope

This PR addresses the console-capture portion of #139. Output-folder setup and cleanup remain outside its scope.

@SkowronskiAndrew SkowronskiAndrew left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for taking this on, but it needs a fair bit more work to be helpful. As it stands the PR adds a second helper class for something the test project already has a helper for, and it only migrates two of the many copies, so overall it adds to the duplication instead of reducing it. Here is what it would need to be mergeable.

  1. Extend ConsoleTestHelper instead of adding CommandTestHelper

UnityDataTool.Tests/ConsoleTestHelper.cs already exists for this ("Helpers for running UnityDataTool commands while capturing their console output"). It has RunCapturingStdErr and RunCapturingStdOut, and several Analyze fixtures already use it. Please delete CommandTestHelper.cs and add the both-streams variant to ConsoleTestHelper, named to match the existing methods, for example:

public static async Task<(int exitCode, string stdOut, string stdErr)> RunCapturingOutput(params string[] args)

  1. Migrate all the private run-and-capture wrappers, not just two

These fixtures each have their own copy of the redirect/Program.Main/restore code in a private method. All of them should call ConsoleTestHelper and either be deleted or shrink to a one-line wrapper that adds the fixed arguments ("analyze", "find-refs" ... --stdout):

  • AnalyzeExitCodeTests.RunAnalyze (both streams)
  • AnalyzeOverwriteTests.RunAnalyze (stderr)
  • AnalyzeDuplicateNameTests.RunAnalyze (stderr; identical to the AnalyzeOverwriteTests copy)
  • SerializedFileCommandTests.RunCaptured (both streams; the same as the proposed helper)
  • FindRefsTests.RunFindRefsOn (stdout)
  • SceneBundleContentTests.RunAndCaptureStdout (stdout; asserts the exit code is 0 inside the helper, so keep that check at the call site)

Keep the existing assertions unchanged. If a wrapper used to capture only one stream, have it call the matching single-stream method, so this PR doesn't change what each test sees.

  1. Inline copies: either include them or list them for a follow-up

Many tests also redirect the console inline, inside the test method itself. The main places are DumpTests, ArchiveTests, SerializedFileCommandTests, UnityDataToolPlayerDataTests (the two no-TypeTree tests named in #139), ExtractedTypeTreeTests, UnityDataToolAssetBundleTests and WebBundleSupportTests. Converting these is welcome but not required for this PR. If you leave them, please say so in the PR description so #139 stays open with an accurate list of what's left.

  1. Out of scope

The output-folder setup and teardown that #139 also mentions is a separate change. Please leave it out of this PR.

  1. Small things
  • Use the repo's commit format: [#139] Short description.
  • Remove the extra blank line after RunAnalyze in AnalyzeExitCodeTests.cs.
  • After the migration, check for using directives that are no longer needed, e.g. System.IO where StringWriter was the only use.
  • Run the full dotnet test and include the results.

@701tkdgns

701tkdgns commented Nov 23, 2026 •

Copy link
Copy Markdown
Author

Thank you for the detailed feedback and guidance!

I missed the existing ConsoleTestHelper in my initial implementation.

I'll update the PR to extend ConsoleTestHelper instead of introducing a new helper class, and migrate all six private capture wrappers while preserving their existing behavior and assertions.

I'll also document the remaining inline copies, follow the repository's commit message format, and rerun the full test suite.

Thanks again for your review!

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.

2 participants