From 1ef9fadf695f5711493b0fd09f718827aa9e680e Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Wed, 11 Jan 2023 21:12:13 +0100 Subject: [PATCH] add specs for version bump rake task --- lib/tasks/.keep | 0 spec/integration/generated_locale_spec.rb | 5 +- spec/integration/version_bump_spec.rb | 84 +++++++++++++++++++++++ spec/support/load_rake_tasks.rb | 7 ++ 4 files changed, 93 insertions(+), 3 deletions(-) delete mode 100644 lib/tasks/.keep create mode 100644 spec/integration/version_bump_spec.rb create mode 100644 spec/support/load_rake_tasks.rb diff --git a/lib/tasks/.keep b/lib/tasks/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/spec/integration/generated_locale_spec.rb b/spec/integration/generated_locale_spec.rb index 00e65d41..64246680 100644 --- a/spec/integration/generated_locale_spec.rb +++ b/spec/integration/generated_locale_spec.rb @@ -1,14 +1,13 @@ # frozen_string_literal: true require "rails_helper" +require "support/load_rake_tasks" describe "test locale generation" do - before(:context) { Rails.application.load_tasks } - it "succeeds" do allow($stdout).to receive(:puts) - Rake::Task["locale:generate"].invoke + Rake::Task["locale:generate"].execute %w[activerecord controllers errors frontend views voc].each do |locale_type| expect($stdout).to have_received(:puts).with a_string_including("generating #{locale_type}.en-xx.yml") diff --git a/spec/integration/version_bump_spec.rb b/spec/integration/version_bump_spec.rb new file mode 100644 index 00000000..656c69e6 --- /dev/null +++ b/spec/integration/version_bump_spec.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +require "rails_helper" +require "support/load_rake_tasks" + +describe "version bumping tasks" do + include ActiveSupport::Testing::TimeHelpers + + describe "version:bump" do + around do |example| + # make a backup of our version file + version_path = Rails.root.join("lib/retrospring/version.rb") + version_contents = File.read(version_path) + example.run + ensure + # restore our version file + File.write(version_path, version_contents) + end + + it "updates the version if the version date differs" do + travel_to(Time.utc(2069, 6, 21, 13, 37, 0)) + allow($stdout).to receive(:puts) + + Rake::Task["version:bump"].execute + + expect($stdout).to have_received(:puts).with "New version: 2069.0621.0" + travel_back + end + + context "when version is already from today" do + before do + allow(Retrospring::Version).to receive(:year).and_return 2069 + allow(Retrospring::Version).to receive(:month).and_return 6 + allow(Retrospring::Version).to receive(:day).and_return 21 + allow(Retrospring::Version).to receive(:patch).and_return 0 + + # just stubbing the constant is not enough, we need to stub away the + # reading of the version file too; otherwise only the patch value is + # updated + version_path = Rails.root.join("lib/retrospring/version.rb") + version_contents = File.read(version_path) + .sub!(/def year = .+/, "def year = 2069") + .sub!(/def month = .+/, "def month = 6") + .sub!(/def day = .+/, "def day = 21") + .sub!(/def patch = .+/, "def patch = 0") + allow(File).to receive(:read).with(version_path).and_return(version_contents) + end + + it "updates the version if the version date differs" do + travel_to(Time.utc(2069, 6, 21, 13, 37, 0)) + allow($stdout).to receive(:puts) + + Rake::Task["version:bump"].execute + + expect($stdout).to have_received(:puts).with "Current version: 2069.0621.0" + expect($stdout).to have_received(:puts).with "New version: 2069.0621.1" + travel_back + end + end + end + + describe "version:commit" do + before do + allow(Retrospring::Version).to receive(:year).and_return 2069 + allow(Retrospring::Version).to receive(:month).and_return 6 + allow(Retrospring::Version).to receive(:day).and_return 21 + allow(Retrospring::Version).to receive(:patch).and_return 3 + end + + it "runs the correct git commands" do + allow($stdout).to receive(:puts) + version_path = Rails.root.join("lib/retrospring/version.rb") + + expect_any_instance_of(Rake::FileUtilsExt) + .to receive(:sh) + .with(%(git commit -m 'Bump version to 2069.0621.3' -- #{version_path.to_s.inspect})) + expect_any_instance_of(Rake::FileUtilsExt) + .to receive(:sh) + .with(%(git tag -a -m 'Bump version to 2069.0621.3' 2069.0621.3)) + + Rake::Task["version:commit"].execute + end + end +end diff --git a/spec/support/load_rake_tasks.rb b/spec/support/load_rake_tasks.rb new file mode 100644 index 00000000..bc079705 --- /dev/null +++ b/spec/support/load_rake_tasks.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +# ensure Rake tasks are only loaded once during the entire RSpec run. +# +# when using it from a `before(:context) { ... }` block this will load the +# tasks more than once and therefore run tasks more often than they should ... +Rails.application.load_tasks