| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  | require "utils/string_inreplace_extension" | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-18 15:11:11 -08:00
										 |  |  | RSpec.describe StringInreplaceExtension do | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |   subject(:string_extension) { described_class.new(string.dup) } | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "#change_make_var!" do | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "with a flag" do | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |       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 | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |           string_extension.change_make_var! "FLAG", "def" | 
					
						
							|  |  |  |           expect(string_extension.inreplace_string).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |             OTHER=def | 
					
						
							|  |  |  |             FLAG=def | 
					
						
							|  |  |  |             FLAG2=abc | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "is successfully appended" do | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |           string_extension.change_make_var! "FLAG", "\\1 def" | 
					
						
							|  |  |  |           expect(string_extension.inreplace_string).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 | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |           string_extension.change_make_var! "CFLAGS", "-O3" | 
					
						
							|  |  |  |           expect(string_extension.inreplace_string).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |             CFLAGS=-O3 | 
					
						
							|  |  |  |             LDFLAGS\t=\t-lcrypto -lssl | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-03-05 03:40:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       context "with newlines" do | 
					
						
							|  |  |  |         let(:string) do | 
					
						
							|  |  |  |           <<~'EOS' | 
					
						
							|  |  |  |             CFLAGS = -Wall -O2 \ | 
					
						
							|  |  |  |                      -DSOME_VAR=1
 | 
					
						
							|  |  |  |             LDFLAGS = -lcrypto -lssl | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "is successfully replaced" do | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |           string_extension.change_make_var! "CFLAGS", "-O3" | 
					
						
							|  |  |  |           expect(string_extension.inreplace_string).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2020-03-05 03:40:23 +00:00
										 |  |  |             CFLAGS=-O3 | 
					
						
							|  |  |  |             LDFLAGS = -lcrypto -lssl | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "with an empty flag between other flags" do | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |       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 | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |         string_extension.change_make_var! "FLAG", "def" | 
					
						
							|  |  |  |         expect(string_extension.inreplace_string).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |           OTHER=def | 
					
						
							|  |  |  |           FLAG=def | 
					
						
							|  |  |  |           FLAG2=abc | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "with an empty flag" do | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |       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 | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |         string_extension.change_make_var! "FLAG", "def" | 
					
						
							|  |  |  |         expect(string_extension.inreplace_string).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |           FLAG=def | 
					
						
							|  |  |  |           mv file_a file_b | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "with shell-style variable" do | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |       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 | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |         string_extension.change_make_var! "FLAG", "def" | 
					
						
							|  |  |  |         expect(string_extension.inreplace_string).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 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "with a flag" do | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |       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 | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |           string_extension.remove_make_var! "FLAG" | 
					
						
							|  |  |  |           expect(string_extension.inreplace_string).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 | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |           string_extension.remove_make_var! "LDFLAGS" | 
					
						
							|  |  |  |           expect(string_extension.inreplace_string).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |             CFLAGS\t=\t-Wall -O2 | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-03-05 03:40:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       context "with newlines" do | 
					
						
							|  |  |  |         let(:string) do | 
					
						
							|  |  |  |           <<~'EOS' | 
					
						
							|  |  |  |             CFLAGS = -Wall -O2 \ | 
					
						
							|  |  |  |                      -DSOME_VAR=1
 | 
					
						
							|  |  |  |             LDFLAGS = -lcrypto -lssl | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "is successfully removed" do | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |           string_extension.remove_make_var! "CFLAGS" | 
					
						
							|  |  |  |           expect(string_extension.inreplace_string).to eq <<~EOS | 
					
						
							| 
									
										
										
										
											2020-03-05 03:40:23 +00:00
										 |  |  |             LDFLAGS = -lcrypto -lssl | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 23:15:33 +00:00
										 |  |  |     context "with multiple flags" do | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |       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 | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |         string_extension.remove_make_var! ["FLAG", "FLAG2"] | 
					
						
							|  |  |  |         expect(string_extension.inreplace_string).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 | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |         expect(string_extension.get_make_var("CFLAGS")).to eq("-Wall -O2") | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |       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 | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |         expect(string_extension.get_make_var("CFLAGS")).to eq("-Wall -O2") | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2020-03-05 03:40:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     context "with newlines" do | 
					
						
							|  |  |  |       let(:string) do | 
					
						
							|  |  |  |         <<~'EOS' | 
					
						
							|  |  |  |           CFLAGS = -Wall -O2 \ | 
					
						
							|  |  |  |                    -DSOME_VAR=1
 | 
					
						
							|  |  |  |           LDFLAGS = -lcrypto -lssl | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "extracts the value for a given variable" do | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |         expect(string_extension.get_make_var("CFLAGS")).to match(/^-Wall -O2 \\\n +-DSOME_VAR=1$/) | 
					
						
							| 
									
										
										
										
											2020-03-05 03:40:23 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#sub!" do | 
					
						
							|  |  |  |     let(:string) { "foo" } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 14:27:29 +10:00
										 |  |  |     it "replaces the first occurrence" do | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |       string_extension.sub!("o", "e") | 
					
						
							|  |  |  |       expect(string_extension.inreplace_string).to eq("feo") | 
					
						
							| 
									
										
										
										
											2024-10-23 12:20:24 -04:00
										 |  |  |       expect(string_extension.errors).to be_empty | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "adds an error to #errors when no replacement was made" do | 
					
						
							|  |  |  |       string_extension.sub! "not here", "test" | 
					
						
							| 
									
										
										
										
											2024-10-23 12:20:24 -04:00
										 |  |  |       expect(string_extension.inreplace_string).to eq(string) | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |       expect(string_extension.errors).to eq(['expected replacement of "not here" with "test"']) | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-10-23 12:20:24 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it "doesn't add an error to #errors when no replace was made and `audit_result: false`" do | 
					
						
							|  |  |  |       string_extension.sub! "not here", "test", audit_result: false | 
					
						
							|  |  |  |       expect(string_extension.inreplace_string).to eq(string) | 
					
						
							|  |  |  |       expect(string_extension.errors).to be_empty | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#gsub!" do | 
					
						
							|  |  |  |     let(:string) { "foo" } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 14:27:29 +10:00
										 |  |  |     it "replaces all occurrences" do | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |       string_extension.gsub!("o", "e") # rubocop:disable Performance/StringReplacement | 
					
						
							|  |  |  |       expect(string_extension.inreplace_string).to eq("fee") | 
					
						
							| 
									
										
										
										
											2020-08-16 10:28:26 -07:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2017-02-12 23:03:46 +01:00
										 |  |  | end |