Convert StringInreplaceExtension test to spec.
This commit is contained in:
parent
25959927fa
commit
1ff6846218
251
Library/Homebrew/test/inreplace_spec.rb
Normal file
251
Library/Homebrew/test/inreplace_spec.rb
Normal file
@ -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
|
||||
@ -1,119 +0,0 @@
|
||||
require "testing_env"
|
||||
require "extend/string"
|
||||
require "utils/inreplace"
|
||||
|
||||
class InreplaceTest < Homebrew::TestCase
|
||||
def test_change_make_var
|
||||
# Replace flag
|
||||
s1 = "OTHER=def\nFLAG = abc\nFLAG2=abc"
|
||||
s1.extend(StringInreplaceExtension)
|
||||
s1.change_make_var! "FLAG", "def"
|
||||
assert_equal "OTHER=def\nFLAG=def\nFLAG2=abc", s1
|
||||
end
|
||||
|
||||
def test_change_make_var_empty
|
||||
# Replace empty flag
|
||||
s1 = "OTHER=def\nFLAG = \nFLAG2=abc"
|
||||
s1.extend(StringInreplaceExtension)
|
||||
s1.change_make_var! "FLAG", "def"
|
||||
assert_equal "OTHER=def\nFLAG=def\nFLAG2=abc", s1
|
||||
end
|
||||
|
||||
def test_change_make_var_empty_2
|
||||
# Replace empty flag
|
||||
s1 = "FLAG = \nmv file_a file_b"
|
||||
s1.extend(StringInreplaceExtension)
|
||||
s1.change_make_var! "FLAG", "def"
|
||||
assert_equal "FLAG=def\nmv file_a file_b", s1
|
||||
end
|
||||
|
||||
def test_change_make_var_append
|
||||
# Append to flag
|
||||
s1 = "OTHER=def\nFLAG = abc\nFLAG2=abc"
|
||||
s1.extend(StringInreplaceExtension)
|
||||
s1.change_make_var! "FLAG", "\\1 def"
|
||||
assert_equal "OTHER=def\nFLAG=abc def\nFLAG2=abc", s1
|
||||
end
|
||||
|
||||
def test_change_make_var_shell_style
|
||||
# Shell variables have no spaces around =
|
||||
s1 = "OTHER=def\nFLAG=abc\nFLAG2=abc"
|
||||
s1.extend(StringInreplaceExtension)
|
||||
s1.change_make_var! "FLAG", "def"
|
||||
assert_equal "OTHER=def\nFLAG=def\nFLAG2=abc", s1
|
||||
end
|
||||
|
||||
def test_remove_make_var
|
||||
# Replace flag
|
||||
s1 = "OTHER=def\nFLAG = abc\nFLAG2 = def"
|
||||
s1.extend(StringInreplaceExtension)
|
||||
s1.remove_make_var! "FLAG"
|
||||
assert_equal "OTHER=def\nFLAG2 = def", s1
|
||||
end
|
||||
|
||||
def test_remove_make_vars
|
||||
# Replace flag
|
||||
s1 = "OTHER=def\nFLAG = abc\nFLAG2 = def\nOTHER2=def"
|
||||
s1.extend(StringInreplaceExtension)
|
||||
s1.remove_make_var! ["FLAG", "FLAG2"]
|
||||
assert_equal "OTHER=def\nOTHER2=def", s1
|
||||
end
|
||||
|
||||
def test_get_make_var
|
||||
s = "CFLAGS = -Wall -O2\nLDFLAGS = -lcrypto -lssl"
|
||||
s.extend(StringInreplaceExtension)
|
||||
assert_equal "-Wall -O2", s.get_make_var("CFLAGS")
|
||||
end
|
||||
|
||||
def test_change_make_var_with_tabs
|
||||
s = "CFLAGS\t=\t-Wall -O2\nLDFLAGS\t=\t-lcrypto -lssl"
|
||||
s.extend(StringInreplaceExtension)
|
||||
|
||||
assert_equal "-Wall -O2", s.get_make_var("CFLAGS")
|
||||
|
||||
s.change_make_var! "CFLAGS", "-O3"
|
||||
assert_equal "CFLAGS=-O3\nLDFLAGS\t=\t-lcrypto -lssl", s
|
||||
|
||||
s.remove_make_var! "LDFLAGS"
|
||||
assert_equal "CFLAGS=-O3\n", s
|
||||
end
|
||||
|
||||
def test_sub_gsub
|
||||
s = "foo"
|
||||
s.extend(StringInreplaceExtension)
|
||||
|
||||
s.sub!("f", "b")
|
||||
assert_equal "boo", s
|
||||
|
||||
# Under current context, we are testing `String#gsub!`, so let's disable rubocop temporarily.
|
||||
s.gsub!("o", "e") # rubocop:disable Performance/StringReplacement
|
||||
assert_equal "bee", s
|
||||
end
|
||||
|
||||
def test_inreplace_errors
|
||||
require "tempfile"
|
||||
extend(Utils::Inreplace)
|
||||
|
||||
file = Tempfile.new("test")
|
||||
|
||||
file.write "a\nb\nc\n"
|
||||
|
||||
assert_raises(Utils::InreplaceError) do
|
||||
inreplace file.path, "d", "f"
|
||||
end
|
||||
|
||||
assert_raises(Utils::InreplaceError) do
|
||||
# Under current context, we are testing `String#gsub!`, so let's disable rubocop temporarily.
|
||||
inreplace(file.path) { |s| s.gsub!("d", "f") } # rubocop:disable Performance/StringReplacement
|
||||
end
|
||||
|
||||
assert_raises(Utils::InreplaceError) do
|
||||
inreplace(file.path) do |s|
|
||||
s.change_make_var! "VAR", "value"
|
||||
s.remove_make_var! "VAR2"
|
||||
end
|
||||
end
|
||||
ensure
|
||||
file.unlink
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user