Skip to content

Commit aab490f

Browse files
committed
Allow Definition#lock to be passed a lockfile:
- Ref #8917 - ### Problem In 6a0c03c, we introduced a deprecation that would disallow calling `lock("NewGemfile.lock")`. The intention was that the lockfile target should be passed when constructing a Definition object. With this deprecation, it is no longer possible to create a Definition from an existing lockfile, make changes to the definition, and dump its content into a new lockfile. This behaviour is used as public API but also internally with the `bundle lock` command (currently broken, a fix will follow up thanks to this commit). ### Solution Remove the deprecation and introduce a new one related to the position of the passed arguments. The method's signature is now: `lock(preserve_unknown_section = false, target_lock = lockfile)`
1 parent e38c8fb commit aab490f

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

bundler/lib/bundler/definition.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -351,23 +351,24 @@ def groups
351351
dependencies.flat_map(&:groups).uniq
352352
end
353353

354-
def lock(file_or_preserve_unknown_sections = false, preserve_unknown_sections_or_unused = false)
354+
def lock(file_or_preserve_unknown_sections = false, file_or_preserve_unknown_sections2 = lockfile)
355355
if [true, false, nil].include?(file_or_preserve_unknown_sections)
356-
target_lockfile = lockfile
356+
target_lockfile = file_or_preserve_unknown_sections2
357357
preserve_unknown_sections = file_or_preserve_unknown_sections
358358
else
359359
target_lockfile = file_or_preserve_unknown_sections
360-
preserve_unknown_sections = preserve_unknown_sections_or_unused
361-
362-
suggestion = if target_lockfile == lockfile
363-
"To fix this warning, remove it from the `Definition#lock` call."
360+
if lockfile == file_or_preserve_unknown_sections2
361+
preserve_unknown_sections = false
364362
else
365-
"Instead, instantiate a new definition passing `#{target_lockfile}`, and call `lock` without a file argument on that definition"
363+
preserve_unknown_sections = file_or_preserve_unknown_sections2
366364
end
367365

368-
msg = "`Definition#lock` was passed a target file argument. #{suggestion}"
366+
Bundler::SharedHelpers.major_deprecation 2, <<~MSG
367+
The lockfile target needs to be passed as second argument to the `Definition#lock` method.
368+
To fix this warning, please modify the caller to:
369369
370-
Bundler::SharedHelpers.major_deprecation 2, msg
370+
`lock(#{preserve_unknown_sections}, "#{file_or_preserve_unknown_sections}")`
371+
MSG
371372
end
372373

373374
write_lock(target_lockfile, preserve_unknown_sections)

bundler/spec/bundler/definition_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,46 @@
3838
expect(bundled_app_lock).not_to be_file
3939
end
4040
end
41+
context "method signature" do
42+
before { Bundler::Definition.no_lock = true }
43+
after { Bundler::Definition.no_lock = false }
44+
45+
it "does not print a deprecation when method is called with no arguments" do
46+
allow(Bundler).to receive(:ui) { double("UI", info: "", debug: "") }
47+
48+
subject.lock
49+
end
50+
51+
it "does not print a deprecation when +preserve_unknown_section+ is given as 1st arg" do
52+
allow(Bundler).to receive(:ui) { double("UI", info: "", debug: "") }
53+
54+
subject.lock(true)
55+
end
56+
57+
it "prints a deprecation when target lockfile is given as first argument" do
58+
expected = <<~MSG
59+
[DEPRECATED] The lockfile target needs to be passed as second argument to the `Definition#lock` method.
60+
To fix this warning, please modify the caller to:
61+
62+
`lock(false, "Gemfile.lock")`
63+
MSG
64+
allow(Bundler).to receive(:ui) { double("UI", info: "", debug: "", warn: expected) }
65+
66+
subject.lock("Gemfile.lock")
67+
end
68+
69+
it "prints a deprecation when target lockfile is given as 1st arg and unknown section as 2nd" do
70+
expected = <<~MSG
71+
[DEPRECATED] The lockfile target needs to be passed as second argument to the `Definition#lock` method.
72+
To fix this warning, please modify the caller to:
73+
74+
`lock(true, "Gemfile.lock")`
75+
MSG
76+
allow(Bundler).to receive(:ui) { double("UI", info: "", debug: "", warn: expected) }
77+
78+
subject.lock("Gemfile.lock", true)
79+
end
80+
end
4181
end
4282

4383
describe "detects changes" do

0 commit comments

Comments
 (0)