Skip to content

[CSM-440]: Add unit tests for component - #1

Open
oykukurtgoz wants to merge 1 commit into
masterfrom
test/csm-440
Open

[CSM-440]: Add unit tests for component#1
oykukurtgoz wants to merge 1 commit into
masterfrom
test/csm-440

Conversation

@oykukurtgoz

Copy link
Copy Markdown

Closes CSM-440.

Adds unit tests for main.rb.

The whole PR is 565 added lines and 14 deleted ones; main.rb accounts for 25 added and 14 deleted. Nothing in the test logic was reworded — the flutter pub global activate junitreport command, the cd … && flutter test … command, the tojunit pipe, the flutter_reports folder and the env-file export are byte-for-byte what they were, and the tests pin that.

Changes to main.rb

Three, all driven by the issue's prerequisite refactor and its "replace nil-unsafe access with an explicit raise carrying context" requirement.

Top-level side effects are guarded. The three get_env_variable reads, the JUnit branch, the final run_command and exit 0 now sit inside a single if __FILE__ == $PROGRAM_NAME … end block, in their original order. The only visible move is that the three variable reads, which used to sit between the two function definitions, now open the guarded block — they had to, since the first one calls abort and would have killed the test process at require time. Appcircle runs the step with ruby main.rb, so the guard is true there and the body executes exactly as before. require-ing the file only defines get_env_variable and run_command.

No begin/rescue LoadError guards were needed: main.rb only requires open3, pathname and fileutils, all stdlib.

AC_OUTPUT_DIR, AC_ENV_FILE_PATH and HOME are validated up front in the JUnit branch. main.rb used to read all three straight from ENV[...]. With AC_OUTPUT_DIR missing, File.join(nil, …) raised a bare TypeError; with AC_ENV_FILE_PATH missing, File.open(nil, 'a') did the same; with either empty, the path silently became /flutter_reports or ''. HOME was the worst of the three: missing or empty, the pipe target became /.pub-cache/bin/tojunit, flutter pub global activate junitreport still ran, and the step only failed when the shell could not find tojunit at the end of the pipe. All three now go through get_env_variable and raise Missing <VAR>. … with the reason before anything is written or executed. Before and after, the step fails in those scenarios and exits 1; the difference is that it fails early and says why. Appcircle always sets these variables, so normal runs are unaffected. When AC_FLUTTER_JUNIT_REPORTS is not YES, none of the three is required, as before.

run_command rejects a nil or blank command. A one-line guard at the top of the function raises run_command: command must not be nil or empty. instead of letting Open3.popen3 fail with a TypeError / Errno::ENOENT. Neither call site in main.rb can pass such a value, so this never triggers in a real run.

Tests (test/test_main.rb)

Self-executing, 42 examples, no Gemfile / Bundler — rspec gem + Ruby stdlib only. ReadableFormatter and the coverage report are reused from CSM-230 / CSM-231.

ruby test/test_main.rb

main.rb exposes two functions plus the script as a whole, and the tests cover all three surfaces:

  • get_env_variable, in-process — returns the value when set, returns nil for both a missing key and an empty string, works as the left side of || default, and the || abort(...) pattern used at the call site raises SystemExit with status 1.
  • run_command, in-process — happy path with true / echo, the echoed @@[command] line, streamed stdout; nil, empty and whitespace-only commands raise with context and never reach Open3.popen3; the failure branch raises RuntimeError carrying the child's stderr and prints that stderr first. Only true, echo and sh -c are ever executed.
  • ENV validation, through Open3.capture3(env, "ruby #{MAIN_RB}") — a missing and an empty case for each of AC_FLUTTER_PROJECT_DIR, AC_OUTPUT_DIR, AC_ENV_FILE_PATH and HOME, each asserting exit status 1, the Missing … message on stderr, no write to the env file and no toolchain invocation. One further case asserts that with AC_FLUTTER_JUNIT_REPORTS=NO none of the three JUnit variables is required.
  • Composed commands, through the same subprocess helper against a fake toolchain — PATH is restricted to a tmpdir bin/ holding a fake flutter that only records its arguments, and a fake $HOME/.pub-cache/bin/tojunit that drains stdin. Ten cases assert the exact @@[command] strings for the JUnit and non-JUnit paths, the default --machine, custom AC_FLUTTER_TEST_EXTRA_ARGS both with and without the tojunit pipe, the AC_TEST_RESULT_PATH=… line appended (not overwritten) to the env file, the flutter_reports folder being created, the fake log showing exactly flutter pub global activate junitreportflutter test --machinetojunit --output …, and a failing flutter test propagating as exit 1 with its stderr surfaced.

No real toolchain, network, or filesystem writes outside Dir.mktmpdir. The real Flutter SDK is never on PATH in any subprocess case.

Coverage

✔  All 42 tests passed
main.rb  59.5%  (22/37 lines)

The number is structural rather than a gap in the tests. Of the 37 executable lines, 22 are the requires and the two function bodies, and those are fully covered in-process. The remaining 15 are the guarded step body: variable reads, JUnit-branch validation, env-file export, mkdir_p and the two run_command calls. That block only runs in the Open3 subprocesses, which in-process Coverage cannot see — but unlike a Gradle or Xcode step, every one of those lines is executed by the fake-toolchain cases, both the JUnit and the non-JUnit path. Merging subprocess coverage the way CSM-448 does would raise the figure to 100% without adding a test; extracting the command composition into a pure function would too. Both are reasonable follow-ups; neither is in this PR.

Docs

README.md gains a ## Running tests section.

🤖 Generated with Claude Code

- Guard top-level side-effecting code with `if __FILE__ == $PROGRAM_NAME`
  so main.rb can be required from tests; function definitions stay top level
- Replace nil-unsafe ENV access in the JUnit branch (AC_OUTPUT_DIR,
  AC_ENV_FILE_PATH, HOME) with explicit raises carrying context
- Guard run_command against nil/empty commands
- Add self-executing test/test_main.rb (rspec + stdlib, no Gemfile):
  get_env_variable, run_command, ENV validation via Open3 subprocess,
  composed-command assertions against a fake flutter/tojunit toolchain
- Print coverage report at the end of the run
- Document `ruby test/test_main.rb` in README

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Sep 7, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: a42682a2-65cb-4d0b-89d3-0413878747c2


Comment @coderabbitai help to get the list of available commands.

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.

1 participant