Skip to content

Commit e38d2e4

Browse files
Enable verbose output from CI debug environment variables. (#56)
1 parent 4e22125 commit e38d2e4

2 files changed

Lines changed: 78 additions & 2 deletions

File tree

lib/sus/config.rb

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ def self.path(root)
2727
# Load configuration from the given root directory.
2828
# @parameter root [String] The root directory to load configuration from.
2929
# @parameter arguments [Array] Command line arguments to parse.
30+
# @parameter env [Hash] The environment to inspect for debug/verbose settings.
3031
# @returns [Config] A new Config instance.
31-
def self.load(root: Dir.pwd, arguments: ARGV)
32+
def self.load(root: Dir.pwd, arguments: ARGV, env: ENV)
3233
derived = Class.new(self)
3334

3435
if path = self.path(root)
@@ -38,12 +39,40 @@ def self.load(root: Dir.pwd, arguments: ARGV)
3839
end
3940

4041
options = {
41-
verbose: !!arguments.delete("--verbose")
42+
verbose: !!arguments.delete("--verbose") || self.verbose_from_environment?(env)
4243
}
4344

4445
return derived.new(root, arguments, **options)
4546
end
4647

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

test/sus/config.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,53 @@ 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"], env: {})
58+
expect(config).to be(:verbose?)
59+
end
60+
61+
it "is enabled by the environment" do
62+
config = subject.load(root: root, arguments: [], env: {"SUS_VERBOSE" => "true"})
63+
expect(config).to be(:verbose?)
64+
end
65+
66+
it "is disabled by default" do
67+
config = subject.load(root: root, arguments: [], env: {})
68+
expect(config).not.to be(:verbose?)
69+
end
70+
end
71+
2572
with "summary output" do
2673
let(:io) {StringIO.new}
2774
let(:output) {Sus::Output::Text.new(io)}

0 commit comments

Comments
 (0)