2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/utils/simpleconfig"
|
2015-09-30 10:14:38 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "SimpleConfig Default Parser" do
|
|
|
|
it "should parse an empty string" do
|
|
|
|
cur = SimpleConfig.new("")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({})
|
2015-10-12 11:22:01 +00:00
|
|
|
end
|
2015-09-30 10:14:38 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "should parse only spaces" do
|
|
|
|
cur = SimpleConfig.new(" ")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({})
|
2015-10-12 11:22:01 +00:00
|
|
|
end
|
2015-09-30 10:14:38 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "should parse only tabs" do
|
2015-10-12 11:22:01 +00:00
|
|
|
cur = SimpleConfig.new("\t")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({})
|
2015-10-12 11:22:01 +00:00
|
|
|
end
|
2015-09-30 10:14:38 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "should parse only newlines" do
|
2015-10-12 11:22:01 +00:00
|
|
|
cur = SimpleConfig.new("\n")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({})
|
2015-10-12 11:22:01 +00:00
|
|
|
end
|
2015-09-30 10:14:38 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "should parse a simple assignment" do
|
|
|
|
cur = SimpleConfig.new("a = b")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({ "a" => "b" })
|
2015-10-12 11:22:01 +00:00
|
|
|
end
|
2015-09-30 10:14:38 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "should parse a multiple assignments" do
|
2015-10-12 11:22:01 +00:00
|
|
|
cur = SimpleConfig.new("a = b\n\nc = d")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({ "a" => "b", "c" => "d" })
|
2015-10-12 11:22:01 +00:00
|
|
|
end
|
2015-09-30 10:14:38 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "handles files with only comments" do
|
|
|
|
cur = SimpleConfig.new("#a comment")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({})
|
2015-10-12 11:22:01 +00:00
|
|
|
end
|
2015-09-30 10:14:38 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "handles separate comments and assignments" do
|
2015-10-12 11:22:01 +00:00
|
|
|
cur = SimpleConfig.new("# hello world\n\na = b")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({ "a" => "b" })
|
2015-10-12 11:22:01 +00:00
|
|
|
end
|
2015-09-30 10:14:38 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "handles comments and assignments combined" do
|
|
|
|
cur = SimpleConfig.new("a = b# hello")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({ "a" => "b" })
|
2015-10-12 11:22:01 +00:00
|
|
|
end
|
2015-09-30 10:14:38 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "handles groups" do
|
|
|
|
cur = SimpleConfig.new("[g]")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({ "g" => {} })
|
|
|
|
_(cur.groups).must_equal(["g"])
|
2015-10-12 11:22:01 +00:00
|
|
|
end
|
2015-09-30 10:14:38 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "handles non-group assignments and groups" do
|
2015-10-12 11:22:01 +00:00
|
|
|
cur = SimpleConfig.new("a = b\n[g]")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({ "a" => "b", "g" => {} })
|
|
|
|
_(cur.groups).must_equal(["g"])
|
2015-10-12 11:22:01 +00:00
|
|
|
end
|
2015-09-30 10:14:38 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "handles assignments in groups" do
|
2015-10-12 11:22:01 +00:00
|
|
|
cur = SimpleConfig.new("[g]\na = b")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({ "g" => { "a" => "b" } })
|
|
|
|
_(cur.groups).must_equal(["g"])
|
2015-10-12 11:22:01 +00:00
|
|
|
end
|
2015-09-30 10:14:38 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "handles multiple groups" do
|
2015-10-12 11:22:01 +00:00
|
|
|
cur = SimpleConfig.new("[g]\na = b\n[k]\n\nc = d")
|
|
|
|
res = {
|
2019-06-11 22:24:35 +00:00
|
|
|
"g" => { "a" => "b" },
|
|
|
|
"k" => { "c" => "d" },
|
2015-10-12 11:22:01 +00:00
|
|
|
}
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal(res)
|
|
|
|
_(cur.groups).must_equal(%w{g k})
|
2015-09-30 10:14:38 +00:00
|
|
|
end
|
2017-03-06 22:46:35 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "provides methods to access returned hashes" do
|
2017-03-06 22:46:35 +00:00
|
|
|
cur = SimpleConfig.new("[section1]\nkey1 = value1\n\n[section2]\nkey2 = value2\n")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params["section1"].key1).must_equal("value1")
|
|
|
|
_(cur.params["section2"].key2).must_equal("value2")
|
|
|
|
_(cur.params["section2"].missing_key).must_be_nil
|
2017-03-06 22:46:35 +00:00
|
|
|
end
|
2017-04-26 21:18:14 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "supports :assignment_regex for specifying the assignment" do
|
2017-04-26 21:18:14 +00:00
|
|
|
cur = SimpleConfig.new("key:::val", assignment_regex: /^(.*):::(.*)$/)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({ "key" => "val" })
|
2017-04-26 21:18:14 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "only reads the first assignment match group by default" do
|
2017-04-26 21:18:14 +00:00
|
|
|
cur = SimpleConfig.new("1:2:3", assignment_regex: /^(.*):(.*):(.*)$/)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({ "1" => "2" })
|
2017-04-26 21:18:14 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "supports :key_values for specifying the number of values" do
|
2017-04-26 21:18:14 +00:00
|
|
|
cur = SimpleConfig.new("1:2:3", assignment_regex: /^(.*):(.*):(.*)$/, key_values: 2)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({ "1" => %w{2 3} })
|
2017-04-26 21:18:14 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "supports :key_values with more values than match groups" do
|
2017-04-26 21:18:14 +00:00
|
|
|
cur = SimpleConfig.new("1:2:3", assignment_regex: /^(.*):(.*):(.*)$/, key_values: 4)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(cur.params).must_equal({ "1" => ["2", "3", nil, nil] })
|
2017-04-26 21:18:14 +00:00
|
|
|
end
|
2015-09-30 10:14:38 +00:00
|
|
|
end
|