summaryrefslogtreecommitdiff
path: root/spec/support/project_helper.rb
diff options
context:
space:
mode:
authorCan Eldem <celdem@gitlab.com>2020-07-10 16:41:15 +0000
committerCan Eldem <celdem@gitlab.com>2020-07-10 16:41:15 +0000
commitc17546f6cfccf07d27f7984321fe62a7788e8da1 (patch)
tree54093c956c0f4dcf4c2ad352847017aee9a88edc /spec/support/project_helper.rb
parentc7385965b4166fb6ab2db3387c67cd54aef1b8df (diff)
parent36cdb0040abda394264455a1fdf3d6782af95ceb (diff)
Merge branch '217897-isolated-omnibus-lm' into 'master'v3.17.0
Isolate license_management ruby from project ruby See merge request gitlab-org/security-products/license-management!181
Diffstat (limited to 'spec/support/project_helper.rb')
-rw-r--r--spec/support/project_helper.rb56
1 files changed, 56 insertions, 0 deletions
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