Skip to content

Commit

Permalink
move fixtures to support, add specs for LocalGit and UserDefinedTags
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 5, 2023
1 parent fe7573d commit 0b57a4c
Show file tree
Hide file tree
Showing 33 changed files with 120 additions and 16 deletions.
56 changes: 56 additions & 0 deletions spec/datadog/ci/ext/environment/local_git_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
RSpec.describe ::Datadog::CI::Ext::Environment::LocalGit do
let(:env) { {} }
let(:environment_variables) { {} }

describe "#tags" do
subject(:extracted_tags) do
ClimateControl.modify(environment_variables) { described_class.new(env).tags }
end

context "example git repository" do
include_context "with git fixture", "gitdir_with_commit"

let(:expected_tags) do
{
"ci.workspace_path" => "#{Dir.pwd}/spec/support/fixtures/git",
"git.branch" => "master",
"git.commit.author.date" => "2011-02-16T13:00:00+00:00",
"git.commit.author.email" => "bot@friendly.test",
"git.commit.author.name" => "Friendly bot",
"git.commit.committer.date" => "2021-06-17T18:35:10+00:00",
"git.commit.committer.email" => "marco.costa@datadoghq.com",
"git.commit.committer.name" => "Marco Costa",
"git.commit.message" => "First commit!",
"git.commit.sha" => "9322ca1d57975b49b8c00b449d21b06660ce8b5b",
"git.repository_url" => "https://datadoghq.com/git/test.git"
}
end

it "matches expected tags" do
is_expected.to eq(expected_tags)
end
end
end

describe "#committer" do
include_context "with git fixture", "gitdir_with_commit"

subject(:committer_email) do
ClimateControl.modify(environment_variables) { described_class.new(env).tags["git.commit.committer.email"] }
end

it "returns committer from the latest commit in the repository" do
is_expected.to eq("marco.costa@datadoghq.com")
end

context "when git show -s returns nothing" do
before do
allow(Open3).to receive(:capture2e).and_return(["", double(success?: true)])
end

it "returns nil and does not fail" do
is_expected.to be_nil
end
end
end
end
48 changes: 48 additions & 0 deletions spec/datadog/ci/ext/environment/user_defined_tags_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
RSpec.describe ::Datadog::CI::Ext::Environment::UserDefinedTags do
describe ".tags" do
subject(:extracted_tags) do
ClimateControl.modify(environment_variables) { described_class.new(env).tags }
end

let(:env) { {} }
let(:environment_variables) { {} }

context "example fixture" do
let(:env) do
{
"DD_GIT_BRANCH" => "usersupplied-branch",
"DD_GIT_COMMIT_AUTHOR_DATE" => "usersupplied-authordate",
"DD_GIT_COMMIT_AUTHOR_EMAIL" => "usersupplied-authoremail",
"DD_GIT_COMMIT_AUTHOR_NAME" => "usersupplied-authorname",
"DD_GIT_COMMIT_COMMITTER_DATE" => "usersupplied-comitterdate",
"DD_GIT_COMMIT_COMMITTER_EMAIL" => "usersupplied-comitteremail",
"DD_GIT_COMMIT_COMMITTER_NAME" => "usersupplied-comittername",
"DD_GIT_COMMIT_MESSAGE" => "usersupplied-message",
"DD_GIT_COMMIT_SHA" => "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"DD_GIT_REPOSITORY_URL" => "git@github.com:DataDog/userrepo.git"
}
end
# Modify HOME so that '~' expansion matches CI home directory.
let(:environment_variables) { super().merge("HOME" => env["HOME"]) }

let(:expected_tags) do
{
"git.branch" => "usersupplied-branch",
"git.commit.author.date" => "usersupplied-authordate",
"git.commit.author.email" => "usersupplied-authoremail",
"git.commit.author.name" => "usersupplied-authorname",
"git.commit.committer.date" => "usersupplied-comitterdate",
"git.commit.committer.email" => "usersupplied-comitteremail",
"git.commit.committer.name" => "usersupplied-comittername",
"git.commit.message" => "usersupplied-message",
"git.commit.sha" => "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"git.repository_url" => "git@github.com:DataDog/userrepo.git"
}
end

it "matches CI tags" do
is_expected.to eq(expected_tags)
end
end
end
end
20 changes: 4 additions & 16 deletions spec/datadog/ci/ext/environment_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require "json"

RSpec.describe ::Datadog::CI::Ext::Environment do
FIXTURE_DIR = "#{File.dirname(__FILE__)}/fixtures/" # rubocop:disable all

let(:logger) { instance_double(Datadog::Core::Logger) }
before do
allow(Datadog).to receive(:logger).and_return(logger)
Expand All @@ -18,16 +16,6 @@
let(:env) { {} }
let(:environment_variables) { {} }

shared_context "with git fixture" do |git_fixture|
let(:environment_variables) do
super().merge("GIT_DIR" => "#{FIXTURE_DIR}/git/#{git_fixture}", "GIT_WORK_TREE" => "#{FIXTURE_DIR}/git/")
end
end

shared_context "without git installed" do
before { allow(Open3).to receive(:capture2e).and_raise(Errno::ENOENT, "No such file or directory - git") }
end

Dir.glob("#{FIXTURE_DIR}/ci/*.json").sort.each do |filename|
# Parse each CI provider file
File.open(filename) do |f|
Expand All @@ -46,7 +34,7 @@
is_expected
.to eq(
{
"ci.workspace_path" => "#{Dir.pwd}/spec/datadog/ci/ext/fixtures/git",
"ci.workspace_path" => "#{Dir.pwd}/spec/support/fixtures/git",
"git.branch" => "master",
"git.commit.author.date" => "2011-02-16T13:00:00+00:00",
"git.commit.author.email" => "bot@friendly.test",
Expand Down Expand Up @@ -79,15 +67,15 @@
context "with a newly created git repository" do
include_context "with git fixture", "gitdir_empty"
it "matches tags" do
is_expected.to eq("ci.workspace_path" => "#{Dir.pwd}/spec/datadog/ci/ext/fixtures/git")
is_expected.to eq("ci.workspace_path" => "#{Dir.pwd}/spec/support/fixtures/git")
end
end

context "with a git repository with a commit" do
include_context "with git fixture", "gitdir_with_commit"
it "matches tags" do
is_expected.to eq(
"ci.workspace_path" => "#{Dir.pwd}/spec/datadog/ci/ext/fixtures/git",
"ci.workspace_path" => "#{Dir.pwd}/spec/support/fixtures/git",
"git.branch" => "master",
"git.commit.author.date" => "2011-02-16T13:00:00+00:00",
"git.commit.author.email" => "bot@friendly.test",
Expand Down Expand Up @@ -143,7 +131,7 @@
it "returns user provided metadata" do
is_expected.to eq(
{
"ci.workspace_path" => "#{Dir.pwd}/spec/datadog/ci/ext/fixtures/git",
"ci.workspace_path" => "#{Dir.pwd}/spec/support/fixtures/git",
"git.branch" => env["DD_GIT_BRANCH"],
"git.tag" => env["DD_GIT_TAG"],
"git.commit.author.date" => env["DD_GIT_COMMIT_AUTHOR_DATE"],
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require_relative "support/span_helpers"
require_relative "support/test_helpers"
require_relative "support/platform_helpers"
require_relative "support/git_helpers"

require "rspec/collection_matchers"
require "climate_control"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions spec/support/git_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FIXTURE_DIR = "#{File.dirname(__FILE__)}/fixtures/" # rubocop:disable all

shared_context "with git fixture" do |git_fixture|
let(:environment_variables) do
super().merge("GIT_DIR" => "#{FIXTURE_DIR}/git/#{git_fixture}", "GIT_WORK_TREE" => "#{FIXTURE_DIR}/git/")
end
end

shared_context "without git installed" do
before { allow(Open3).to receive(:capture2e).and_raise(Errno::ENOENT, "No such file or directory - git") }
end

0 comments on commit 0b57a4c

Please sign in to comment.