Skip to content

Commit e490f5f

Browse files
Enable verbose output from CI debug environment variables.
Automatically enable verbose output when a CI provider is running in debug mode, e.g. GitHub Actions re-run with "Enable debug logging" (RUNNER_DEBUG=1). Also supports GitLab CI, Buildkite, Azure Pipelines, and an explicit SUS_VERBOSE toggle. Assisted-By: devx/0e90505d-10c5-4dfa-be04-c43abc87164a
1 parent 4e22125 commit e490f5f

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

lib/sus/config.rb

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,40 @@ def self.load(root: Dir.pwd, arguments: ARGV)
3838
end
3939

4040
options = {
41-
verbose: !!arguments.delete("--verbose")
41+
verbose: !!arguments.delete("--verbose") || self.verbose_from_environment?
4242
}
4343

4444
return derived.new(root, arguments, **options)
4545
end
4646

47+
# Maps CI environment variables to the values they take when the CI provider is running in debug/verbose mode. When any of these match, we enable verbose output automatically.
48+
#
49+
# - `RUNNER_DEBUG` is set by GitHub Actions when a workflow is re-run with "Enable debug logging".
50+
# - `CI_DEBUG_TRACE` is set by GitLab CI when debug logging (tracing) is enabled.
51+
# - `BUILDKITE_AGENT_DEBUG` is set by Buildkite when agent debug is enabled.
52+
# - `SYSTEM_DEBUG` is set by Azure Pipelines when the `system.debug` variable is enabled.
53+
# - `SUS_VERBOSE` can be set explicitly to enable verbose output regardless of the CI provider.
54+
DEBUG_ENVIRONMENT = {
55+
"SUS_VERBOSE" => "true",
56+
"RUNNER_DEBUG" => "1",
57+
"CI_DEBUG_TRACE" => "true",
58+
"BUILDKITE_AGENT_DEBUG" => "true",
59+
"SYSTEM_DEBUG" => "true",
60+
}
61+
62+
# Whether verbose output should be enabled based on the environment.
63+
#
64+
# Detects CI environments that request debug logging, e.g. GitHub Actions
65+
# sets `RUNNER_DEBUG=1` when a workflow is re-run with "Enable debug logging".
66+
#
67+
# @parameter env [Hash] The environment to inspect.
68+
# @returns [Boolean] Whether verbose output should be enabled.
69+
def self.verbose_from_environment?(env = ENV)
70+
DEBUG_ENVIRONMENT.any? do |key, value|
71+
env[key] == value
72+
end
73+
end
74+
4775
# Initialize a new Config instance.
4876
# @parameter root [String] The root directory for the project.
4977
# @parameter paths [Array] Optional paths to specific test files.

test/sus/config.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,48 @@ def print(output)
2222
expect(config).not.to be(:nil?)
2323
end
2424

25+
with ".verbose_from_environment?" do
26+
it "is enabled by GitHub Actions debug logging" do
27+
expect(subject.verbose_from_environment?({"RUNNER_DEBUG" => "1"})).to be == true
28+
end
29+
30+
it "is enabled by GitLab CI debug tracing" do
31+
expect(subject.verbose_from_environment?({"CI_DEBUG_TRACE" => "true"})).to be == true
32+
end
33+
34+
it "is enabled by Buildkite agent debug" do
35+
expect(subject.verbose_from_environment?({"BUILDKITE_AGENT_DEBUG" => "true"})).to be == true
36+
end
37+
38+
it "is enabled by Azure Pipelines system debug" do
39+
expect(subject.verbose_from_environment?({"SYSTEM_DEBUG" => "true"})).to be == true
40+
end
41+
42+
it "is enabled by the SUS_VERBOSE environment variable" do
43+
expect(subject.verbose_from_environment?({"SUS_VERBOSE" => "true"})).to be == true
44+
end
45+
46+
it "is disabled by default" do
47+
expect(subject.verbose_from_environment?({})).to be == false
48+
end
49+
50+
it "ignores unrelated values" do
51+
expect(subject.verbose_from_environment?({"RUNNER_DEBUG" => "0"})).to be == false
52+
end
53+
end
54+
55+
with "#verbose?" do
56+
it "is enabled by the --verbose argument" do
57+
config = subject.load(root: root, arguments: ["--verbose"])
58+
expect(config).to be(:verbose?)
59+
end
60+
61+
it "is disabled by default" do
62+
config = subject.load(root: root, arguments: [])
63+
expect(config).not.to be(:verbose?)
64+
end
65+
end
66+
2567
with "summary output" do
2668
let(:io) {StringIO.new}
2769
let(:output) {Sus::Output::Text.new(io)}

0 commit comments

Comments
 (0)