Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #326 - supporting upgrade files and version #496

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ Style/MultilineBlockChain:
Enabled: false
Layout/MultilineOperationIndentation:
Enabled: false
Security/Eval:
Enabled: false
Naming/UncommunicativeMethodParamName:
MinNameLength: 1
2 changes: 1 addition & 1 deletion lib/zold/upgrades.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def initialize(version, directory, log: Log::Verbose.new)
end

def run
Dir.glob("#{@directory}/*.rb").select { |f| f =~ /^(\d+)\.rb$/ }.sort.each do |script|
Dir.glob("#{@directory}/*.rb").select { |f| f =~ /\d\.\d\.\d\.rb/ }.sort.each do |script|
@version.apply(script)
end
end
Expand Down
27 changes: 22 additions & 5 deletions lib/zold/version_file.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,28 @@ def initialize(path, log: Log::Verbose.new)
@log = log
end

# @todo #285:30min Replace this stub with functionality.
# We need to run the script (`yield`) if the version of
# the script is between the saved version and the current one.
def apply(version)
@log.info("Version #{version} doesn't need to be applied.")
def apply(script)
version = extract_version_from_script_name(script)
return unless ::Gem::Version.new(version) > current_version
code = File.read(script)
eval(code)
end

def extract_version_from_script_name(file_name)
file_name.scan(/\d\.\d\.\d/).last
end

def current_version
return ::Gem::Version.new('0.0.0') unless File.exist?(version_file)
@current_version ||= ::Gem::Version.new(current_version_from_version_file)
end

def current_version_from_version_file
File.read(version_file).strip
end

def version_file
File.join(@path, 'version')
end
end
end
21 changes: 7 additions & 14 deletions test/test_upgrades.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
# Copyright:: Copyright (c) 2018 Yegor Bugayenko
# License:: MIT
class TestUpgrades < Minitest::Test
# @todo #327:30min Uncomment, when you're ready to work on upgrade manager's
# test case of absent version file. Start with running the test first.
def test_no_version_file_is_ok
skip
Dir.mktmpdir do |dir|
script_version = '0.0.1'
create_upgrade_file(dir, script_version)
Expand All @@ -43,17 +40,13 @@ def test_no_version_file_is_ok
end
end

# @todo #327:30min Uncomment, when you're ready to work on upgrade manager's
# test case of running only pending upgrade scripts (i.e. the scripts with
# versions greater than those in the version file).
def test_pending_scripts_run
skip
Dir.mktmpdir do |dir|
%w[1 2].each do |script_version|
create_upgrade_file(dir, script_version)
create_upgrade_file(dir, "0.0.#{script_version}")
end
create_version_file(dir, '1')
assert_output(/#{expected_upgrade_script_output('2')}/) do
create_version_file(dir, '0.0.1')
assert_output(/#{expected_upgrade_script_output('0.0.2')}/) do
run_upgrades(dir)
end
end
Expand All @@ -62,13 +55,13 @@ def test_pending_scripts_run
def test_already_ran_scripts_dont_run
Dir.mktmpdir do |dir|
%w[1 2].each do |script_version|
create_upgrade_file(dir, script_version)
create_upgrade_file(dir, "0.0.#{script_version}")
end
create_version_file(dir, '1')
create_version_file(dir, '0.0.1')
out, _err = capture_io do
run_upgrades(dir)
end
refute_match(/#{expected_upgrade_script_output('1')}/, out)
refute_match(/#{expected_upgrade_script_output('0.0.1')}/, out)
end
end

Expand All @@ -79,7 +72,7 @@ def run_upgrades(dir)
end

def version_file(dir)
Zold::VersionFile.new(File.join(dir, 'version'))
Zold::VersionFile.new(dir)
end

def create_version_file(dir, version)
Expand Down