diff options
| author | mo khan <mo.khan@gmail.com> | 2020-09-10 17:54:55 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-09-10 18:06:35 -0600 |
| commit | 584d83b75385535017158ba65b5b97a9cfcad2b2 (patch) | |
| tree | 63700df091de09ba0607991de5bc38334bf6986e | |
| parent | cc3564f398a2537340e4a91ee349bea491dc20ac (diff) | |
refactor: collapse assertions to speed up tests
| -rw-r--r-- | spec/integration/c/conan_spec.rb | 79 | ||||
| -rw-r--r-- | spec/integration/java/gradle_spec.rb | 82 | ||||
| -rw-r--r-- | spec/integration/java/maven_spec.rb | 43 |
3 files changed, 110 insertions, 94 deletions
diff --git a/spec/integration/c/conan_spec.rb b/spec/integration/c/conan_spec.rb index baae361..4b60dc4 100644 --- a/spec/integration/c/conan_spec.rb +++ b/spec/integration/c/conan_spec.rb @@ -3,71 +3,74 @@ require 'spec_helper' RSpec.describe "conan" do + subject { runner.scan(env: env) } + + let(:env) { {} } + include_examples "each report version", "c", "conan" context "when scanning a C++ project" do - subject { runner.scan(env: env) } - let(:env) { { 'LICENSE_FINDER_CLI_OPTS' => '--project-path=libraries/poco/md5' } } before do runner.clone('https://github.com/conan-io/examples.git') end - specify { expect(subject).to match_schema } specify { expect { subject }.to perform_under(60).sec.warmup(0).times } - specify { expect(subject.dependency_names).to match_array(%w[bzip2 expat openssl pcre poco sqlite3 zlib]) } - specify { expect(subject.licenses_for('openssl')).to match_array(['OpenSSL']) } - specify { expect(subject.licenses_for('poco')).to match_array(['BSL-1.0']) } + + specify do + expect(subject).to match_schema + expect(subject.dependency_names).to match_array(%w[bzip2 expat openssl pcre poco sqlite3 zlib]) + expect(subject.licenses_for('openssl')).to match_array(['OpenSSL']) + expect(subject.licenses_for('poco')).to match_array(['BSL-1.0']) + end end context "when scanning a folly project" do - subject { runner.scan(env: env) } - let(:env) { { 'LICENSE_FINDER_CLI_OPTS' => '--project-path=libraries/folly/basic' } } before do runner.clone('https://github.com/conan-io/examples.git') end - specify { expect(subject).to match_schema } - specify { expect(subject.licenses_for('boost')).to match_array(['BSL-1.0']) } - specify { expect(subject.licenses_for('bzip2')).to match_array(['bzip2-1.0.8']) } - specify { expect(subject.licenses_for('double-conversion')).to match_array(['BSD-3-Clause']) } - specify { expect(subject.licenses_for('folly')).to match_array(['Apache-2.0']) } - specify { expect(subject.licenses_for('gflags')).to match_array(['BSD-3-Clause']) } - specify { expect(subject.licenses_for('glog')).to match_array(['BSD-3-Clause']) } - specify { expect(subject.licenses_for('libdwarf')).to match_array(['LGPL-2.1']) } - specify { expect(subject.licenses_for('libelf')).to match_array(['LGPL-2.0']) } - specify { expect(subject.licenses_for('libevent')).to match_array(['BSD-3-Clause']) } - specify { expect(subject.licenses_for('libiberty')).to match_array(['LGPL-2.1']) } - specify { expect(subject.licenses_for('libsodium')).to match_array(['ISC']) } - specify { expect(subject.licenses_for('libunwind')).to match_array(['MIT']) } - specify { expect(subject.licenses_for('lz4')).to match_array(['BSD-2-Clause', 'BSD-3-Clause']) } - specify { expect(subject.licenses_for('openssl')).to match_array(['OpenSSL']) } - specify { expect(subject.licenses_for('snappy')).to match_array(['BSD-3-Clause']) } - specify { expect(subject.licenses_for('zlib')).to match_array(['Zlib']) } - specify { expect(subject.licenses_for('zstd')).to match_array(['BSD-3-Clause']) } + specify do + expect(subject).to match_schema + expect(subject.licenses_for('boost')).to match_array(['BSL-1.0']) + expect(subject.licenses_for('bzip2')).to match_array(['bzip2-1.0.8']) + expect(subject.licenses_for('double-conversion')).to match_array(['BSD-3-Clause']) + expect(subject.licenses_for('folly')).to match_array(['Apache-2.0']) + expect(subject.licenses_for('gflags')).to match_array(['BSD-3-Clause']) + expect(subject.licenses_for('glog')).to match_array(['BSD-3-Clause']) + expect(subject.licenses_for('libdwarf')).to match_array(['LGPL-2.1']) + expect(subject.licenses_for('libelf')).to match_array(['LGPL-2.0']) + expect(subject.licenses_for('libevent')).to match_array(['BSD-3-Clause']) + expect(subject.licenses_for('libiberty')).to match_array(['LGPL-2.1']) + expect(subject.licenses_for('libsodium')).to match_array(['ISC']) + expect(subject.licenses_for('libunwind')).to match_array(['MIT']) + expect(subject.licenses_for('lz4')).to match_array(['BSD-2-Clause', 'BSD-3-Clause']) + expect(subject.licenses_for('openssl')).to match_array(['OpenSSL']) + expect(subject.licenses_for('snappy')).to match_array(['BSD-3-Clause']) + expect(subject.licenses_for('zlib')).to match_array(['Zlib']) + expect(subject.licenses_for('zstd')).to match_array(['BSD-3-Clause']) + end end context "when scanning a project with cmake" do - subject { runner.scan(env: env) } - let(:env) { { 'LICENSE_FINDER_CLI_OPTS' => '--project-path=libraries/protobuf/serialization' } } before do runner.clone('https://github.com/conan-io/examples.git') end - specify { expect(subject).to match_schema } - specify { expect(subject.dependency_names).to match_array(%w[protobuf protoc_installer]) } - specify { expect(subject.licenses_for('protobuf')).to match_array(['BSD-3-Clause']) } - specify { expect(subject.licenses_for('protoc_installer')).to match_array(['BSD-3-Clause']) } + specify do + expect(subject).to match_schema + expect(subject.dependency_names).to match_array(%w[protobuf protoc_installer]) + expect(subject.licenses_for('protobuf')).to match_array(['BSD-3-Clause']) + expect(subject.licenses_for('protoc_installer')).to match_array(['BSD-3-Clause']) + end end context "when pulling packages from a custom conan remote" do - subject { runner.scan } - let(:package_name) { "#{project_namespace.tr('/', '+')}+#{project_name}/stable" } let(:project_namespace) { ENV.fetch('CI_PROJECT_NAMESPACE', 'gitlab-org/security-products') } let(:project_name) { ENV.fetch('CI_PROJECT_NAME', 'license-management') } @@ -89,8 +92,10 @@ RSpec.describe "conan" do end end - specify { expect(subject).to match_schema } - specify { expect(subject.dependency_names).to match_array(['example']) } - specify { expect(subject.licenses_for('example')).to match_array(['MIT']) } + specify do + expect(subject).to match_schema + expect(subject.dependency_names).to match_array(['example']) + expect(subject.licenses_for('example')).to match_array(['MIT']) + end end end diff --git a/spec/integration/java/gradle_spec.rb b/spec/integration/java/gradle_spec.rb index e6fbc1d..dd0ba26 100644 --- a/spec/integration/java/gradle_spec.rb +++ b/spec/integration/java/gradle_spec.rb @@ -3,41 +3,40 @@ require 'spec_helper' RSpec.describe "gradle" do + subject { runner.scan(env: env) } + + let(:env) { {} } + include_examples "each report version", "java", "gradle" context "when running a default gradle build" do - it 'scans a gradle project' do - content = <<~GRADLE -/* - * This file was generated by the Gradle 'init' task. - * - * This is a general purpose Gradle build. - * Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds - */ + before do + runner.add_file('build.gradle') do + <<~GRADLE plugins { id "com.github.hierynomus.license" version "0.15.0" } - GRADLE - runner.add_file('build.gradle', content) + GRADLE + end + end - report = runner.scan - expect(report).to match_schema - expect(report[:licenses]).to be_empty - expect(report[:dependencies]).to be_empty + it 'scans a gradle project' do + expect(subject).to match_schema + expect(subject[:licenses]).to be_empty + expect(subject[:dependencies]).to be_empty end end context 'when scanning a gradle project that does not include the `com.github.hierynomus.license` plugin' do let(:project_url) { 'https://gitlab.com/one-touch-pipeline/otp.git' } - let(:result) { runner.scan } before do runner.clone(project_url) end it 'is able to detect licenses' do - expect(result).to match_schema - expect(result[:licenses]).not_to be_empty + expect(subject).to match_schema + expect(subject[:licenses]).not_to be_empty [ { name: 'ant', licenses: ['Apache-2.0'] }, @@ -46,26 +45,25 @@ plugins { { name: 'sitemesh', licenses: ['Apache-1.1'] }, { name: 'hibernate-jpa-2.1-api', licenses: ['BSD-3-Clause', 'EPL-1.0'] } ].each do |dependency| - expect(result.licenses_for(dependency[:name])).to match_array(dependency[:licenses]) + expect(subject.licenses_for(dependency[:name])).to match_array(dependency[:licenses]) end end end context 'when scanning a project that needs to connect to multiple TLS endpoints with different custom certificate chains' do - subject do - runner.scan(env: { + let(:env) do + { 'ADDITIONAL_CA_CERT_BUNDLE' => x509_certificate.read, 'PRIVATE_MAVEN_HOST' => 'maven.test' - }) + } end before do runner.mount(dir: fixture_file('java/gradle/offline-environment')) end - specify { expect(subject).to match_schema } - specify do + expect(subject).to match_schema expect(subject.dependency_names).to match_array([ "antlr", "commons-beanutils", @@ -98,20 +96,23 @@ plugins { end context "when scanning a gradle project with a custom option to generate a profiler report" do - subject { runner.scan(env: { 'GRADLE_CLI_OPTS' => '--profile' }) } + let(:env) { { 'GRADLE_CLI_OPTS' => '--profile' } } before do runner.mount(dir: fixture_file("java/gradle/java-11")) end - specify { expect(subject).to match_schema } specify { expect { subject }.to change { Dir.glob("#{runner.project_path}/build/reports/profile/profile-*.html").count }.from(0).to(1) } - specify { expect(subject.dependency_names).to match_array(['postgresql']) } - specify { expect(subject.licenses_for('postgresql')).to match_array(['BSD-2-Clause']) } + + specify do + expect(subject).to match_schema + expect(subject.dependency_names).to match_array(['postgresql']) + expect(subject.licenses_for('postgresql')).to match_array(['BSD-2-Clause']) + end end context 'when using Java 8 with version 1.* of gradle' do - subject { runner.scan(env: { 'LM_JAVA_VERSION' => '8' }) } + let(:env) { { 'LM_JAVA_VERSION' => '8' } } before do runner.mount(dir: fixture_file("java/gradle/java-8")) @@ -127,7 +128,12 @@ plugins { ['4.9', '5.6', '6.3'].each do |gradle_version| context "when using Java v11 with a kotlin project using gradle v#{gradle_version}" do - let(:report) { runner.scan(env: { 'LM_JAVA_VERSION' => '11', 'GRADLE_CLI_OPTS' => '-b build.gradle.kts' }) } + let(:env) do + { + 'LM_JAVA_VERSION' => '11', + 'GRADLE_CLI_OPTS' => '-b build.gradle.kts' + } + end before do runner.add_file('.tool-versions', "gradle #{gradle_version}") @@ -135,9 +141,11 @@ plugins { runner.add_file('settings.gradle.kts', 'rootProject.name = "example"') end - specify { expect(report).to match_schema } - specify { expect(report.dependency_names).to match_array(['postgresql']) } - specify { expect(report.licenses_for('postgresql')).to match_array(['BSD-2-Clause']) } + specify do + expect(subject).to match_schema + expect(subject.dependency_names).to match_array(['postgresql']) + expect(subject.licenses_for('postgresql')).to match_array(['BSD-2-Clause']) + end end end @@ -147,7 +155,7 @@ plugins { ].each do |item| item[:gradle].each do |gradle_version| context "when using Java v#{item[:java]} with a gradle v#{gradle_version} on a groovy project" do - let(:report) { runner.scan(env: { 'LM_JAVA_VERSION' => item[:java] }) } + let(:env) { { 'LM_JAVA_VERSION' => item[:java] } } before do runner.add_file('.tool-versions', "gradle #{gradle_version}") @@ -155,9 +163,11 @@ plugins { runner.add_file('settings.gradle', 'rootProject.name = "example"') end - specify { expect(report).to match_schema } - specify { expect(report.dependency_names).to match_array(['postgresql']) } - specify { expect(report.licenses_for('postgresql')).to match_array(['BSD-2-Clause']) } + specify do + expect(subject).to match_schema + expect(subject.dependency_names).to match_array(['postgresql']) + expect(subject.licenses_for('postgresql')).to match_array(['BSD-2-Clause']) + end end end end diff --git a/spec/integration/java/maven_spec.rb b/spec/integration/java/maven_spec.rb index b112fd2..671243e 100644 --- a/spec/integration/java/maven_spec.rb +++ b/spec/integration/java/maven_spec.rb @@ -3,11 +3,15 @@ require 'spec_helper' RSpec.describe "maven" do + subject { runner.scan(env: env) } + + let(:env) { {} } + include_examples "each report version", "java", "maven" include_examples "each report version", "java", "maven-multimodules" context "when the maven dependencies come from the same projects public maven repository" do - subject { runner.scan(env: { 'CI_PROJECT_ID' => '6130122' }) } + let(:env) { { 'CI_PROJECT_ID' => '6130122' } } before do runner.mount(dir: fixture_file('java/maven/gitlab-repo')) @@ -22,11 +26,11 @@ RSpec.describe "maven" do end context "when packages are sourced from an external package registry" do - subject do - runner.scan(env: { + let(:env) do + { 'CI_PROJECT_ID' => 'invalid', 'MAVEN_CLI_OPTS' => "--settings settings.xml" - }) + } end before do @@ -42,7 +46,9 @@ RSpec.describe "maven" do end describe "When using the `SETUP_CMD`" do - it 'executes the custom script' do + let(:env) { { 'SETUP_CMD' => 'bash custom.sh' } } + + before do runner.add_file('custom.sh') do <<~SCRIPT #!/bin/bash -l @@ -50,18 +56,12 @@ RSpec.describe "maven" do echo 'hello' SCRIPT end - - report = runner.scan(env: { - 'SETUP_CMD' => 'bash custom.sh' - }) - - expect(report).to match_schema end + + specify { expect(subject).to match_schema } end describe "When scanning a project with multiple modules" do - subject { runner.scan } - before do runner.mount(dir: fixture_file('java/maven/multimodule')) end @@ -91,7 +91,7 @@ RSpec.describe "maven" do end context "when connecting to a custom package registry with a self signed certificate" do - let(:report) { runner.scan(env: { 'ADDITIONAL_CA_CERT_BUNDLE' => x509_certificate.read }) } + let(:env) { { 'ADDITIONAL_CA_CERT_BUNDLE' => x509_certificate.read } } before do runner.add_file('pom.xml') do @@ -105,9 +105,11 @@ RSpec.describe "maven" do end end - specify { expect(report).to match_schema } - specify { expect(report.dependency_names).to match_array(['jackson-core']) } - specify { expect(report.licenses_for('jackson-core')).to match_array(['Apache-2.0']) } + specify do + expect(subject).to match_schema + expect(subject.dependency_names).to match_array(['jackson-core']) + expect(subject.licenses_for('jackson-core')).to match_array(['Apache-2.0']) + end end [ @@ -116,7 +118,6 @@ RSpec.describe "maven" do ].each do |item| item[:maven].each do |maven_version| context "when using Java v#{item[:java]} with maven v#{maven_version}" do - let(:report) { runner.scan(env: env) } let(:env) do { 'LM_JAVA_VERSION' => item[:java], @@ -132,9 +133,9 @@ RSpec.describe "maven" do end specify do - expect(report).to match_schema - expect(report.dependency_names).to match_array(['netty-all']) - expect(report.licenses_for('netty-all')).to match_array(['Apache-2.0']) + expect(subject).to match_schema + expect(subject.dependency_names).to match_array(['netty-all']) + expect(subject.licenses_for('netty-all')).to match_array(['Apache-2.0']) end end end |
