Skip to content

Refactor tests #14

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

Merged
merged 3 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions test/features/app_env_test.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
require_relative "../test_helper"
require_relative "../env_helpers"
require_relative "env_helpers"

module Rails::AppEnv::FeaturesTest
class AppEnvTest < ActiveSupport::TestCase
include EnvHelpers

def setup
Rails.instance_variable_set :@_app_env, nil
end

test "Rails.app_env is a kind of ActiveSupport::EnvironmentInquirer when APP_ENV is present" do
switch_env "APP_ENV", "foo" do
with_app_env("foo") do
assert_kind_of ActiveSupport::EnvironmentInquirer, Rails.app_env
end
end

test "Rails.app_env is a kind of ActiveSupport::EnvironmentInquirer when APP_ENV is blank" do
switch_env "APP_ENV", nil do
with_app_env(nil) do
assert_kind_of ActiveSupport::EnvironmentInquirer, Rails.app_env
end
end

test "Rails.app_env is an instance of Rails::AppEnv::EnvironmentInquirer when APP_ENV is present" do
switch_env "APP_ENV", "foo" do
with_app_env("foo") do
assert_instance_of Rails::AppEnv::EnvironmentInquirer, Rails.app_env
end
end

test "Rails.app_env is set from APP_ENV when both APP_ENV are RAILS_ENV present" do
switch_env "APP_ENV", "foo" do
with_app_env("foo") do
with_rails_env("bar") do
assert_equal "foo", Rails.app_env
assert_equal "bar", Rails.env
Expand All @@ -37,7 +33,7 @@ def setup
end

test "Rails.app_env is set from APP_ENV when APP_ENV is present but RAILS_ENV is blank" do
switch_env "APP_ENV", "foo" do
with_app_env("foo") do
with_rails_env(nil) do
assert_equal "foo", Rails.app_env
assert_equal DEFAULT_RAILS_ENV, Rails.env
Expand All @@ -46,7 +42,7 @@ def setup
end

test "Rails.app_env falls back to Rails.env when APP_ENV is blank but RAILS_ENV is present" do
switch_env "APP_ENV", nil do
with_app_env(nil) do
with_rails_env("foo") do
assert_equal "foo", Rails.app_env
assert_equal "foo", Rails.env
Expand All @@ -55,7 +51,7 @@ def setup
end

test "Rails.app_env falls back to default Rails.env when both APP_ENV and RAILS_ENV are blank" do
switch_env "APP_ENV", nil do
with_app_env(nil) do
with_rails_env(nil) do
assert_equal DEFAULT_RAILS_ENV, Rails.app_env
assert_equal DEFAULT_RAILS_ENV, Rails.env
Expand All @@ -64,7 +60,7 @@ def setup
end

test "Rails.app_env does not follow Rails.env changes when both APP_ENV and RAILS_ENV are present" do
switch_env "APP_ENV", "foo" do
with_app_env("foo") do
with_rails_env("bar") do
assert_equal "foo", Rails.app_env
assert_equal "bar", Rails.env
Expand All @@ -78,7 +74,7 @@ def setup
end

test "Rails.app_env does not follow Rails.env changes when APP_ENV is present and RAILS_ENV is blank" do
switch_env "APP_ENV", "foo" do
with_app_env("foo") do
with_rails_env(nil) do
assert_equal "foo", Rails.app_env
assert_equal DEFAULT_RAILS_ENV, Rails.env
Expand All @@ -92,7 +88,7 @@ def setup
end

test "Rails.app_env does not follow Rails.env changes when APP_ENV is blank but RAILS_ENV is present" do
switch_env "APP_ENV", nil do
with_app_env(nil) do
with_rails_env("foo") do
assert_equal "foo", Rails.app_env
assert_equal "foo", Rails.env
Expand All @@ -106,7 +102,7 @@ def setup
end

test "Rails.app_env does not follow Rails.env changes when both APP_ENV and RAILS_ENV are blank" do
switch_env "APP_ENV", nil do
with_app_env(nil) do
with_rails_env(nil) do
assert_equal DEFAULT_RAILS_ENV, Rails.app_env
assert_equal DEFAULT_RAILS_ENV, Rails.env
Expand Down
4 changes: 4 additions & 0 deletions test/features/credentials_command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module Rails::AppEnv::FeaturesTest
class CredentialsCommandTest < ActiveSupport::TestCase
include FileHelpers

def setup
cleanup_credentials_files
end

def teardown
cleanup_credentials_files
end
Expand Down
9 changes: 5 additions & 4 deletions test/env_helpers.rb → test/features/env_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

require "rails"

module EnvHelpers
private

def with_app_env(app_env, &block)
Rails.instance_variable_set :@_app_env, nil
switch_env "APP_ENV", app_env, &block
end

def with_rails_env(env, &block)
Rails.instance_variable_set :@_env, nil
switch_env "RAILS_ENV", env do
Expand Down