kramdown-man 0.1.9 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/spec/cli_spec.rb ADDED
@@ -0,0 +1,223 @@
1
+ require 'spec_helper'
2
+ require 'kramdown/man/cli'
3
+
4
+ require 'tempfile'
5
+
6
+ describe Kramdown::Man::CLI do
7
+ describe "#initialize" do
8
+ it "must default #output to nil" do
9
+ expect(subject.output).to be(nil)
10
+ end
11
+
12
+ it "must initialize #option_parser" do
13
+ expect(subject.option_parser).to be_kind_of(OptionParser)
14
+ end
15
+ end
16
+
17
+ describe "#print_error" do
18
+ let(:error) { "error!" }
19
+
20
+ it "must print the program name and the error message to stderr" do
21
+ expect {
22
+ subject.print_error(error)
23
+ }.to output("#{described_class::PROGRAM_NAME}: #{error}#{$/}").to_stderr
24
+ end
25
+ end
26
+
27
+ describe "#print_backtrace" do
28
+ let(:exception) { RuntimeError.new("error!") }
29
+
30
+ it "must print the program name and the error message to stderr" do
31
+ expect {
32
+ subject.print_backtrace(exception)
33
+ }.to output(
34
+ %r{Oops! Looks like you've found a bug!
35
+ Please report the following text to: #{Regexp.escape(described_class::BUG_REPORT_URL)}
36
+
37
+ ```}m
38
+ ).to_stderr
39
+ end
40
+ end
41
+
42
+ describe "#option_parser" do
43
+ it do
44
+ expect(subject.option_parser).to be_kind_of(OptionParser)
45
+ end
46
+
47
+ describe "#parse" do
48
+ %w[-o --output].each do |flag|
49
+ context "when given #{flag}" do
50
+ let(:output) { 'man-page.1' }
51
+ let(:argv) { [flag, output] }
52
+
53
+ before { subject.option_parser.parse(argv) }
54
+
55
+ it "must set #output" do
56
+ expect(subject.output).to eq(output)
57
+ end
58
+ end
59
+ end
60
+
61
+ %w[-V --version].each do |flag|
62
+ context "when given #{flag}" do
63
+ let(:argv) { [flag] }
64
+
65
+ it "must print the program name and the Kramdown::Man::VERSION" do
66
+ expect(subject).to receive(:exit)
67
+
68
+ expect {
69
+ subject.option_parser.parse(argv)
70
+ }.to output("#{described_class::PROGRAM_NAME} #{Kramdown::Man::VERSION}#{$/}").to_stdout
71
+ end
72
+ end
73
+ end
74
+
75
+ %w[-h --help].each do |flag|
76
+ context "when given #{flag}" do
77
+ let(:argv) { [flag] }
78
+
79
+ it "must print the option paresr --help output" do
80
+ expect(subject).to receive(:exit)
81
+
82
+ expect {
83
+ subject.option_parser.parse(argv)
84
+ }.to output("#{subject.option_parser}").to_stdout
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ describe ".run" do
92
+ subject { described_class }
93
+
94
+ context "when Interrupt is raised" do
95
+ before do
96
+ expect_any_instance_of(described_class).to receive(:run).and_raise(Interrupt)
97
+ end
98
+
99
+ it "must exit with 130" do
100
+ expect(subject.run([])).to eq(130)
101
+ end
102
+ end
103
+
104
+ context "when Errno::EPIPE is raised" do
105
+ before do
106
+ expect_any_instance_of(described_class).to receive(:run).and_raise(Errno::EPIPE)
107
+ end
108
+
109
+ it "must exit with 0" do
110
+ expect(subject.run([])).to eq(0)
111
+ end
112
+ end
113
+ end
114
+
115
+ let(:man_dir) { File.join(__dir__,'..','man') }
116
+ let(:markdown_file) { File.join(man_dir,'kramdown-man.1.md') }
117
+
118
+ describe "#run" do
119
+ let(:version) { Kramdown::Man::VERSION }
120
+ let(:man_page_regex) do
121
+ /\A.\\" Generated by kramdown-man #{Regexp.escape(version)}\n/
122
+ end
123
+
124
+ context "when given a markdown file" do
125
+ let(:argv) { [markdown_file] }
126
+
127
+ context "and when the -o,--output option is also given" do
128
+ let(:tempfile) { Tempfile.new(['kramdown-man-', '.1']) }
129
+ let(:output) { tempfile.path }
130
+
131
+ let(:argv) { ['--output', output, markdown_file] }
132
+ before { subject.option_parser.parse(argv) }
133
+
134
+ it "must write the man page output to the output file" do
135
+ subject.run(markdown_file)
136
+
137
+ expect(File.read(output)).to match(man_page_regex)
138
+ end
139
+ end
140
+
141
+ context "but STDOUT is a TTY" do
142
+ before { expect($stdout).to receive(:tty?).and_return(true) }
143
+
144
+ it "must open the man page output using the `man` command" do
145
+ # TODO: need to test that IO.popen is actually being called
146
+ expect(subject).to receive(:view_man_page).with(String)
147
+
148
+ subject.run(argv)
149
+ end
150
+ end
151
+
152
+ context "but STDOUT is not a TTY" do
153
+ it "must print the man page to stdout" do
154
+ expect {
155
+ subject.run(argv)
156
+ }.to output(man_page_regex).to_stdout
157
+ end
158
+ end
159
+
160
+ context "but the given markdown file does not exist" do
161
+ let(:markdown_file) { 'does/not/exist.md' }
162
+
163
+ it "must print an error and return -1" do
164
+ expect {
165
+ expect(subject.run(argv)).to eq(-1)
166
+ }.to output("kramdown-man: no such file or directory: #{markdown_file}#{$/}").to_stderr
167
+ end
168
+ end
169
+ end
170
+
171
+ context "when given no arguments" do
172
+ let(:argv) { [] }
173
+
174
+ it "must print an error and return -1" do
175
+ expect {
176
+ expect(subject.run(argv)).to eq(-1)
177
+ }.to output("kramdown-man: a MARKDOWN_FILE argument is required#{$/}").to_stderr
178
+ end
179
+ end
180
+
181
+ context "when too many arguments are given" do
182
+ let(:argv) { ['one', 'two'] }
183
+
184
+ it "must print an error and return -1" do
185
+ expect {
186
+ expect(subject.run(argv)).to eq(-1)
187
+ }.to output("kramdown-man: too many arguments given#{$/}").to_stderr
188
+ end
189
+ end
190
+
191
+ context "when given an invalid option" do
192
+ let(:opt) { '--foo' }
193
+ let(:argv) { [opt] }
194
+
195
+ it "must print 'kramdown-man: invalid option ...' to $stderr and exit with -1" do
196
+ expect {
197
+ expect(subject.run(argv)).to eq(-1)
198
+ }.to output("kramdown-man: invalid option: #{opt}#{$/}").to_stderr
199
+ end
200
+ end
201
+
202
+ context "when another type of Exception is raised" do
203
+ let(:argv) { [markdown_file] }
204
+
205
+ let(:exception) { RuntimeError.new("error!") }
206
+
207
+ before do
208
+ expect(Kramdown::Document).to receive(:new).and_raise(exception)
209
+ end
210
+
211
+ it "must print a backtrace and exit with -1" do
212
+ expect {
213
+ expect(subject.run(argv)).to eq(-1)
214
+ }.to output(
215
+ %r{Oops! Looks like you've found a bug!
216
+ Please report the following text to: #{Regexp.escape(described_class::BUG_REPORT_URL)}
217
+
218
+ ```}m
219
+ ).to_stderr
220
+ end
221
+ end
222
+ end
223
+ end