Skip to content

Commit b54d499

Browse files
authored
fix(ruby): gemfile.lock should not update when gem name is empty (#1805)
PR #1790 introduced support to update Gemfile.lock files. However this introduced an issue that updates random gem versions when the `package-name` (gemName) is not provided (which is optional). This fix ensures the gemName is valid before attempting to update the Gemfile.lock file.
1 parent 24e0338 commit b54d499

3 files changed

Lines changed: 82 additions & 0 deletions

File tree

__snapshots__/gemfile-lock.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,67 @@
1+
exports['Gemfile.lock updateContent does not update anything if gem name is not provided 1'] = `
2+
PATH
3+
remote: .
4+
specs:
5+
foo (0.1.0)
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
ast (2.4.2)
11+
foobar (1.0.1)
12+
diff-lcs (1.5.0)
13+
json (2.6.3)
14+
parallel (1.22.1)
15+
parser (3.1.3.0)
16+
ast (~> 2.4.1)
17+
rainbow (3.1.1)
18+
rake (13.0.6)
19+
regexp_parser (2.6.1)
20+
rexml (3.2.5)
21+
rspec (3.12.0)
22+
rspec-core (~> 3.12.0)
23+
rspec-expectations (~> 3.12.0)
24+
rspec-mocks (~> 3.12.0)
25+
rspec-core (3.12.0)
26+
rspec-support (~> 3.12.0)
27+
rspec-expectations (3.12.0)
28+
diff-lcs (>= 1.2.0, < 2.0)
29+
rspec-support (~> 3.12.0)
30+
rspec-mocks (3.12.0)
31+
diff-lcs (>= 1.2.0, < 2.0)
32+
rspec-support (~> 3.12.0)
33+
rspec-support (3.12.0)
34+
rubocop (1.39.0)
35+
json (~> 2.3)
36+
parallel (~> 1.10)
37+
parser (>= 3.1.2.1)
38+
rainbow (>= 2.2.2, < 4.0)
39+
regexp_parser (>= 1.8, < 3.0)
40+
rexml (>= 3.2.5, < 4.0)
41+
rubocop-ast (>= 1.23.0, < 2.0)
42+
ruby-progressbar (~> 1.7)
43+
unicode-display_width (>= 1.4.0, < 3.0)
44+
rubocop-ast (1.24.0)
45+
parser (>= 3.1.1.0)
46+
ruby-progressbar (1.11.0)
47+
unicode-display_width (2.3.0)
48+
49+
PLATFORMS
50+
ruby
51+
52+
DEPENDENCIES
53+
bundler
54+
foo!
55+
foobar
56+
rake
57+
rspec
58+
rubocop
59+
60+
BUNDLED WITH
61+
2.3.26
62+
63+
`
64+
165
exports['Gemfile.lock updateContent updates prerelease in Gemfile.lock 1'] = `
266
PATH
367
remote: .

src/updaters/ruby/gemfile-lock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export class GemfileLock extends DefaultUpdater {
4646
* @returns {string} The updated content
4747
*/
4848
updateContent(content: string): string {
49+
if (!this.gemName) {
50+
return content;
51+
}
52+
4953
// Bundler will convert 1.0.0-alpha1 to 1.0.0.pre.alpha1, so we need to
5054
// do the same here.
5155
const versionString = resolveRubyGemfileLockVersion(

test/updaters/gemfile-lock.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ describe('Gemfile.lock', () => {
4040
['foo', '1.0.0', 'foo 1.0.0', 'foo 1.0.0', false, 'no parantheses around version'],
4141
['foo', '1.0.0', 'barfoo (1.0.0)', 'barfoo (1.0.0)', false, 'prefixed gem name'],
4242
['foo', '1.0.0', 'foobar (0.1.0)', 'foobar (0.1.0)', false, 'suffixed gem name'],
43+
['', '1.0.0', 'foobar (0.1.0)', 'foobar (0.1.0)', false, 'empty gem name'],
4344
];
4445

4546
testTable.forEach(
@@ -64,6 +65,19 @@ describe('Gemfile.lock', () => {
6465
}
6566
);
6667

68+
it('does not update anything if gem name is not provided', async () => {
69+
const oldContent = readFileSync(
70+
resolve(fixturesPath, './Gemfile.lock'),
71+
'utf8'
72+
).replace(/\r\n/g, '\n');
73+
const version = new GemfileLock({
74+
version: Version.parse('0.2.0'),
75+
gemName: '',
76+
});
77+
const newContent = version.updateContent(oldContent);
78+
snapshot(newContent);
79+
});
80+
6781
it('updates version in Gemfile.lock', async () => {
6882
const oldContent = readFileSync(
6983
resolve(fixturesPath, './Gemfile.lock'),

0 commit comments

Comments
 (0)