Skip to content

Commit 143747a

Browse files
Add tests for env fallback in normalize publish script
Agent-Logs-Url: https://github.com/rails/devcontainer/sessions/23db5954-a602-4ea7-94c3-bd7a0be7c1f5 Co-authored-by: rafaelfranca <47848+rafaelfranca@users.noreply.github.com>
1 parent 764953e commit 143747a

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "minitest/autorun"
5+
require "open3"
6+
require "rbconfig"
7+
require "tempfile"
8+
9+
class NormalizePublishInputsScriptTest < Minitest::Test
10+
def test_reads_values_from_env_when_args_are_omitted
11+
output = Tempfile.new("github-output")
12+
13+
status = run_script(
14+
{
15+
"RUBY_VERSIONS" => "3.3.1,3.2.4",
16+
"IMAGE_VERSIONS" => "ruby-1.1.0",
17+
"REPOSITORY" => "rails/devcontainer",
18+
"GITHUB_OUTPUT" => output.path
19+
}
20+
)
21+
22+
assert status.success?
23+
assert_equal '["3.3.1","3.2.4"]', parse_output(output.path).fetch("ruby_versions_json")
24+
ensure
25+
output&.close!
26+
end
27+
28+
def test_prefers_args_over_env_values
29+
output = Tempfile.new("github-output")
30+
31+
status = run_script(
32+
{
33+
"RUBY_VERSIONS" => "3.0.0",
34+
"IMAGE_VERSIONS" => "ruby-1.1.0",
35+
"REPOSITORY" => "rails/devcontainer",
36+
"GITHUB_OUTPUT" => output.path
37+
},
38+
"--ruby-versions", "3.3.1",
39+
"--image-versions", "ruby-1.2.0",
40+
"--repository", "rails/devcontainer"
41+
)
42+
43+
assert status.success?
44+
result = parse_output(output.path)
45+
assert_equal '["3.3.1"]', result.fetch("ruby_versions_json")
46+
assert_equal '["ruby-1.2.0"]', result.fetch("image_versions_json")
47+
ensure
48+
output&.close!
49+
end
50+
51+
private
52+
53+
def run_script(env, *args)
54+
_stdout, _stderr, status = Open3.capture3(
55+
env,
56+
RbConfig.ruby,
57+
script_path,
58+
*args,
59+
chdir: repo_root
60+
)
61+
status
62+
end
63+
64+
def parse_output(path)
65+
File.read(path).lines.to_h do |line|
66+
key, value = line.strip.split("=", 2)
67+
[key, value]
68+
end
69+
end
70+
71+
def script_path
72+
File.join(repo_root, "bin/normalize-publish-inputs")
73+
end
74+
75+
def repo_root
76+
File.expand_path("../..", __dir__)
77+
end
78+
end

0 commit comments

Comments
 (0)