| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  | require "extend/string" | 
					
						
							|  |  |  | require "tempfile" | 
					
						
							|  |  |  | require "utils/inreplace" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe StringInreplaceExtension do | 
					
						
							| 
									
										
										
										
											2019-04-20 14:07:29 +09:00
										 |  |  |   subject { string.dup.extend(described_class) } | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "#change_make_var!" do | 
					
						
							|  |  |  |     context "flag" do | 
					
						
							|  |  |  |       context "with spaces" do | 
					
						
							|  |  |  |         let(:string) do | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |           <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |             OTHER=def | 
					
						
							|  |  |  |             FLAG = abc | 
					
						
							|  |  |  |             FLAG2=abc | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "is successfully replaced" do | 
					
						
							|  |  |  |           subject.change_make_var! "FLAG", "def" | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |           expect(subject).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |             OTHER=def | 
					
						
							|  |  |  |             FLAG=def | 
					
						
							|  |  |  |             FLAG2=abc | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "is successfully appended" do | 
					
						
							|  |  |  |           subject.change_make_var! "FLAG", "\\1 def" | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |           expect(subject).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |             OTHER=def | 
					
						
							|  |  |  |             FLAG=abc def | 
					
						
							|  |  |  |             FLAG2=abc | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       context "with tabs" do | 
					
						
							|  |  |  |         let(:string) do | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |           <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |             CFLAGS\t=\t-Wall -O2 | 
					
						
							|  |  |  |             LDFLAGS\t=\t-lcrypto -lssl | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "is successfully replaced" do | 
					
						
							|  |  |  |           subject.change_make_var! "CFLAGS", "-O3" | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |           expect(subject).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |             CFLAGS=-O3 | 
					
						
							|  |  |  |             LDFLAGS\t=\t-lcrypto -lssl | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "empty flag between other flags" do | 
					
						
							|  |  |  |       let(:string) do | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |         <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |           OTHER=def | 
					
						
							|  |  |  |           FLAG = | 
					
						
							|  |  |  |           FLAG2=abc | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "is successfully replaced" do | 
					
						
							|  |  |  |         subject.change_make_var! "FLAG", "def" | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |         expect(subject).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |           OTHER=def | 
					
						
							|  |  |  |           FLAG=def | 
					
						
							|  |  |  |           FLAG2=abc | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "empty flag" do | 
					
						
							|  |  |  |       let(:string) do | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |         <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |           FLAG = | 
					
						
							|  |  |  |           mv file_a file_b | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "is successfully replaced" do | 
					
						
							|  |  |  |         subject.change_make_var! "FLAG", "def" | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |         expect(subject).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |           FLAG=def | 
					
						
							|  |  |  |           mv file_a file_b | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "shell-style variable" do | 
					
						
							|  |  |  |       let(:string) do | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |         <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |           OTHER=def | 
					
						
							|  |  |  |           FLAG=abc | 
					
						
							|  |  |  |           FLAG2=abc | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "is successfully replaced" do | 
					
						
							|  |  |  |         subject.change_make_var! "FLAG", "def" | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |         expect(subject).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |           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 | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |           <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |             OTHER=def | 
					
						
							|  |  |  |             FLAG = abc | 
					
						
							|  |  |  |             FLAG2 = def | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "is successfully removed" do | 
					
						
							|  |  |  |           subject.remove_make_var! "FLAG" | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |           expect(subject).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |             OTHER=def | 
					
						
							|  |  |  |             FLAG2 = def | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       context "with tabs" do | 
					
						
							|  |  |  |         let(:string) do | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |           <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |             CFLAGS\t=\t-Wall -O2 | 
					
						
							|  |  |  |             LDFLAGS\t=\t-lcrypto -lssl | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "is successfully removed" do | 
					
						
							|  |  |  |           subject.remove_make_var! "LDFLAGS" | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |           expect(subject).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |             CFLAGS\t=\t-Wall -O2 | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "multiple flags" do | 
					
						
							|  |  |  |       let(:string) do | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |         <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |           OTHER=def | 
					
						
							|  |  |  |           FLAG = abc | 
					
						
							|  |  |  |           FLAG2 = def | 
					
						
							|  |  |  |           OTHER2=def | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       specify "are be successfully removed" do | 
					
						
							|  |  |  |         subject.remove_make_var! ["FLAG", "FLAG2"] | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |         expect(subject).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |           OTHER=def | 
					
						
							|  |  |  |           OTHER2=def | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#get_make_var" do | 
					
						
							|  |  |  |     context "with spaces" do | 
					
						
							|  |  |  |       let(:string) do | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |         <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |           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 | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |         <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |           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") } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  |   before do | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |     file.write <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |       a | 
					
						
							|  |  |  |       b | 
					
						
							|  |  |  |       c | 
					
						
							|  |  |  |     EOS | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  |   after { file.unlink } | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it "raises error if there is nothing to replace" do | 
					
						
							|  |  |  |     expect { | 
					
						
							|  |  |  |       described_class.inreplace file.path, "d", "f" | 
					
						
							|  |  |  |     }.to raise_error(Utils::InreplaceError) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-28 19:16:56 +00:00
										 |  |  |   it "raises error if there is nothing to replace in block form" do | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |     expect { | 
					
						
							|  |  |  |       described_class.inreplace(file.path) do |s| | 
					
						
							|  |  |  |         s.gsub!("d", "f") # rubocop:disable Performance/StringReplacement | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     }.to raise_error(Utils::InreplaceError) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-28 19:16:56 +00:00
										 |  |  |   it "raises error if there is no make variables to replace" do | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |     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 |