Skip to content

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

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

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

Conversation

@oykukurtgoz

Copy link
Copy Markdown

Closes CSM-540.

Adds unit tests for main.rb.

The whole PR is 462 added lines and 12 deleted ones; main.rb accounts for 17 added and 12 deleted. Nothing in the install logic was moved or reworded — the version selection, the Selected node version is … line, the n / sudo n commands and the Unexpected OS abort are byte-for-byte what they were.

Changes to main.rb

Two, both driven by the issue's prerequisite refactor.

Top-level side effects are guarded. The version-selection lines that used to sit between get_version and run_command, and the OS branch at the bottom of the file, now sit together inside a single if __FILE__ == $PROGRAM_NAME ... end block, in their original order. 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 env_has_key, get_version and run_command.

os is loaded inside begin/rescue LoadError. It is a gem rather than stdlib, so a bare test environment may not have it. When it is installed nothing changes; when it is not, the file still loads and the tests supply a stub OS module. open3 is stdlib and is left as a plain require.

No nil-unsafe access surfaced in the real flow, so no explicit raise was added. env_has_key(nil) and run_command(nil) are never reached by the script; the tests pin their current TypeError behaviour rather than change it.

Tests (test/test_main.rb)

Self-executing, 28 examples, no Gemfile / Bundler — rspec gem + Ruby stdlib only.

ruby test/test_main.rb

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

  • env_has_key, in-process — returns the value when set, returns nil for a missing key, an empty string and an empty key name, raises TypeError for a nil key.
  • get_version, in-process — returns the config version when set, falls back to the component version when the config version is nil, returns an empty config version as-is, returns nil when both are nil.
  • run_command, in-process — Open3.popen3 is stubbed with StringIO streams and a fake wait_thr, so no real command runs. Happy path, the echoed @@[command] line and forwarded stdout lines, the exact command string handed to popen3, the failure branch (SystemExit status 1 carrying the child's stderr, and an empty stderr), plus nil and empty commands. Two further cases run a real echo and a real sh -c "exit 1" the same way the reference PRs do.
  • The script as a whole, in-process — $PROGRAM_NAME is pointed at main.rb and the file is load-ed with Open3.popen3 stubbed and OS.linux? / OS.mac? stubbed per case. This asserts the composed command string end to end: n lts by default, n 18 from AC_SELECTED_NODE_VERSION, AC_NODE_JS_VERSION overriding it, empty values of either being ignored, sudo n … on macOS, Unexpected OS abort with no command on any other OS, and the stderr of a failing install being propagated through abort.
  • Loading as a libraryrequire-ing main.rb in a fresh process through Open3.capture3 exits 0 and prints no Selected node version line.

The script declares no required environment variables, so there are no missing-and-empty validation subprocess cases; the empty-value behaviour of the two optional variables is covered by the in-process step cases above.

No real toolchain (n, sudo), network, or filesystem writes.

Coverage

✔  All 28 tests passed
main.rb  100.0%  (29/29 lines)

One detail worth knowing: Ruby's Coverage resets a file's counters every time that file is load-ed again. Since the step-level cases load main.rb once each, the test file takes a Coverage.peek_result snapshot before every load and sums the snapshots into the final report. Without that the report would only reflect the last load.

Docs

README.md gains a ## Running tests section.

🤖 Generated with Claude Code

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: f6109bae-bbe1-4c1f-9cfa-88015140bac1


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