diff options
| author | mo khan <mo.khan@gmail.com> | 2020-09-16 17:19:55 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-09-16 17:19:55 -0600 |
| commit | 20595e44b91a72dc39f72090bf9aed3b7db2a1bc (patch) | |
| tree | 834a87f83ec44e44f090e264887a8d0413abac1e /spec | |
| parent | 46b002a96345ce18b0f7a95ef00fca1c34cb9298 (diff) | |
test: add spec to validate the dependency_scanning report schema
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/integration/gemnasium_maven_spec.rb | 1 | ||||
| m--------- | spec/schemas | 0 | ||||
| -rw-r--r-- | spec/spec_helper.rb | 1 | ||||
| -rw-r--r-- | spec/support/matchers/match_schema.rb | 19 | ||||
| -rw-r--r-- | spec/support/shared/with_expected.rb | 8 |
5 files changed, 25 insertions, 4 deletions
diff --git a/spec/integration/gemnasium_maven_spec.rb b/spec/integration/gemnasium_maven_spec.rb index 46cef08..4372110 100644 --- a/spec/integration/gemnasium_maven_spec.rb +++ b/spec/integration/gemnasium_maven_spec.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true -require 'openssl' RSpec.describe 'gemnasium-maven' do let(:scanner) { 'gemnasium-maven' } diff --git a/spec/schemas b/spec/schemas new file mode 160000 +Subproject cdeadc11c700e0daddd2206095eeee6402a50cf diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 303f328..5487c1a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,6 +16,7 @@ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration $LOAD_PATH.unshift(Pathname.pwd.join('lib')) require 'e2e' +require 'json-schema' Dir["./spec/support/**/*.rb"].sort.each { |f| require f } RSpec.configure do |config| diff --git a/spec/support/matchers/match_schema.rb b/spec/support/matchers/match_schema.rb new file mode 100644 index 0000000..eef6106 --- /dev/null +++ b/spec/support/matchers/match_schema.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +RSpec::Matchers.define :match_schema do |report_type| + def schema_for(type) + relative_path = "spec/schemas/dist/#{type.gsub('_', '-')}-report-format.json" + json = JSON.parse(Pathname.pwd.join(relative_path).read) + json.delete('$schema') + json + end + + match do |actual| + !actual.nil? && (@errors = JSON::Validator.fully_validate(schema_for(report_type.to_s), actual.to_h)).empty? + end + + failure_message do |response| + "didn't match the schema for #{report_type}" \ + " The validation errors were:\n#{@errors.join("\n")}" + end +end diff --git a/spec/support/shared/with_expected.rb b/spec/support/shared/with_expected.rb index be4d7cc..00fc420 100644 --- a/spec/support/shared/with_expected.rb +++ b/spec/support/shared/with_expected.rb @@ -6,11 +6,13 @@ RSpec.shared_examples "with expected" do |language, package_manager, version, br let(:git_branch) { branch } let(:expected_content) { JSON.parse(fixture_file_content("expected/#{language}/#{package_manager}/#{branch}/v#{version}.json")) } + pending { expect(subject).to match_schema(report_type) } + pending do - actual_pretty = JSON.pretty_generate(subject.to_h) - expected_pretty = JSON.pretty_generate(expected_content) + actual = JSON.pretty_generate(subject.to_h) + expected = JSON.pretty_generate(expected_content) - expect(actual_pretty).to eq(expected_pretty) + expect(actual).to eq(expected) end end end |
