aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/inreplace_spec.rb
diff options
context:
space:
mode:
authorNaoto Kaneko2017-02-27 14:23:53 +0900
committerNaoto Kaneko2017-02-27 14:23:53 +0900
commit928eaca26720fd38b07c1e7df3f9f567477d48db (patch)
treec08fd030cca960e74bab72b59ce057684e554334 /Library/Homebrew/test/inreplace_spec.rb
parentccc9b2dc6dc27026673db3c8871c691be9541342 (diff)
parente3f4701f385c286a2cc72c5d07870cc9a6ce0bf4 (diff)
downloadbrew-928eaca26720fd38b07c1e7df3f9f567477d48db.tar.bz2
Merge branch 'master' into exclude-executable-metafiles
Added tests in keg_test and pathname_test was moved into keg_spec and pathname_spec.
Diffstat (limited to 'Library/Homebrew/test/inreplace_spec.rb')
-rw-r--r--Library/Homebrew/test/inreplace_spec.rb251
1 files changed, 251 insertions, 0 deletions
diff --git a/Library/Homebrew/test/inreplace_spec.rb b/Library/Homebrew/test/inreplace_spec.rb
new file mode 100644
index 000000000..5be44f50d
--- /dev/null
+++ b/Library/Homebrew/test/inreplace_spec.rb
@@ -0,0 +1,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.undent
+ OTHER=def
+ FLAG = abc
+ FLAG2=abc
+ EOS
+ end
+
+ it "is successfully replaced" do
+ subject.change_make_var! "FLAG", "def"
+ expect(subject).to eq <<-EOS.undent
+ 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.undent
+ OTHER=def
+ FLAG=abc def
+ FLAG2=abc
+ EOS
+ end
+ end
+
+ context "with tabs" do
+ let(:string) do
+ <<-EOS.undent
+ 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.undent
+ CFLAGS=-O3
+ LDFLAGS\t=\t-lcrypto -lssl
+ EOS
+ end
+ end
+ end
+
+ context "empty flag between other flags" do
+ let(:string) do
+ <<-EOS.undent
+ OTHER=def
+ FLAG =
+ FLAG2=abc
+ EOS
+ end
+
+ it "is successfully replaced" do
+ subject.change_make_var! "FLAG", "def"
+ expect(subject).to eq <<-EOS.undent
+ OTHER=def
+ FLAG=def
+ FLAG2=abc
+ EOS
+ end
+ end
+
+ context "empty flag" do
+ let(:string) do
+ <<-EOS.undent
+ FLAG =
+ mv file_a file_b
+ EOS
+ end
+
+ it "is successfully replaced" do
+ subject.change_make_var! "FLAG", "def"
+ expect(subject).to eq <<-EOS.undent
+ FLAG=def
+ mv file_a file_b
+ EOS
+ end
+ end
+
+ context "shell-style variable" do
+ let(:string) do
+ <<-EOS.undent
+ OTHER=def
+ FLAG=abc
+ FLAG2=abc
+ EOS
+ end
+
+ it "is successfully replaced" do
+ subject.change_make_var! "FLAG", "def"
+ expect(subject).to eq <<-EOS.undent
+ 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.undent
+ OTHER=def
+ FLAG = abc
+ FLAG2 = def
+ EOS
+ end
+
+ it "is successfully removed" do
+ subject.remove_make_var! "FLAG"
+ expect(subject).to eq <<-EOS.undent
+ OTHER=def
+ FLAG2 = def
+ EOS
+ end
+ end
+
+ context "with tabs" do
+ let(:string) do
+ <<-EOS.undent
+ 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.undent
+ CFLAGS\t=\t-Wall -O2
+ EOS
+ end
+ end
+ end
+
+ context "multiple flags" do
+ let(:string) do
+ <<-EOS.undent
+ 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.undent
+ OTHER=def
+ OTHER2=def
+ EOS
+ end
+ end
+ end
+
+ describe "#get_make_var" do
+ context "with spaces" do
+ let(:string) do
+ <<-EOS.undent
+ 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.undent
+ 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.undent
+ 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