Skip to content

Commit 96236b5

Browse files
Merge pull request rails#45700 from jonathanhefner/encrypted-config-aborted-edit-confirmation-message
Prevent "saved" message when edit command aborted
2 parents 2b3245d + d4e87ca commit 96236b5

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

railties/lib/rails/commands/credentials/credentials_command.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,7 @@ def edit
3636
ensure_credentials_have_been_added
3737
ensure_diffing_driver_is_configured
3838

39-
catch_editing_exceptions do
40-
change_credentials_in_system_editor
41-
end
42-
43-
say "File encrypted and saved."
44-
warn_if_credentials_are_invalid
45-
rescue ActiveSupport::MessageEncryptor::InvalidMessage
46-
say "Couldn't decrypt #{content_path}. Perhaps you passed the wrong key?"
39+
change_credentials_in_system_editor
4740
end
4841

4942
def show
@@ -99,9 +92,16 @@ def ensure_credentials_have_been_added
9992
end
10093

10194
def change_credentials_in_system_editor
102-
credentials.change do |tmp_path|
103-
system(*Shellwords.split(ENV["EDITOR"]), tmp_path.to_s)
95+
catch_editing_exceptions do
96+
credentials.change do |tmp_path|
97+
system(*Shellwords.split(ENV["EDITOR"]), tmp_path.to_s)
98+
end
99+
100+
say "File encrypted and saved."
101+
warn_if_credentials_are_invalid
104102
end
103+
rescue ActiveSupport::MessageEncryptor::InvalidMessage
104+
say "Couldn't decrypt #{content_path}. Perhaps you passed the wrong key?"
105105
end
106106

107107
def warn_if_credentials_are_invalid

railties/lib/rails/commands/encrypted/encrypted_command.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,7 @@ def edit(*)
2727
ensure_encryption_key_has_been_added
2828
ensure_encrypted_configuration_has_been_added
2929

30-
catch_editing_exceptions do
31-
change_encrypted_configuration_in_system_editor
32-
end
33-
34-
say "File encrypted and saved."
35-
warn_if_encrypted_configuration_is_invalid
36-
rescue ActiveSupport::MessageEncryptor::InvalidMessage
37-
say "Couldn't decrypt #{content_path}. Perhaps you passed the wrong key?"
30+
change_encrypted_configuration_in_system_editor
3831
end
3932

4033
def show(*)
@@ -67,9 +60,16 @@ def ensure_encrypted_configuration_has_been_added
6760
end
6861

6962
def change_encrypted_configuration_in_system_editor
70-
encrypted_configuration.change do |tmp_path|
71-
system("#{ENV["EDITOR"]} #{tmp_path}")
63+
catch_editing_exceptions do
64+
encrypted_configuration.change do |tmp_path|
65+
system("#{ENV["EDITOR"]} #{tmp_path}")
66+
end
67+
68+
say "File encrypted and saved."
69+
warn_if_encrypted_configuration_is_invalid
7270
end
71+
rescue ActiveSupport::MessageEncryptor::InvalidMessage
72+
say "Couldn't decrypt #{content_path}. Perhaps you passed the wrong key?"
7373
end
7474

7575
def warn_if_encrypted_configuration_is_invalid

railties/test/commands/credentials_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase
142142
assert_match %r/provides_secret_key_base: true/, run_edit_command
143143
end
144144

145+
test "edit command does not display save confirmation message if interrupted" do
146+
assert_match %r/file encrypted and saved/i, run_edit_command
147+
148+
interrupt_command_process = %(ruby -e "Process.kill 'INT', Process.ppid")
149+
output = run_edit_command(editor: interrupt_command_process)
150+
151+
assert_no_match %r/file encrypted and saved/i, output
152+
assert_match %r/nothing saved/i, output
153+
end
154+
145155
test "edit command preserves user's content even if it contains invalid YAML" do
146156
write_invalid_yaml = %(ruby -e "File.write ARGV[0], 'foo: bar: bad'")
147157

railties/test/commands/encrypted_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ class Rails::Command::EncryptedCommandTest < ActiveSupport::TestCase
8282
assert_match(/access_key_id: 123/, run_edit_command(key: "config/tokens.key"))
8383
end
8484

85+
test "edit command does not display save confirmation message if interrupted" do
86+
assert_match %r/file encrypted and saved/i, run_edit_command
87+
88+
interrupt_command_process = %(exec ruby -e "Process.kill 'INT', Process.ppid")
89+
output = run_edit_command(editor: interrupt_command_process)
90+
91+
assert_no_match %r/file encrypted and saved/i, output
92+
assert_match %r/nothing saved/i, output
93+
end
94+
8595
test "edit command preserves user's content even if it contains invalid YAML" do
8696
write_invalid_yaml = %(ruby -e "File.write ARGV[0], 'foo: bar: bad'")
8797

0 commit comments

Comments
 (0)