2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/dependencies/requirement"
|
2017-11-09 11:29:41 +00:00
|
|
|
|
|
|
|
describe Inspec::Requirement do
|
2018-06-26 17:04:31 +00:00
|
|
|
let(:config) { { cwd: nil, backend: nil } }
|
2019-06-11 22:24:35 +00:00
|
|
|
let(:req) { Inspec::Requirement.new("foo", constraints, config, {}) }
|
2018-03-12 13:02:37 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#source_satisfies_spec?" do
|
2017-11-09 11:29:41 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when there are no version constraints" do
|
2017-11-09 11:29:41 +00:00
|
|
|
let(:constraints) { nil }
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns true" do
|
2017-11-09 11:29:41 +00:00
|
|
|
req.source_satisfies_spec?.must_equal true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when there is a single, matching version constraint" do
|
|
|
|
let(:constraints) { ">= 1" }
|
|
|
|
it "returns true" do
|
2017-11-09 11:29:41 +00:00
|
|
|
profile = mock
|
2019-06-11 22:24:35 +00:00
|
|
|
profile.stubs(:version).returns("2.0.0")
|
2017-11-09 11:29:41 +00:00
|
|
|
req.stubs(:profile).returns(profile)
|
|
|
|
req.source_satisfies_spec?.must_equal true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when there is a single, non-matching version constraint" do
|
|
|
|
let(:constraints) { ">= 2" }
|
|
|
|
it "returns true" do
|
2017-11-09 11:29:41 +00:00
|
|
|
profile = mock
|
2019-06-11 22:24:35 +00:00
|
|
|
profile.stubs(:version).returns("1.0.0")
|
2017-11-09 11:29:41 +00:00
|
|
|
req.stubs(:profile).returns(profile)
|
|
|
|
req.source_satisfies_spec?.must_equal false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when there are multiple matching version constraints" do
|
|
|
|
let(:constraints) { [">= 1", "< 3"] }
|
|
|
|
it "returns true" do
|
2017-11-09 11:29:41 +00:00
|
|
|
profile = mock
|
2019-06-11 22:24:35 +00:00
|
|
|
profile.stubs(:version).returns("2.0.0")
|
2017-11-09 11:29:41 +00:00
|
|
|
req.stubs(:profile).returns(profile)
|
|
|
|
req.source_satisfies_spec?.must_equal true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when there are multiple version constraints and one does not match" do
|
|
|
|
let(:constraints) { [">= 1", "< 3"] }
|
|
|
|
it "returns true" do
|
2017-11-09 11:29:41 +00:00
|
|
|
profile = mock
|
2019-06-11 22:24:35 +00:00
|
|
|
profile.stubs(:version).returns("4.0.0")
|
2017-11-09 11:29:41 +00:00
|
|
|
req.stubs(:profile).returns(profile)
|
|
|
|
req.source_satisfies_spec?.must_equal false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when a profile has a version with a build number and the constraint matches" do
|
|
|
|
let(:constraints) { ">= 1" }
|
|
|
|
it "returns true" do
|
2017-11-09 11:29:41 +00:00
|
|
|
profile = mock
|
2019-06-11 22:24:35 +00:00
|
|
|
profile.stubs(:version).returns("2.0.0+1")
|
2017-11-09 11:29:41 +00:00
|
|
|
req.stubs(:profile).returns(profile)
|
|
|
|
req.source_satisfies_spec?.must_equal true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when the constraint is the default >=0 and the profile has a pre-release number" do
|
|
|
|
let(:constraints) { ">= 0" }
|
|
|
|
it "returns true" do
|
2017-11-09 11:29:41 +00:00
|
|
|
profile = mock
|
2019-06-11 22:24:35 +00:00
|
|
|
profile.stubs(:version).returns("2.0.0-1")
|
2017-11-09 11:29:41 +00:00
|
|
|
req.stubs(:profile).returns(profile)
|
|
|
|
req.source_satisfies_spec?.must_equal true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-12 13:02:37 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#to_hash" do
|
2018-03-12 13:02:37 +00:00
|
|
|
let(:constraints) { nil }
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns the correct Hash" do
|
|
|
|
resolved_source = { compliance: "spam", url: "eggs", sha256: "bacon" }
|
2018-03-12 13:02:37 +00:00
|
|
|
req.stubs(:resolved_source).returns(resolved_source)
|
|
|
|
req.stubs(:dependencies).returns({})
|
|
|
|
|
|
|
|
correct_hash = {
|
2019-06-11 22:24:35 +00:00
|
|
|
"name" => "foo",
|
|
|
|
"resolved_source" => resolved_source,
|
|
|
|
"version_constraints" => [],
|
2018-03-12 13:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req.to_hash.must_equal correct_hash
|
|
|
|
end
|
|
|
|
end
|
2017-11-09 11:29:41 +00:00
|
|
|
end
|