From b1b78c4f9ff0ff3a2570980b0012893eb03fc597 Mon Sep 17 00:00:00 2001 From: Eric Mueller Date: Tue, 28 Nov 2023 21:13:03 -0500 Subject: [rubygems/rubygems] Introduce the Gem::CIDetector This is based on the list in Gem::UpdateSuggestion and Bundler::Fetcher; these have similar purposes (determining whether/what CI we're executing in), and can benefit from being combined and updated (they're both slightly out of date). Noteable changes: * We'll consider ourselves to be on a CI in more cases - if CI_NAME or TASKCLUSTER_ROOT_URL are set specifically, since those represent two cases that were either overlooked or are no longer covered by the existing implementation. (Or possibly TaskCluster still does provide RUN_ID, but failed to document it) * We will notice/track a few additional services in ci_strings (cirrus, dsari, taskcluster), stop tracking 'snap' (they went under in 2017), and update buildbox to buildkite (they've been called that for 8 years, and the BUILDBOX envs have been deprecated for 3). * We'll also sort/uniq/downcase the values (all of which only matter because of the special case of CI_NAME). https://github.com/rubygems/rubygems/commit/60652b942f --- test/rubygems/test_gem_ci_detector.rb | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 test/rubygems/test_gem_ci_detector.rb (limited to 'test/rubygems/test_gem_ci_detector.rb') diff --git a/test/rubygems/test_gem_ci_detector.rb b/test/rubygems/test_gem_ci_detector.rb new file mode 100644 index 0000000000..3caefce97d --- /dev/null +++ b/test/rubygems/test_gem_ci_detector.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require_relative "helper" +require "rubygems" + +class TestCiDetector < Test::Unit::TestCase + def test_ci? + with_env("FOO" => "bar") { assert_equal(false, Gem::CIDetector.ci?) } + with_env("CI" => "true") { assert_equal(true, Gem::CIDetector.ci?) } + with_env("CONTINUOUS_INTEGRATION" => "1") { assert_equal(true, Gem::CIDetector.ci?) } + with_env("RUN_ID" => "0", "TASKCLUSTER_ROOT_URL" => "2") do + assert_equal(true, Gem::CIDetector.ci?) + end + end + + def test_ci_strings + with_env("FOO" => "bar") { assert_empty(Gem::CIDetector.ci_strings) } + with_env("TRAVIS" => "true") { assert_equal(["travis"], Gem::CIDetector.ci_strings) } + with_env("CI" => "true", "CIRCLECI" => "true", "GITHUB_ACTIONS" => "true") do + assert_equal(["ci", "circle", "github"], Gem::CIDetector.ci_strings) + end + with_env("CI" => "true", "CI_NAME" => "MYCI") do + assert_equal(["ci", "myci"], Gem::CIDetector.ci_strings) + end + with_env("GITHUB_ACTIONS" => "true", "CI_NAME" => "github") do + assert_equal(["github"], Gem::CIDetector.ci_strings) + end + with_env("TASKCLUSTER_ROOT_URL" => "https://foo.bar", "DSARI" => "1", "CI_NAME" => "") do + assert_equal(["dsari", "taskcluster"], Gem::CIDetector.ci_strings) + end + end + + private + + def with_env(overrides, &block) + @orig_env = ENV.to_h + ENV.replace(overrides) + begin + block.call + ensure + ENV.replace(@orig_env) + end + end +end -- cgit v1.2.3