Skip to content

Commit 20177c7

Browse files
committed
Restore backup database.yml when retesting after interrupt
MSP-11153 Restore the config/database.yml backed up to config/database.yml.cucumber.bak in the db:config:restore task, which is made a dependency of the environment rake task so that config/database.yml is restored before Rails tries to use it in the environment task. This specifically, allows for rake cucumber to be interrupted when the config/database.yml has been moved to config/database.yml.cucumber.bak and a subsequence rake cucumber to succeed and restore config/database.yml, but any task that depends on environment will restore the config/database.yml.
1 parent 7a8d7a3 commit 20177c7

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

features/step_definitions/project.rb

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,14 @@
1-
project_database_yaml_path = Rails.root.join('config', 'database.yml').to_path
2-
backup_project_database_yaml_path = "#{project_database_yaml_path}.cucumber.bak"
3-
4-
Before do
5-
if File.exist?(backup_project_database_yaml_path)
6-
File.delete(backup_project_database_yaml_path)
7-
end
8-
end
1+
require 'metasploit/framework/database/cucumber'
92

103
Given /^the project "database.yml" does not exist$/ do
11-
if File.exist?(project_database_yaml_path)
12-
File.rename(project_database_yaml_path, backup_project_database_yaml_path)
13-
end
4+
Metasploit::Framework::Database::Cucumber.backup_project_configurations
145
end
156

167
Given /^the project "database.yml" exists with:$/ do |file_content|
17-
if File.exist?(project_database_yaml_path)
18-
File.rename(project_database_yaml_path, backup_project_database_yaml_path)
19-
end
20-
21-
write_file(project_database_yaml_path, file_content)
8+
Metasploit::Framework::Database::Cucumber.backup_project_configurations
9+
write_file(Metasploit::Framework::Database::Cucumber.project_configurations_path, file_content)
2210
end
2311

2412
After do
25-
if File.exist?(backup_project_database_yaml_path)
26-
File.rename(backup_project_database_yaml_path, project_database_yaml_path)
27-
end
13+
Metasploit::Framework::Database::Cucumber.restore_project_configurations
2814
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'metasploit/framework/database'
2+
3+
module Metasploit::Framework::Database::Cucumber
4+
def self.project_configurations_path
5+
Rails.root.join('config', 'database.yml').to_path
6+
end
7+
8+
def self.backup_project_configurations
9+
if File.exist?(project_configurations_path)
10+
# assume that the backup file is from a previously aborted run and it contains the real database.yml data, so
11+
# just delete the fake database.yml and the After hook will restore the real database.yml from the backup location
12+
if File.exist?(backup_project_configurations_path)
13+
File.delete(project_configurations_path)
14+
else
15+
# project contains the real database.yml and there was no previous, aborted run.
16+
File.rename(project_configurations_path, backup_project_configurations_path)
17+
end
18+
end
19+
end
20+
21+
def self.backup_project_configurations_path
22+
"#{project_configurations_path}.cucumber.bak"
23+
end
24+
25+
def self.restore_project_configurations
26+
if File.exist?(backup_project_configurations_path)
27+
if File.exist?(project_configurations_path)
28+
# Remove fake, leftover database.yml
29+
File.delete(project_configurations_path)
30+
end
31+
32+
File.rename(backup_project_configurations_path, project_configurations_path)
33+
end
34+
end
35+
end
36+

lib/tasks/cucumber.rake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ begin
5454
task 'db:test:prepare' do
5555
end
5656

57+
task 'db:config:restore' do
58+
require 'metasploit/framework/database/cucumber'
59+
Metasploit::Framework::Database::Cucumber.restore_project_configurations
60+
end
61+
62+
# Restore the config/database.yml from config/database.cucumber.yml before attempting to copy development to test
63+
# database in order to recover from interrupted cucumber runs
64+
task 'environment' => 'db:config:restore'
65+
5766
task :stats => 'cucumber:statsetup'
5867
rescue LoadError
5968
desc 'cucumber rake task not available (cucumber not installed)'

0 commit comments

Comments
 (0)