Skip to content

Commit

Permalink
Fix __FILE__ and __dir__ in primary Gemfiles (#193)
Browse files Browse the repository at this point in the history
Gemfile classes call instance_eval without telling Ruby what file the string
came from, and so Ruby thinks that FILE should point to
`appraisal/lib/appraisal/gemfile.rb` instead of the Gemfile itself, and that breaks
anything inside the Gemfile that references either __FILE__ or __dir__.

This fixes the case where `ruby Pathname.new(__dir__).join(".ruby-version").read`
is used in the original `Gemfile` across Appraisals.
  • Loading branch information
indirect committed Feb 3, 2022
1 parent 2f5be65 commit 97079f0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/appraisal/gemfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@ module Appraisal
# Load bundler Gemfiles and merge dependencies
class Gemfile < BundlerDSL
def load(path)
if File.exist?(path)
run(IO.read(path))
end
run(IO.read(path), path) if File.exist?(path)
end

def run(definitions)
instance_eval(definitions, __FILE__, __LINE__) if definitions
def run(definitions, path, line = 1)
instance_eval(definitions, path, line) if definitions
end

def dup
Gemfile.new.tap do |gemfile|
gemfile.git_sources = @git_sources
gemfile.run(for_dup)
gemfile.run(for_dup, __FILE__, __LINE__)
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/appraisal/gemfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,13 @@
expect(gemfile.to_s).to eq %(gem "bacon", git: "../path/bacon_pancake")
end
end

it "preserves the Gemfile's __FILE__" do
gemfile = Appraisal::Gemfile.new
Tempfile.open do |tmpfile|
tmpfile.write "__FILE__"
tmpfile.rewind
expect(gemfile.load(tmpfile.path)).to include(File.dirname(tmpfile.path))
end
end
end

0 comments on commit 97079f0

Please sign in to comment.