Skip to content
This repository was archived by the owner on Oct 27, 2025. It is now read-only.

Commit db99f1c

Browse files
committed
Add export:live_document(s)_and_assets rake tasks
I've created two rake tasks: 1. `export:live_document_and_assets`, which exports a single document, given its content ID. By default it outputs to STDOUT but will write to a JSON file if a path is provided. 2. `export:live_documents_and_assets`, which exports all of the live documents and assets. By default it outputs to STDOUT but will write a separate JSON file for each exported document if an output directory is provided. Usage (respectively): ``` rake export:live_document_and_assets["4c2ff601-4494-458b-bdc3-7d358122c2ae","./chris/foo.json"] ``` ``` rake export:live_documents_and_assets["./chris"] ```
1 parent 7dfa2bb commit db99f1c

2 files changed

Lines changed: 106 additions & 7 deletions

File tree

lib/tasks/export.rake

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1+
require "json"
2+
13
namespace :export do
2-
desc "Export live documents and assets"
3-
task live_documents_and_assets: :environment do
4+
desc "Export a specific live document and its assets, by its content ID"
5+
task :live_document_and_assets, %i[content_id output_file] => :environment do |_, args|
6+
document = Document.find_by(content_id: args[:content_id])
7+
hash = WhitehallMigration::DocumentExport.export_to_hash(document)
8+
9+
if args[:output_file]
10+
File.write(args[:output_file], JSON.pretty_generate(hash))
11+
else
12+
pp hash
13+
end
14+
end
15+
16+
desc "Export all live documents and assets"
17+
task :live_documents_and_assets, %i[output_directory] => :environment do |_, args|
418
documents = WhitehallMigration::DocumentExport.exportable_documents
519

620
puts "Exporting #{documents.count} live editions"
721

8-
# TODO: delete the override below. We're temporarily only looking
9-
# at a pre-selected few for testing.
10-
documents = [Document.find_by(content_id: "4c2ff601-4494-458b-bdc3-7d358122c2ae")]
1122
documents.each do |document|
12-
pp WhitehallMigration::DocumentExport.export_to_hash(document)
23+
hash = WhitehallMigration::DocumentExport.export_to_hash(document)
24+
25+
if args[:output_directory]
26+
File.write("#{args[:output_directory]}/#{hash[:base_path].split("/").last}.json", JSON.pretty_generate(hash))
27+
else
28+
pp hash
29+
end
1330
end
14-
# TODO: think about how to avoid triggering an update to subscribers when republishing these docs.
1531
end
1632
end

spec/lib/tasks/export_spec.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
RSpec.describe "Export tasks" do
2+
include ActiveJob::TestHelper
3+
4+
describe "export:live_document_and_assets" do
5+
before do
6+
Rake::Task["export:live_document_and_assets"].reenable
7+
end
8+
9+
it "calls WhitehallMigration::DocumentExport.export_to_hash with correct arguments" do
10+
document = create(:document, :with_live_edition)
11+
allow(WhitehallMigration::DocumentExport).to receive(:export_to_hash)
12+
Rake::Task["export:live_document_and_assets"].invoke(document.content_id)
13+
expect(WhitehallMigration::DocumentExport).to have_received(:export_to_hash).with(document)
14+
end
15+
16+
it "pretty-prints the result to STDOUT if no output_file is specified" do
17+
document = create(:document, :with_live_edition)
18+
allow(WhitehallMigration::DocumentExport).to receive(:export_to_hash).and_return({ foo: "bar" })
19+
expect { Rake::Task["export:live_document_and_assets"].invoke(document.content_id) }.to output("{:foo=>\"bar\"}\n").to_stdout
20+
end
21+
22+
it "writes the result as JSON to the given output_file if specified" do
23+
document = create(:document, :with_live_edition)
24+
allow(WhitehallMigration::DocumentExport).to receive(:export_to_hash).and_return({ foo: "bar", baz: "qux" })
25+
26+
output_file = Tempfile.new("export")
27+
Rake::Task["export:live_document_and_assets"].invoke(document.content_id, output_file.path)
28+
29+
expected = <<~JSON
30+
{
31+
"foo": "bar",
32+
"baz": "qux"
33+
}
34+
JSON
35+
expect(File.read(output_file.path)).to match(expected.strip)
36+
end
37+
end
38+
39+
describe "export:live_documents_and_assets" do
40+
before do
41+
allow($stdout).to receive(:puts) # suppress output for cleanliness
42+
Rake::Task["export:live_documents_and_assets"].reenable
43+
44+
# create 3 documents
45+
Document.find_each(&:destroy) # Clean slate
46+
@documents = [
47+
create(:document, :with_live_edition),
48+
create(:document, :with_live_edition),
49+
create(:document, :with_live_edition),
50+
]
51+
allow(WhitehallMigration::DocumentExport).to receive(:exportable_documents).and_return(@documents)
52+
end
53+
54+
it "lists how many documents it is about to export" do
55+
expect { Rake::Task["export:live_documents_and_assets"].invoke }.to output(/^Exporting 3 live editions/).to_stdout
56+
end
57+
58+
it "calls WhitehallMigration::DocumentExport.export_to_hash with correct arguments" do
59+
allow(WhitehallMigration::DocumentExport).to receive(:export_to_hash)
60+
Rake::Task["export:live_documents_and_assets"].invoke
61+
expect(WhitehallMigration::DocumentExport).to have_received(:export_to_hash).with(@documents[0])
62+
expect(WhitehallMigration::DocumentExport).to have_received(:export_to_hash).with(@documents[1])
63+
expect(WhitehallMigration::DocumentExport).to have_received(:export_to_hash).with(@documents[2])
64+
end
65+
66+
it "pretty-prints the result to STDOUT if no output_directory is specified" do
67+
allow(WhitehallMigration::DocumentExport).to receive(:export_to_hash).and_return({ foo: "bar" })
68+
expect { Rake::Task["export:live_documents_and_assets"].invoke }.to output(/{:foo=>"bar"}\n{:foo=>"bar"}\n{:foo=>"bar"}\n$/).to_stdout
69+
end
70+
71+
it "writes the result as JSON files to the given output_directory if specified" do
72+
allow(WhitehallMigration::DocumentExport).to receive(:export_to_hash) do |document|
73+
{ base_path: "/news/example-path-#{document.id}" }
74+
end
75+
output_directory = Dir.mktmpdir
76+
Rake::Task["export:live_documents_and_assets"].invoke(output_directory)
77+
78+
expected_files = @documents.map { |doc| "#{output_directory}/example-path-#{doc.id}.json" }
79+
actual_files = Dir.glob("#{output_directory}/*.json").sort
80+
expect(actual_files).to match_array(expected_files)
81+
end
82+
end
83+
end

0 commit comments

Comments
 (0)