Skip to content

Commit 130aa46

Browse files
committed
feat: Small improvement to rake tasks
1 parent 356ef3e commit 130aa46

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

lib/tasks/active_storage_db_tasks.rake

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ module ActiveStorage
99
end
1010

1111
def print_blob(blob, digits: 0)
12-
size = (blob.byte_size / 1024).to_s.rjust(7)
12+
size = if blob.byte_size < 1024
13+
"#{blob.byte_size}B".rjust(8)
14+
else
15+
"#{blob.byte_size / 1024}K".rjust(8)
16+
end
1317
date = blob.created_at.strftime("%Y-%m-%d %H:%M")
14-
puts "#{size}K #{date} #{blob.id.to_s.rjust(digits)} #{blob.filename}"
18+
puts "#{size} #{date} #{blob.id.to_s.rjust(digits)} #{blob.filename}"
1519
end
1620
end
1721
end

spec/tasks/active_storage_db_tasks_spec.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
describe "asdb:list" do
77
subject(:task) { execute_task("asdb:list") }
88

9-
let!(:first_file) { create(:active_storage_blob, filename: "some file 1", created_at: 1.hour.ago) }
10-
let!(:second_file) { create(:active_storage_blob, filename: "some file 2", created_at: 5.hours.ago) }
11-
let!(:third_file) { create(:active_storage_blob, filename: "some file 3", created_at: 3.hours.ago) }
9+
let!(:first_file) { create(:active_storage_blob, filename: "some file 1") }
10+
let!(:second_file) { create(:active_storage_blob, filename: "some file 2") }
11+
let!(:third_file) { create(:active_storage_blob, filename: "some file 3") }
1212

1313
it "prints the columns header + the list of 3 files", :aggregate_failures do
1414
pattern = /#{third_file.id} #{third_file.filename}.+#{second_file.id} #{second_file.filename}.+#{first_file.id} #{first_file.filename}/m
@@ -29,10 +29,6 @@
2929
context "with a missing source" do
3030
subject(:task) { execute_task("asdb:download", blob_id: "some_file") }
3131

32-
before do
33-
allow(File).to receive(:writable?).and_return(true)
34-
end
35-
3632
it "exits showing a not found error" do
3733
with_captured_stderr do
3834
expect { task }.to raise_exception(SystemExit, /Source file not found/)
@@ -46,7 +42,7 @@
4642
let(:blob) { build_stubbed(:active_storage_blob) }
4743

4844
before do
49-
allow(File).to receive(:writable?).and_return(false)
45+
allow(File).to receive(:writable?).with(".").and_return(false)
5046
allow(ActiveStorage::Blob).to receive(:find_by).and_return(blob)
5147
end
5248

@@ -63,13 +59,16 @@
6359
let(:blob) { build_stubbed(:active_storage_blob) }
6460

6561
before do
66-
allow(File).to receive_messages(binwrite: 1000, writable?: true)
62+
allow(File).to receive_messages(binwrite: 1000)
63+
allow(File).to receive(:writable?).with(".").and_return(true)
6764
allow(ActiveStorage::Blob).to receive(:find_by).and_return(blob)
6865
allow(blob).to receive(:download).and_return("some data")
6966
end
7067

71-
it "prints the number of bytes written" do
68+
it "downloads the blob and writes it to the destination", :aggregate_failures do
7269
expect(task).to eq "1000 bytes written - some_path\n"
70+
expect(blob).to have_received(:download)
71+
expect(File).to have_received(:binwrite).with("some_path", "some data")
7372
end
7473
end
7574
end
@@ -93,13 +92,13 @@
9392
end
9493
end
9594

96-
context "with there are some results" do
95+
context "when there are some results" do
9796
subject(:task) { execute_task("asdb:search", filename: "just ") }
9897

9998
before do
100-
create(:active_storage_blob, filename: "just a file", created_at: 1.hour.ago)
101-
create(:active_storage_blob, filename: "just another file", created_at: 5.hours.ago)
102-
create(:active_storage_blob, filename: "the last file", created_at: 3.hours.ago)
99+
create(:active_storage_blob, filename: "just a file")
100+
create(:active_storage_blob, filename: "just another file")
101+
create(:active_storage_blob, filename: "the last file")
103102
end
104103

105104
it "prints the files that matches", :aggregate_failures do

0 commit comments

Comments
 (0)