52 lines
1.5 KiB
Ruby
52 lines
1.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe Hledger::Runner do
|
|
def runner(path:, timeout: 5, max_output: 1_000)
|
|
described_class.new(path: path, timeout: timeout, max_output: max_output)
|
|
end
|
|
|
|
it "captures stdout and exit status" do
|
|
result = runner(path: "/bin/echo").execute(["hello"])
|
|
|
|
expect(result).to be_success
|
|
expect(result.stdout).to eq("hello\n")
|
|
expect(result.exit_status).to eq(0)
|
|
end
|
|
|
|
it "reports failure for a non-zero exit" do
|
|
expect(runner(path: "/bin/false").execute([])).not_to be_success
|
|
end
|
|
|
|
it "kills a process that exceeds the timeout" do
|
|
result = runner(path: "/bin/sleep", timeout: 1).execute(["5"])
|
|
|
|
expect(result.timed_out).to be(true)
|
|
expect(result).not_to be_success
|
|
end
|
|
|
|
it "caps captured output" do
|
|
result = runner(path: "/bin/sh", max_output: 10).execute(["-c", "printf '%0.sa' {1..100}"])
|
|
|
|
expect(result.stdout.bytesize).to be <= 10
|
|
end
|
|
|
|
it "returns a failure when the binary is missing" do
|
|
expect(runner(path: "/nonexistent/hledger").execute([])).not_to be_success
|
|
end
|
|
|
|
it "feeds stdin to the process" do
|
|
result = runner(path: "/bin/cat").execute([], stdin_data: "journal")
|
|
|
|
expect(result.stdout).to eq("journal")
|
|
end
|
|
|
|
it "does not inherit the parent environment" do
|
|
ENV["HLEDGER_SECRET"] = "leak"
|
|
result = runner(path: "/usr/bin/env").execute([])
|
|
|
|
expect(result.stdout).not_to include("HLEDGER_SECRET")
|
|
ensure
|
|
ENV.delete("HLEDGER_SECRET")
|
|
end
|
|
end
|