From 853ae09d4c6d0220a3f77492065961e65f96e860 Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 14 Sep 2020 16:59:36 -0600 Subject: chore: move classes into lib dir --- lib/e2e.rb | 3 +++ lib/e2e/docker.rb | 34 ++++++++++++++++++++++++++++++++++ lib/e2e/project.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ spec/gemnasium_maven_spec.rb | 5 ++++- spec/spec_helper.rb | 5 ++--- spec/support/docker.rb | 29 ----------------------------- spec/support/project.rb | 42 ------------------------------------------ 7 files changed, 85 insertions(+), 75 deletions(-) create mode 100644 lib/e2e.rb create mode 100644 lib/e2e/docker.rb create mode 100644 lib/e2e/project.rb delete mode 100644 spec/support/docker.rb delete mode 100644 spec/support/project.rb diff --git a/lib/e2e.rb b/lib/e2e.rb new file mode 100644 index 0000000..50d7fc4 --- /dev/null +++ b/lib/e2e.rb @@ -0,0 +1,3 @@ +require 'json' +require 'e2e/docker' +require 'e2e/project' diff --git a/lib/e2e/docker.rb b/lib/e2e/docker.rb new file mode 100644 index 0000000..3c4d589 --- /dev/null +++ b/lib/e2e/docker.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +class Docker + DEFAULT_ENV = { + 'CI_DEBUG_TRACE' => 'true', + 'CI_PROJECT_DIR' => '/tmp/app', + 'SECURE_LOG_LEVEL' => 'debug' + }.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}" } + command = expand([:docker, :run, '-it', '--rm', "--volume=#{project_path}:/tmp/app", '--network=host', env_options, image, '/analyzer run']) + Dir.chdir pwd do + system(command, exception: true) + end + end + + private + + def expand(command) + command.flatten.map(&:to_s).join(' ') + end +end diff --git a/lib/e2e/project.rb b/lib/e2e/project.rb new file mode 100644 index 0000000..a90dfd6 --- /dev/null +++ b/lib/e2e/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 diff --git a/spec/gemnasium_maven_spec.rb b/spec/gemnasium_maven_spec.rb index 5dac7bc..f4e8f32 100644 --- a/spec/gemnasium_maven_spec.rb +++ b/spec/gemnasium_maven_spec.rb @@ -24,6 +24,9 @@ RSpec.describe 'gemnasium-maven' do project.cleanup end - specify { expect(subject.count).to eql(1) } + specify do + puts subject.inspect + expect(subject.count).to eql(1) + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0280338..c7b393b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,9 +13,8 @@ # it. # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration -require 'json' -require 'support/docker' -require 'support/project' +$LOAD_PATH.unshift(Pathname.pwd.join('lib')) +require 'e2e' require 'support/fixture_file_helper' RSpec.configure do |config| diff --git a/spec/support/docker.rb b/spec/support/docker.rb deleted file mode 100644 index 331ee17..0000000 --- a/spec/support/docker.rb +++ /dev/null @@ -1,29 +0,0 @@ -# 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/project.rb b/spec/support/project.rb deleted file mode 100644 index a90dfd6..0000000 --- a/spec/support/project.rb +++ /dev/null @@ -1,42 +0,0 @@ -# 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 -- cgit v1.2.3