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
|
# frozen_string_literal: true
require "guard/internals/debugging"
RSpec.describe Guard::Internals::Debugging do
let(:null) { IO::NULL }
let(:ui) { class_double(::Guard::UI) }
let(:tracing) { class_spy(::Guard::Internals::Tracing) }
before do
stub_const("::Guard::Internals::Tracing", tracing)
stub_const("::Guard::UI", ui)
allow(ui).to receive(:debug)
allow(ui).to receive(:level=)
allow(Thread).to receive(:abort_on_exception=)
end
after do
described_class.send(:_reset)
end
describe "#start" do
it "traces Kernel.system" do
expect(tracing).to receive(:trace).with(Kernel, :system) do |*_, &block|
expect(ui).to receive(:debug).with("Command execution: foo")
block.call "foo"
end
described_class.start
end
it "traces Kernel.`" do
expect(tracing).to receive(:trace).with(Kernel, :`) do |*_, &block|
expect(ui).to receive(:debug).with("Command execution: foo")
block.call("foo")
end
described_class.start
end
it "traces Open3.popen3" do
expect(tracing).to receive(:trace).with(Open3, :popen3) do |*_, &block|
expect(ui).to receive(:debug).with("Command execution: foo")
block.call("foo")
end
described_class.start
end
it "traces Kernel.spawn" do
expect(tracing).to receive(:trace).with(Kernel, :spawn) do |*_, &block|
expect(ui).to receive(:debug).with("Command execution: foo")
block.call("foo")
end
described_class.start
end
context "when not started" do
before { described_class.start }
it "sets logger to debug" do
expect(ui).to have_received(:level=).with(Logger::DEBUG)
end
it "makes threads abort on exceptions" do
expect(Thread).to have_received(:abort_on_exception=).with(true)
end
end
context "when already started" do
before do
allow(tracing).to receive(:trace)
described_class.start
end
it "does not set log level" do
expect(ui).to_not receive(:level=)
described_class.start
end
end
end
describe "#stop" do
context "when already started" do
before do
described_class.start
described_class.stop
end
it "sets logger level to info" do
expect(ui).to have_received(:level=).with(Logger::INFO)
end
it "untraces Kernel.system" do
expect(tracing).to have_received(:untrace).with(Kernel, :system)
end
it "untraces Kernel.`" do
expect(tracing).to have_received(:untrace).with(Kernel, :`)
end
it "untraces Open3.popen3" do
expect(tracing).to have_received(:untrace).with(Kernel, :popen3)
end
end
context "when not started" do
it "does not set logger level" do
described_class.stop
expect(ui).to_not have_received(:level=)
end
end
end
end
|