From 47b673882a84d7683a07b4e366132a80a3ff1313 Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 16 Sep 2020 12:16:56 -0600 Subject: refactor: Extract shared test context --- lib/e2e/project.rb | 4 ++++ spec/spec_helper.rb | 3 +-- spec/spotbugs_spec.rb | 30 ++++++++++++++++++------------ spec/support/docker_helper.rb | 6 +++--- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/lib/e2e/project.rb b/lib/e2e/project.rb index a90dfd6..d2d56bc 100644 --- a/lib/e2e/project.rb +++ b/lib/e2e/project.rb @@ -9,6 +9,10 @@ class Project @path = Pathname(path) end + def report_for(type:) + JSON.parse(path.join("gl-#{type}-report.json").read) + end + def mount(dir:) FileUtils.cp_r("#{dir}/.", path) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 19f2cdb..7d513b4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -15,8 +15,7 @@ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration $LOAD_PATH.unshift(Pathname.pwd.join('lib')) require 'e2e' -require 'support/docker_helper' -require 'support/fixture_file_helper' +Dir["./spec/support/**/*.rb"].sort.each { |f| require f } RSpec.configure do |config| # rspec-expectations config goes here. You can use an alternate diff --git a/spec/spotbugs_spec.rb b/spec/spotbugs_spec.rb index 6f493d9..4bad4ff 100644 --- a/spec/spotbugs_spec.rb +++ b/spec/spotbugs_spec.rb @@ -1,20 +1,26 @@ -RSpec.describe 'spotbugs' do - subject do - docker.run(image: "#{self.class.description}:latest", project_path: project.path) - JSON.parse(project.path.join('gl-sast-report.json').read) - end +RSpec.shared_context 'a scanner' do + subject { project.report_for(type: report_type) } - let(:docker) { Docker.new(pwd: Pathname.pwd.join("src/#{self.class.description}")) } + let(:docker) { Docker.new(pwd: Pathname.pwd.join("src/#{scanner}")) } + let(:docker_image) { "#{scanner}:latest" } + let(:scanner) { self.class.description } let(:project) { Project.new } + let(:report_type) { raise 'report_type not specified' } - around(:each) do |example| - project.mount(dir: fixture_file('java/maven/custom-tls')) + around :example do |example| + project.mount(dir: fixture_file(project_fixture)) + docker.run(image: docker_image, project_path: project.path) example.run project.cleanup end +end - specify do - expect(subject).not_to be_nil - expect(subject['version']).to eql('3.0') - end +RSpec.describe 'spotbugs' do + include_context 'a scanner' + + let(:project_fixture) { 'java/maven/custom-tls' } + let(:report_type) { :sast } + + specify { expect(subject).not_to be_nil } + specify { expect(subject['version']).to eql('3.0') } end diff --git a/spec/support/docker_helper.rb b/spec/support/docker_helper.rb index 76c9195..4df1e6e 100644 --- a/spec/support/docker_helper.rb +++ b/spec/support/docker_helper.rb @@ -3,9 +3,9 @@ RSpec.configure do |config| Pathname.pwd.join('src').each_child do |file| next unless file.directory? - docker = Docker.new(pwd: file) - docker_image = "#{file.basename.to_s}:latest" - docker.build(tag: docker_image) + Docker + .new(pwd: file) + .build(tag: "#{file.basename}:latest") end end end -- cgit v1.2.3