Skip to content

Commit 9e0746f

Browse files
authored
Merge pull request #184 from hlascelles/enforce-uppercase-env
Enforce uppercase ENV keys
2 parents 8b016a5 + a8a8d3a commit 9e0746f

File tree

7 files changed

+62
-20
lines changed

7 files changed

+62
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.0.0 (2025-04-12)
2+
3+
BREAKING CHANGE:
4+
- Enforce uppercase ENV keys [#184](https://github.com/hlascelles/figjam/pull/184)
5+
16
## 1.6.2 (2024-09-10)
27

38
- Reduce override log level to info [#160](https://github.com/hlascelles/figjam/pull/160)

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
figjam (1.6.2)
4+
figjam (2.0.0)
55
thor (>= 0.14.0, < 2)
66

77
GEM

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ figjam
77
[![specs workflow](https://github.com/hlascelles/figjam/actions/workflows/specs.yml/badge.svg)](https://github.com/hlascelles/figjam/actions)
88
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
99

10-
1110
Figjam makes it easy to configure ruby and Rails apps `ENV` values by just using a single YAML file.
1211

1312
PRs to applications often need to come with default configuration values, but hardcoding these into

lib/figjam/application.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require "figjam/figaro_alias"
44

55
module Figjam
6+
class InvalidKeyNameError < StandardError; end
7+
68
# :reek:TooManyMethods
79
class Application
810
FIGARO_ENV_PREFIX = "_FIGARO_".freeze
@@ -40,7 +42,13 @@ def configuration
4042

4143
def load
4244
each do |key, value|
43-
skip?(key) ? key_skipped(key) : set(key, value)
45+
if !valid_key_name?(key)
46+
invalid_key_name(key)
47+
elsif skip?(key)
48+
key_skipped(key)
49+
else
50+
set(key, value)
51+
end
4452
end
4553
end
4654

@@ -110,5 +118,13 @@ def each(&block)
110118
private def key_skipped(key)
111119
puts "INFO: Skipping key #{key.inspect}. Already set in ENV."
112120
end
121+
122+
private def valid_key_name?(key)
123+
key.to_s == key.to_s.upcase
124+
end
125+
126+
private def invalid_key_name(key)
127+
raise InvalidKeyNameError, "Environment variable names must be uppercase: #{key.inspect}"
128+
end
113129
end
114130
end

lib/figjam/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Figjam
2-
VERSION = "1.6.2".freeze
2+
VERSION = "2.0.0".freeze
33
end

spec/figjam/application_spec.rb

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,44 +200,66 @@ def yaml_to_path(yaml)
200200
let!(:application) { described_class.new }
201201

202202
before do
203-
allow(application).to receive(:configuration).and_return({ "foo" => "bar" })
203+
# Update to use uppercase keys in the default configuration
204+
allow(application).to receive(:configuration).and_return({ "FOO" => "bar" })
204205
end
205206

206207
it "merges values into ENV" do
207208
expect {
208209
application.load
209210
}.to change {
210-
::ENV["foo"]
211+
::ENV["FOO"]
211212
}.from(nil).to("bar")
212213
end
213214

214215
it "skips keys (and warns) that have already been set externally" do
215-
::ENV["foo"] = "baz"
216+
::ENV["FOO"] = "baz"
216217

217218
expect(application)
218-
.to receive(:puts).with('INFO: Skipping key "foo". Already set in ENV.')
219+
.to receive(:puts).with('INFO: Skipping key "FOO". Already set in ENV.')
219220

220221
expect {
221222
application.load
222223
}.not_to(
223224
change {
224-
::ENV["foo"]
225+
::ENV["FOO"]
225226
}
226227
)
227228
end
228229

229230
it "sets keys that have already been set internally" do
230231
application.load
231232

232-
allow(application).to receive(:configuration).and_return({ "foo" => "baz" })
233+
allow(application).to receive(:configuration).and_return({ "FOO" => "baz" })
233234

234235
expect {
235236
application.load
236237
}.to change {
237-
::ENV["foo"]
238+
::ENV["FOO"]
238239
}.from("bar").to("baz")
239240
end
240241

242+
it "raises an error for keys that are not all uppercase" do
243+
allow(application).to receive(:configuration).and_return({ "foo" => "bar",
244+
"FOO_BAR" => "baz" })
245+
246+
expect {
247+
application.load
248+
}.to raise_error(Figjam::InvalidKeyNameError,
249+
'Environment variable names must be uppercase: "foo"')
250+
end
251+
252+
it "accepts keys that are all uppercase" do
253+
allow(application).to receive(:configuration).and_return({ "FOO" => "bar",
254+
"BAR_BAZ" => "qux" })
255+
256+
expect {
257+
application.load
258+
}.to change {
259+
[::ENV["FOO"], ::ENV["BAR_BAZ"]]
260+
}.from([nil, nil]).to(%w[bar qux])
261+
end
262+
241263
shared_examples "correct warning with and without silence override" do
242264
[
243265
{ FIGARO_SILENCE_STRING_WARNINGS: true },
@@ -286,19 +308,19 @@ def yaml_to_path(yaml)
286308
end
287309

288310
context "when warning when a value isn't a string" do
289-
let(:config) { { "string key" => :SYMBOL_VALUE } }
311+
let(:config) { { "STRING_KEY" => :SYMBOL_VALUE } }
290312

291313
include_examples "correct warning with and without silence override"
292314
end
293315

294316
it "allows nil values" do
295-
allow(application).to receive(:configuration).and_return({ "foo" => nil })
317+
allow(application).to receive(:configuration).and_return({ "FOO" => nil })
296318

297319
expect {
298320
application.load
299321
}.not_to(
300322
change {
301-
::ENV["foo"]
323+
::ENV["FOO"]
302324
}
303325
)
304326
end

spec/figjam/rails_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444

4545
describe "initialization" do
4646
before do
47-
write_file("config/application.yml", "foo: bar")
47+
write_file("config/application.yml", "FOO: bar")
4848
end
4949

5050
it "loads application.yml" do
51-
run_command_and_stop("rails runner 'puts Figjam.env.foo'")
51+
run_command_and_stop("rails runner 'puts Figjam.env.FOO'")
5252

5353
expect(all_stdout).to include("bar")
5454
end
@@ -57,11 +57,11 @@
5757
write_file("config/database.yml", <<~STR)
5858
development:
5959
adapter: sqlite3
60-
database: db/<%= ENV["foo"] %>.sqlite3
60+
database: db/<%= ENV["FOO"] %>.sqlite3
6161
6262
test:
6363
adapter: sqlite3
64-
database: db/<%= ENV["foo"] %>.sqlite3
64+
database: db/<%= ENV["FOO"] %>.sqlite3
6565
STR
6666

6767
run_command_and_stop("rake db:migrate")
@@ -71,10 +71,10 @@
7171

7272
it "happens before application configuration" do
7373
insert_into_file_after("config/application.rb", /< Rails::Application$/, <<-STR)
74-
config.foo = ENV["foo"]
74+
config.FOO = ENV["FOO"]
7575
STR
7676

77-
run_command_and_stop("rails runner 'puts Rails.application.config.foo'")
77+
run_command_and_stop("rails runner 'puts Rails.application.config.FOO'")
7878

7979
expect(all_stdout).to include("bar")
8080
end

0 commit comments

Comments
 (0)