Skip to content

Commit

Permalink
Handle BUNDLED WITH deletion in windows (#1108)
Browse files Browse the repository at this point in the history
Windows Gemfile.lock uses `\r\n` instead of `\n` for newlines which can cause issues when matching regex. We are using regex to strip out BUNDLED WITH and were not previously handling windows Gemfile.lock by mistake.

There is other code that reads in the major version of BUNDLED WITH and it uses a read mode of `rt` which solves the problem.

The solution is to change this other location to also use a read mode of `rt`:

```
irb(main):009:0> !!File.read("./spec/fixtures/windows_lockfile/Gemfile.lock").match(/^BUNDLED WITH$(\r?\n)   (?<major>\d+)\.\d+\.\d+/m)
=> false
irb(main):010:0> !!File.read("./spec/fixtures/windows_lockfile/Gemfile.lock", mode: 'rt').match(/^BUNDLED WITH$(\r?\n)   (?<major>\d+)\.\d+
\.\d+/m)
=> true
```

The test now also assert the desired behavior of removing BUNDLED WITH occurs.
  • Loading branch information
schneems authored Jan 15, 2021
1 parent bba6fa8 commit 60b5586
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Main (unreleased)

* Fix Gemfile.lock read bug from preventing propper removal of BUNDLED WITH declaration ()

## v222 (11/02/2020)

* CNB support for Heroku-20 (https://github.com/heroku/heroku-buildpack-ruby/pull/1096)
Expand Down
15 changes: 8 additions & 7 deletions lib/language_pack/helpers/bundler_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ def needs_ruby_global_append_path?
Gem::Version.new(@version) < Gem::Version.new("2.1.4")
end

def bundler_version_escape_valve!
topic("Removing BUNDLED WITH version in the Gemfile.lock")
contents = File.read(@gemfile_lock_path, mode: "rt")
File.open(@gemfile_lock_path, "w") do |f|
f.write contents.sub(/^BUNDLED WITH$(\r?\n) (?<major>\d+)\.\d+\.\d+/m, '')
end
end

private
def fetch_bundler
instrument 'fetch_bundler' do
Expand Down Expand Up @@ -222,11 +230,4 @@ def detect_bundler_version_and_dir_name!
end
end

def bundler_version_escape_valve!
topic("Removing BUNDLED WITH version in the Gemfile.lock")
contents = File.read("Gemfile.lock")
File.open("Gemfile.lock", "w") do |f|
f.write contents.sub(/^BUNDLED WITH$(\r?\n) (?<major>\d+)\.\d+\.\d+/m, '')
end
end
end
Empty file.
19 changes: 17 additions & 2 deletions spec/helpers/bundler_wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,24 @@
end

it "handles windows BUNDLED WITH" do
wrapper = LanguagePack::Helpers::BundlerWrapper.new(gemfile_path: fixture_path("windows_lockfile/Gemfile"))
Dir.mktmpdir do |dir|
tmp_dir = Pathname(dir)
FileUtils.cp_r(fixture_path("windows_lockfile/."), tmp_dir)

expect(wrapper.version).to eq(LanguagePack::Helpers::BundlerWrapper::BLESSED_BUNDLER_VERSIONS["2"])
tmp_gemfile_path = tmp_dir.join("Gemfile")
tmp_gemfile_lock_path = tmp_dir.join("Gemfile.lock")

expect(tmp_gemfile_lock_path.read).to match("BUNDLED")

wrapper = LanguagePack::Helpers::BundlerWrapper.new(gemfile_path: tmp_gemfile_path )

expect(wrapper.version).to eq(LanguagePack::Helpers::BundlerWrapper::BLESSED_BUNDLER_VERSIONS["2"])

def wrapper.topic(*args); end # Silence output in tests
wrapper.bundler_version_escape_valve!

expect(tmp_gemfile_lock_path.read).to_not match("BUNDLED")
end
end

it "detects windows gemfiles" do
Expand Down

0 comments on commit 60b5586

Please sign in to comment.