epub_tools 0.3.1 → 0.4.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.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.rubocop.yml +41 -0
- data/Gemfile +13 -9
- data/Gemfile.lock +49 -0
- data/README.md +16 -0
- data/bin/epub-tools +3 -109
- data/epub_tools.gemspec +6 -8
- data/lib/epub_tools/add_chapters.rb +41 -19
- data/lib/epub_tools/cli/command_registry.rb +47 -0
- data/lib/epub_tools/cli/option_builder.rb +164 -0
- data/lib/epub_tools/cli/runner.rb +164 -0
- data/lib/epub_tools/cli.rb +45 -0
- data/lib/epub_tools/compile_book.rb +60 -28
- data/lib/epub_tools/epub_initializer.rb +38 -28
- data/lib/epub_tools/loggable.rb +11 -0
- data/lib/epub_tools/pack_ebook.rb +18 -12
- data/lib/epub_tools/split_chapters.rb +31 -21
- data/lib/epub_tools/{text_style_class_finder.rb → style_finder.rb} +21 -17
- data/lib/epub_tools/unpack_ebook.rb +17 -12
- data/lib/epub_tools/version.rb +1 -1
- data/lib/epub_tools/xhtml_cleaner.rb +17 -13
- data/lib/epub_tools/xhtml_extractor.rb +20 -11
- data/lib/epub_tools.rb +2 -1
- data/test/add_chapters_test.rb +12 -5
- data/test/cli/command_registry_test.rb +66 -0
- data/test/cli/option_builder_test.rb +173 -0
- data/test/cli/runner_test.rb +91 -0
- data/test/cli_commands_test.rb +100 -0
- data/test/cli_test.rb +4 -0
- data/test/cli_version_test.rb +5 -3
- data/test/compile_book_test.rb +11 -2
- data/test/epub_initializer_test.rb +51 -31
- data/test/pack_ebook_test.rb +14 -8
- data/test/split_chapters_test.rb +22 -1
- data/test/{text_style_class_finder_test.rb → style_finder_test.rb} +7 -6
- data/test/test_helper.rb +4 -5
- data/test/unpack_ebook_test.rb +21 -5
- data/test/xhtml_cleaner_test.rb +13 -7
- data/test/xhtml_extractor_test.rb +17 -1
- metadata +19 -36
- data/lib/epub_tools/cli_helper.rb +0 -31
@@ -0,0 +1,173 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
require_relative '../../lib/epub_tools'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
class OptionBuilderTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@builder = EpubTools::CLI::OptionBuilder.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_initialize_with_defaults
|
11
|
+
builder = EpubTools::CLI::OptionBuilder.new({ default: 'value' }, [:required])
|
12
|
+
|
13
|
+
assert_equal({ default: 'value' }, builder.options)
|
14
|
+
assert_equal [:required], builder.required_keys
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_with_banner
|
18
|
+
@builder.with_banner('Test Banner')
|
19
|
+
|
20
|
+
assert_equal 'Test Banner', @builder.parser.banner
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_with_help_option
|
24
|
+
# This test is tricky as --help would exit, so we'll test indirectly
|
25
|
+
@builder.with_help_option
|
26
|
+
|
27
|
+
assert_includes @builder.parser.to_s, '-h, --help'
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_with_verbose_option
|
31
|
+
@builder.with_verbose_option
|
32
|
+
|
33
|
+
assert @builder.options[:verbose]
|
34
|
+
|
35
|
+
# Parse args with --quiet to change verbose to false
|
36
|
+
@builder.parse(['--quiet'])
|
37
|
+
|
38
|
+
refute @builder.options[:verbose]
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_with_input_file
|
42
|
+
@builder.with_input_file('Test input')
|
43
|
+
|
44
|
+
assert_includes @builder.parser.to_s, '-i, --input-file FILE'
|
45
|
+
assert_includes @builder.parser.to_s, 'Test input (required)'
|
46
|
+
|
47
|
+
# Test with required=false
|
48
|
+
builder2 = EpubTools::CLI::OptionBuilder.new
|
49
|
+
builder2.with_input_file('Test input', false)
|
50
|
+
|
51
|
+
assert_includes builder2.parser.to_s, 'Test input'
|
52
|
+
refute_includes builder2.parser.to_s, 'Test input (required)'
|
53
|
+
|
54
|
+
# Test with actual parsing
|
55
|
+
@builder.parse(['-i', 'file.txt'])
|
56
|
+
|
57
|
+
assert_equal 'file.txt', @builder.options[:input_file]
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_with_input_dir
|
61
|
+
@builder.with_input_dir('Test input dir')
|
62
|
+
|
63
|
+
assert_includes @builder.parser.to_s, '-i, --input-dir DIR'
|
64
|
+
assert_includes @builder.parser.to_s, 'Test input dir (required)'
|
65
|
+
|
66
|
+
@builder.parse(['-i', 'dir/path'])
|
67
|
+
|
68
|
+
assert_equal 'dir/path', @builder.options[:input_dir]
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_with_output_dir
|
72
|
+
# Test with default value
|
73
|
+
@builder.with_output_dir('Test output dir', 'default/path')
|
74
|
+
|
75
|
+
assert_includes @builder.parser.to_s, '-o, --output-dir DIR'
|
76
|
+
assert_includes @builder.parser.to_s, 'Test output dir (default: default/path)'
|
77
|
+
assert_equal 'default/path', @builder.options[:output_dir]
|
78
|
+
|
79
|
+
# Test without default
|
80
|
+
builder2 = EpubTools::CLI::OptionBuilder.new
|
81
|
+
builder2.with_output_dir('Test output dir')
|
82
|
+
|
83
|
+
assert_includes builder2.parser.to_s, 'Test output dir (required)'
|
84
|
+
|
85
|
+
# Test actual parsing
|
86
|
+
@builder.parse(['-o', 'new/path'])
|
87
|
+
|
88
|
+
assert_equal 'new/path', @builder.options[:output_dir]
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_with_output_file
|
92
|
+
@builder.with_output_file('Test output file')
|
93
|
+
|
94
|
+
assert_includes @builder.parser.to_s, '-o, --output-file FILE'
|
95
|
+
assert_includes @builder.parser.to_s, 'Test output file (required)'
|
96
|
+
|
97
|
+
@builder.parse(['-o', 'output.txt'])
|
98
|
+
|
99
|
+
assert_equal 'output.txt', @builder.options[:output_file]
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_with_title_option
|
103
|
+
@builder.with_title_option
|
104
|
+
|
105
|
+
assert_includes @builder.parser.to_s, '-t, --title TITLE'
|
106
|
+
assert_includes @builder.parser.to_s, 'Book title (required)'
|
107
|
+
|
108
|
+
@builder.parse(['-t', 'Book Title'])
|
109
|
+
|
110
|
+
assert_equal 'Book Title', @builder.options[:title]
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_with_author_option
|
114
|
+
@builder.with_author_option
|
115
|
+
|
116
|
+
assert_includes @builder.parser.to_s, '-a, --author AUTHOR'
|
117
|
+
assert_includes @builder.parser.to_s, 'Author name (required)'
|
118
|
+
|
119
|
+
@builder.parse(['-a', 'Author Name'])
|
120
|
+
|
121
|
+
assert_equal 'Author Name', @builder.options[:author]
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_with_cover_option
|
125
|
+
@builder.with_cover_option
|
126
|
+
|
127
|
+
assert_includes @builder.parser.to_s, '-c, --cover PATH'
|
128
|
+
assert_includes @builder.parser.to_s, 'Cover image file path (optional)'
|
129
|
+
|
130
|
+
@builder.parse(['-c', 'cover.jpg'])
|
131
|
+
|
132
|
+
assert_equal 'cover.jpg', @builder.options[:cover_image]
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_with_option
|
136
|
+
@builder.with_option('-x', '--extra VALUE', 'Extra option', :extra_option)
|
137
|
+
|
138
|
+
assert_includes @builder.parser.to_s, '-x, --extra VALUE'
|
139
|
+
assert_includes @builder.parser.to_s, 'Extra option'
|
140
|
+
|
141
|
+
@builder.parse(['-x', 'extra-value'])
|
142
|
+
|
143
|
+
assert_equal 'extra-value', @builder.options[:extra_option]
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_with_custom_options
|
147
|
+
@builder.with_custom_options do |opts, options|
|
148
|
+
opts.on('-c', '--custom VALUE', 'Custom option') { |v| options[:custom] = v }
|
149
|
+
end
|
150
|
+
|
151
|
+
assert_includes @builder.parser.to_s, '-c, --custom VALUE'
|
152
|
+
assert_includes @builder.parser.to_s, 'Custom option'
|
153
|
+
|
154
|
+
@builder.parse(['-c', 'custom-value'])
|
155
|
+
|
156
|
+
assert_equal 'custom-value', @builder.options[:custom]
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_parse_validates_required_keys
|
160
|
+
builder = EpubTools::CLI::OptionBuilder.new({}, [:required_option])
|
161
|
+
assert_output('', /Missing required options/) do
|
162
|
+
assert_raises(SystemExit) { builder.parse([]) }
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_method_chaining
|
167
|
+
result = @builder.with_banner('Test')
|
168
|
+
.with_verbose_option
|
169
|
+
.with_title_option
|
170
|
+
|
171
|
+
assert_equal @builder, result
|
172
|
+
end
|
173
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
require_relative '../../lib/epub_tools'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
class RunnerTest < Minitest::Test
|
6
|
+
class TestCommand
|
7
|
+
attr_reader :options, :run_called
|
8
|
+
|
9
|
+
def initialize(options)
|
10
|
+
@options = options
|
11
|
+
@run_called = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def run
|
15
|
+
@run_called = true
|
16
|
+
puts 'Called!'
|
17
|
+
true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def setup
|
22
|
+
@runner = EpubTools::CLI::Runner.new('test-program')
|
23
|
+
|
24
|
+
# Register a test command
|
25
|
+
@runner.registry.register('test-cmd', TestCommand,
|
26
|
+
[:required_option],
|
27
|
+
{ default_option: 'default' })
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_initialize
|
31
|
+
assert_equal 'test-program', @runner.program_name
|
32
|
+
assert_kind_of EpubTools::CLI::CommandRegistry, @runner.registry
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_handle_command
|
36
|
+
assert_output(/Usage: test-program test-cmd/) do
|
37
|
+
assert_raises(SystemExit) { @runner.handle_command('test-cmd', ['-h']) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_handle_command_with_required_args
|
42
|
+
runner = EpubTools::CLI::Runner.new('test-program')
|
43
|
+
runner.registry.register('test-cmd', TestCommand)
|
44
|
+
assert_output("Called!\n") { assert runner.handle_command('test-cmd') }
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_handle_nonexistent_command
|
48
|
+
result = @runner.handle_command('nonexistent')
|
49
|
+
|
50
|
+
refute result # Should return false for nonexistent command
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_run_with_version_flag
|
54
|
+
assert_output(/^\d+\.\d+\.\d+$/) do
|
55
|
+
assert_raises(SystemExit) { @runner.run(['-v']) }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_run_with_no_args_shows_usage
|
60
|
+
assert_output(/Usage: test-program COMMAND \[options\]/) do
|
61
|
+
assert_raises(SystemExit) { @runner.run([]) }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_program_name_defaults_to_current_program
|
66
|
+
default_runner = EpubTools::CLI::Runner.new
|
67
|
+
|
68
|
+
assert_equal File.basename($PROGRAM_NAME), default_runner.program_name
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_configure_command_options
|
72
|
+
# This is testing a private method, which is generally not recommended,
|
73
|
+
# but it's useful to ensure all command configurations work
|
74
|
+
|
75
|
+
# Use send to access private method
|
76
|
+
builder = EpubTools::CLI::OptionBuilder.new
|
77
|
+
|
78
|
+
# Test each command configuration - we'll just check one example
|
79
|
+
@runner.send(:configure_command_options, 'add', builder)
|
80
|
+
|
81
|
+
# Check add command options were added
|
82
|
+
assert_includes builder.parser.to_s, '--chapters-dir DIR'
|
83
|
+
assert_includes builder.parser.to_s, '--epub-oebps-dir DIR'
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_run_with_unknown_command
|
87
|
+
assert_output(/Usage: test-program COMMAND \[options\]/) do
|
88
|
+
assert_raises(SystemExit) { @runner.run(['unknown-command']) }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'tempfile'
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
class CLICommandsTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
@bin_path = File.expand_path('../bin/epub-tools', __dir__)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_show_usage_with_no_args
|
12
|
+
output = `#{@bin_path}`
|
13
|
+
|
14
|
+
assert_match(/Usage: epub-tools COMMAND \[options\]/, output)
|
15
|
+
assert_match(/Commands:/, output)
|
16
|
+
assert_includes output, 'init'
|
17
|
+
assert_includes output, 'extract'
|
18
|
+
assert_includes output, 'split'
|
19
|
+
assert_includes output, 'add'
|
20
|
+
assert_includes output, 'pack'
|
21
|
+
assert_includes output, 'unpack'
|
22
|
+
assert_includes output, 'compile'
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_show_version
|
26
|
+
output = `#{@bin_path} --version`
|
27
|
+
|
28
|
+
assert_match(/^\d+\.\d+\.\d+$/, output.strip)
|
29
|
+
|
30
|
+
output = `#{@bin_path} -v`
|
31
|
+
|
32
|
+
assert_match(/^\d+\.\d+\.\d+$/, output.strip)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_compile_command_requires_required_options
|
36
|
+
output = `#{@bin_path} compile 2>&1`
|
37
|
+
|
38
|
+
assert_match(/Missing required options/, output)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_run_compile_command
|
42
|
+
# Mock the execution - we won't actually run the full compile since it's complex
|
43
|
+
# Instead we'll use the --help flag for each command to check it's available and formatted correctly
|
44
|
+
output = `#{@bin_path} compile --help`
|
45
|
+
|
46
|
+
assert_match(/Usage: epub-tools compile \[options\]/, output)
|
47
|
+
assert_includes output, '--title TITLE'
|
48
|
+
assert_includes output, '--author AUTHOR'
|
49
|
+
assert_includes output, '--source-dir DIR'
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_extract_command
|
53
|
+
output = `#{@bin_path} extract --help`
|
54
|
+
|
55
|
+
assert_match(/Usage: epub-tools extract \[options\]/, output)
|
56
|
+
assert_includes output, '--source-dir DIR'
|
57
|
+
assert_includes output, '--target-dir DIR'
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_split_command
|
61
|
+
output = `#{@bin_path} split --help`
|
62
|
+
|
63
|
+
assert_match(/Usage: epub-tools split \[options\]/, output)
|
64
|
+
assert_includes output, '--input FILE'
|
65
|
+
assert_includes output, '--title TITLE'
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_init_command
|
69
|
+
output = `#{@bin_path} init --help`
|
70
|
+
|
71
|
+
assert_match(/Usage: epub-tools init \[options\]/, output)
|
72
|
+
assert_includes output, '--title TITLE'
|
73
|
+
assert_includes output, '--author AUTHOR'
|
74
|
+
assert_includes output, '--output-dir DIR'
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_add_command
|
78
|
+
output = `#{@bin_path} add --help`
|
79
|
+
|
80
|
+
assert_match(/Usage: epub-tools add \[options\]/, output)
|
81
|
+
assert_includes output, '--chapters-dir DIR'
|
82
|
+
assert_includes output, '--epub-oebps-dir DIR'
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_pack_command
|
86
|
+
output = `#{@bin_path} pack --help`
|
87
|
+
|
88
|
+
assert_match(/Usage: epub-tools pack \[options\]/, output)
|
89
|
+
assert_includes output, '--input-dir DIR'
|
90
|
+
assert_includes output, '--output-file FILE'
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_unpack_command
|
94
|
+
output = `#{@bin_path} unpack --help`
|
95
|
+
|
96
|
+
assert_match(/Usage: epub-tools unpack \[options\]/, output)
|
97
|
+
assert_includes output, '--input-file FILE'
|
98
|
+
assert_includes output, '--output-dir DIR'
|
99
|
+
end
|
100
|
+
end
|
data/test/cli_test.rb
ADDED
data/test/cli_version_test.rb
CHANGED
@@ -9,12 +9,14 @@ class CLIVersionTest < Minitest::Test
|
|
9
9
|
cli = File.join(project_root, 'bin', 'epub-tools')
|
10
10
|
# Run with --version
|
11
11
|
out, status = Open3.capture2(cli, '--version')
|
12
|
+
|
12
13
|
assert_equal "#{EpubTools::VERSION}\n", out
|
13
|
-
|
14
|
+
assert_predicate status, :success?, "Expected exit status 0, got #{status.exitstatus}"
|
14
15
|
|
15
16
|
# Run with -v
|
16
17
|
out2, status2 = Open3.capture2(cli, '-v')
|
18
|
+
|
17
19
|
assert_equal "#{EpubTools::VERSION}\n", out2
|
18
|
-
|
20
|
+
assert_predicate status2, :success?, "Expected exit status 0, got #{status2.exitstatus}"
|
19
21
|
end
|
20
|
-
end
|
22
|
+
end
|
data/test/compile_book_test.rb
CHANGED
@@ -21,6 +21,7 @@ class CompileBookTest < Minitest::Test
|
|
21
21
|
source_dir: @source,
|
22
22
|
build_dir: @tmp
|
23
23
|
)
|
24
|
+
|
24
25
|
assert_equal 'My_Title.epub', cb.output_file
|
25
26
|
end
|
26
27
|
|
@@ -32,6 +33,7 @@ class CompileBookTest < Minitest::Test
|
|
32
33
|
output_file: 'custom.epub',
|
33
34
|
build_dir: @tmp
|
34
35
|
)
|
36
|
+
|
35
37
|
assert_equal 'custom.epub', cb.output_file
|
36
38
|
end
|
37
39
|
|
@@ -41,6 +43,7 @@ class CompileBookTest < Minitest::Test
|
|
41
43
|
author: @author,
|
42
44
|
source_dir: @source
|
43
45
|
)
|
46
|
+
|
44
47
|
assert cb.build_dir.end_with?('.epub_tools_build')
|
45
48
|
end
|
46
49
|
|
@@ -54,6 +57,7 @@ class CompileBookTest < Minitest::Test
|
|
54
57
|
build_dir: 'bd',
|
55
58
|
verbose: false
|
56
59
|
)
|
60
|
+
|
57
61
|
assert_equal @title, cb.title
|
58
62
|
assert_equal @author, cb.author
|
59
63
|
assert_equal @source, cb.source_dir
|
@@ -73,8 +77,10 @@ class CompileBookTest < Minitest::Test
|
|
73
77
|
source_dir: @source,
|
74
78
|
build_dir: build
|
75
79
|
)
|
80
|
+
|
76
81
|
assert Dir.exist?(build)
|
77
82
|
cb.send(:clean_build_dir)
|
83
|
+
|
78
84
|
refute Dir.exist?(build)
|
79
85
|
end
|
80
86
|
|
@@ -87,8 +93,9 @@ class CompileBookTest < Minitest::Test
|
|
87
93
|
build_dir: build
|
88
94
|
)
|
89
95
|
cb.send(:prepare_dirs)
|
90
|
-
xhtml_dir
|
96
|
+
xhtml_dir = cb.send(:xhtml_dir)
|
91
97
|
chapters_dir = cb.send(:chapters_dir)
|
98
|
+
|
92
99
|
assert Dir.exist?(xhtml_dir)
|
93
100
|
assert Dir.exist?(chapters_dir)
|
94
101
|
end
|
@@ -156,6 +163,7 @@ class CompileBookTest < Minitest::Test
|
|
156
163
|
File.write(File.join(chapters, 'chap_1.xhtml'), '')
|
157
164
|
File.write(File.join(chapters, 'chap_2.xhtml'), '')
|
158
165
|
File.write(File.join(chapters, 'chap_3.xhtml'), '')
|
166
|
+
|
159
167
|
assert_nil cb.send(:validate_sequence)
|
160
168
|
end
|
161
169
|
|
@@ -184,8 +192,9 @@ class CompileBookTest < Minitest::Test
|
|
184
192
|
[:log, /Done\. Output EPUB: .*o\.epub/],
|
185
193
|
:clean
|
186
194
|
]
|
195
|
+
|
187
196
|
assert_equal expected[0..7], seq[0..7]
|
188
|
-
|
197
|
+
assert_kind_of Array, seq[8]
|
189
198
|
assert_equal :log, seq[8][0]
|
190
199
|
assert_match expected[8][1], seq[8][1]
|
191
200
|
assert_equal expected[9], seq[9]
|
@@ -14,26 +14,33 @@ class EpubInitializerTest < Minitest::Test
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def test_run_creates_basic_structure
|
17
|
-
EpubTools::EpubInitializer.new(@title, @author, @dest).run
|
17
|
+
result = EpubTools::EpubInitializer.new(title: @title, author: @author, destination: @dest).run
|
18
|
+
|
19
|
+
# Check return value is the destination directory
|
20
|
+
assert_equal @dest, result
|
21
|
+
|
18
22
|
# Check directories
|
19
23
|
assert Dir.exist?(@dest)
|
20
24
|
assert File.directory?(File.join(@dest, 'META-INF'))
|
21
25
|
assert File.directory?(File.join(@dest, 'OEBPS'))
|
22
26
|
# Check files
|
23
27
|
mimetype = File.join(@dest, 'mimetype')
|
24
|
-
|
28
|
+
|
29
|
+
assert_path_exists mimetype
|
25
30
|
assert_equal 'application/epub+zip', File.read(mimetype)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
+
assert_path_exists File.join(@dest, 'META-INF', 'container.xml')
|
32
|
+
assert_path_exists File.join(@dest, 'OEBPS', 'title.xhtml')
|
33
|
+
assert_path_exists File.join(@dest, 'OEBPS', 'nav.xhtml')
|
34
|
+
assert_path_exists File.join(@dest, 'OEBPS', 'package.opf')
|
35
|
+
assert_path_exists File.join(@dest, 'OEBPS', 'style.css')
|
31
36
|
# Check content of title.xhtml
|
32
37
|
title_page = File.read(File.join(@dest, 'OEBPS', 'title.xhtml'))
|
38
|
+
|
33
39
|
assert_includes title_page, "<h1 class=\"title\">#{@title}</h1>"
|
34
40
|
assert_includes title_page, "<p class=\"author\">by #{@author}</p>"
|
35
41
|
# Check package.opf metadata
|
36
42
|
opf = File.read(File.join(@dest, 'OEBPS', 'package.opf'))
|
43
|
+
|
37
44
|
assert_includes opf, "<dc:title>#{@title}</dc:title>"
|
38
45
|
assert_includes opf, "<dc:creator>#{@author}</dc:creator>"
|
39
46
|
refute_includes opf, 'cover.xhtml'
|
@@ -43,11 +50,12 @@ class EpubInitializerTest < Minitest::Test
|
|
43
50
|
# create dummy image
|
44
51
|
cover = File.join(@tmp, 'cover.png')
|
45
52
|
File.write(cover, 'PNGDATA')
|
46
|
-
EpubTools::EpubInitializer.new(@title, @author, @dest, cover).run
|
53
|
+
EpubTools::EpubInitializer.new(title: @title, author: @author, destination: @dest, cover_image: cover).run
|
47
54
|
# Check cover file and page
|
48
|
-
|
49
|
-
|
55
|
+
assert_path_exists File.join(@dest, 'OEBPS', 'cover.png')
|
56
|
+
assert_path_exists File.join(@dest, 'OEBPS', 'cover.xhtml')
|
50
57
|
opf = File.read(File.join(@dest, 'OEBPS', 'package.opf'))
|
58
|
+
|
51
59
|
assert_includes opf, 'cover-image'
|
52
60
|
assert_includes opf, '<item id="cover-page"'
|
53
61
|
assert_includes opf, '<itemref idref="cover-page"'
|
@@ -56,64 +64,76 @@ class EpubInitializerTest < Minitest::Test
|
|
56
64
|
def test_run_with_cover_jpg
|
57
65
|
cover = File.join(@tmp, 'cover.jpg')
|
58
66
|
File.write(cover, 'JPGDATA')
|
59
|
-
EpubTools::EpubInitializer.new(@title, @author, @dest, cover).run
|
60
|
-
|
61
|
-
|
67
|
+
EpubTools::EpubInitializer.new(title: @title, author: @author, destination: @dest, cover_image: cover).run
|
68
|
+
|
69
|
+
assert_path_exists File.join(@dest, 'OEBPS', 'cover.jpg')
|
70
|
+
assert_path_exists File.join(@dest, 'OEBPS', 'cover.xhtml')
|
62
71
|
opf = File.read(File.join(@dest, 'OEBPS', 'package.opf'))
|
63
|
-
|
72
|
+
|
73
|
+
assert_includes opf, '<item id="cover-image" href="cover.jpg" media-type="image/jpeg"'
|
64
74
|
assert_includes opf, '<item id="cover-page"'
|
65
75
|
assert_includes opf, '<itemref idref="cover-page"'
|
66
76
|
cover_xhtml = File.read(File.join(@dest, 'OEBPS', 'cover.xhtml'))
|
77
|
+
|
67
78
|
assert_includes cover_xhtml, 'src="cover.jpg"'
|
68
79
|
end
|
69
80
|
|
70
81
|
def test_run_with_cover_jpeg
|
71
82
|
cover = File.join(@tmp, 'cover.jpeg')
|
72
83
|
File.write(cover, 'JPEGDATA')
|
73
|
-
EpubTools::EpubInitializer.new(@title, @author, @dest, cover).run
|
74
|
-
|
75
|
-
|
84
|
+
EpubTools::EpubInitializer.new(title: @title, author: @author, destination: @dest, cover_image: cover).run
|
85
|
+
|
86
|
+
assert_path_exists File.join(@dest, 'OEBPS', 'cover.jpeg')
|
87
|
+
assert_path_exists File.join(@dest, 'OEBPS', 'cover.xhtml')
|
76
88
|
opf = File.read(File.join(@dest, 'OEBPS', 'package.opf'))
|
77
|
-
|
89
|
+
|
90
|
+
assert_includes opf, '<item id="cover-image" href="cover.jpeg" media-type="image/jpeg"'
|
78
91
|
cover_xhtml = File.read(File.join(@dest, 'OEBPS', 'cover.xhtml'))
|
92
|
+
|
79
93
|
assert_includes cover_xhtml, 'src="cover.jpeg"'
|
80
94
|
end
|
81
95
|
|
82
96
|
def test_run_with_cover_gif
|
83
97
|
cover = File.join(@tmp, 'cover.gif')
|
84
98
|
File.write(cover, 'GIFDATA')
|
85
|
-
EpubTools::EpubInitializer.new(@title, @author, @dest, cover).run
|
86
|
-
|
99
|
+
EpubTools::EpubInitializer.new(title: @title, author: @author, destination: @dest, cover_image: cover).run
|
100
|
+
|
101
|
+
assert_path_exists File.join(@dest, 'OEBPS', 'cover.gif')
|
87
102
|
opf = File.read(File.join(@dest, 'OEBPS', 'package.opf'))
|
88
|
-
|
103
|
+
|
104
|
+
assert_includes opf, '<item id="cover-image" href="cover.gif" media-type="image/gif"'
|
89
105
|
end
|
90
106
|
|
91
107
|
def test_run_with_cover_svg
|
92
108
|
cover = File.join(@tmp, 'cover.svg')
|
93
109
|
File.write(cover, '<svg/>')
|
94
|
-
EpubTools::EpubInitializer.new(@title, @author, @dest, cover).run
|
95
|
-
|
110
|
+
EpubTools::EpubInitializer.new(title: @title, author: @author, destination: @dest, cover_image: cover).run
|
111
|
+
|
112
|
+
assert_path_exists File.join(@dest, 'OEBPS', 'cover.svg')
|
96
113
|
opf = File.read(File.join(@dest, 'OEBPS', 'package.opf'))
|
97
|
-
|
114
|
+
|
115
|
+
assert_includes opf, '<item id="cover-image" href="cover.svg" media-type="image/svg+xml"'
|
98
116
|
end
|
99
117
|
|
100
118
|
def test_run_with_unsupported_cover_image
|
101
119
|
cover = File.join(@tmp, 'cover.bmp')
|
102
120
|
File.write(cover, 'BMPDATA')
|
103
|
-
ei = EpubTools::EpubInitializer.new(@title, @author, @dest, cover)
|
104
|
-
assert_output(
|
105
|
-
|
106
|
-
|
121
|
+
ei = EpubTools::EpubInitializer.new(title: @title, author: @author, destination: @dest, cover_image: cover)
|
122
|
+
assert_output('', /unsupported cover image type/) { ei.run }
|
123
|
+
refute_path_exists File.join(@dest, 'OEBPS', 'cover.bmp')
|
124
|
+
refute_path_exists File.join(@dest, 'OEBPS', 'cover.xhtml')
|
107
125
|
opf = File.read(File.join(@dest, 'OEBPS', 'package.opf'))
|
126
|
+
|
108
127
|
refute_includes opf, 'cover-image'
|
109
128
|
end
|
110
129
|
|
111
130
|
def test_run_without_cover_but_declaring_cover
|
112
131
|
cover = File.join(@tmp, 'cover.bmp')
|
113
|
-
ei = EpubTools::EpubInitializer.new(@title, @author, @dest, cover)
|
114
|
-
assert_output(
|
115
|
-
|
132
|
+
ei = EpubTools::EpubInitializer.new(title: @title, author: @author, destination: @dest, cover_image: cover)
|
133
|
+
assert_output('', /not found/) { ei.run }
|
134
|
+
refute_path_exists File.join(@dest, 'OEBPS', 'cover.xhtml')
|
116
135
|
opf = File.read(File.join(@dest, 'OEBPS', 'package.opf'))
|
136
|
+
|
117
137
|
refute_includes opf, 'cover-image'
|
118
138
|
end
|
119
139
|
end
|
data/test/pack_ebook_test.rb
CHANGED
@@ -17,14 +17,16 @@ class PackEbookTest < Minitest::Test
|
|
17
17
|
# Create minimal EPUB directory
|
18
18
|
File.write(File.join(@epub_dir, 'mimetype'), 'application/epub+zip')
|
19
19
|
FileUtils.mkdir_p(File.join(@epub_dir, 'META-INF'))
|
20
|
-
File.write(File.join(@epub_dir, 'META-INF', 'container.xml'), '<container/>'
|
20
|
+
File.write(File.join(@epub_dir, 'META-INF', 'container.xml'), '<container/>')
|
21
21
|
FileUtils.mkdir_p(File.join(@epub_dir, 'OEBPS'))
|
22
|
-
File.write(File.join(@epub_dir, 'OEBPS', 'title.xhtml'), '<html/>'
|
22
|
+
File.write(File.join(@epub_dir, 'OEBPS', 'title.xhtml'), '<html/>')
|
23
23
|
|
24
24
|
output = File.join(@tmp, 'out.epub')
|
25
|
-
EpubTools::PackEbook.new(@epub_dir, output).run
|
25
|
+
result = EpubTools::PackEbook.new(input_dir: @epub_dir, output_file: output).run
|
26
26
|
|
27
|
-
|
27
|
+
# Check return value is the output file path
|
28
|
+
assert_equal output, result
|
29
|
+
assert_path_exists output, 'Expected output EPUB to exist'
|
28
30
|
entries = []
|
29
31
|
Zip::File.open(output) do |zip|
|
30
32
|
zip.each do |entry|
|
@@ -38,13 +40,14 @@ class PackEbookTest < Minitest::Test
|
|
38
40
|
|
39
41
|
# Check presence of other files
|
40
42
|
names = entries.map { |e| e[:name] }
|
43
|
+
|
41
44
|
assert_includes names, 'META-INF/container.xml'
|
42
45
|
assert_includes names, 'OEBPS/title.xhtml'
|
43
46
|
end
|
44
47
|
|
45
48
|
def test_missing_input_dir_raises_error
|
46
49
|
assert_raises(ArgumentError) do
|
47
|
-
EpubTools::PackEbook.new(File.join(@tmp, 'nonexistent'), 'out.epub').run
|
50
|
+
EpubTools::PackEbook.new(input_dir: File.join(@tmp, 'nonexistent'), output_file: 'out.epub').run
|
48
51
|
end
|
49
52
|
end
|
50
53
|
|
@@ -53,7 +56,7 @@ class PackEbookTest < Minitest::Test
|
|
53
56
|
dir = File.join(@tmp, 'no_mime')
|
54
57
|
Dir.mkdir(dir)
|
55
58
|
assert_raises(ArgumentError) do
|
56
|
-
EpubTools::PackEbook.new(dir, 'out.epub').run
|
59
|
+
EpubTools::PackEbook.new(input_dir: dir, output_file: 'out.epub').run
|
57
60
|
end
|
58
61
|
end
|
59
62
|
|
@@ -61,8 +64,11 @@ class PackEbookTest < Minitest::Test
|
|
61
64
|
# Setup minimal structure with mimetype
|
62
65
|
File.write(File.join(@epub_dir, 'mimetype'), 'application/epub+zip')
|
63
66
|
# Run without specifying output; default is "<basename>.epub" in parent
|
64
|
-
EpubTools::PackEbook.new(@epub_dir).run
|
67
|
+
result = EpubTools::PackEbook.new(input_dir: @epub_dir).run
|
65
68
|
default_path = File.join(@tmp, 'my_epub.epub')
|
66
|
-
|
69
|
+
|
70
|
+
# Check return value is the default output path
|
71
|
+
assert_equal 'my_epub.epub', result
|
72
|
+
assert_path_exists default_path, "Expected default EPUB at \\#{default_path}"
|
67
73
|
end
|
68
74
|
end
|