Skip to content

chore: Add unit tests #461

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft

Conversation

MaxymVlasov
Copy link
Collaborator

@MaxymVlasov MaxymVlasov commented Jan 27, 2023

Put an x into the box if that apply:

  • This PR introduces breaking change.
  • This PR fixes a bug.
  • This PR adds new functionality.
  • This PR enhances existing functionality.

Description of your changes

Closes #303

How can we test changes

  1. Download shunit2 from https://github.com/kward/shunit2
  2. Add shunit2 to PATH
  3. cd to pre-commit-terraform/tests/shunit2
  4. Run ./_common_test.sh

Summary by CodeRabbit

  • Tests
    • Added new automated tests to verify command line argument parsing and environment variable handling in shell scripts.

@MaxymVlasov MaxymVlasov changed the title Add unit tests chore: Add unit tests Jan 27, 2023
@MaxymVlasov MaxymVlasov changed the title chore: Add unit tests tests: Add unit tests Jan 27, 2023
@MaxymVlasov MaxymVlasov changed the title tests: Add unit tests chore: Add unit tests Jan 27, 2023
@github-actions

This comment was marked as outdated.

@github-actions github-actions bot added the stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 27, 2023
@MaxymVlasov MaxymVlasov removed the stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 27, 2023
@github-actions

This comment was marked as outdated.

@github-actions github-actions bot added the stale Denotes an issue or PR has remained open with no activity and has become stale. label Mar 30, 2023
@MaxymVlasov MaxymVlasov removed the stale Denotes an issue or PR has remained open with no activity and has become stale. label Mar 30, 2023
@github-actions

This comment was marked as outdated.

@github-actions github-actions bot added the stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 30, 2023
@MaxymVlasov MaxymVlasov added wip Indicates that an issue or PR is actively being worked on by a contributor. and removed stale Denotes an issue or PR has remained open with no activity and has become stale. labels May 2, 2023
@MaxymVlasov MaxymVlasov added on-hold Indicates that an issue or PR should not be auto-closed due to staleness. and removed wip Indicates that an issue or PR is actively being worked on by a contributor. labels May 28, 2025
Copy link

coderabbitai bot commented May 28, 2025

Walkthrough

A new shell test script was added for shunit2 to test functions in the _common.sh hook script. The script sets up the environment, sources the target script, and implements tests for command-line parsing and environment variable expansion functions, using assertions to verify correct behavior.

Changes

File(s) Change Summary
tests/shunit2/_common_test.sh Added new shunit2-based shell test script for _common.sh functions, including setup and assertions

Assessment against linked issues

Objective Addressed Explanation
Add unit-tests that will found problems with new code (#303)
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (4)
tests/shunit2/_common_test.sh (4)

13-14: Fix typo in comment
The word “clolrs” should be “colors” for clarity.

-  # Disable hooks clolrs
+  # Disable hook colors

21-24: Remove commented-out dead code
The test__initialize stub is empty and commented out. Consider deleting it to keep the test file clean.


25-76: Split the large parsing test into focused subtests
This function covers multiple assertions on different arrays (ARGS, HOOK_CONFIG, TF_INIT_ARGS, ENV_VARS, FILES). Extracting each section into its own test function will improve readability, ease debugging on failure, and allow isolated maintenance.


97-98: Check for shunit2 availability
Sourcing shunit2 without verifying its presence can lead to a confusing “file not found” error. Consider adding a guard with a clear message if shunit2 isn’t on PATH.

-. shunit2
+command -v shunit2 >/dev/null 2>&1 || {
+  echo "ERROR: shunit2 not found in PATH; please install and retry." >&2
+  exit 1
+}
+. shunit2
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 55d0143 and 80aff3a.

📒 Files selected for processing (1)
  • tests/shunit2/_common_test.sh (1 hunks)
🔇 Additional comments (2)
tests/shunit2/_common_test.sh (2)

78-95: Approve parsing-and-export test
The test__parse_and_export_env_vars function correctly sets up ARGS, invokes common::parse_and_export_env_vars, and verifies the placeholders are expanded. This covers the intended functionality well.


17-20:

❓ Verification inconclusive

Verify HOOK_ID evaluation
The test asserts $HOOK_ID equals _common_test, which depends on how common.sh sets HOOK_ID when sourced. Please confirm that sourcing via the test harness produces the expected value and that this behavior won’t change unintentionally.


🏁 Script executed:

#!/bin/bash
# Verify where and how HOOK_ID is set in common.sh
rg -n "HOOK_ID" hooks/_common.sh

Length of output: 87


Could you please show how hooks/_common.sh is being sourced in tests/shunit2/_common_test.sh (and how the test is executed under Shunit2)? Specifically, I’d like to confirm whether $0 inside hooks/_common.sh really points to _common_test.sh (not the Shunit2 runner), so that HOOK_ID=${0##*/} yields _common_test.sh_common_test. Please share the snippet around the source (or .) invocation and any invocation line for Shunit2 that runs this test.

Comment on lines +6 to +9
function oneTimeSetUp {
# Load stuff for tests.
. ../../hooks/_common.sh
# Global ENV vars in --args
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Make hook script sourcing path robust
Using a relative path (../../hooks/_common.sh) assumes the test is always run from tests/shunit2. To avoid breakage if the working directory changes, compute the script’s directory via ${BASH_SOURCE[0]} and source from there.

 function oneTimeSetUp {
-  . ../../hooks/_common.sh
+  local script_dir
+  script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+  . "$script_dir"/../../hooks/_common.sh
   # Global ENV vars in --args
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function oneTimeSetUp {
# Load stuff for tests.
. ../../hooks/_common.sh
# Global ENV vars in --args
function oneTimeSetUp {
# Load stuff for tests.
local script_dir
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "$script_dir"/../../hooks/_common.sh
# Global ENV vars in --args
}
🤖 Prompt for AI Agents
In tests/shunit2/_common_test.sh around lines 6 to 9, the script sources
../../hooks/_common.sh using a relative path, which breaks if the working
directory changes. Modify the script to determine its own directory using
${BASH_SOURCE[0]} and then source _common.sh relative to that directory to
ensure the path is always correct regardless of where the test is run from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
on-hold Indicates that an issue or PR should not be auto-closed due to staleness.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add unit-tests that will found problems with new code
1 participant