|
| 1 | +require "spec_helper" |
| 2 | +require "cc/engine/duplication" |
| 3 | + |
| 4 | +module CC::Engine::Analyzers |
| 5 | + RSpec.describe CommandLineRunner do |
| 6 | + describe "#run" do |
| 7 | + it "runs the command on the input and yields the output" do |
| 8 | + runner = CommandLineRunner.new("cat; echo hi") |
| 9 | + |
| 10 | + output = runner.run("oh ") { |o| o } |
| 11 | + |
| 12 | + expect(output).to eq "oh hi\n" |
| 13 | + end |
| 14 | + |
| 15 | + |
| 16 | + it "raises on errors" do |
| 17 | + runner = CommandLineRunner.new("echo error output >&2; false") |
| 18 | + |
| 19 | + expect { runner.run("") }.to raise_error( |
| 20 | + ParserError, /code 1:\nerror output/ |
| 21 | + ) |
| 22 | + end |
| 23 | + |
| 24 | + it "times out commands" do |
| 25 | + runner = CommandLineRunner.new("sleep 3", 0.01) |
| 26 | + |
| 27 | + expect { runner.run("") }.to raise_error(Timeout::Error) |
| 28 | + end |
| 29 | + |
| 30 | + context "when Open3 returns a nil status" do |
| 31 | + it "accepts it if the output parses as JSON" do |
| 32 | + runner = CommandLineRunner.new("") |
| 33 | + |
| 34 | + allow(Open3).to receive(:capture3).and_return(["{\"type\":\"issue\"}", "", nil]) |
| 35 | + |
| 36 | + output = runner.run("") { |o| o } |
| 37 | + expect(output).to eq "{\"type\":\"issue\"}" |
| 38 | + end |
| 39 | + |
| 40 | + it "raises if the output was not valid JSON" do |
| 41 | + runner = CommandLineRunner.new("") |
| 42 | + |
| 43 | + allow(Open3).to receive(:capture3).and_return(["", "error output", nil]) |
| 44 | + |
| 45 | + expect { runner.run("") }.to raise_error( |
| 46 | + ParserError, /code 1:\nerror output/ |
| 47 | + ) |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | +end |
0 commit comments