Skip to content

Commit

Permalink
Switch to using Bundler.with_original_env (#202)
Browse files Browse the repository at this point in the history
This fixes a long-running issue with more recent Bundler versions and
compatibility with GitHub Actions (and other CI services), where the
environment wasn't being handled correctly.

Introduce a new 'APPRAISAL_UNDER_TEST' environment variable that
`Command` uses to determine if Appraisal itself is under test, and if
so, tweaks the environment in the way the tests require. This allows the
tests to pass while using `Bundler.with_original_env`.

Fixes #173

Co-authored-by: Kyle Fazzari <kyrofa@ubuntu.com>
Co-authored-by: Chris Oliver <excid3@gmail.com>
  • Loading branch information
kyrofa and excid3 authored Jan 27, 2023
1 parent 40956d3 commit ea2baff
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
46 changes: 18 additions & 28 deletions lib/appraisal/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
module Appraisal
# Executes commands with a clean environment
class Command
BUNDLER_ENV_VARS = %w(RUBYOPT BUNDLE_PATH BUNDLE_BIN_PATH BUNDLE_GEMFILE).freeze

attr_reader :command, :env, :gemfile, :original_env
attr_reader :command, :env, :gemfile

def initialize(command, options = {})
@gemfile = options[:gemfile]
@env = options.fetch(:env, {})
@command = command_starting_with_bundle(command)
@original_env = {}
end

def run
with_clean_env { ensure_bundler_is_available }
announce
run_env = test_environment.merge(env)

Bundler.with_original_env do
ensure_bundler_is_available
announce

with_clean_env do
env.each_pair do |key, value|
ENV["BUNDLE_GEMFILE"] = gemfile
ENV["APPRAISAL_INITIALIZED"] = "1"
run_env.each_pair do |key, value|
ENV[key] = value
end

Expand All @@ -31,15 +32,6 @@ def run

private

def with_clean_env
unset_bundler_env_vars
ENV['BUNDLE_GEMFILE'] = gemfile
ENV['APPRAISAL_INITIALIZED'] = '1'
yield
ensure
restore_env
end

def ensure_bundler_is_available
version = Utils.bundler_version
unless system %(gem list --silent -i bundler -v #{version})
Expand All @@ -66,17 +58,6 @@ def announce
end
end

def unset_bundler_env_vars
BUNDLER_ENV_VARS.each do |key|
original_env[key] = ENV[key]
ENV[key] = nil
end
end

def restore_env
original_env.each { |key, value| ENV[key] = value }
end

def command_starts_with_bundle?(original_command)
if original_command.is_a?(Array)
original_command.first =~ /^bundle/
Expand All @@ -100,5 +81,14 @@ def command_as_string
command
end
end

def test_environment
return {} unless ENV["APPRAISAL_UNDER_TEST"] == "1"

{
"GEM_HOME" => ENV["GEM_HOME"],
"GEM_PATH" => "",
}
end
end
end
4 changes: 3 additions & 1 deletion spec/acceptance/bundle_without_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
output = run "appraisal install --without drinks"

expect(output).to include("Bundle complete")
expect(output).to include("Gems in the group drinks were not installed.")
expect(output).to(
match(/Gems in the group ['"]?drinks['"]? were not installed/),
)
expect(output).not_to include("orange_juice")
expect(output).not_to include("coffee")
expect(output).not_to include("soda")
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')).freeze
TMP_GEM_ROOT = File.join(PROJECT_ROOT, "tmp", "gems")
ENV["APPRAISAL_UNDER_TEST"] = "1"

RSpec.configure do |config|
config.raise_errors_for_deprecations!
Expand Down

0 comments on commit ea2baff

Please sign in to comment.