diff --git a/test/features/credentials_test.rb b/test/features/credentials_test.rb index d25435d..3ac9291 100644 --- a/test/features/credentials_test.rb +++ b/test/features/credentials_test.rb @@ -1,137 +1,144 @@ require_relative "../test_helper" require_relative "runner_helper" +require_relative "file_helper" module Rails::AppEnv::FeaturesTest class CredentialsTest < ActiveSupport::TestCase include RunnerHelper + include FileHelper - CREDENTIALS_PATH_TEST_CASES = [ - { - name: "is {APP_ENV}.yml.enc when both APP_ENV and RAILS_ENV are present", + CREDENTIALS_PATH_TEST_CASES = { + "is {APP_ENV}.yml.enc when both APP_ENV and RAILS_ENV are present": { expected_path: "config/credentials/foo.yml.enc", - app_env: "foo", - rails_env: "bar", - secret_key_base_dummy: "1", - touch_file: "config/credentials/foo.yml.enc" + touch_file: "config/credentials/foo.yml.enc", + env: { + "APP_ENV" => "foo", + "RAILS_ENV" => "bar" + } }, - { - name: "falls back to credentials.yml.enc when {APP_ENV}.yml.enc is absent and both APP_ENV and RAILS_ENV are present", + "falls back to credentials.yml.enc when {APP_ENV}.yml.enc is absent and both APP_ENV and RAILS_ENV are present": { expected_path: "config/credentials.yml.enc", - app_env: "foo", - rails_env: "bar", - secret_key_base_dummy: "1" + env: { + "APP_ENV" => "foo", + "RAILS_ENV" => "bar" + } }, - { - name: "is {APP_ENV}.yml.enc when APP_ENV is present but RAILS_ENV is blank", + "is {APP_ENV}.yml.enc when APP_ENV is present but RAILS_ENV is blank": { expected_path: "config/credentials/foo.yml.enc", - app_env: "foo", - touch_file: "config/credentials/foo.yml.enc" + touch_file: "config/credentials/foo.yml.enc", + env: { + "APP_ENV" => "foo" + } }, - { - name: "falls back to credentials.yml.enc when {APP_ENV}.yml.enc is absent and APP_ENV is present but RAILS_ENV is blank", + "falls back to credentials.yml.enc when {APP_ENV}.yml.enc is absent and APP_ENV is present but RAILS_ENV is blank": { expected_path: "config/credentials.yml.enc", - app_env: "foo", - secret_key_base_dummy: "1" + env: { + "APP_ENV" => "foo" + } }, - { - name: "is {RAILS_ENV}.yml.enc when APP_ENV is blank but RAILS_ENV is present", + "is {RAILS_ENV}.yml.enc when APP_ENV is blank but RAILS_ENV is present": { expected_path: "config/credentials/foo.yml.enc", - rails_env: "foo", - secret_key_base_dummy: "1", - touch_file: "config/credentials/foo.yml.enc" + touch_file: "config/credentials/foo.yml.enc", + env: { + "RAILS_ENV" => "foo" + } }, - { - name: "falls back to credentials.yml.enc when {RAILS_ENV}.yml.enc is absent and when APP_ENV is blank but RAILS_ENV is present", + "falls back to credentials.yml.enc when {RAILS_ENV}.yml.enc is absent and when APP_ENV is blank but RAILS_ENV is present": { expected_path: "config/credentials.yml.enc", - rails_env: "foo", - secret_key_base_dummy: "1" + env: { + "RAILS_ENV" => "foo" + } }, - { - name: "is {DEFAULT_RAILS_ENV}.yml.enc when both APP_ENV and RAILS_ENV are blank", + "is {DEFAULT_RAILS_ENV}.yml.enc when both APP_ENV and RAILS_ENV are blank": { expected_path: "config/credentials/#{DEFAULT_RAILS_ENV}.yml.enc", touch_file: "config/credentials/#{DEFAULT_RAILS_ENV}.yml.enc" }, - { - name: "falls back to credentials.yml.enc when {DEFAULT_RAILS_ENV}.yml.enc is absent and both APP_ENV and RAILS_ENV are blank", + "falls back to credentials.yml.enc when {DEFAULT_RAILS_ENV}.yml.enc is absent and both APP_ENV and RAILS_ENV are blank": { expected_path: "config/credentials.yml.enc" } - ] + } - KEY_PATH_TEST_CASES = [ - { - name: "is {APP_ENV}.key when both APP_ENV and RAILS_ENV are present", + KEY_PATH_TEST_CASES = { + "is {APP_ENV}.key when both APP_ENV and RAILS_ENV are present": { expected_path: "config/credentials/foo.key", - app_env: "foo", - rails_env: "bar", - secret_key_base_dummy: "1", - touch_file: "config/credentials/foo.key" + touch_file: "config/credentials/foo.key", + env: { + "APP_ENV" => "foo", + "RAILS_ENV" => "bar" + } }, - { - name: "falls back to master.key when {APP_ENV}.key is absent and both APP_ENV and RAILS_ENV are present", + "falls back to master.key when {APP_ENV}.key is absent and both APP_ENV and RAILS_ENV are present": { expected_path: "config/master.key", - app_env: "foo", - rails_env: "bar", - secret_key_base_dummy: "1" + env: { + "APP_ENV" => "foo", + "RAILS_ENV" => "bar" + } }, - { - name: "is {APP_ENV}.key when APP_ENV is present but RAILS_ENV is blank", + "is {APP_ENV}.key when APP_ENV is present but RAILS_ENV is blank": { expected_path: "config/credentials/foo.key", - app_env: "foo", - touch_file: "config/credentials/foo.key" + touch_file: "config/credentials/foo.key", + env: { + "APP_ENV" => "foo" + } }, - { - name: "falls back to master.key when {APP_ENV}.key is absent and APP_ENV is present but RAILS_ENV is blank", + "falls back to master.key when {APP_ENV}.key is absent and APP_ENV is present but RAILS_ENV is blank": { expected_path: "config/master.key", - app_env: "foo" + env: { + "APP_ENV" => "foo" + } }, - { - name: "is {RAILS_ENV}.key when APP_ENV is blank but RAILS_ENV is present", + "is {RAILS_ENV}.key when APP_ENV is blank but RAILS_ENV is present": { expected_path: "config/credentials/foo.key", - rails_env: "foo", touch_file: "config/credentials/foo.key", - secret_key_base_dummy: "1" + env: { + "RAILS_ENV" => "foo" + } }, - { - name: "falls back to master.key when {RAILS_ENV}.key is absent and when APP_ENV is blank but RAILS_ENV is present", + "falls back to master.key when {RAILS_ENV}.key is absent and when APP_ENV is blank but RAILS_ENV is present": { expected_path: "config/master.key", - rails_env: "foo", - secret_key_base_dummy: "1" + env: { + "RAILS_ENV" => "foo" + } }, - { - name: "is {DEFAULT_RAILS_ENV}.key when both APP_ENV and RAILS_ENV are blank", + "is {DEFAULT_RAILS_ENV}.key when both APP_ENV and RAILS_ENV are blank": { expected_path: "config/credentials/#{DEFAULT_RAILS_ENV}.key", touch_file: "config/credentials/#{DEFAULT_RAILS_ENV}.key" }, - { - name: "falls back to master.key when {DEFAULT_RAILS_ENV}.key is absent and both APP_ENV and RAILS_ENV are blank", + "falls back to master.key when {DEFAULT_RAILS_ENV}.key is absent and both APP_ENV and RAILS_ENV are blank": { expected_path: "config/master.key" } - ] + } - CREDENTIALS_PATH_TEST_CASES.each_with_index do |args, i| + CREDENTIALS_PATH_TEST_CASES.each_key do |name| class_eval <<~RUBY, __FILE__, __LINE__ + 1 - test "content path #{args[:name]}" do - assert_credentials_path(**CREDENTIALS_PATH_TEST_CASES[#{i}]) + test "content path #{name}" do + assert_credentials_path(**CREDENTIALS_PATH_TEST_CASES[:"#{name}"]) end RUBY end - KEY_PATH_TEST_CASES.each_with_index do |args, i| + KEY_PATH_TEST_CASES.each_key do |name| class_eval <<~RUBY, __FILE__, __LINE__ + 1 - test "key path #{args[:name]}" do - assert_key_path(**KEY_PATH_TEST_CASES[#{i}]) + test "key path #{name}" do + assert_key_path(**KEY_PATH_TEST_CASES[:"#{name}"]) end RUBY end private - def assert_credentials_path(expected_path:, **options) - assert_runner Rails.root.join(expected_path), "Rails.configuration.credentials.content_path", **options + def assert_credentials_path(expected_path:, touch_file: nil, **options) + assert_runner_puts_with_file Rails.root.join(expected_path), "Rails.configuration.credentials.content_path", touch_file, **options + end + + def assert_key_path(expected_path:, touch_file: nil, **options) + assert_runner_puts_with_file Rails.root.join(expected_path), "Rails.configuration.credentials.key_path", touch_file, **options end - def assert_key_path(expected_path:, **options) - assert_runner Rails.root.join(expected_path), "Rails.configuration.credentials.key_path", **options + def assert_runner_puts_with_file(expected, subject, file_path, **options) + with_file(file_path) do + assert_runner_puts expected, subject, **options + end end end end diff --git a/test/features/file_helper.rb b/test/features/file_helper.rb new file mode 100644 index 0000000..e3de1cf --- /dev/null +++ b/test/features/file_helper.rb @@ -0,0 +1,16 @@ +module FileHelper + private + + def with_file(path, &block) + return block.call nil if path.nil? + + full_path = Rails.root.join path + + FileUtils.mkdir_p File.dirname(full_path) + FileUtils.touch full_path + + block.call full_path + ensure + FileUtils.rm_f full_path unless full_path.nil? + end +end diff --git a/test/features/info_test.rb b/test/features/info_test.rb index 8588cb1..1107269 100644 --- a/test/features/info_test.rb +++ b/test/features/info_test.rb @@ -5,37 +5,43 @@ module Rails::AppEnv::FeaturesTest class InfoTest < ActiveSupport::TestCase include RunnerHelper - TEST_CASES = [ - { - name: "is APP_ENV when both APP_ENV and RAILS_ENV are present", + TEST_CASES = { + "is APP_ENV when both APP_ENV and RAILS_ENV are present": { expected: "foo", - app_env: "foo", - rails_env: "bar", - secret_key_base_dummy: "1" + env: { + "APP_ENV" => "foo", + "RAILS_ENV" => "bar" + } }, - { - name: "is APP_ENV when APP_ENV is present but RAILS_ENV is blank", + "is APP_ENV when APP_ENV is present but RAILS_ENV is blank": { expected: "foo", - app_env: "foo" + env: { + "APP_ENV" => "foo" + } }, - { - name: "falls back to RAILS_ENV when APP_ENV is blank but RAILS_ENV is present", + "falls back to RAILS_ENV when APP_ENV is blank but RAILS_ENV is present": { expected: "foo", - rails_env: "foo", - secret_key_base_dummy: "1" + env: { + "RAILS_ENV" => "foo" + } }, - { - name: "falls back to DEFAULT_RAILS_ENV when both APP_ENV and RAILS_ENV are blank", + "falls back to DEFAULT_RAILS_ENV when both APP_ENV and RAILS_ENV are blank": { expected: DEFAULT_RAILS_ENV # development } - ] + } - TEST_CASES.each_with_index do |args, i| + TEST_CASES.each_key do |name| class_eval <<~RUBY, __FILE__, __LINE__ + 1 - test "Rails::Info's 'Application environment' #{args[:name]}" do - assert_runner args[:expected], 'Rails::Info.properties.value_for("Application environment")', **args + test "Rails::Info's 'Application environment' #{name}" do + assert_rails_info_property(**TEST_CASES[:"#{name}"]) end RUBY end + + private + + def assert_rails_info_property(expected:, **options) + assert_runner_puts expected, 'Rails::Info.properties.value_for("Application environment")', **options + end end end diff --git a/test/features/runner_helper.rb b/test/features/runner_helper.rb index 085e7a3..6425168 100644 --- a/test/features/runner_helper.rb +++ b/test/features/runner_helper.rb @@ -7,31 +7,12 @@ module RunnerHelper DUMMY_RAILS = File.expand_path "../../dummy/bin/rails", __FILE__ - def assert_runner(expected, subject, app_env: nil, rails_env: nil, secret_key_base_dummy: nil, touch_file: nil, **options) - switch_env "APP_ENV", app_env do - with_rails_env(rails_env) do - switch_env "SECRET_KEY_BASE_DUMMY", secret_key_base_dummy do - with_file(touch_file) do - stdout, status = Open3.capture2(DUMMY_RAILS, "runner", "puts #{subject}") + def assert_runner_puts(expected, subject, env: {}) + env = env.with_defaults({"APP_ENV" => nil, "RAILS_ENV" => nil, "SECRET_KEY_BASE_DUMMY" => "1"}) - assert_predicate status, :success? - assert_equal expected.to_s, stdout.chomp - end - end - end - end - end - - def with_file(path, &block) - return block.call nil if path.nil? - - full_path = Rails.root.join path - - FileUtils.mkdir_p File.dirname(full_path) - FileUtils.touch full_path + stdout, status = Open3.capture2(env, DUMMY_RAILS, "runner", "puts #{subject}") - block.call full_path - ensure - FileUtils.rm_f full_path unless full_path.nil? + assert_predicate status, :success? + assert_equal expected.to_s, stdout.chomp end end