🫥 Unobtrusively stub/hide ENV keys and values during testing
[![Version][👽versioni]][👽version] [![Ruby Users Forum][✉️ruby-forum-top-img]][✉️ruby-forum] [![GitHub tag (latest SemVer)][⛳️tag-img]][⛳️tag] [![License: MIT][📄license-img]][📄license] [![Total downloads][👽dl-ranki]][👽dl-rank] [![CodeCov Test Coverage][🏀codecovi]][🏀codecov] [![Coveralls Test Coverage][🏀coveralls-img]][🏀coveralls] [![QLTY Test Coverage][🏀qlty-covi]][🏀qlty-cov] [![QLTY Maintainability][🏀qlty-mnti]][🏀qlty-mnt] [![CI Heads][🚎3-hd-wfi]][🚎3-hd-wf] [![CI Runtime Dependencies @ HEAD][🚎12-crh-wfi]][🚎12-crh-wf] [![CI Current][🚎11-c-wfi]][🚎11-c-wf] [![CI Truffle Ruby][🚎9-t-wfi]][🚎9-t-wf] [![CI JRuby][🚎10-j-wfi]][🚎10-j-wf] [![Deps Locked][🚎13-🔒️-wfi]][🚎13-🔒️-wf] [![Deps Unlocked][🚎14-🔓️-wfi]][🚎14-🔓️-wf] [![CI Test Coverage][🚎2-cov-wfi]][🚎2-cov-wf] [![CI Style][🚎5-st-wfi]][🚎5-st-wf] [![Apache SkyWalking Eyes License Compatibility Check][🚎15-🪪-wfi]][🚎15-🪪-wf]
if ci_badges.map(&:color).detect { it != "green"} ☝️ [let me know on Discord][✉️discord-invite] or [RubyForum][✉️ruby-forum], as I may have missed the notification.
if ci_badges.map(&:color).all? { it == "green"} 👇️ send money so I can do more of this. FLOSS maintenance is now my full-time job.
[![OpenCollective Backers][🖇osc-backers-i]][🖇osc-backers] [![OpenCollective Sponsors][🖇osc-sponsors-i]][🖇osc-sponsors] [![Sponsor Me on Github][🖇sponsor-img]][🖇sponsor] [![Liberapay Goal Progress][⛳liberapay-img]][⛳liberapay] [![Donate on PayPal][🖇paypal-img]][🖇paypal] [![Buy me a coffee][🖇buyme-small-img]][🖇buyme] [![Donate at ko-fi.com][🖇kofi-img]][🖇kofi]
An ENV stubbing / hiding library for sophisticated RSpec test suites.
hide_env("FOO") was added in v1.0.2dotenv just for the spec suitedescribe "my stubbed test" do
include_context "with stubbed env"
include_context "with hidden env"
context "with FOO=is bar" do
before do
stub_env("FOO" => "is bar")
end
it "has a value" do
expect(ENV.fetch("FOO", nil)).to(eq("is bar"))
expect(ENV.fetch("FOO")).to(eq("is bar"))
expect(ENV["FOO"]).to(eq("is bar"))
end
end
context "without BAR set" do
before do
hide_env("BAR")
end
it "is nil" do
expect(ENV.fetch("BAR", nil)).to(be_nil)
expect(ENV["BAR"]).to(be_nil)
end
it "raises error" do
expect { ENV.fetch("BAR") }.to(raise_error(KeyNotFound))
end
end
end
This gem has no runtime dependencies.
I expect the current release of this gem to be compatible with Ruby 1.8.7+, but it is only tested on CI against Ruby 2.3+, due to the inherent limitations of GitHub Actions.
| 🚚 Amazing test matrix was brought to you by | 🔎 appraisal2 🔎 |
|---|---|
| 👟 Check it out! | ✨ github.com/appraisal-rb/appraisal2 ✨ |
Install the gem and add to the application's Gemfile by executing:
bundle add rspec-stubbed_env
If bundler is not being used to manage dependencies, install the gem by executing:
gem install rspec-stubbed_env
Configure RSpec to use the :expect syntax, then require
rspec/stubbed_env from the spec helper. The gem has no global ENV list: each
example declares the keys it stubs or hides, so restoration remains scoped to
that example.
You must configure RSpec to use the :expect syntax, or some compatible alternative.
RSpec.configure do |config|
config.expect_with(:rspec) do |c|
c.syntax = :expect
end
end
Require the library in your spec/test helper somewhere:
require "rspec/stubbed_env"
before, subject, or let at the same level.See the spec suite for detailed examples.
# This is normal, without stubbing, ENV is not set
describe "vanilla" do
it "has no ENV stub" do
expect(ENV.fetch("FOO", nil)).to(be_nil)
expect(ENV["FOO"]).to(be_nil)
end
end
# With a stubbed ENV!
describe "my stubbed test" do
include_context "with stubbed env"
before do
stub_env("FOO" => "is bar")
end
it "has a value" do
expect(ENV.fetch("FOO", nil)).to(eq("is bar"))
expect(ENV["FOO"]).to(eq("is bar"))
end
end
ENV can be stubbed trough the stub_env method, or key/value pairs to be stubbed can be
provided directly to the include_context call:
describe "my stubbed
Alias stub_env to stubbed_env
https://github.com/galtzo-floss/rspec-stubbed_env/issues/34
Labels: enhancement, good first issue
Support for usage as block
https://github.com/galtzo-floss/rspec-stubbed_env/issues/33
Labels: enhancement, good first issue
Bug reports and pull requests are welcome on [CodeBerg][📜src-cb], [GitLab][📜src-gl], or [GitHub][📜src-gh]. This project should be a safe, welcoming space for collaboration, so contributors agree to adhere to the [code of conduct][🤝conduct].
To submit a patch, please fork the project, create a patch with tests, and send a pull request.
Remember to [![Keep A Changelog][📗keep-changelog-img]][📗keep-changelog] if you make changes.
In order to protect users of this project, we require all contributors to comply with the Developer Certificate of Origin. This ensures that all contributions are properly licensed and attributed.
Take a look at the open issues and pull requests, or use the gem and find something to improve.
Follow these instructions:
git checkout -b my-new-feature)git commit -am 'feat(core): ✨ add feature')git push origin my-new-feature)AI-assisted PRs are welcome as long as you review the diff, run the test suite, and comply with the DCO above.
This repository ships an AGENTS.md with project conventions and is actively developed using [Cloudroom][🖇cloudroom].
If you want to try Cloudroom, signing up with [my referral link][🖇cloudroom] supports ongoing maintenance of this project at no extra cost to you.
Pull requests run MRI workflows by default. JRuby and TruffleRuby workflows are available by branch naming convention so ordinary PRs do not spend CI time on alternate Ruby engines.
jruby/* to run JRuby workflows for a pull request.truffleruby/* to run TruffleRuby workflows for a pull
request.engines/* to run both JRuby and TruffleRuby workflows for
a pull request.Pushes to the default branch continue to run the configured engine workflows.
Executables shipped by dependencies, such as kettle-dev, and stone_checksums, are available
after running bin/setup. These include:
There are many Rake tasks available as well. You can see them by running:
bin/rake -T
Run the Reek task when you want a smell check that fails on current findings:
bin/rake reek
Refresh the checked-in REEK backlog through the rake task, not by redirecting
the raw reek executable output. The rake task uses the project bundle and
avoids stale generated binstubs shadowing the Reek gem executable:
bin/rake reek:update
Below are the primary environment variables recognized by stone_checksums (and its integrated tools). Unless otherwise noted, set boolean values to the string "true" to enable.
General/runtime
require_bench to profile requires (default: false)Coverage (kettle-soup-cover / SimpleCov)
mise.toml)K_SOUP_COV_MIN_HARD=false to avoid failing thresholds for a partial run.GitHub API and CI helpers
ci:act and release workflow checks to query GitHub Actions status at higher rate limitsReleasing and signing
certs/<USER>.pem (defaults to $USER)kettle-release will set this automatically for the session.Git hooks and commit message helpers (exe/kettle-commit-msg)
Start commit subjects with a Conventional Commit prefix followed by a Gitmoji,
for example feat(core): ✨ add feature. Emoji-first subjects remain supported.
jira) or false to disableGit diff driver setup
.gitattributes entries and local Git diff.smorg-* command config so this checkout uses StructuredMerge semantic diffs.diff.smorg-* commands on