| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "tempfile" | 
					
						
							|  |  |  | require "utils/inreplace" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe Utils::Inreplace do | 
					
						
							|  |  |  |   let(:file) { Tempfile.new("test") } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   before do | 
					
						
							|  |  |  |     file.write <<~EOS | 
					
						
							|  |  |  |       a | 
					
						
							|  |  |  |       b | 
					
						
							|  |  |  |       c | 
					
						
							|  |  |  |     EOS | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   after { file.unlink } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it "raises error if there are no files given to replace" do | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |     expect do | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |       described_class.inreplace [], "d", "f" | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |     end.to raise_error(Utils::Inreplace::Error) | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it "raises error if there is nothing to replace" do | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |     expect do | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |       described_class.inreplace file.path, "d", "f" | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |     end.to raise_error(Utils::Inreplace::Error) | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it "raises error if there is nothing to replace in block form" do | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |     expect do | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |       described_class.inreplace(file.path) do |s| | 
					
						
							|  |  |  |         s.gsub!("d", "f") # rubocop:disable Performance/StringReplacement | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |     end.to raise_error(Utils::Inreplace::Error) | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it "raises error if there is no make variables to replace" do | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |     expect do | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |       described_class.inreplace(file.path) do |s| | 
					
						
							|  |  |  |         s.change_make_var! "VAR", "value" | 
					
						
							|  |  |  |         s.remove_make_var! "VAR2" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |     end.to raise_error(Utils::Inreplace::Error) | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#inreplace_pairs" do | 
					
						
							|  |  |  |     it "raises error if there is no old value" do | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |       expect do | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |         described_class.inreplace_pairs(file.path, [[nil, "f"]]) | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |       end.to raise_error(Utils::Inreplace::Error) | 
					
						
							| 
									
										
										
										
											2020-10-10 15:04:46 +02:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |