1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
# frozen_string_literal: true
describe TestProf do
describe "#artifact_path" do
before do
described_class.config.output_dir = "tmp/test"
end
after { FileUtils.rmdir(described_class.config.output_dir) if File.directory?(described_class.config.output_dir) }
context "ensures creation of output_dir" do
subject { described_class.artifact_path("c.html") }
it { expect { subject }.to(change { File.exist?(described_class.config.output_dir) }) }
end
context "without timestamps" do
before { described_class.config.timestamps = false }
it { expect(described_class.artifact_path("c.html")).to eq "tmp/test/c.html" }
end
context "with timestamps" do
before do
described_class.config.timestamps = true
expect(described_class).to receive(:now).and_return(double("now", to_i: 123_454_321))
end
it { expect(described_class.artifact_path("c.html")).to eq "tmp/test/c-123454321.html" }
it { expect(described_class.artifact_path("c")).to eq "tmp/test/c-123454321" }
end
context "with report suffix" do
before do
described_class.config.report_suffix = "run-1"
end
it { expect(described_class.artifact_path("c.html")).to eq "tmp/test/c-run-1.html" }
it { expect(described_class.artifact_path("c")).to eq "tmp/test/c-run-1" }
end
context "with report suffix and timestamps" do
before do
described_class.config.report_suffix = "run-2"
described_class.config.timestamps = true
expect(described_class).to receive(:now).and_return(double("now", to_i: 123_454_321))
end
it { expect(described_class.artifact_path("c.html")).to eq "tmp/test/c-run-2-123454321.html" }
it { expect(described_class.artifact_path("c")).to eq "tmp/test/c-run-2-123454321" }
end
end
describe "#require" do
context "when second argument omitted" do
context "when Kernel.require fails" do
it "returns false without logging" do
allow(Kernel).to receive(:require).with("non-existent").and_raise(LoadError)
allow(described_class).to receive(:require).with("non-existent")
.and_call_original
expect(described_class.require("non-existent")).to eq false
expect(described_class).not_to receive(:log).with(:error, nil)
described_class.require("non-existent", nil)
end
end
context "when Kernel.require succeeds" do
context "when block given" do
it "yields block" do
allow(Kernel).to receive(:require).with("something").and_return(true)
allow(described_class).to receive(:require).with("something") { 2 + 2 }
.and_call_original
expect(described_class.require("something") { 2 + 2 }).to eq 4
end
end
context "when no block given" do
it "returns true" do
allow(Kernel).to receive(:require).with("something").and_return(true)
allow(described_class).to receive(:require).with("something").and_call_original
expect(described_class.require("something")).to eq true
end
end
end
end
context "when two arguments supplied" do
context "when Kernel.require fails" do
it "returns false with log output" do
allow(Kernel).to receive(:require).with("non-existent").and_raise(LoadError)
allow(described_class).to receive(:require).with("non-existent", "message")
.and_call_original
expect(described_class.require("non-existent", "message")).to eq false
expect(described_class).to receive(:log).with(:error, "message")
described_class.require("non-existent", "message")
end
end
context "when Kernel.require succeeds" do
context "when block given" do
it "yields block" do
allow(Kernel).to receive(:require).with("something").and_return(true)
allow(described_class).to receive(:require).with("something", "message") { 2 + 2 }
.and_call_original
expect(described_class.require("something", "message") { 2 + 2 }).to eq 4
end
end
context "when no block given" do
it "returns true" do
allow(Kernel).to receive(:require).with("something").and_return(true)
allow(described_class).to receive(:require).with("something").and_call_original
expect(described_class.require("something")).to eq true
end
end
end
end
end
end
|