2014-09-28 01:08:31 -05:00
|
|
|
require "testing_env"
|
|
|
|
require "extend/string"
|
|
|
|
require "utils/inreplace"
|
2010-03-16 11:47:10 -07:00
|
|
|
|
2014-06-18 20:32:51 -05:00
|
|
|
class InreplaceTest < Homebrew::TestCase
|
2010-01-29 10:41:03 -08:00
|
|
|
def test_change_make_var
|
|
|
|
# Replace flag
|
2014-09-26 21:55:26 -05:00
|
|
|
s1 = "OTHER=def\nFLAG = abc\nFLAG2=abc"
|
2010-11-12 21:05:35 -08:00
|
|
|
s1.extend(StringInreplaceExtension)
|
2010-01-29 10:41:03 -08:00
|
|
|
s1.change_make_var! "FLAG", "def"
|
2010-03-16 11:47:10 -07:00
|
|
|
assert_equal "OTHER=def\nFLAG=def\nFLAG2=abc", s1
|
2010-01-29 10:41:03 -08:00
|
|
|
end
|
2010-03-12 13:24:04 +01:00
|
|
|
|
|
|
|
def test_change_make_var_empty
|
|
|
|
# Replace empty flag
|
2014-09-26 21:55:26 -05:00
|
|
|
s1 = "OTHER=def\nFLAG = \nFLAG2=abc"
|
2010-11-12 21:05:35 -08:00
|
|
|
s1.extend(StringInreplaceExtension)
|
2010-03-12 13:24:04 +01:00
|
|
|
s1.change_make_var! "FLAG", "def"
|
2010-03-16 11:47:10 -07:00
|
|
|
assert_equal "OTHER=def\nFLAG=def\nFLAG2=abc", s1
|
2010-03-12 13:24:04 +01:00
|
|
|
end
|
2012-06-20 00:51:01 -05:00
|
|
|
|
2010-03-16 11:57:55 -07:00
|
|
|
def test_change_make_var_empty_2
|
|
|
|
# Replace empty flag
|
2014-09-26 21:55:26 -05:00
|
|
|
s1 = "FLAG = \nmv file_a file_b"
|
2010-11-12 21:05:35 -08:00
|
|
|
s1.extend(StringInreplaceExtension)
|
2010-03-16 11:57:55 -07:00
|
|
|
s1.change_make_var! "FLAG", "def"
|
|
|
|
assert_equal "FLAG=def\nmv file_a file_b", s1
|
|
|
|
end
|
2012-06-20 00:51:01 -05:00
|
|
|
|
2010-01-29 10:41:03 -08:00
|
|
|
def test_change_make_var_append
|
|
|
|
# Append to flag
|
2014-09-26 21:55:26 -05:00
|
|
|
s1 = "OTHER=def\nFLAG = abc\nFLAG2=abc"
|
2010-11-12 21:05:35 -08:00
|
|
|
s1.extend(StringInreplaceExtension)
|
2010-01-29 10:41:03 -08:00
|
|
|
s1.change_make_var! "FLAG", "\\1 def"
|
2010-03-16 11:47:10 -07:00
|
|
|
assert_equal "OTHER=def\nFLAG=abc def\nFLAG2=abc", s1
|
2010-01-29 10:41:03 -08:00
|
|
|
end
|
2012-06-20 00:51:01 -05:00
|
|
|
|
2010-02-09 09:21:25 -08:00
|
|
|
def test_change_make_var_shell_style
|
|
|
|
# Shell variables have no spaces around =
|
2014-09-26 21:55:26 -05:00
|
|
|
s1 = "OTHER=def\nFLAG=abc\nFLAG2=abc"
|
2010-11-12 21:05:35 -08:00
|
|
|
s1.extend(StringInreplaceExtension)
|
2010-02-09 09:21:25 -08:00
|
|
|
s1.change_make_var! "FLAG", "def"
|
2010-03-16 11:47:10 -07:00
|
|
|
assert_equal "OTHER=def\nFLAG=def\nFLAG2=abc", s1
|
2010-02-09 09:21:25 -08:00
|
|
|
end
|
2010-01-29 10:41:03 -08:00
|
|
|
|
|
|
|
def test_remove_make_var
|
|
|
|
# Replace flag
|
2014-09-26 21:55:26 -05:00
|
|
|
s1 = "OTHER=def\nFLAG = abc\nFLAG2 = def"
|
2010-11-12 21:05:35 -08:00
|
|
|
s1.extend(StringInreplaceExtension)
|
2010-01-29 10:41:03 -08:00
|
|
|
s1.remove_make_var! "FLAG"
|
2010-03-16 11:47:10 -07:00
|
|
|
assert_equal "OTHER=def\nFLAG2 = def", s1
|
2010-01-29 10:41:03 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_remove_make_vars
|
|
|
|
# Replace flag
|
2014-09-26 21:55:26 -05:00
|
|
|
s1 = "OTHER=def\nFLAG = abc\nFLAG2 = def\nOTHER2=def"
|
2010-11-12 21:05:35 -08:00
|
|
|
s1.extend(StringInreplaceExtension)
|
2010-01-29 10:41:03 -08:00
|
|
|
s1.remove_make_var! ["FLAG", "FLAG2"]
|
2010-03-16 11:47:10 -07:00
|
|
|
assert_equal "OTHER=def\nOTHER2=def", s1
|
2010-01-29 10:41:03 -08:00
|
|
|
end
|
2013-02-15 00:39:22 -06:00
|
|
|
|
|
|
|
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
|
2014-09-26 22:01:00 -05:00
|
|
|
|
|
|
|
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
|
2014-09-27 23:31:08 -05:00
|
|
|
|
|
|
|
def test_sub_gsub
|
|
|
|
s = "foo"
|
|
|
|
s.extend(StringInreplaceExtension)
|
|
|
|
|
|
|
|
s.sub!("f", "b")
|
|
|
|
assert_equal "boo", s
|
|
|
|
|
2015-08-06 16:51:50 +08:00
|
|
|
# Under current context, we are testing `String#gsub!`, so let's disable rubocop temporarily.
|
|
|
|
s.gsub!("o", "e") # rubocop:disable Performance/StringReplacement
|
2014-09-27 23:31:08 -05:00
|
|
|
assert_equal "bee", s
|
|
|
|
end
|
2014-09-28 01:08:31 -05:00
|
|
|
|
|
|
|
def test_inreplace_errors
|
2016-09-19 23:00:58 +01:00
|
|
|
require "tempfile"
|
2014-09-28 01:08:31 -05:00
|
|
|
extend(Utils::Inreplace)
|
|
|
|
|
2016-09-19 23:00:58 +01:00
|
|
|
file = Tempfile.new("test")
|
|
|
|
|
|
|
|
file.write "a\nb\nc\n"
|
2014-09-28 01:08:31 -05:00
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
assert_raises(Utils::InreplaceError) do
|
2016-09-19 23:00:58 +01:00
|
|
|
inreplace file.path, "d", "f"
|
2015-08-03 13:09:07 +01:00
|
|
|
end
|
2014-09-28 01:08:31 -05:00
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
assert_raises(Utils::InreplaceError) do
|
2015-08-06 16:51:50 +08:00
|
|
|
# Under current context, we are testing `String#gsub!`, so let's disable rubocop temporarily.
|
2016-09-19 23:00:58 +01:00
|
|
|
inreplace(file.path) { |s| s.gsub!("d", "f") } # rubocop:disable Performance/StringReplacement
|
2015-08-03 13:09:07 +01:00
|
|
|
end
|
2014-09-28 01:08:31 -05:00
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
assert_raises(Utils::InreplaceError) do
|
2016-09-19 23:00:58 +01:00
|
|
|
inreplace(file.path) do |s|
|
2014-09-28 01:08:31 -05:00
|
|
|
s.change_make_var! "VAR", "value"
|
|
|
|
s.remove_make_var! "VAR2"
|
2015-08-03 13:09:07 +01:00
|
|
|
end
|
|
|
|
end
|
2014-09-28 01:08:31 -05:00
|
|
|
ensure
|
2016-09-19 23:00:58 +01:00
|
|
|
file.unlink
|
2014-09-28 01:08:31 -05:00
|
|
|
end
|
2010-01-29 10:41:03 -08:00
|
|
|
end
|