kramdown-man 0.1.9 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/spec/cli_spec.rb ADDED
@@ -0,0 +1,218 @@
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
+ context "when given a markdown file" do
120
+ let(:argv) { [markdown_file] }
121
+
122
+ context "and when the -o,--output option is also given" do
123
+ let(:tempfile) { Tempfile.new(['kramdown-man-', '.1']) }
124
+ let(:output) { tempfile.path }
125
+
126
+ let(:argv) { ['--output', output, markdown_file] }
127
+ before { subject.option_parser.parse(argv) }
128
+
129
+ it "must write the man page output to the output file" do
130
+ subject.run(markdown_file)
131
+
132
+ expect(File.read(output)).to match(/\A.\\" Generated by kramdown-man 1.0.0\n/)
133
+ end
134
+ end
135
+
136
+ context "but STDOUT is a TTY" do
137
+ before { expect($stdout).to receive(:tty?).and_return(true) }
138
+
139
+ it "must open the man page output using the `man` command" do
140
+ # TODO: need to test that IO.popen is actually being called
141
+ expect(subject).to receive(:view_man_page).with(String)
142
+
143
+ subject.run(argv)
144
+ end
145
+ end
146
+
147
+ context "but STDOUT is not a TTY" do
148
+ it "must print the man page to stdout" do
149
+ expect {
150
+ subject.run(argv)
151
+ }.to output(/\A.\\" Generated by kramdown-man 1.0.0\n/).to_stdout
152
+ end
153
+ end
154
+
155
+ context "but the given markdown file does not exist" do
156
+ let(:markdown_file) { 'does/not/exist.md' }
157
+
158
+ it "must print an error and return -1" do
159
+ expect {
160
+ expect(subject.run(argv)).to eq(-1)
161
+ }.to output("kramdown-man: no such file or directory: #{markdown_file}#{$/}").to_stderr
162
+ end
163
+ end
164
+ end
165
+
166
+ context "when given no arguments" do
167
+ let(:argv) { [] }
168
+
169
+ it "must print an error and return -1" do
170
+ expect {
171
+ expect(subject.run(argv)).to eq(-1)
172
+ }.to output("kramdown-man: a MARKDOWN_FILE argument is required#{$/}").to_stderr
173
+ end
174
+ end
175
+
176
+ context "when too many arguments are given" do
177
+ let(:argv) { ['one', 'two'] }
178
+
179
+ it "must print an error and return -1" do
180
+ expect {
181
+ expect(subject.run(argv)).to eq(-1)
182
+ }.to output("kramdown-man: too many arguments given#{$/}").to_stderr
183
+ end
184
+ end
185
+
186
+ context "when given an invalid option" do
187
+ let(:opt) { '--foo' }
188
+ let(:argv) { [opt] }
189
+
190
+ it "must print 'kramdown-man: invalid option ...' to $stderr and exit with -1" do
191
+ expect {
192
+ expect(subject.run(argv)).to eq(-1)
193
+ }.to output("kramdown-man: invalid option: #{opt}#{$/}").to_stderr
194
+ end
195
+ end
196
+
197
+ context "when another type of Exception is raised" do
198
+ let(:argv) { [markdown_file] }
199
+
200
+ let(:exception) { RuntimeError.new("error!") }
201
+
202
+ before do
203
+ expect(Kramdown::Document).to receive(:new).and_raise(exception)
204
+ end
205
+
206
+ it "must print a backtrace and exit with -1" do
207
+ expect {
208
+ expect(subject.run(argv)).to eq(-1)
209
+ }.to output(
210
+ %r{Oops! Looks like you've found a bug!
211
+ Please report the following text to: #{Regexp.escape(described_class::BUG_REPORT_URL)}
212
+
213
+ ```}m
214
+ ).to_stderr
215
+ end
216
+ end
217
+ end
218
+ end