Skip to content
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

Introduce stub_env helper #210

Merged
merged 1 commit into from
Jul 19, 2024
Merged

Introduce stub_env helper #210

merged 1 commit into from
Jul 19, 2024

Conversation

zzaakiirr
Copy link
Contributor

@zzaakiirr zzaakiirr commented Jul 16, 2024

What does this PR do?

This PR adds stub_env helper which fully stubs ENV variables and more reliable than current approach

Why?

Noticed that ENV variables stubbing in specs are "too wordy" and strongly linked to how they are accessed in classes/modules which are being tested:

allow(ENV).to receive(:fetch).with("CPLN_ENDPOINT", "https://api.cpln.io").and_return("https://api.cpln.io")
  1. We shouldn't care about 2nd argument passed to ENV.fetch (which is default ENV variable). All we care about is that ENV variable is set and returns some value
  2. Other related methods will behave as if ENV variable isn't set. ENV.key?("CPLN_ENDPOINT") will return false, for example

Note

StubENV module is copied from gitlab-rspec gem

Summary by CodeRabbit

  • Tests

    • Enhanced test setup by introducing stub_env method for stubbing environment variables.
    • Updated multiple test files to use stub_env for setting environment variables, improving test isolation and management.
    • Added a new module StubENV and corresponding tests to support environment variable stubbing.
  • Chores

    • Included StubENV in RSpec configuration for consistent use across tests.

Copy link

coderabbitai bot commented Jul 16, 2024

Walkthrough

The changes introduce a new method, stub_env, to handle environment variables in tests. This method replaces direct assignments to ENV, enhancing test isolation and control. Specifically, it ensures a more predictable and consistent test environment by stubbing environment variables within multiple test files. Additionally, a new module StubENV and its corresponding tests are added to support this functionality.

Changes

Files Change Summary
spec/command/copy_image_from_upstream_spec.rb, spec/command/deploy_image_spec.rb, spec/command/promote_app_from_upstream_spec.rb, spec/command/setup_app_spec.rb Replaced direct ENV assignments with stub_env for setting environment variables to improve test management.
spec/core/controlplane_api_direct_spec.rb, spec/core/controlplane_spec.rb Updated environment variable handling using stub_env to ensure proper settings in test scenarios.
spec/spec_helper.rb Added StubENV module to RSpec configuration for centralized environment variable stubbing in tests.
spec/support/stub_env.rb Introduced StubENV module with methods to stub environment variables, providing controlled handling of ENV variables in tests.
spec/support_specs/stub_env_spec.rb Added tests for StubENV module to verify the correct behavior of the stub_env method in various scenarios, ensuring environment variables are stubbed as expected.

Poem

In the code's quiet stream they pledge,
Environment variables now hedge,
stub_env they call with care,
Ensuring tests are light as air.
A bunny hops through fields of green,
With code that's clean and always keen.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @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.

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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

@zzaakiirr zzaakiirr force-pushed the stub-env-helper-for-specs branch from c08905a to 23f8fd0 Compare July 16, 2024 16:52
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: 0

Outside diff range, codebase verification and nitpick comments (1)
spec/support_specs/stub_env_spec.rb (1)

5-5: Clarify the source of the copied code.

The repository file spec/support/stub_env.rb differs from the provided external source. The comment in the repository file mentions it was inspired by a different URL (https://github.com/ljkbennett/stub_env/blob/master/lib/stub_env/helpers.rb). Please verify and update the attribution to ensure proper compliance with licensing requirements.

  • spec/support/stub_env.rb: Update the comment to reflect the correct source URL.
Analysis chain

Verify the source of the copied code.

The comment indicates that the code was copied from an external source. It's essential to ensure that this is compliant with licensing requirements and that the code is up to date.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the external source of the copied code.

# Test: Open the URL in a browser or check the latest version of the file.
echo "Please manually verify the source at https://gitlab.com/gitlab-org/gitlab/-/blob/master/gems/gitlab-rspec/lib/gitlab/rspec/stub_env.rb"

Length of output: 278


Script:

#!/bin/bash
# Description: Verify the content of the copied code in the repository and compare it with the external source.

# Step 1: Extract the content of the specified file from the external source.
curl -s https://gitlab.com/gitlab-org/gitlab/-/raw/master/gems/gitlab-rspec/lib/gitlab/rspec/stub_env.rb -o /tmp/external_stub_env.rb

# Step 2: Find the corresponding file in the repository.
fd stub_env.rb

# Step 3: Compare the content of the external file with the repository file.
fd stub_env.rb --exec diff -q {} /tmp/external_stub_env.rb

Length of output: 310


Script:

#!/bin/bash
# Description: Show the differences between the external source file and the repository file.

# Step 1: Extract the content of the specified file from the external source.
curl -s https://gitlab.com/gitlab-org/gitlab/-/raw/master/gems/gitlab-rspec/lib/gitlab/rspec/stub_env.rb -o /tmp/external_stub_env.rb

# Step 2: Find the corresponding file in the repository.
repo_file=$(fd stub_env.rb)

# Step 3: Show the differences between the external file and the repository file.
diff -u "$repo_file" /tmp/external_stub_env.rb

Length of output: 873

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 38acf16 and 23f8fd0.

Files selected for processing (9)
  • spec/command/copy_image_from_upstream_spec.rb (3 hunks)
  • spec/command/deploy_image_spec.rb (2 hunks)
  • spec/command/promote_app_from_upstream_spec.rb (3 hunks)
  • spec/command/setup_app_spec.rb (1 hunks)
  • spec/core/controlplane_api_direct_spec.rb (4 hunks)
  • spec/core/controlplane_spec.rb (2 hunks)
  • spec/spec_helper.rb (2 hunks)
  • spec/support/stub_env.rb (1 hunks)
  • spec/support_specs/stub_env_spec.rb (1 hunks)
Files skipped from review due to trivial changes (2)
  • spec/command/promote_app_from_upstream_spec.rb
  • spec/spec_helper.rb
Additional comments not posted (15)
spec/support_specs/stub_env_spec.rb (3)

9-17: Approve the basic functionality test of stub_env.

The test checks all the basic functionalities of the stub_env method, ensuring it correctly stubs the environment variable for different access methods.


19-25: Approve the handling of nil values in stub_env.

This test ensures that stub_env correctly handles nil values by using default values or raising errors as expected.


3-3: Ensure required module is correctly referenced.

The require statement is critical for functionality. Ensure that the path provided matches the actual location of the stub_env module in the project structure.

Verification successful

The required module is correctly referenced.

The require "support/stub_env" statement correctly references the spec/support/stub_env.rb file.

  • spec/support/stub_env.rb exists and matches the required path.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence of the required module file.

# Test: Search for the file. Expect: At least one occurrence.
fd --type file "stub_env.rb"

Length of output: 53

spec/support/stub_env.rb (4)

13-23: Approve the flexible handling of input types in stub_env.

The method correctly handles both Hash and single key-value pairs, providing flexibility in how environment variables can be stubbed.


29-38: Ensure correct and secure handling of stubbed values.

The method correctly intercepts calls to ENV methods and returns the appropriate stubbed values or raises errors as expected. This is crucial for ensuring the integrity of the stubbing process.


40-49: Verify the initialization and re-initialization of stubs.

The method init_stub ensures that original behaviors are restored before new stubs are added, which is important for test isolation.


3-3: Verify the source of the copied code.

As with the test file, ensure compliance with licensing requirements and verify the code's origin.

spec/core/controlplane_api_direct_spec.rb (1)

10-10: Approve the integration of stub_env in various scenarios.

The changes effectively replace previous ENV stubbing methods with stub_env, enhancing consistency and isolation in tests.

Also applies to: 18-18, 42-42, 51-51, 61-61

spec/core/controlplane_spec.rb (1)

22-22: Approve the integration of stub_env in command output scenarios.

The changes effectively replace previous ENV stubbing methods with stub_env, enhancing consistency and isolation in tests.

Also applies to: 51-51, 59-59

spec/command/deploy_image_spec.rb (2)

65-65: Correct use of stub_env method.

The introduction of stub_env("APP_NAME", app) correctly replaces the direct setting of the APP_NAME environment variable, improving test isolation and consistency.


95-95: Correct use of stub_env method.

The usage of `stub_env("APP

_NAME", app)` is consistent and appropriate, ensuring that the environment variable is correctly set for the test context.

spec/command/copy_image_from_upstream_spec.rb (3)

49-49: Correct use of stub_env method.

The stub_env("CPLN_UPSTREAM", upstream_app) is correctly used to replace direct environment variable manipulation, enhancing test isolation.


65-67: Correct use of stub_env method.

The use of stub_env for both CPLN_UPSTREAM and CPLN_ORG_UPSTREAM is appropriate and improves test isolation by avoiding direct environment variable manipulation.


96-98: Correct use of stub_env method.

The consistent use of stub_env for setting environment variables ensures better test control and isolation.

spec/command/setup_app_spec.rb (1)

165-165: Correct use of stub_env method.

Replacing ENV.fetch with stub_env("CPLN_ORG", nil) is a good practice for test isolation, ensuring that the environment variable is unset in a controlled manner.

@borela borela requested a review from rafaelgomesxyz July 16, 2024 17:13
@borela borela merged commit c595899 into main Jul 19, 2024
5 checks passed
@borela borela deleted the stub-env-helper-for-specs branch July 19, 2024 00:24
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