Skip to content

Commit f3338d7

Browse files
committed
Tests for line endings and removed some apparently unnecessary code
1 parent eb39a5a commit f3338d7

2 files changed

Lines changed: 67 additions & 47 deletions

File tree

‎lib/fastlane/plugin/patch/helper/patch_helper.rb‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,9 @@ def revert_patch(contents, regexp, text, global, mode, offset)
5151
patched_regexp =
5252
case mode
5353
when :append
54-
suffix = ""
55-
if regexp_string.end_with? "$"
56-
suffix = "\n"
57-
regexp_string.sub!(/\$$/, "")
58-
end
59-
60-
/#{regexp_string}#{Regexp.quote(text)}#{suffix}/m
54+
/#{regexp_string}#{Regexp.quote(text)}/m
6155
when :prepend
62-
prefix = ""
63-
if regexp_string.start_with? "^"
64-
prefix = "\n"
65-
regexp_string.sub!(/^\^/, "")
66-
end
67-
68-
/#{prefix}#{Regexp.quote(text)}#{regexp_string}/m
56+
/#{Regexp.quote(text)}#{regexp_string}/m
6957
else
7058
raise ArgumentError, "Invalid mode argument. Specify :append or :prepend."
7159
end

‎spec/patch_helper_spec.rb‎

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,82 @@
22
let (:helper) { Fastlane::Helper::PatchHelper }
33

44
describe 'apply_patch' do
5-
it 'inserts text after a pattern in :append mode' do
6-
original = 'alpha beta gamma'
7-
modified = helper.apply_patch original, /beta/, ' beta and a half', false, :append, 0
8-
expect(modified).to eq 'alpha beta beta and a half gamma'
9-
end
5+
describe 'modes' do
6+
it 'inserts text after a pattern in :append mode' do
7+
original = 'alpha beta gamma'
8+
modified = helper.apply_patch original, /beta/, ' beta and a half', false, :append, 0
9+
expect(modified).to eq 'alpha beta beta and a half gamma'
10+
end
1011

11-
it 'inserts text before a pattern in :prepend mode' do
12-
original = 'alpha beta gamma'
13-
modified = helper.apply_patch original, /beta/, 'alpha and a half ', false, :prepend, 0
14-
expect(modified).to eq 'alpha alpha and a half beta gamma'
15-
end
12+
it 'inserts text before a pattern in :prepend mode' do
13+
original = 'alpha beta gamma'
14+
modified = helper.apply_patch original, /beta/, 'alpha and a half ', false, :prepend, 0
15+
expect(modified).to eq 'alpha alpha and a half beta gamma'
16+
end
1617

17-
it 'replaces a pattern with text in :replace mode' do
18-
original = 'alpha beta gamma'
19-
modified = helper.apply_patch original, /beta/, 'two', false, :replace, 0
20-
expect(modified).to eq 'alpha two gamma'
18+
it 'replaces a pattern with text in :replace mode' do
19+
original = 'alpha beta gamma'
20+
modified = helper.apply_patch original, /beta/, 'two', false, :replace, 0
21+
expect(modified).to eq 'alpha two gamma'
22+
end
23+
24+
it 'raises for any other mode' do
25+
original = 'alpha beta gamma'
26+
expect do
27+
helper.apply_patch original, /beta/, ' beta and a half', false, :add_somewhere_i_dont_know_where, 0
28+
end.to raise_error ArgumentError
29+
end
2130
end
2231

23-
it 'raises for any other mode' do
24-
original = 'alpha beta gamma'
25-
expect do
26-
helper.apply_patch original, /beta/, ' beta and a half', false, :add_somewhere_i_dont_know_where, 0
27-
end.to raise_error ArgumentError
32+
describe 'line endings' do
33+
it 'handles things at the end of a line' do
34+
original = "alpha\nbeta\ngamma\n"
35+
modified = helper.apply_patch original, /beta$/, "\nbeta and a half", false, :append, 0
36+
expect(modified).to eq "alpha\nbeta\nbeta and a half\ngamma\n"
37+
end
38+
39+
it 'handles things at the beginning of a line' do
40+
original = "alpha\nbeta\ngamma\n"
41+
modified = helper.apply_patch original, /^beta/, "alpha and a half\n", false, :prepend, 0
42+
expect(modified).to eq "alpha\nalpha and a half\nbeta\ngamma\n"
43+
end
2844
end
2945
end
3046

3147
describe 'revert_patch' do
32-
it 'removes a patch applied in :append mode' do
33-
original = 'alpha beta beta and a half gamma'
34-
modified = helper.revert_patch original, /beta/, ' beta and a half', false, :append, 0
35-
expect(modified).to eq 'alpha beta gamma'
36-
end
48+
describe 'modes' do
49+
it 'removes a patch applied in :append mode' do
50+
original = 'alpha beta beta and a half gamma'
51+
modified = helper.revert_patch original, /beta/, ' beta and a half', false, :append, 0
52+
expect(modified).to eq 'alpha beta gamma'
53+
end
54+
55+
it 'reverts a patch applied in :prepend mode' do
56+
original = 'alpha alpha and a half beta gamma'
57+
modified = helper.revert_patch original, /beta/, 'alpha and a half ', false, :prepend, 0
58+
expect(modified).to eq 'alpha beta gamma'
59+
end
3760

38-
it 'reverts a patch applied in :prepend mode' do
39-
original = 'alpha alpha and a half beta gamma'
40-
modified = helper.revert_patch original, /beta/, 'alpha and a half ', false, :prepend, 0
41-
expect(modified).to eq 'alpha beta gamma'
61+
it 'raises if :replace mode specified' do
62+
original = 'alpha two gamma'
63+
expect do
64+
helper.revert_patch original, /beta/, 'two', false, :replace, 0
65+
end.to raise_error ArgumentError
66+
end
4267
end
4368

44-
it 'raises if :replace mode specified' do
45-
original = 'alpha two gamma'
46-
expect do
47-
helper.revert_patch original, /beta/, 'two', false, :replace, 0
48-
end.to raise_error ArgumentError
69+
describe 'line endings' do
70+
it 'handles things at the end of a line' do
71+
original = "alpha\nbeta\nbeta and a half\ngamma\n"
72+
modified = helper.revert_patch original, /beta$/, "\nbeta and a half", false, :append, 0
73+
expect(modified).to eq "alpha\nbeta\ngamma\n"
74+
end
75+
76+
it 'handles things at the beginning of a line' do
77+
original = "alpha\nalpha and a half\nbeta\ngamma\n"
78+
modified = helper.revert_patch original, /^beta/, "alpha and a half\n", false, :prepend, 0
79+
expect(modified).to eq "alpha\nbeta\ngamma\n"
80+
end
4981
end
5082
end
5183
end

0 commit comments

Comments
 (0)