Skip to content

Commit

Permalink
Adds codecommit client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
userhas404d committed Sep 11, 2019
1 parent ebc8349 commit 36bc4a1
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 7 deletions.
12 changes: 7 additions & 5 deletions common/lib/dependabot/clients/codecommit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ def fetch_repo_contents(repo, commit = nil, path = nil)
end

def fetch_file_contents(repo, commit, path)
@cc_client.get_file(
repository_name: repo,
commit_specifier: commit,
file_path: path
).file_content
@cc_client.get_file(
repository_name: repo,
commit_specifier: commit,
file_path: path
).file_content
rescue Aws::CodeCommit::Errors::FileDoesNotExistException
raise NotFound
end

def branch(branch_name)
Expand Down
3 changes: 1 addition & 2 deletions common/lib/dependabot/file_fetchers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class Base
Gitlab::Error::NotFound,
Dependabot::Clients::Azure::NotFound,
Dependabot::Clients::Bitbucket::NotFound,
Dependabot::Clients::CodeCommit::NotFound,
Aws::CodeCommit::Errors::FileDoesNotExistException
Dependabot::Clients::CodeCommit::NotFound
].freeze

def self.required_files_in?(_filename_array)
Expand Down
56 changes: 56 additions & 0 deletions common/spec/dependabot/clients/codecommit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "aws-sdk-codecommit"
require "spec_helper"
require "dependabot/clients/codecommit"

RSpec.describe Dependabot::Clients::CodeCommit do
let(:branch) { "master" }
let(:repo) { "gocardless" }
let(:credentials) do
[{
"type" => "git_source",
"region" => "us-east-1",
"username" => "AWS_ACCESS_KEY_ID",
"password" => "AWS_SECRET_ACCESS_KEY"
}]
end
let(:source) { Dependabot::Source.from_url("codecommit") }
let(:client) do
described_class.for_source(source: source, credentials: credentials)
end

describe "#fetch_commit" do
subject { client.fetch_commit(nil, branch) }

context "when a response is returned" do
before do
client.
stub_responses(
:get_branch,
branch:
{
branch_name: "master",
commit_id: "9c8376e9b2e943c2c72fac4b239876f377f0305a"
}
)
end

specify { expect { subject }.to_not raise_error }

it { is_expected.to eq("9c8376e9b2e943c2c72fac4b239876f377f0305a") }
end

context "when the target branch does not exist" do
before do
client.stub_responses(:get_branch, "BranchDoesNotExistException")
end

it "raises a helpful error" do
expect { subject }.to raise_error(
Aws::CodeCommit::Errors::BranchDoesNotExistException
)
end
end
end
end
152 changes: 152 additions & 0 deletions common/spec/dependabot/file_fetchers/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
[{
"type" => "git_source",
"host" => "github.com",
"region" => "us-east-1",
"username" => "x-access-token",
"password" => "token"
}]
Expand Down Expand Up @@ -197,6 +198,43 @@ def fetch_files
end
end

context "with a CodeCommit source" do
let(:provider) { "codecommit" }
let(:repo) { "gocardless" }

before do
file_fetcher_instance.
stub_responses(
:get_branch,
branch:
{
branch_name: "master",
commit_id: "9c8376e9b2e943c2c72fac4b239876f377f0305a"
}
)
end

it { is_expected.to eq("9c8376e9b2e943c2c72fac4b239876f377f0305a") }

context "with a traget branch" do
let(:branch) { "my_branch" }

before do
file_fetcher_instance.
stub_responses(
:get_branch,
branch:
{
branch_name: "my_branch",
commit_id: "8c8376e9b2e943c2c72fac4b239876f377f0305b"
}
)
end

it { is_expected.to eq("8c8376e9b2e943c2c72fac4b239876f377f0305b") }
end
end

context "with a Azure DevOps source" do
let(:provider) { "azure" }
let(:repo) { "org/gocardless/_git/bump" }
Expand Down Expand Up @@ -1091,6 +1129,120 @@ def fetch_files
end
end

context "with a CodeCommit source" do
let(:provider) { "codecommit" }
let(:repo) { "gocardless" }

before do
file_fetcher_instance.
stub_responses(
:get_file,
commit_id: "9c8376e9b2e943c2c72fac4b239876f377f0305a",
blob_id: "123",
file_path: "",
file_mode: "NORMAL",
file_size: 0,
file_content: fixture("codecommit", "gemspec_content")
)
end

its(:length) { is_expected.to eq(1) }

describe "the file" do
subject { files.find { |file| file.name == "requirements.txt" } }

it { is_expected.to be_a(Dependabot::DependencyFile) }
its(:content) { is_expected.to include("required_rubygems_version") }
end

context "with a directory specified" do
let(:file_fetcher_instance) do
child_class.new(source: source, credentials: credentials)
end

context "that ends in a slash" do
before do
file_fetcher_instance.
stub_responses(
:get_file,
commit_id: "",
blob_id: "",
file_path: "app/requirements.txt",
file_mode: "NORMAL",
file_size: 0,
file_content: "foo"
)
end
let(:directory) { "app/" }

it "gets the file" do
files
expect { subject }.to_not raise_error
end
end

context "that beings with a slash" do
before do
file_fetcher_instance.
stub_responses(
:get_file,
commit_id: "",
blob_id: "",
file_path: "/app/requirements.txt",
file_mode: "NORMAL",
file_size: 0,
file_content: "foo"
)
end
let(:directory) { "/app" }

it "gets the file" do
files
expect { subject }.to_not raise_error
end
end

context "that includes a slash" do
before do
file_fetcher_instance.
stub_responses(
:get_file,
commit_id: "",
blob_id: "",
file_path: "a/pp/requirements.txt",
file_mode: "NORMAL",
file_size: 0,
file_content: "foo"
)
end
let(:directory) { "a/pp" }

it "gets the file" do
files
expect { subject }.to_not raise_error
end
end
end

context "when a dependency file can't be found" do
before do
file_fetcher_instance.
stub_responses(
:get_file,
"FileDoesNotExistException"
)
end

it "raises a custom error" do
expect { file_fetcher_instance.files }.
to raise_error(Dependabot::DependencyFileNotFound) do |error|
expect(error.file_path).to eq("/requirements.txt")
end
end
end

end

context "with an interesting filename" do
let(:file_fetcher_instance) do
child_class.new(source: source, credentials: credentials)
Expand Down
61 changes: 61 additions & 0 deletions common/spec/fixtures/codecommit/gemspec_content
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- encoding: utf-8 -*-
# stub: pg 1.2.0.pre20180828173948 ruby lib
# stub: ext/extconf.rb

Gem::Specification.new do |s|
s.name = "pg".freeze
s.version = "1.2.0.pre20180828173948"

s.required_rubygems_version = Gem::Requirement.new("> 1.3.1".freeze) if s.respond_to? :required_rubygems_version=
s.require_paths = ["lib".freeze]
s.authors = ["Michael Granger".freeze, "Lars Kanis".freeze]
s.cert_chain = ["certs/ged.pem".freeze]
s.date = "2018-08-29"
s.description = "Pg is the Ruby interface to the {PostgreSQL RDBMS}[http://www.postgresql.org/].\n\nIt works with {PostgreSQL 9.2 and later}[http://www.postgresql.org/support/versioning/].\n\nA small example usage:\n\n #!/usr/bin/env ruby\n\n require 'pg'\n\n # Output a table of current connections to the DB\n conn = PG.connect( dbname: 'sales' )\n conn.exec( \"SELECT * FROM pg_stat_activity\" ) do |result|\n puts \" PID | User | Query\"\n result.each do |row|\n puts \" %7d | %-16s | %s \" %\n row.values_at('procpid', 'usename', 'current_query')\n end\n end".freeze
s.email = ["ged@FaerieMUD.org".freeze, "lars@greiz-reinsdorf.de".freeze]
s.extensions = ["ext/extconf.rb".freeze]
s.extra_rdoc_files = ["Contributors.rdoc".freeze, "History.rdoc".freeze, "Manifest.txt".freeze, "README-OS_X.rdoc".freeze, "README-Windows.rdoc".freeze, "README.ja.rdoc".freeze, "README.rdoc".freeze, "ext/errorcodes.txt".freeze, "Contributors.rdoc".freeze, "History.rdoc".freeze, "README-OS_X.rdoc".freeze, "README-Windows.rdoc".freeze, "README.ja.rdoc".freeze, "README.rdoc".freeze, "POSTGRES".freeze, "LICENSE".freeze, "ext/gvl_wrappers.c".freeze, "ext/pg.c".freeze, "ext/pg_binary_decoder.c".freeze, "ext/pg_binary_encoder.c".freeze, "ext/pg_coder.c".freeze, "ext/pg_connection.c".freeze, "ext/pg_copy_coder.c".freeze, "ext/pg_errors.c".freeze, "ext/pg_result.c".freeze, "ext/pg_text_decoder.c".freeze, "ext/pg_text_encoder.c".freeze, "ext/pg_tuple.c".freeze, "ext/pg_type_map.c".freeze, "ext/pg_type_map_all_strings.c".freeze, "ext/pg_type_map_by_class.c".freeze, "ext/pg_type_map_by_column.c".freeze, "ext/pg_type_map_by_mri_type.c".freeze, "ext/pg_type_map_by_oid.c".freeze, "ext/pg_type_map_in_ruby.c".freeze, "ext/util.c".freeze]
s.files = ["BSDL".freeze, "ChangeLog".freeze, "Contributors.rdoc".freeze, "History.rdoc".freeze, "LICENSE".freeze, "Manifest.txt".freeze, "POSTGRES".freeze, "README-OS_X.rdoc".freeze, "README-Windows.rdoc".freeze, "README.ja.rdoc".freeze, "README.rdoc".freeze, "Rakefile".freeze, "Rakefile.cross".freeze, "ext/errorcodes.def".freeze, "ext/errorcodes.rb".freeze, "ext/errorcodes.txt".freeze, "ext/extconf.rb".freeze, "ext/gvl_wrappers.c".freeze, "ext/gvl_wrappers.h".freeze, "ext/pg.c".freeze, "ext/pg.h".freeze, "ext/pg_binary_decoder.c".freeze, "ext/pg_binary_encoder.c".freeze, "ext/pg_coder.c".freeze, "ext/pg_connection.c".freeze, "ext/pg_copy_coder.c".freeze, "ext/pg_errors.c".freeze, "ext/pg_result.c".freeze, "ext/pg_text_decoder.c".freeze, "ext/pg_text_encoder.c".freeze, "ext/pg_tuple.c".freeze, "ext/pg_type_map.c".freeze, "ext/pg_type_map_all_strings.c".freeze, "ext/pg_type_map_by_class.c".freeze, "ext/pg_type_map_by_column.c".freeze, "ext/pg_type_map_by_mri_type.c".freeze, "ext/pg_type_map_by_oid.c".freeze, "ext/pg_type_map_in_ruby.c".freeze, "ext/util.c".freeze, "ext/util.h".freeze, "ext/vc/pg.sln".freeze, "ext/vc/pg_18/pg.vcproj".freeze, "ext/vc/pg_19/pg_19.vcproj".freeze, "lib/pg.rb".freeze, "lib/pg/basic_type_mapping.rb".freeze, "lib/pg/binary_decoder.rb".freeze, "lib/pg/coder.rb".freeze, "lib/pg/connection.rb".freeze, "lib/pg/constants.rb".freeze, "lib/pg/exceptions.rb".freeze, "lib/pg/result.rb".freeze, "lib/pg/text_decoder.rb".freeze, "lib/pg/text_encoder.rb".freeze, "lib/pg/tuple.rb".freeze, "lib/pg/type_map_by_column.rb".freeze, "spec/data/expected_trace.out".freeze, "spec/data/random_binary_data".freeze, "spec/helpers.rb".freeze, "spec/pg/basic_type_mapping_spec.rb".freeze, "spec/pg/connection_spec.rb".freeze, "spec/pg/connection_sync_spec.rb".freeze, "spec/pg/result_spec.rb".freeze, "spec/pg/tuple_spec.rb".freeze, "spec/pg/type_map_by_class_spec.rb".freeze, "spec/pg/type_map_by_column_spec.rb".freeze, "spec/pg/type_map_by_mri_type_spec.rb".freeze, "spec/pg/type_map_by_oid_spec.rb".freeze, "spec/pg/type_map_in_ruby_spec.rb".freeze, "spec/pg/type_map_spec.rb".freeze, "spec/pg/type_spec.rb".freeze, "spec/pg_spec.rb".freeze]
s.homepage = "https://bitbucket.org/ged/ruby-pg".freeze
s.licenses = ["BSD-3-Clause".freeze]
s.rdoc_options = ["--main".freeze, "README.rdoc".freeze]
s.required_ruby_version = Gem::Requirement.new(">= 2.0.0".freeze)
s.rubygems_version = "2.7.6".freeze
s.summary = "Pg is the Ruby interface to the {PostgreSQL RDBMS}[http://www.postgresql.org/]".freeze

if s.respond_to? :specification_version then
s.specification_version = 4

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<hoe-mercurial>.freeze, ["~> 1.4"])
s.add_development_dependency(%q<hoe-deveiate>.freeze, ["~> 0.9"])
s.add_development_dependency(%q<hoe-highline>.freeze, ["~> 0.2"])
s.add_development_dependency(%q<rake-compiler>.freeze, ["~> 1.0"])
s.add_development_dependency(%q<rake-compiler-dock>.freeze, [">= 0.6.2", "~> 0.6"])
s.add_development_dependency(%q<hoe-bundler>.freeze, ["~> 1.0"])
s.add_development_dependency(%q<rspec>.freeze, ["~> 3.5"])
s.add_development_dependency(%q<rdoc>.freeze, ["~> 5.1"])
s.add_development_dependency(%q<hoe>.freeze, ["~> 3.16"])
else
s.add_dependency(%q<hoe-mercurial>.freeze, ["~> 1.4"])
s.add_dependency(%q<hoe-deveiate>.freeze, ["~> 0.9"])
s.add_dependency(%q<hoe-highline>.freeze, ["~> 0.2"])
s.add_dependency(%q<rake-compiler>.freeze, ["~> 1.0"])
s.add_dependency(%q<rake-compiler-dock>.freeze, [">= 0.6.2", "~> 0.6"])
s.add_dependency(%q<hoe-bundler>.freeze, ["~> 1.0"])
s.add_dependency(%q<rspec>.freeze, ["~> 3.5"])
s.add_dependency(%q<rdoc>.freeze, ["~> 5.1"])
s.add_dependency(%q<hoe>.freeze, ["~> 3.16"])
end
else
s.add_dependency(%q<hoe-mercurial>.freeze, ["~> 1.4"])
s.add_dependency(%q<hoe-deveiate>.freeze, ["~> 0.9"])
s.add_dependency(%q<hoe-highline>.freeze, ["~> 0.2"])
s.add_dependency(%q<rake-compiler>.freeze, ["~> 1.0"])
s.add_dependency(%q<rake-compiler-dock>.freeze, [">= 0.6.2", "~> 0.6"])
s.add_dependency(%q<hoe-bundler>.freeze, ["~> 1.0"])
s.add_dependency(%q<rspec>.freeze, ["~> 3.5"])
s.add_dependency(%q<rdoc>.freeze, ["~> 5.1"])
s.add_dependency(%q<hoe>.freeze, ["~> 3.16"])
end
end

0 comments on commit 36bc4a1

Please sign in to comment.