From c00aaf4613eaa1d8ef6c5831efd592d33250bb30 Mon Sep 17 00:00:00 2001 From: Emiliano Della Casa Date: Wed, 17 Oct 2018 17:37:36 +0200 Subject: [PATCH] Issue #326 - supporting upgrade files and version --- .rubocop.yml | 2 ++ lib/zold/upgrades.rb | 2 +- lib/zold/version_file.rb | 27 ++++++++++++++++++++++----- test/test_upgrades.rb | 21 +++++++-------------- 4 files changed, 32 insertions(+), 20 deletions(-) mode change 100644 => 100755 lib/zold/upgrades.rb mode change 100644 => 100755 lib/zold/version_file.rb diff --git a/.rubocop.yml b/.rubocop.yml index 4beae1ce..1206509b 100755 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -31,5 +31,7 @@ Style/MultilineBlockChain: Enabled: false Layout/MultilineOperationIndentation: Enabled: false +Security/Eval: + Enabled: false Naming/UncommunicativeMethodParamName: MinNameLength: 1 diff --git a/lib/zold/upgrades.rb b/lib/zold/upgrades.rb old mode 100644 new mode 100755 index bcefc421..cb36b518 --- a/lib/zold/upgrades.rb +++ b/lib/zold/upgrades.rb @@ -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 diff --git a/lib/zold/version_file.rb b/lib/zold/version_file.rb old mode 100644 new mode 100755 index 44195835..46b64b87 --- a/lib/zold/version_file.rb +++ b/lib/zold/version_file.rb @@ -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 diff --git a/test/test_upgrades.rb b/test/test_upgrades.rb index aaa42e85..77d67a27 100755 --- a/test/test_upgrades.rb +++ b/test/test_upgrades.rb @@ -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) @@ -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 @@ -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 @@ -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)