Skip to content

Commit

Permalink
Setup testing via rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed Feb 5, 2023
1 parent db8d3b7 commit 6716983
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 7 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ jobs:
with:
useConfigFile: true

- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true

- name: bundle exec rspec spec
run: bundle exec rspec spec
working-directory: updater

- name: Pull Docker base image & warm Docker cache
run: docker pull "tingle/$IMAGE_NAME:latest"

Expand All @@ -67,13 +76,11 @@ jobs:

- name: Push image to Docker Hub (latest)
if: github.ref == 'refs/heads/main'
run: |
docker push "tingle/$IMAGE_NAME:latest"
run: docker push "tingle/$IMAGE_NAME:latest"

- name: Push image to Docker Hub (FullSemVer)
if: "!startsWith(github.ref, 'refs/pull')"
run: |
docker push "tingle/$IMAGE_NAME:$GITVERSION_FULLSEMVER"
run: docker push "tingle/$IMAGE_NAME:$GITVERSION_FULLSEMVER"

- name: Push image to Docker Hub (major, minor)
if: startsWith(github.ref, 'refs/tags')
Expand Down
130 changes: 130 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# frozen_string_literal: true

require "fileutils"
require "English"
require "net/http"
require "uri"
require "json"
require "rubygems/package"
require "bundler"
# require "./common/lib/dependabot/version"
require "yaml"

# GEMSPECS = %w(
# common/dependabot-common.gemspec
# go_modules/dependabot-go_modules.gemspec
# terraform/dependabot-terraform.gemspec
# docker/dependabot-docker.gemspec
# git_submodules/dependabot-git_submodules.gemspec
# github_actions/dependabot-github_actions.gemspec
# nuget/dependabot-nuget.gemspec
# gradle/dependabot-gradle.gemspec
# maven/dependabot-maven.gemspec
# bundler/dependabot-bundler.gemspec
# elm/dependabot-elm.gemspec
# cargo/dependabot-cargo.gemspec
# npm_and_yarn/dependabot-npm_and_yarn.gemspec
# composer/dependabot-composer.gemspec
# hex/dependabot-hex.gemspec
# python/dependabot-python.gemspec
# pub/dependabot-pub.gemspec
# omnibus/dependabot-omnibus.gemspec
# ).freeze
GEMSPECS = []

def run_command(command)
puts "> #{command}"
exit 1 unless system(command)
end

# rubocop:disable Metrics/BlockLength
namespace :gems do
task build: :clean do
root_path = Dir.getwd
pkg_path = File.join(root_path, "pkg")
Dir.mkdir(pkg_path) unless File.directory?(pkg_path)

GEMSPECS.each do |gemspec_path|
puts "> Building #{gemspec_path}"
Dir.chdir(File.dirname(gemspec_path)) do
gemspec = Bundler.load_gemspec_uncached(File.basename(gemspec_path))
pkg = ::Gem::Package.build(gemspec)
FileUtils.mv(pkg, File.join(pkg_path, pkg))
end
end
end

task release: [:build] do
guard_tag_match

GEMSPECS.each do |gemspec_path|
gem_name = File.basename(gemspec_path).sub(/\.gemspec$/, "")
gem_path = "pkg/#{gem_name}-#{Dependabot::VERSION}.gem"

attempts = 0
loop do
if rubygems_release_exists?(gem_name, Dependabot::VERSION)
puts "- Skipping #{gem_path} as it already exists on rubygems"
break
else
puts "> Releasing #{gem_path}"
attempts += 1
sleep(2)
begin
sh "gem push #{gem_path}"
break
rescue StandardError => e
puts "! `gem push` failed with error: #{e}"
raise if attempts >= 3
end
end
end
end
end

task :clean do
FileUtils.rm(Dir["pkg/*.gem"])
end
end

class Hash
def sort_by_key(recursive = false, &block)
keys.sort(&block).each_with_object({}) do |key, seed|
seed[key] = self[key]
seed[key] = seed[key].sort_by_key(true, &block) if recursive && seed[key].is_a?(Hash)
seed
end
end
end

namespace :rubocop do
task :sort do
File.write(
".rubocop.yml",
YAML.load_file(".rubocop.yml").sort_by_key(true).to_yaml
)
end
end

def guard_tag_match
tag = "v#{Dependabot::VERSION}"
tag_commit = `git rev-list -n 1 #{tag} 2> /dev/null`.strip
abort "Can't release - tag #{tag} does not exist" unless $CHILD_STATUS == 0

head_commit = `git rev-parse HEAD`.strip
return if tag_commit == head_commit

abort "Can't release - HEAD (#{head_commit[0..9]}) does not match " \
"tag #{tag} (#{tag_commit[0..9]})"
end

def rubygems_release_exists?(name, version)
uri = URI.parse("https://rubygems.org/api/v1/versions/#{name}.json")
response = Net::HTTP.get_response(uri)
abort "Gem #{name} doesn't exist on rubygems" if response.code != "200"

body = JSON.parse(response.body)
existing_versions = body.map { |b| b["number"] }
existing_versions.include?(version)
end
# rubocop:enable Metrics/BlockLength
19 changes: 19 additions & 0 deletions updater/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,22 @@ gem "dependabot-nuget", path: "../dependabot-core/nuget"
gem "dependabot-pub", path: "../dependabot-core/pub"
gem "dependabot-python", path: "../dependabot-core/python"
gem "dependabot-terraform", path: "../dependabot-core/terraform"

group :test do
common_gemspec = File.expand_path("../dependabot-core/common/dependabot-common.gemspec", __dir__)

deps_shared_with_core = %w(
debug
rspec
vcr
webmock
)

Dir.chdir(File.dirname(common_gemspec)) do
Gem::Specification.load(common_gemspec).development_dependencies.each do |dep|
next unless deps_shared_with_core.include?(dep.name)

gem dep.name, *dep.requirement.as_list
end
end
end
28 changes: 28 additions & 0 deletions updater/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ GEM
citrus (3.0.2)
commonmarker (0.23.8)
concurrent-ruby (1.2.0)
crack (0.4.5)
rexml
debug (1.7.1)
diff-lcs (1.5.0)
docker_registry2 (1.13.0)
rest-client (>= 1.8.0)
domain_name (0.5.20190701)
Expand All @@ -158,6 +162,7 @@ GEM
gitlab (4.19.0)
httparty (~> 0.20)
terminal-table (>= 1.5.1)
hashdiff (1.0.1)
http-accept (1.7.0)
http-cookie (1.0.5)
domain_name (~> 0.5)
Expand Down Expand Up @@ -191,6 +196,20 @@ GEM
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rexml (3.2.5)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.1)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
ruby2_keywords (0.0.5)
sawyer (0.9.2)
addressable (>= 2.3.5)
Expand All @@ -205,12 +224,18 @@ GEM
unf_ext
unf_ext (0.0.8.2)
unicode-display_width (2.4.2)
vcr (6.1.0)
webmock (3.18.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
arm64-darwin-22
x86_64-linux

DEPENDENCIES
debug (~> 1.7.1)
dependabot-bundler!
dependabot-cargo!
dependabot-common!
Expand All @@ -228,6 +253,9 @@ DEPENDENCIES
dependabot-pub!
dependabot-python!
dependabot-terraform!
rspec (~> 3.12)
vcr (~> 6.1)
webmock (~> 3.18)

BUNDLED WITH
2.3.26
3 changes: 0 additions & 3 deletions updater/spec/README.md

This file was deleted.

98 changes: 98 additions & 0 deletions updater/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# frozen_string_literal: true

require "dependabot/logger"
require "dependabot/python"
require "dependabot/terraform"
require "dependabot/elm"
require "dependabot/docker"
require "dependabot/git_submodules"
require "dependabot/github_actions"
require "dependabot/composer"
require "dependabot/nuget"
require "dependabot/gradle"
require "dependabot/maven"
require "dependabot/hex"
require "dependabot/cargo"
require "dependabot/go_modules"
require "dependabot/npm_and_yarn"
require "dependabot/bundler"
require "dependabot/pub"
require "logger"
require "vcr"
require "webmock/rspec"

# TODO: Stop rescuing StandardError in Dependabot::BaseJob#run
#
# For now we log errors as these can surface exceptions that currently get rescued
# in integration tests.
#
# This includes missing VCR fixtures.
Dependabot.logger = Logger.new($stdout, level: Logger::ERROR)

WebMock.disable_net_connect!

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.shared_context_metadata_behavior = :apply_to_host_groups
config.filter_run_when_matching :focus
config.example_status_persistence_file_path = "spec/examples.txt"
config.disable_monkey_patching!
config.default_formatter = "doc" if config.files_to_run.one?
config.profile_examples = 10
config.order = :random

Kernel.srand config.seed

def fixture(path)
File.read(File.join("spec", "fixtures", path))
end
end

VCR.configure do |config|
config.cassette_library_dir = "spec/fixtures/vcr_cassettes"
config.hook_into :webmock
config.configure_rspec_metadata!
config.allow_http_connections_when_no_cassette = false

config.filter_sensitive_data("<AZURE_ACCESS_TOKEN>") do
ENV.fetch("AZURE_ACCESS_TOKEN", nil)
end

config.filter_sensitive_data("<AWS_ACCESS_KEY_ID>") do
ENV.fetch("AWS_ACCESS_KEY_ID", nil)
end

config.filter_sensitive_data("<AWS_SECRET_ACCESS_KEY>") do
ENV.fetch("AWS_SECRET_ACCESS_KEY", nil)
end

config.filter_sensitive_data("<AUTHORIZATION>") do |interaction|
interaction.request.headers["Authorization"]&.first
end

# Prevent access tokens being written to VCR cassettes
unless ENV["DEPENDABOT_TEST_ACCESS_TOKEN"].nil?
config.filter_sensitive_data("<TOKEN>") do
ENV["DEPENDABOT_TEST_ACCESS_TOKEN"]
end
end

# Let's you set default VCR mode with VCR=all for re-recording
# episodes. :once is VCR default
record_mode = ENV["VCR"] ? ENV["VCR"].to_sym : :none
config.default_cassette_options = {
record: record_mode,
allow_unused_http_interactions: false
}
end

def test_access_token
ENV.fetch("DEPENDABOT_TEST_ACCESS_TOKEN", "missing-test-access-token")
end

0 comments on commit 6716983

Please sign in to comment.