-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_integration_spec.rb
113 lines (94 loc) · 3.77 KB
/
cli_integration_spec.rb
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
require 'open3'
require 'timeout'
RSpec.describe "bin/check_please executable", :cli do
###############################################
## ##
## ## ## ##### ######## ######## ##
## ### ## ## ## ## ## ##
## #### ## ## ## ## ## ##
## ## ## ## ## ## ## ###### ##
## ## #### ## ## ## ## ##
## ## ### ## ## ## ## ##
## ## ## ##### ## ######## ##
## ##
###############################################
# NOTE: These tests are slow (relative to everything else).
# Please only add specs here for behavior that you can't possibly test any other way.
###############################################
specify "output of -h/--help" do
# NOTE: this spec is hand-rolled because I don't expect to have any more like
# it. I considered using the 'approvals' gem, but it drags in a dependency
# on Nokogiri that I wanted to avoid. If you do find yourself needing to do
# more specs like this, 'approvals' might be useful...
expected = fixture_file_contents("cli-help-output").rstrip
output = run_cli("--help").rstrip
begin
expect( output ).to eq( expected )
rescue RSpec::Expectations::ExpectationNotMetError => e
puts <<~EOF
--> NOTE: the output of the executable's `--help` flag has changed.
--> If you want to keep these changes, please run:
-->
--> bundle exec rake spec:approve_cli_help_output
EOF
raise e
end
end
context "for a ref/can pair with a few discrepancies" do
let(:ref_file) { "spec/fixtures/forty-two-reference.json" }
let(:can_file) { "spec/fixtures/forty-two-candidate.json" }
let(:expected_output) { fixture_file_contents("forty-two-expected-table").strip }
describe "running the executable with two filenames" do
it "produces tabular output" do
output = run_cli(ref_file, can_file)
expect( output ).to eq( expected_output )
end
specify "adding `--fail-fast` limits output to one row" do
output = run_cli(ref_file, can_file, "--fail-fast")
expect( output.lines.length ).to be < expected_output.lines.length
end
end
describe "running the executable with one filename" do
it "reads the candidate from piped stdin" do
output = run_cli(ref_file, pipe: can_file)
expect( output ).to eq( expected_output )
end
specify "prints help and exits if the user didn't pipe anything in" do
output = run_cli(ref_file)
expect( output ).to include( CheckPlease::ELEVATOR_PITCH )
end
end
describe "running the executable with no arguments" do
specify "prints a message about the missing reference and exits" do
output = run_cli()
expect( output ).to include( CheckPlease::ELEVATOR_PITCH )
expect( output ).to_not include( "Missing <reference>" )
end
end
end
context "for a ref/can pair with two simple objects in reverse order" do
let(:ref_file) { "spec/fixtures/match-by-key-reference.json" }
let(:can_file) { "spec/fixtures/match-by-key-candidate.json" }
specify "--match-by-key works end to end" do
output = run_cli(ref_file, can_file, "--match-by-key", "/:id")
expect( output ).to be_empty
end
end
TIME_OUT_CLI_AFTER = 1 # seconds
def run_cli(*args, pipe: nil)
args.flatten!
cmd = []
if pipe
cmd << "cat"
cmd << pipe
cmd << "|"
end
cmd << "exe/check_please"
cmd.concat args
out = nil # scope hack
Timeout.timeout(TIME_OUT_CLI_AFTER) do
out = `#{cmd.compact.join(' ')}`
end
strip_trailing_whitespace(out)
end
end