Skip to content

Commit

Permalink
Chore: Update UAT digest Rake task exclusion
Browse files Browse the repository at this point in the history
It became problematic to test the digest on UAT branches when
you have to comment out code that is tested...

This allows a dev to run the task with a `BYPASS=` ENVVAR to
get a branch to run the export on demand without additional
code changes

It also outputs a warning to guide us when it occurs
  • Loading branch information
colinbruce committed Nov 11, 2024
1 parent 6c89746 commit 84924a2
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 16 deletions.
23 changes: 20 additions & 3 deletions lib/tasks/digest.rake
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
def warning_message(task_name)
<<~WARNING.freeze.red
Digest rake tasks are deactivated on UAT environments, call with
BYPASS=true rake digest:#{task_name}
To bypass the UAT block
WARNING
end

namespace :digest do
desc "Run DigestExtractor to update application digests"
task extract: :environment do
next if HostEnv.uat?
if !HostEnv.staging_or_production? && ENV.fetch("BYPASS", nil).nil?
Rails.logger.info warning_message("extract")
next
end

DigestExtractor.call
end

desc "Run DigestExporter to export digest records to google sheet"
task export: :environment do
next if HostEnv.uat?
if !HostEnv.staging_or_production? && ENV.fetch("BYPASS", nil).nil?
Rails.logger.info warning_message("export")
next
end

DigestExporter.call
end

namespace :extraction_date do
desc "Reset the last_extracted date so that all application_digest records are refreshed"
task reset: :environment do
next if HostEnv.uat?
if !HostEnv.staging_or_production? && ENV.fetch("BYPASS", nil).nil?
Rails.logger.info warning_message("extraction_date")
next
end

Setting.setting.update!(digest_extracted_at: 20.years.ago)
end
Expand Down
86 changes: 73 additions & 13 deletions spec/lib/tasks/digest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
RSpec.describe "digest:", type: :task do
before do
Rails.application.load_tasks if Rake::Task.tasks.empty?
allow(HostEnv).to receive(:uat?).and_return(false)
allow(HostEnv).to receive(:staging_or_production?).and_return(staging_or_production)
allow(Rails.logger).to receive(:info)
end

let(:staging_or_production) { true }

describe "extract" do
subject(:task) { Rake::Task["digest:extract"] }

Expand All @@ -19,11 +22,30 @@
end

context "when host env is uat" do
before { allow(HostEnv).to receive(:uat?).and_return(true) }
before do
allow(ENV).to receive(:fetch)
allow(ENV).to receive(:fetch).with("BYPASS", nil).and_return(bypass)
end

let(:staging_or_production) { false }

context "and the BYPASS value is missing" do
let(:bypass) { nil }

it "does not call the service" do
task.execute
expect(DigestExtractor).not_to have_received(:call)
expect(Rails.logger).to have_received(:info).once
end
end

context "and the BYPASS value is present" do
let(:bypass) { "true" }

it "does not the service" do
task.execute
expect(DigestExtractor).not_to have_received(:call)
it "calls the service" do
task.execute
expect(DigestExtractor).to have_received(:call)
end
end
end
end
Expand All @@ -41,11 +63,30 @@
end

context "when host env is uat" do
before { allow(HostEnv).to receive(:uat?).and_return(true) }
before do
allow(ENV).to receive(:fetch)
allow(ENV).to receive(:fetch).with("BYPASS", nil).and_return(bypass)
end

let(:staging_or_production) { false }

context "and the BYPASS value is missing" do
let(:bypass) { nil }

it "does not call the service" do
task.execute
expect(DigestExporter).not_to have_received(:call)
it "does not call the service" do
task.execute
expect(DigestExporter).not_to have_received(:call)
expect(Rails.logger).to have_received(:info).once
end
end

context "and the BYPASS value is present" do
let(:bypass) { "true" }

it "calls the service" do
task.execute
expect(DigestExporter).to have_received(:call)
end
end
end
end
Expand All @@ -66,11 +107,30 @@
end

context "when host env is uat" do
before { allow(HostEnv).to receive(:uat?).and_return(true) }
before do
allow(ENV).to receive(:fetch)
allow(ENV).to receive(:fetch).with("BYPASS", nil).and_return(bypass)
end

let(:staging_or_production) { false }

context "and the BYPASS value is missing" do
let(:bypass) { nil }

it "does not call the service" do
task.execute
expect(setting).not_to have_received(:update!)
expect(Rails.logger).to have_received(:info).once
end
end

context "and the BYPASS value is present" do
let(:bypass) { "true" }

it "does not call the service" do
task.execute
expect(setting).not_to have_received(:update!)
it "calls the service" do
task.execute
expect(setting).to have_received(:update!)
end
end
end
end
Expand Down

0 comments on commit 84924a2

Please sign in to comment.