Skip to content

Commit

Permalink
Check for version from last build and remove build directories if the…
Browse files Browse the repository at this point in the history
… versions does not match

fixes #52
  • Loading branch information
samnung committed Sep 15, 2018
1 parent 27f3101 commit 2aeea1a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
9 changes: 9 additions & 0 deletions lib/epuber/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,14 @@ def write_lockfile
Epuber::Config.instance.save_lockfile
end
end

def pre_build_checks
Config.instance.warn_for_outdated_versions!

if !Config.instance.same_version_as_last_run? && File.exist?(Config.instance.working_path)
UI.warning('Using different version of Epuber or Bade, removing all build caches')
Config.instance.remove_build_caches
end
end
end
end
2 changes: 1 addition & 1 deletion lib/epuber/command/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def validate!
verify_one_bookspec_exists!
verify_all_targets_exists!

Config.instance.warn_for_outdated_versions!
pre_build_checks
end

def run
Expand Down
2 changes: 1 addition & 1 deletion lib/epuber/command/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def validate!
super
verify_one_bookspec_exists!

Config.instance.warn_for_outdated_versions!
pre_build_checks
end

def run
Expand Down
12 changes: 12 additions & 0 deletions lib/epuber/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ def warn_for_outdated_versions!
end
end

def same_version_as_last_run?
!(bookspec_lockfile.epuber_version != Epuber::VERSION ||
bookspec_lockfile.bade_version.nil? ||
bookspec_lockfile.bade_version != Bade::VERSION)
end

def remove_build_caches
FileUtils.rm_rf(File.join(working_path, 'build_cache'))
FileUtils.rm_rf(File.join(working_path, 'build'))
FileUtils.rm_rf(File.join(working_path, 'metadata'))
end

# ---------------------------------------------------------------------------------------------------------------- #

class << self
Expand Down
53 changes: 46 additions & 7 deletions spec/unit/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ module Epuber
describe Config do
include FakeFS::SpecHelpers

it "warns if the previous version of Epuber is newer" do
before do
Config.clear_instance!
end

it "warns if the previous version of Epuber is newer" do
lockfile = {
'epuber_version' => '200000.0.0',
'bade_version' => Bade::VERSION,
Expand All @@ -24,8 +26,6 @@ module Epuber
end

it "warns if the previous version of Bade is newer" do
Config.clear_instance!

lockfile = {
'epuber_version' => Epuber::VERSION,
'bade_version' => '200000.0.0',
Expand All @@ -40,8 +40,6 @@ module Epuber
end

it "is backward compatible" do
Config.clear_instance!

lockfile = {
'version' => '0.1',
}
Expand All @@ -58,11 +56,52 @@ module Epuber
end

it "is working when there is no lockfile yet" do
Config.clear_instance!

File.write('some.bookspec', '')

expect(Config.instance.bookspec_lockfile.epuber_version).to_not be_nil
end

context '#diff_version_from_last_build?' do
it 'detects when using different version of Epuber or Bade' do
lockfile = {
'epuber_version' => '200000.0.0',
'bade_version' => '200000.0.0',
}

File.write('some.bookspec', '')
File.write('some.bookspec.lock', lockfile.to_yaml)

expect(Config.instance.same_version_as_last_run?).to be_falsey
end

it 'detects when using same versions of Epuber and Bade' do
lockfile = {
'epuber_version' => Epuber::VERSION,
'bade_version' => Bade::VERSION,
}

File.write('some.bookspec', '')
File.write('some.bookspec.lock', lockfile.to_yaml)

expect(Config.instance.same_version_as_last_run?).to be_truthy
end

it 'is backward compatible' do
lockfile = {
'version' => '0.1',
}

File.write('some.bookspec', '')
File.write('some.bookspec.lock', lockfile.to_yaml)

expect(Config.instance.same_version_as_last_run?).to be_falsey
end

it 'is working when there is no lockfile yet' do
File.write('some.bookspec', '')

expect(Config.instance.same_version_as_last_run?).to_not be_falsey
end
end
end
end

0 comments on commit 2aeea1a

Please sign in to comment.