From 33b2876d7cf61e67a904eb03562850a00ab8c9a7 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Mon, 22 Feb 2016 05:35:47 +0100 Subject: [PATCH] fix tests and lint --- lib/inspec/profile.rb | 2 +- lib/inspec/runner.rb | 7 +++++++ lib/utils/base_cli.rb | 6 +----- test/docker_test.rb | 2 +- test/unit/fetchers/tar_test.rb | 3 ++- test/unit/fetchers/url_test.rb | 8 ++++---- test/unit/fetchers/zip_test.rb | 3 ++- test/unit/profile_test.rb | 6 +++--- 8 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/inspec/profile.rb b/lib/inspec/profile.rb index 0c1533668..0dcee49ca 100644 --- a/lib/inspec/profile.rb +++ b/lib/inspec/profile.rb @@ -214,7 +214,7 @@ module Inspec def load_params params = @source_reader.metadata.params - params[:name] = @profile_id + params[:name] = @profile_id unless @profile_id.nil? params[:rules] = rules = {} prefix = @source_reader.target.prefix || '' diff --git a/lib/inspec/runner.rb b/lib/inspec/runner.rb index 3478c7a65..4ee7aa77a 100644 --- a/lib/inspec/runner.rb +++ b/lib/inspec/runner.rb @@ -8,6 +8,7 @@ require 'forwardable' require 'uri' require 'inspec/backend' require 'inspec/profile_context' +require 'inspec/profile' require 'inspec/metadata' # spec requirements @@ -60,6 +61,12 @@ module Inspec end end + def add_target(target, options = {}) + profile = Inspec::Profile.for_target(target, options) + fail "Could not resolve #{target} to valid input." if profile.nil? + add_profile(profile) + end + def create_context Inspec::ProfileContext.new(@profile_id, @backend) end diff --git a/lib/utils/base_cli.rb b/lib/utils/base_cli.rb index 052686438..73583f6f9 100644 --- a/lib/utils/base_cli.rb +++ b/lib/utils/base_cli.rb @@ -48,11 +48,7 @@ module Inspec o[:logger].level = get_log_level(o.log_level) runner = Inspec::Runner.new(o) - targets.map { |t| - profile = Inspec::Profile.for_target(t, opts) - fail "Could not resolve #{t} to valid input." if profile.nil? - runner.add_profile(profile) - } + targets.each { |target| runner.add_target(target, opts) } exit runner.run rescue RuntimeError => e puts e.message diff --git a/test/docker_test.rb b/test/docker_test.rb index e88680337..5a1c89ca3 100644 --- a/test/docker_test.rb +++ b/test/docker_test.rb @@ -42,7 +42,7 @@ class DockerTester puts "--> run test on docker #{container.id}" opts = { 'target' => "docker://#{container.id}" } runner = Inspec::Runner.new(opts) - runner.add_tests(@tests) + @tests.each { |test| runner.add_target(test, opts) } runner.tests.map { |g| g.run(report) } end end diff --git a/test/unit/fetchers/tar_test.rb b/test/unit/fetchers/tar_test.rb index be059a8e1..eb58d4fb4 100644 --- a/test/unit/fetchers/tar_test.rb +++ b/test/unit/fetchers/tar_test.rb @@ -21,7 +21,8 @@ describe Fetchers::Tar do end it 'must contain all files' do - _(res.files).must_equal ["inspec.yml", "libraries", "libraries/testlib.rb", "controls", "controls/filesystem_spec.rb"] + _(res.files.sort).must_equal %w{inspec.yml libraries libraries/testlib.rb + controls controls/filesystem_spec.rb}.sort end it 'must not read if the file isnt included' do diff --git a/test/unit/fetchers/url_test.rb b/test/unit/fetchers/url_test.rb index 543ca8f38..ed5ed75ef 100644 --- a/test/unit/fetchers/url_test.rb +++ b/test/unit/fetchers/url_test.rb @@ -94,8 +94,8 @@ describe Fetchers::Url do end it 'must contain all files' do - _(res.files).must_equal %w{inspec.yml libraries libraries/testlib.rb - controls controls/filesystem_spec.rb} + _(res.files.sort).must_equal %w{inspec.yml libraries libraries/testlib.rb + controls controls/filesystem_spec.rb}.sort end it 'must not read if the file isnt included' do @@ -123,8 +123,8 @@ describe Fetchers::Url do end it 'must contain all files' do - _(res.files).must_equal %w{inspec.yml libraries libraries/testlib.rb - controls controls/filesystem_spec.rb} + _(res.files.sort).must_equal %w{inspec.yml libraries libraries/testlib.rb + controls controls/filesystem_spec.rb}.sort end it 'must not read if the file isnt included' do diff --git a/test/unit/fetchers/zip_test.rb b/test/unit/fetchers/zip_test.rb index d9ff97d1c..2386da05c 100644 --- a/test/unit/fetchers/zip_test.rb +++ b/test/unit/fetchers/zip_test.rb @@ -21,7 +21,8 @@ describe Fetchers::Zip do end it 'must contain all files' do - _(res.files).must_equal ["inspec.yml", "libraries", "libraries/testlib.rb", "controls", "controls/filesystem_spec.rb"] + _(res.files.sort).must_equal %w{inspec.yml libraries libraries/testlib.rb + controls controls/filesystem_spec.rb}.sort end it 'must not read if the file isnt included' do diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 043aa75e3..fb19deebd 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -183,7 +183,7 @@ describe Inspec::Profile do logger.expect :info, nil, ["Checking profile in #{home}/mock/profiles/#{profile_id}"] logger.expect :info, nil, ['Metadata OK.'] logger.expect :info, nil, ['Found 1 controls.'] - logger.expect :info, nil, ["Verify all controls in #{home}/mock/profiles/#{profile_id}/controls/filesystem_spec.rb"] + logger.expect :info, nil, ["Verify all controls in controls/filesystem_spec.rb"] logger.expect :info, nil, ['Control definitions OK.'] result = MockLoader.load_profile(profile_id, {logger: logger}).check @@ -209,7 +209,7 @@ describe Inspec::Profile do logger.expect :info, nil, ["Checking profile in #{home}/mock/profiles/#{profile_id}"] logger.expect :info, nil, ['Metadata OK.'] logger.expect :info, nil, ['Found 1 controls.'] - logger.expect :info, nil, ["Verify all controls in #{home}/mock/profiles/#{profile_id}/controls/filesystem_spec.rb"] + logger.expect :info, nil, ["Verify all controls in controls/filesystem_spec.rb"] logger.expect :info, nil, ['Control definitions OK.'] result = MockLoader.load_profile(profile_id, {logger: logger}).check @@ -235,7 +235,7 @@ describe Inspec::Profile do logger.expect :info, nil, ["Checking profile in #{home}/mock/profiles/#{profile_id}"] logger.expect :info, nil, ['Metadata OK.'] logger.expect :info, nil, ['Found 1 controls.'] - logger.expect :info, nil, ["Verify all controls in #{home}/mock/profiles/#{profile_id}/controls/filesystem_spec.rb"] + logger.expect :info, nil, ["Verify all controls in controls/filesystem_spec.rb"] logger.expect :info, nil, ['Control definitions OK.'] result = MockLoader.load_profile(profile_id, {logger: logger}).check