From 955ab369229ada0669552a5ab7b83aa486b11cef Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Wed, 10 Jun 2020 19:23:52 -0400 Subject: [PATCH] Update junit tests to use nokogiri Signed-off-by: Clinton Wolfe --- Gemfile | 1 + test/functional/inspec_exec_junit_test.rb | 34 +++++++++++------------ 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/Gemfile b/Gemfile index 59b26b0f1..56ddae642 100644 --- a/Gemfile +++ b/Gemfile @@ -25,6 +25,7 @@ group :test do gem "rake", ">= 10" gem "simplecov", ["~> 0.10", "<=0.18.2"] gem "concurrent-ruby", "~> 1.0" + gem "nokogiri", "~> 1.9" gem "mocha", "~> 1.1" gem "ruby-progressbar", "~> 1.8" gem "webmock", "~> 3.0" diff --git a/test/functional/inspec_exec_junit_test.rb b/test/functional/inspec_exec_junit_test.rb index e3da1bfa6..9fcdb3791 100644 --- a/test/functional/inspec_exec_junit_test.rb +++ b/test/functional/inspec_exec_junit_test.rb @@ -1,5 +1,5 @@ require "functional/helper" -require "rexml/document" +require "nokogiri" describe "inspec exec with junit formatter" do include FunctionalHelper @@ -9,11 +9,10 @@ describe "inspec exec with junit formatter" do it "can execute a simple file with the junit formatter" do out = inspec("exec " + example_control + " --reporter junit --no-create-lockfile") - # TODO: rexml is about as slow as you can go. Use nokogiri - doc = REXML::Document.new(out.stdout) - _(doc.has_elements?).must_equal true - _(out.stderr).must_equal "" + doc = Nokogiri::XML(out.stdout) + _(doc.document?).must_equal true + _(doc.errors).must_be_empty assert_exit_code 0, out end @@ -21,44 +20,43 @@ describe "inspec exec with junit formatter" do it "can execute the profile with the junit formatter" do out = inspec("exec " + complete_profile + " --reporter junit --no-create-lockfile") - # TODO: _never_ use rexml. Anything else is guaranteed faster - doc = REXML::Document.new(out.stdout) - _(doc.has_elements?).must_equal true - _(out.stderr).must_equal "" + doc = Nokogiri::XML(out.stdout) + _(doc.document?).must_equal true + _(doc.errors).must_be_empty assert_exit_code 0, out end describe "execute a profile with junit formatting" do - let(:doc) { REXML::Document.new(inspec("exec " + example_profile + " --reporter junit --no-create-lockfile").stdout) } + let(:doc) { Nokogiri::XML(inspec("exec " + example_profile + " --reporter junit --no-create-lockfile").stdout) } describe "the document" do it "has only one testsuite" do - _(doc.elements.to_a("//testsuite").length).must_equal 1 + _(doc.xpath("//testsuite").length).must_equal 1 end end describe "the test suite" do - let(:suite) { doc.elements.to_a("//testsuites/testsuite").first } + let(:suite) { doc.xpath("//testsuites/testsuite").first } it "must have 4 testcase children" do - _(suite.elements.to_a("//testcase").length).must_equal 4 + _(suite.xpath("//testcase").length).must_equal 4 end it "has the tests attribute with 4 total tests" do - _(suite.attribute("tests").value).must_equal "4" + _(suite.attr("tests")).must_equal "4" end it "has the failures attribute with 0 total tests" do - _(suite.attribute("failed").value).must_equal "0" + _(suite.attr("failed")).must_equal "0" end - it 'has 2 elements named "File / should be directory"' do - _(REXML::XPath.match(suite, "//testcase[@name='File / is expected to be directory']").length).must_equal 3 + it 'has 3 elements named "File / should be directory"' do + _(suite.xpath("//testcase[@name='File / is expected to be directory']").length).must_equal 3 end describe 'the testcase named "example_config Can\'t find file ..."' do - let(:example_yml_tests) { REXML::XPath.match(suite, "//testcase[@classname='profile.example-1.0' and @name='example_config']") } + let(:example_yml_tests) { suite.xpath("//testcase[@classname='profile.example-1.0' and @name='example_config']") } let(:first_example_test) { example_yml_tests.first } it "should be unique" do