summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/fixtures/java/maven/custom-tls/pom.xml27
-rw-r--r--spec/gemnasium_maven_spec.rb26
-rw-r--r--spec/spec_helper.rb3
-rw-r--r--spec/support/docker.rb29
-rw-r--r--spec/support/fixture_file_helper.rb15
-rw-r--r--spec/support/project.rb42
6 files changed, 133 insertions, 9 deletions
diff --git a/spec/fixtures/java/maven/custom-tls/pom.xml b/spec/fixtures/java/maven/custom-tls/pom.xml
new file mode 100644
index 0000000..6c647c0
--- /dev/null
+++ b/spec/fixtures/java/maven/custom-tls/pom.xml
@@ -0,0 +1,27 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.gitlab.secure</groupId>
+ <artifactId>license-scanning</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0-SNAPSHOT</version>
+ <name>example</name>
+ <dependencies>
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-core</artifactId>
+ <version>2.10.0</version>
+ </dependency>
+ </dependencies>
+ <repositories>
+ <repository>
+ <id>custom</id>
+ <url>https://maven.test/maven2</url>
+ </repository>
+ </repositories>
+ <distributionManagement>
+ <repository>
+ <id>custom</id>
+ <url>https://maven.test/maven2</url>
+ </repository>
+ </distributionManagement>
+</project>
diff --git a/spec/gemnasium_maven_spec.rb b/spec/gemnasium_maven_spec.rb
index 63c9aae..5dac7bc 100644
--- a/spec/gemnasium_maven_spec.rb
+++ b/spec/gemnasium_maven_spec.rb
@@ -1,20 +1,28 @@
RSpec.describe 'gemnasium-maven' do
context "when scanning a java project with packages from a custom source" do
subject do
- Dir.chdir Pathname.pwd.join('src/gemnasium-maven') do
- system("docker build --network=host -t #{docker_image} .")
-
- if system([ :docker, :run, '-it', '--rm', '--volume', "#{project_path}:/tmp/app", '--network=host', '--env', 'CI_PROJECT_DIR=/tmp/app', docker_image, '/analyzer run' ].map(&:to_s).join(' '))
- report_path = project_path.join('gl-license-scanning-report.json')
- report_path.exist? ? JSON.parse(report_path.read) : {}
- else
- {}
- end
+ docker.build(tag: docker_image)
+ if docker.run(image: docker_image, project_path: project_path, env: env)
+ report_path = project_path.join('gl-license-scanning-report.json')
+ report_path.exist? ? JSON.parse(report_path.read) : {}
+ else
+ {}
end
end
+ let(:docker) { Docker.new(pwd: Pathname.pwd.join('src/gemnasium-maven')) }
let(:project_path) { Dir.pwd }
let(:docker_image) { 'gemnasium-maven:latest' }
+ let(:env) { { 'ADDITIONAL_CA_CERT_BUNDLE' => '' } }
+ let(:project) { Project.new }
+
+ before do
+ project.mount(dir: fixture_file('java/maven/custom-tls'))
+ end
+
+ after do
+ project.cleanup
+ end
specify { expect(subject.count).to eql(1) }
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 8cfc067..0280338 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -14,6 +14,9 @@
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
require 'json'
+require 'support/docker'
+require 'support/project'
+require 'support/fixture_file_helper'
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
diff --git a/spec/support/docker.rb b/spec/support/docker.rb
new file mode 100644
index 0000000..331ee17
--- /dev/null
+++ b/spec/support/docker.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class Docker
+ DEFAULT_ENV = { 'CI_PROJECT_DIR' => '/tmp/app' }.freeze
+ attr_reader :pwd
+
+ def initialize(pwd: Pathname.pwd)
+ @pwd = pwd
+ end
+
+ def build(tag:)
+ Dir.chdir pwd do
+ system("docker build --network=host -t #{tag} .")
+ end
+ end
+
+ def run(image:, project_path: Pathname.pwd, env: {})
+ env_options = DEFAULT_ENV.merge(env).map { |(key, value)| "--env #{key}=#{value}" }
+ Dir.chdir pwd do
+ system([
+ :docker, :run, '-it', '--rm',
+ '--volume', "#{project_path}:/tmp/app",
+ '--network=host',
+ env_options,
+ image, '/analyzer run'
+ ].flatten.map(&:to_s).join(' '))
+ end
+ end
+end
diff --git a/spec/support/fixture_file_helper.rb b/spec/support/fixture_file_helper.rb
new file mode 100644
index 0000000..e5b8bde
--- /dev/null
+++ b/spec/support/fixture_file_helper.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+module FixtureFileHelper
+ def fixture_file_content(path, data = {})
+ fixture_file(path).read
+ end
+
+ def fixture_file(path)
+ Pathname.pwd.join("spec/fixtures/#{path}")
+ end
+end
+
+RSpec.configure do |config|
+ config.include(FixtureFileHelper)
+end
diff --git a/spec/support/project.rb b/spec/support/project.rb
new file mode 100644
index 0000000..a90dfd6
--- /dev/null
+++ b/spec/support/project.rb
@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+require 'securerandom'
+
+class Project
+ attr_reader :path
+
+ def initialize(path = Pathname.pwd.join('tmp').join(SecureRandom.uuid))
+ FileUtils.mkdir_p(path)
+ @path = Pathname(path)
+ end
+
+ def mount(dir:)
+ FileUtils.cp_r("#{dir}/.", path)
+ end
+
+ def chdir
+ Dir.chdir path do
+ yield
+ end
+ end
+
+ def clone(repo, branch: 'master')
+ if branch.match?(/\b[0-9a-f]{5,40}\b/)
+ execute({}, 'git', 'clone', '--quiet', repo, path.to_s)
+ chdir do
+ execute({}, 'git', 'checkout', branch)
+ end
+ else
+ execute({}, 'git', 'clone', '--quiet', '--depth=1', '--single-branch', '--branch', branch, repo, path.to_s)
+ end
+ end
+
+ def execute(env = {}, *args)
+ Bundler.with_unbundled_env do
+ system(env, *args, exception: true)
+ end
+ end
+
+ def cleanup
+ FileUtils.rm_rf(path) if path.exist?
+ end
+end