Skip to content

Commit

Permalink
Merge pull request #163 from buildkite/pie-1279-pass-custom-run-env
Browse files Browse the repository at this point in the history
Add ability to pass custom run env to TA API
  • Loading branch information
blaknite authored Nov 15, 2022
2 parents 7d6a86e + 634b113 commit 96bbfd0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/pkg/
/spec/reports/
/tmp/
/vendor/bundle

# rspec failure tracking
.rspec_status
4 changes: 3 additions & 1 deletion lib/buildkite/test_collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ class << self
attr_accessor :session
attr_accessor :debug_enabled
attr_accessor :tracing_enabled
attr_accessor :env
end

def self.configure(hook:, token: nil, url: nil, debug_enabled: false, tracing_enabled: true)
def self.configure(hook:, token: nil, url: nil, debug_enabled: false, tracing_enabled: true, env: {})
self.api_token = (token || ENV["BUILDKITE_ANALYTICS_TOKEN"])&.strip
self.url = url || DEFAULT_URL
self.debug_enabled = debug_enabled || !!(ENV["BUILDKITE_ANALYTICS_DEBUG_ENABLED"])
self.tracing_enabled = tracing_enabled
self.env = env

self.hook_into(hook)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/buildkite/test_collector/ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.env
# The analytics env are more specific than the automatic ci platform env.
# If they've been specified we'll assume the user wants to use that value instead.
def env
ci_env.merge(analytics_env)
ci_env.merge(analytics_env).merge(Buildkite::TestCollector.env)
end

private
Expand Down
39 changes: 29 additions & 10 deletions spec/test_collector/ci_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
let(:debug) { "true" }
let(:version) { Buildkite::TestCollector::VERSION }
let(:name) { Buildkite::TestCollector::NAME }
let(:test_value) { "test_value" }

before do
allow(ENV).to receive(:[]).and_call_original
Expand All @@ -24,6 +25,14 @@
fake_env("BUILDKITE_BUILD_ID", nil)
fake_env("GITHUB_RUN_NUMBER", nil)
fake_env("CIRCLE_BUILD_NUM", nil)

Buildkite::TestCollector.configure(hook: :rspec, env: { "test" => test_value })
end

it "merges in the custom env" do
result = Buildkite::TestCollector::CI.env

expect(result["test"]).to eq test_value
end

context "when running on Buildkite" do
Expand Down Expand Up @@ -60,7 +69,8 @@
"message" => bk_message,
"debug" => debug,
"version" => version,
"collector" => name
"collector" => name,
"test" => test_value,
})
end

Expand Down Expand Up @@ -93,7 +103,8 @@
"execution_name_prefix" => "execution_name_prefix",
"execution_name_suffix" => "execution_name_suffix",
"version" => version,
"collector" => name
"collector" => name,
"test" => test_value,
})
end
end
Expand Down Expand Up @@ -131,7 +142,8 @@
"number" => gha_run_number,
"debug" => debug,
"version" => version,
"collector" => name
"collector" => name,
"test" => test_value,
})
end

Expand Down Expand Up @@ -160,7 +172,8 @@
"message" => message,
"debug" => debug,
"version" => version,
"collector" => name
"collector" => name,
"test" => test_value,
})
end
end
Expand Down Expand Up @@ -194,7 +207,8 @@
"number" => c_number,
"debug" => debug,
"version" => version,
"collector" => name
"collector" => name,
"test" => test_value,
})
end

Expand Down Expand Up @@ -223,7 +237,8 @@
"message" => message,
"debug" => debug,
"version" => version,
"collector" => name
"collector" => name,
"test" => test_value,
})
end
end
Expand All @@ -244,7 +259,8 @@
"key" => key,
"debug" => debug,
"version" => version,
"collector" => name
"collector" => name,
"test" => test_value,
})
end

Expand Down Expand Up @@ -273,7 +289,8 @@
"message" => message,
"debug" => debug,
"version" => version,
"collector" => name
"collector" => name,
"test" => test_value,
})
end
end
Expand All @@ -292,7 +309,8 @@
"key" => "845ac829-2ab3-4bbb-9e24-3529755a6d37",
"debug" => debug,
"version" => version,
"collector" => name
"collector" => name,
"test" => test_value,
})
end

Expand Down Expand Up @@ -321,7 +339,8 @@
"message" => message,
"debug" => debug,
"version" => version,
"collector" => name
"collector" => name,
"test" => test_value,
})
end
end
Expand Down
26 changes: 24 additions & 2 deletions spec/test_collector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,49 @@

RSpec.describe Buildkite::TestCollector do
context "RSpec" do
let(:hook) { :rspec }

it "can configure api_token and url" do
analytics = Buildkite::TestCollector
ENV["BUILDKITE_ANALYTICS_TOKEN"] = "MyToken"

analytics.configure(hook: :rspec)
analytics.configure(hook: hook)

expect(analytics.api_token).to eq "MyToken"
expect(analytics.url).to eq "https://analytics-api.buildkite.com/v1/uploads"
end

it "can configure custom env" do
analytics = Buildkite::TestCollector
env = { test: "test value" }

analytics.configure(hook: hook, env: env)

expect(analytics.env).to match env
end
end

context "Minitest" do
let(:hook) { :minitest }

it "can configure api_token and url" do
analytics = Buildkite::TestCollector
ENV["BUILDKITE_ANALYTICS_TOKEN"] = "MyToken"

analytics.configure(hook: :minitest)
analytics.configure(hook: hook)

expect(analytics.api_token).to eq "MyToken"
expect(analytics.url).to eq "https://analytics-api.buildkite.com/v1/uploads"
end

it "can configure custom env" do
analytics = Buildkite::TestCollector
env = { test: "test value" }

analytics.configure(hook: hook, env: env)

expect(analytics.env).to match env
end
end

describe ".safe" do
Expand Down

0 comments on commit 96bbfd0

Please sign in to comment.