Skip to content

Commit 60908a8

Browse files
committed
fix spec; use tempdir instead of spec on generating epub files
1 parent 96ba83c commit 60908a8

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ rdoc
1919
pkg
2020

2121
## PROJECT::SPECIFIC
22-
testepub*.epub
23-
example_test*.epub
2422

2523
## BUNDLER
2624
Gemfile.lock

spec/example_spec.rb

+13-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
end
88

99
context 'On generating EPUB' do
10+
before do
11+
@tempdir = Dir.mktmpdir
12+
end
13+
14+
after do
15+
FileUtils.remove_entry_secure @tempdir
16+
end
17+
1018
it 'should generate simple EPUB3 with Builder and buffer' do
1119
workdir = File.join(File.dirname(__FILE__), 'fixtures', 'testdata')
1220
builder = GEPUB::Builder.new {
@@ -33,7 +41,7 @@
3341
}
3442
}
3543
}
36-
epubname = File.join(File.dirname(__FILE__), 'example_test_with_builder_buffer.epub')
44+
epubname = File.join(@tempdir, 'example_test_with_builder_buffer.epub')
3745
File.open(epubname, 'wb') { |io| io.write builder.generate_epub_stream.string }
3846
epubcheck(epubname)
3947
end
@@ -64,7 +72,7 @@
6472
}
6573
}
6674
}
67-
epubname = File.join(File.dirname(__FILE__), 'example_test_with_builder.epub')
75+
epubname = File.join(@tempdir, 'example_test_with_builder.epub')
6876
builder.generate_epub(epubname)
6977
epubcheck(epubname)
7078
end
@@ -106,7 +114,7 @@
106114
book.add_item('text/chap1-1.xhtml').add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>the second page</p></body></html>')) # do not appear on table of contents
107115
book.add_item('text/chap2.xhtml').add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c3</title></head><body><p>the third page</p></body></html>')).toc_text('Chapter 2')
108116
}
109-
epubname = File.join(File.dirname(__FILE__), 'example_test.epub')
117+
epubname = File.join(@tempdir, 'example_test.epub')
110118
book.generate_epub(epubname)
111119
epubcheck(epubname)
112120
end
@@ -124,7 +132,7 @@
124132
item.add_content StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c1</title></head><body><p>the first page</p></body></html>')
125133
end
126134

127-
epubname = File.join(File.dirname(__FILE__), 'example_test.epub')
135+
epubname = File.join(@tempdir, 'example_test.epub')
128136
book.generate_epub(epubname)
129137
epubcheck(epubname)
130138
end
@@ -167,7 +175,7 @@
167175
book.add_item('text/chap1-1.xhtml').add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>the second page</p></body></html>')) # do not appear on table of contents
168176
book.add_item('text/chap2.xhtml').add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c3</title></head><body><p>the third page</p></body></html>')).toc_text('Chapter 2')
169177
}
170-
epubname = File.join(File.dirname(__FILE__), 'example_test.epub')
178+
epubname = File.join(@tempdir, 'example_test.epub')
171179

172180
# check nav doc
173181
xml = Nokogiri::XML::Document.parse book.nav_doc

spec/gepub_deprectad_api_spec.rb

+10-5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@
7575
</html>
7676
EOF
7777
item3 = @book.add_ordered_item('text/nav.xhtml', StringIO.new(nav_string), 'nav').add_property('nav')
78+
@tempdir = Dir.mktmpdir
79+
end
80+
81+
after do
82+
FileUtils.remove_entry_secure @tempdir
7883
end
7984

8085
it "should have title" do
@@ -155,12 +160,12 @@
155160
end
156161

157162
it "should generate correct epub" do
158-
epubname = File.join(File.dirname(__FILE__), 'testepub.epub')
163+
epubname = File.join(@tempdir, 'testepub.epub')
159164
@book.generate_epub(epubname)
160165
epubcheck(epubname)
161166
end
162167
it "should generate correct epub with buffer" do
163-
epubname = File.join(File.dirname(__FILE__), 'testepub_buf.epub')
168+
epubname = File.join(@tempdir, 'testepub_buf.epub')
164169
File.open(epubname, 'wb') {
165170
|io|
166171
io.write @book.generate_epub_stream.string
@@ -169,7 +174,7 @@
169174
end
170175

171176
it "should generate correct epub2.0" do
172-
epubname = File.join(File.dirname(__FILE__), 'testepub2.epub')
177+
epubname = File.join(@tempdir, 'testepub2.epub')
173178
@book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '2.0'} )
174179
@book.title = 'thetitle'
175180
@book.creator = "theauthor"
@@ -189,14 +194,14 @@
189194
epubcheck(epubname)
190195
end
191196
it 'should generate epub with extra file' do
192-
epubname = File.join(File.dirname(__FILE__), 'testepub3.epub')
197+
epubname = File.join(@tempdir, 'testepub3.epub')
193198
@book.add_optional_file('META-INF/foobar.xml', StringIO.new('<foo></foo>'))
194199
@book.generate_epub(epubname)
195200
epubcheck(epubname)
196201
end
197202

198203
it 'should generate valid EPUB when @toc is empty' do
199-
epubname = File.join(File.dirname(__FILE__), 'testepub4.epub')
204+
epubname = File.join(@tempdir, 'testepub4.epub')
200205
@book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '3.0'} )
201206
@book.title = 'thetitle'
202207
@book.creator = "theauthor"

spec/gepub_spec.rb

+18-12
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,14 @@
7373
</html>
7474
EOF
7575
item3 = @book.add_ordered_item('text/nav.xhtml', content: StringIO.new(nav_string), id: 'nav').add_property('nav')
76+
77+
@tempdir = Dir.mktmpdir
7678
end
7779

80+
after do
81+
FileUtils.remove_entry_secure @tempdir
82+
end
83+
7884
it "should have title" do
7985
expect(@book.title.to_s).to eq('thetitle')
8086
end
@@ -153,13 +159,13 @@
153159
end
154160

155161
it "should generate correct epub" do
156-
epubname = File.join(File.dirname(__FILE__), 'testepub.epub')
162+
epubname = File.join(@tempdir, 'testepub.epub')
157163
@book.generate_epub(epubname)
158164
epubcheck(epubname)
159165
end
160166

161167
it "should generate correct epub with buffer" do
162-
epubname = File.join(File.dirname(__FILE__), 'testepub_buf.epub')
168+
epubname = File.join(@tempdir, 'testepub_buf.epub')
163169
File.open(epubname, 'wb') {
164170
|io|
165171
io.write @book.generate_epub_stream.string
@@ -168,7 +174,7 @@
168174
end
169175

170176
it "should generate correct epub2.0" do
171-
epubname = File.join(File.dirname(__FILE__), 'testepub2.epub')
177+
epubname = File.join(@tempdir, 'testepub2.epub')
172178
@book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '2.0'} )
173179
@book.title = 'thetitle'
174180
@book.creator = "theauthor"
@@ -188,14 +194,14 @@
188194
epubcheck(epubname)
189195
end
190196
it 'should generate epub with extra file' do
191-
epubname = File.join(File.dirname(__FILE__), 'testepub3.epub')
197+
epubname = File.join(@tempdir, 'testepub3.epub')
192198
@book.add_optional_file('META-INF/foobar.xml', StringIO.new('<foo></foo>'))
193199
@book.generate_epub(epubname)
194200
epubcheck(epubname)
195201
end
196202

197203
it 'should generate valid EPUB when @toc is empty' do
198-
epubname = File.join(File.dirname(__FILE__), 'testepub4.epub')
204+
epubname = File.join(@tempdir, 'testepub4.epub')
199205
@book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '3.0'} )
200206
@book.title = 'thetitle'
201207
@book.creator = "theauthor"
@@ -215,7 +221,7 @@
215221
end
216222

217223
it 'should generate EPUB with specified lastmodified' do
218-
epubname = File.join(File.dirname(__FILE__), 'testepub.epub')
224+
epubname = File.join(@tempdir, 'testepub.epub')
219225
mod_time = Time.mktime(2010,5,5,8,10,15)
220226
@book.lastmodified = mod_time
221227
@book.generate_epub(epubname)
@@ -227,7 +233,7 @@
227233

228234

229235
it 'should generate EPUB with specified lastmodified by string' do
230-
epubname = File.join(File.dirname(__FILE__), 'testepub.epub')
236+
epubname = File.join(@tempdir, 'testepub.epub')
231237
mod_time = "2010-05-05T08:10:15Z"
232238
@book.lastmodified = mod_time
233239
@book.generate_epub(epubname)
@@ -239,7 +245,7 @@
239245

240246
it 'should generate parsed and generated EPUB with renewed lastmodified' do
241247
originalfile = File.join(File.dirname(__FILE__), 'fixtures/testdata/wasteland-20120118.epub')
242-
epubname = File.join(File.dirname(__FILE__), 'testepub.epub')
248+
epubname = File.join(@tempdir, 'testepub.epub')
243249

244250
original_book = File.open(originalfile) do |f|
245251
GEPUB::Book.parse(f)
@@ -256,7 +262,7 @@
256262

257263
it 'should generate parsed and generated EPUB with newly set lastmodified' do
258264
originalfile = File.join(File.dirname(__FILE__), 'fixtures/testdata/wasteland-20120118.epub')
259-
epubname = File.join(File.dirname(__FILE__), 'testepub.epub')
265+
epubname = File.join(@tempdir, 'testepub.epub')
260266
mod_time = Time.mktime(2010,5,5,8,10,15)
261267

262268
original_book = File.open(originalfile) do |f|
@@ -281,8 +287,8 @@
281287
end
282288

283289
it 'should produce deterministic output when lastmodified is specified' do
284-
epubname1 = File.join(File.dirname(__FILE__), 'testepub1.epub')
285-
epubname2 = File.join(File.dirname(__FILE__), 'testepub2.epub')
290+
epubname1 = File.join(@tempdir, 'testepub1.epub')
291+
epubname2 = File.join(@tempdir, 'testepub2.epub')
286292
mod_time = "2010-05-05T08:10:15Z"
287293
@book.lastmodified = mod_time
288294

@@ -297,7 +303,7 @@
297303
@book = GEPUB::Book.new
298304
@book.identifier = 'test'
299305
@book.add_ordered_item('foobar.xhtml', content: StringIO.new('<html><img src="image.svg" /></html>')).add_property 'svg'
300-
epubname = File.join(__dir__, 'testepub.epub')
306+
epubname = File.join(@tempdir, 'testepub.epub')
301307
@book.generate_epub(epubname)
302308
File.open(epubname) do |f|
303309
parsed_book = GEPUB::Book.parse(f)

0 commit comments

Comments
 (0)