summaryrefslogtreecommitdiff
path: root/spec/support
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/integration_test_helper.rb94
-rw-r--r--spec/support/project_helper.rb56
-rw-r--r--spec/support/report.rb37
3 files changed, 94 insertions, 93 deletions
diff --git a/spec/support/integration_test_helper.rb b/spec/support/integration_test_helper.rb
index 7dc64fe..244f363 100644
--- a/spec/support/integration_test_helper.rb
+++ b/spec/support/integration_test_helper.rb
@@ -1,95 +1,4 @@
module IntegrationTestHelper
- class Report
- attr_reader :report
-
- def initialize(raw)
- @report = JSON.parse(raw, symbolize_names: true)
- end
-
- def [](key)
- report[key]
- end
-
- def dependency_names
- report[:dependencies].map { |x| x[:name] }
- end
-
- def licenses_for(name)
- (find(name) || {}).fetch(:licenses, [])
- end
-
- def find(name)
- report[:dependencies].find do |dependency|
- dependency[:name] == name
- end
- end
-
- def nil?
- report.nil?
- end
-
- def to_hash
- to_h
- end
-
- def to_h
- report
- end
- end
-
- class IntegrationTestRunner
- attr_reader :project_path
-
- def initialize(project_path = Pathname.pwd.join('tmp').join(SecureRandom.uuid))
- FileUtils.mkdir_p(project_path)
- @project_path = Pathname(project_path)
- end
-
- def add_file(name, content = nil)
- full_path = project_path.join(name)
- FileUtils.mkdir_p(full_path.dirname)
- IO.write(full_path, block_given? ? yield : content)
- end
-
- def mount(dir:)
- FileUtils.cp_r("#{dir}/.", project_path)
- end
-
- def clone(repo, branch: 'master')
- if branch.match?(/\b[0-9a-f]{5,40}\b/)
- execute({}, 'git', 'clone', '--quiet', repo, project_path.to_s)
- Dir.chdir project_path do
- execute({}, 'git', 'checkout', branch)
- end
- else
- execute({}, 'git', 'clone', '--quiet', '--depth=1', '--single-branch', '--branch', branch, repo, project_path.to_s)
- end
- end
-
- def scan(env: {})
- execute(env, 'mkdir -p pkg/ && gem build --silent -o pkg/license-management-test.gem ./*.gemspec')
- Dir.chdir project_path do
- merged_env = { 'CI_PROJECT_DIR' => project_path.to_s }.merge(env)
- return {} unless execute(merged_env, "#{License::Management.root.join('run.sh')} analyze .")
-
- report_path = project_path.join('gl-license-scanning-report.json')
- return {} unless report_path.exist?
-
- Report.new(report_path.read)
- end
- end
-
- def execute(env = {}, *args)
- Bundler.with_unbundled_env do
- system(env, *args, exception: true)
- end
- end
-
- def cleanup
- FileUtils.rm_rf(project_path) if project_path.exist?
- end
- end
-
def private_npm_host
@private_npm_host ||= ENV.fetch('PRIVATE_NPM_HOST').tap do |host|
add_host(host, ENV.fetch('PRIVATE_NPM_IP'))
@@ -109,11 +18,10 @@ module IntegrationTestHelper
end
def runner(*args)
- @runner ||= IntegrationTestRunner.new(*args)
+ @runner ||= ProjectHelper.new(*args)
end
def add_host(name, ip)
- return unless ENV['LM_HOME']
return if system("grep #{name} /etc/hosts")
system("echo '#{ip} #{name}' >> /etc/hosts")
diff --git a/spec/support/project_helper.rb b/spec/support/project_helper.rb
new file mode 100644
index 0000000..4ae490e
--- /dev/null
+++ b/spec/support/project_helper.rb
@@ -0,0 +1,56 @@
+class ProjectHelper
+ attr_reader :project_path
+
+ def initialize(project_path = License::Management.root.join('tmp').join(SecureRandom.uuid))
+ FileUtils.mkdir_p(project_path)
+ @project_path = Pathname(project_path)
+ end
+
+ def add_file(name, content = nil)
+ full_path = project_path.join(name)
+ FileUtils.mkdir_p(full_path.dirname)
+ IO.write(full_path, block_given? ? yield : content)
+ end
+
+ def mount(dir:)
+ FileUtils.cp_r("#{dir}/.", project_path)
+ end
+
+ def chdir
+ Dir.chdir project_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, project_path.to_s)
+ chdir do
+ execute({}, 'git', 'checkout', branch)
+ end
+ else
+ execute({}, 'git', 'clone', '--quiet', '--depth=1', '--single-branch', '--branch', branch, repo, project_path.to_s)
+ end
+ end
+
+ def scan(env: {})
+ chdir do
+ return {} unless execute({ 'CI_PROJECT_DIR' => project_path.to_s }.merge(env), "#{License::Management.root.join('run.sh')} analyze .")
+
+ report_path = project_path.join('gl-license-scanning-report.json')
+ return {} unless report_path.exist?
+
+ Report.new(report_path.read)
+ end
+ end
+
+ def execute(env = {}, *args)
+ Bundler.with_unbundled_env do
+ system(env, *args, exception: true)
+ end
+ end
+
+ def cleanup
+ FileUtils.rm_rf(project_path) if project_path.exist?
+ end
+end
diff --git a/spec/support/report.rb b/spec/support/report.rb
new file mode 100644
index 0000000..d6546ae
--- /dev/null
+++ b/spec/support/report.rb
@@ -0,0 +1,37 @@
+class Report
+ attr_reader :report
+
+ def initialize(raw)
+ @report = JSON.parse(raw, symbolize_names: true)
+ end
+
+ def [](key)
+ report[key]
+ end
+
+ def dependency_names
+ report[:dependencies].map { |x| x[:name] }
+ end
+
+ def licenses_for(name)
+ (find(name) || {}).fetch(:licenses, [])
+ end
+
+ def find(name)
+ report[:dependencies].find do |dependency|
+ dependency[:name] == name
+ end
+ end
+
+ def nil?
+ report.nil?
+ end
+
+ def to_hash
+ to_h
+ end
+
+ def to_h
+ report
+ end
+end