| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
 | require "extend/string"
require "tempfile"
require "utils/inreplace"
describe StringInreplaceExtension do
  subject { string.extend(described_class) }
  describe "#change_make_var!" do
    context "flag" do
      context "with spaces" do
        let(:string) do
          <<~EOS
            OTHER=def
            FLAG = abc
            FLAG2=abc
          EOS
        end
        it "is successfully replaced" do
          subject.change_make_var! "FLAG", "def"
          expect(subject).to eq <<~EOS
            OTHER=def
            FLAG=def
            FLAG2=abc
          EOS
        end
        it "is successfully appended" do
          subject.change_make_var! "FLAG", "\\1 def"
          expect(subject).to eq <<~EOS
            OTHER=def
            FLAG=abc def
            FLAG2=abc
          EOS
        end
      end
      context "with tabs" do
        let(:string) do
          <<~EOS
            CFLAGS\t=\t-Wall -O2
            LDFLAGS\t=\t-lcrypto -lssl
          EOS
        end
        it "is successfully replaced" do
          subject.change_make_var! "CFLAGS", "-O3"
          expect(subject).to eq <<~EOS
            CFLAGS=-O3
            LDFLAGS\t=\t-lcrypto -lssl
          EOS
        end
      end
    end
    context "empty flag between other flags" do
      let(:string) do
        <<~EOS
          OTHER=def
          FLAG =
          FLAG2=abc
        EOS
      end
      it "is successfully replaced" do
        subject.change_make_var! "FLAG", "def"
        expect(subject).to eq <<~EOS
          OTHER=def
          FLAG=def
          FLAG2=abc
        EOS
      end
    end
    context "empty flag" do
      let(:string) do
        <<~EOS
          FLAG =
          mv file_a file_b
        EOS
      end
      it "is successfully replaced" do
        subject.change_make_var! "FLAG", "def"
        expect(subject).to eq <<~EOS
          FLAG=def
          mv file_a file_b
        EOS
      end
    end
    context "shell-style variable" do
      let(:string) do
        <<~EOS
          OTHER=def
          FLAG=abc
          FLAG2=abc
        EOS
      end
      it "is successfully replaced" do
        subject.change_make_var! "FLAG", "def"
        expect(subject).to eq <<~EOS
          OTHER=def
          FLAG=def
          FLAG2=abc
        EOS
      end
    end
  end
  describe "#remove_make_var!" do
    context "flag" do
      context "with spaces" do
        let(:string) do
          <<~EOS
            OTHER=def
            FLAG = abc
            FLAG2 = def
          EOS
        end
        it "is successfully removed" do
          subject.remove_make_var! "FLAG"
          expect(subject).to eq <<~EOS
            OTHER=def
            FLAG2 = def
          EOS
        end
      end
      context "with tabs" do
        let(:string) do
          <<~EOS
            CFLAGS\t=\t-Wall -O2
            LDFLAGS\t=\t-lcrypto -lssl
          EOS
        end
        it "is successfully removed" do
          subject.remove_make_var! "LDFLAGS"
          expect(subject).to eq <<~EOS
            CFLAGS\t=\t-Wall -O2
          EOS
        end
      end
    end
    context "multiple flags" do
      let(:string) do
        <<~EOS
          OTHER=def
          FLAG = abc
          FLAG2 = def
          OTHER2=def
        EOS
      end
      specify "are be successfully removed" do
        subject.remove_make_var! ["FLAG", "FLAG2"]
        expect(subject).to eq <<~EOS
          OTHER=def
          OTHER2=def
        EOS
      end
    end
  end
  describe "#get_make_var" do
    context "with spaces" do
      let(:string) do
        <<~EOS
          CFLAGS = -Wall -O2
          LDFLAGS = -lcrypto -lssl
        EOS
      end
      it "extracts the value for a given variable" do
        expect(subject.get_make_var("CFLAGS")).to eq("-Wall -O2")
      end
    end
    context "with tabs" do
      let(:string) do
        <<~EOS
          CFLAGS\t=\t-Wall -O2
          LDFLAGS\t=\t-lcrypto -lssl
        EOS
      end
      it "extracts the value for a given variable" do
        expect(subject.get_make_var("CFLAGS")).to eq("-Wall -O2")
      end
    end
  end
  describe "#sub!" do
    let(:string) { "foo" }
    it "replaces the first occurence" do
      subject.sub!("o", "e")
      expect(subject).to eq("feo")
    end
  end
  describe "#gsub!" do
    let(:string) { "foo" }
    it "replaces the all occurences" do
      subject.gsub!("o", "e") # rubocop:disable Performance/StringReplacement
      expect(subject).to eq("fee")
    end
  end
end
describe Utils::Inreplace do
  let(:file) { Tempfile.new("test") }
  before(:each) do
    file.write <<~EOS
      a
      b
      c
    EOS
  end
  after(:each) { file.unlink }
  it "raises error if there is nothing to replace" do
    expect {
      described_class.inreplace file.path, "d", "f"
    }.to raise_error(Utils::InreplaceError)
  end
  it "raises error if there is nothing to replace" do
    expect {
      described_class.inreplace(file.path) do |s|
        s.gsub!("d", "f") # rubocop:disable Performance/StringReplacement
      end
    }.to raise_error(Utils::InreplaceError)
  end
  it "raises error if there is nothing to replace" do
    expect {
      described_class.inreplace(file.path) do |s|
        s.change_make_var! "VAR", "value"
        s.remove_make_var! "VAR2"
      end
    }.to raise_error(Utils::InreplaceError)
  end
end
 |