diff --git a/railties/lib/rails/commands/credentials/credentials_command.rb b/railties/lib/rails/commands/credentials/credentials_command.rb index 1da5cc29e16..75a832ab4a2 100644 --- a/railties/lib/rails/commands/credentials/credentials_command.rb +++ b/railties/lib/rails/commands/credentials/credentials_command.rb @@ -128,7 +128,11 @@ module Rails end def extract_environment_from_path(path) - available_environments.find { |env| path.end_with?("#{env}.yml.enc") } + available_environments.find { |env| path.end_with?("#{env}.yml.enc") } || extract_custom_environment(path) + end + + def extract_custom_environment(path) + path =~ %r{config/credentials/(.+)\.yml\.enc} && $1 end end end diff --git a/railties/test/commands/credentials_test.rb b/railties/test/commands/credentials_test.rb index 4b29ed656fc..45ebd886ead 100644 --- a/railties/test/commands/credentials_test.rb +++ b/railties/test/commands/credentials_test.rb @@ -285,6 +285,20 @@ class Rails::Command::CredentialsTest < ActiveSupport::TestCase assert_match(raw_content, run_diff_command("config/credentials.yml.enc")) end + test "diff for custom environment" do + run_edit_command(environment: "custom") + + assert_match(/access_key_id: 123/, run_diff_command("config/credentials/custom.yml.enc")) + end + + test "diff for custom environment when key is not available" do + run_edit_command(environment: "custom") + remove_file "config/credentials/custom.key" + + raw_content = File.read(app_path("config", "credentials", "custom.yml.enc")) + assert_match(raw_content, run_diff_command("config/credentials/custom.yml.enc")) + end + test "diff returns raw encrypted content when errors occur" do run_edit_command(environment: "development")