From 81041d04f8a0cff9c309f924cea67ca0ed8f1fde Mon Sep 17 00:00:00 2001 From: Scott Chacon Date: Mon, 21 Feb 2011 14:10:38 -0800 Subject: [PATCH] tests for document generation --- lib/git-scribe/generate.rb | 60 +++++++++++++++-------------- test/gen_test.rb | 78 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 29 deletions(-) create mode 100644 test/gen_test.rb diff --git a/lib/git-scribe/generate.rb b/lib/git-scribe/generate.rb index abf3c6a..f8f23c1 100644 --- a/lib/git-scribe/generate.rb +++ b/lib/git-scribe/generate.rb @@ -9,18 +9,20 @@ def gen(args = []) types = type == 'all' ? OUTPUT_TYPES : [type] + ret = false output = [] Dir.chdir("output") do types.each do |out_type| call = 'do_' + out_type if self.respond_to? call - self.send call + ret = self.send call else - puts "NOT A THING: #{call}" + die "NOT A THING: #{call}" end end # clean up `rm #{BOOK_FILE}` + ret end end @@ -28,9 +30,7 @@ def prepare_output_dir Dir.mkdir('output') rescue nil Dir.chdir('output') do Dir.mkdir('stylesheets') rescue nil - puts SCRIBE_ROOT from_stdir = File.join(SCRIBE_ROOT, 'stylesheets') - pp from_stdir FileUtils.cp_r from_stdir, '.' end end @@ -44,18 +44,18 @@ def a2x_wss(type) end def do_pdf - puts "GENERATING PDF" + info "GENERATING PDF" # TODO: syntax highlighting (fop?) - puts `asciidoc -b docbook #{BOOK_FILE}` + ex("asciidoc -b docbook #{BOOK_FILE}") strparams = {'callout.graphics' => 0, 'navig.graphics' => 0, 'admon.textlabel' => 1, 'admon.graphics' => 0} param = strparams.map { |k, v| "--stringparam #{k} #{v}" }.join(' ') - puts cmd = "xsltproc --nonet #{param} --output #{local('book.fo')} #{base('docbook-xsl/fo.xsl')} #{local('book.xml')}" - puts `#{cmd}` + cmd = "xsltproc --nonet #{param} --output #{local('book.fo')} #{base('docbook-xsl/fo.xsl')} #{local('book.xml')}" + ex(cmd) cmd = "fop -fo #{local('book.fo')} -pdf #{local('book.pdf')}" - puts `#{cmd}` + ex(cmd) #puts `#{a2x('pdf')} -v --fop #{BOOK_FILE}` if $?.exitstatus == 0 'book.pdf' @@ -63,30 +63,30 @@ def do_pdf end def do_epub - puts "GENERATING EPUB" + info "GENERATING EPUB" # TODO: look for custom stylesheets - `#{a2x_wss('epub')} -v #{BOOK_FILE}` - puts 'exit status', $?.exitstatus - 'book.epub' + cmd = "#{a2x_wss('epub')} -v #{BOOK_FILE}" + if ex(cmd) + 'book.epub' + end end def do_html - puts "GENERATING HTML" + info "GENERATING HTML" # TODO: look for custom stylesheets #puts `#{a2x_wss('xhtml')} -v #{BOOK_FILE}` styledir = local('stylesheets') - puts cmd = "asciidoc -a stylesdir=#{styledir} -a theme=handbookish #{BOOK_FILE}" - `#{cmd}` - puts 'exit status', $?.exitstatus + cmd = "asciidoc -a stylesdir=#{styledir} -a theme=handbookish #{BOOK_FILE}" + ex(cmd) 'book.html' end def do_site - puts "GENERATING SITE" + info "GENERATING SITE" # TODO: check if html was already done - puts `asciidoc -b docbook #{BOOK_FILE}` + ex("asciidoc -b docbook #{BOOK_FILE}") xsldir = base('docbook-xsl/xhtml') - `xsltproc --stringparam html.stylesheet stylesheets/handbookish.css --nonet #{xsldir}/chunk.xsl book.xml` + ex("xsltproc --stringparam html.stylesheet stylesheets/handbookish.css --nonet #{xsldir}/chunk.xsl book.xml") source = File.read('index.html') html = Nokogiri::HTML.parse(source, nil, 'utf-8') @@ -121,17 +121,13 @@ def do_site end end end - puts end - pp sections - book_title = html.css('head > title').text content = html.css('body > div')[1] content.css('.toc').first.remove content = content.inner_html - puts content sections.each do |s| content.gsub!(s['href'], s['link']) end @@ -199,15 +195,15 @@ def do_site end #File.unlink(section['href']) - puts i - puts section['title'] - puts section['href'] - puts section['link'] - puts + info i + info section['title'] + info section['href'] + info section['link'] end #File.unlink end + sections end # create a new file by concatenating all the ones we find @@ -216,5 +212,11 @@ def gather_and_process FileUtils.cp_r files, 'output' end + def ex(command) + out = `#{command} 2>&1` + info out + $?.exitstatus == 0 + end + end end diff --git a/test/gen_test.rb b/test/gen_test.rb new file mode 100644 index 0000000..441d2af --- /dev/null +++ b/test/gen_test.rb @@ -0,0 +1,78 @@ +require File.expand_path "../test_helper", __FILE__ + +context "scribe gen tests" do + setup do + @scribe = GitScribe.new + end + + test "will not respond to non-thing" do + assert_raise RuntimeError do + @scribe.gen('mofo') + end + end + + test "scribe can generate single page html" do + in_temp_dir do + @scribe.init('t') + Dir.chdir('t') do + file = @scribe.gen('html') + assert_equal 'book.html', file + out = Dir.glob('output/**/*') + assert out.include? 'output/book.html' + assert out.include? 'output/image' + assert out.include? 'output/stylesheets/handbookish.css' + end + end + end + + test "scribe can generate site html" do + in_temp_dir do + @scribe.init('t') + Dir.chdir('t') do + data = @scribe.gen('site') + out = Dir.glob('output/**/*') + assert out.include? 'output/index.html' + assert out.include? 'output/the_first_chapter.html' + assert out.include? 'output/the_second_chapter.html' + assert out.include? 'output/image' + assert out.include? 'output/stylesheets/handbookish.css' + end + end + end + + test "scribe can generate a pdf" do + in_temp_dir do + @scribe.init('t') + Dir.chdir('t') do + data = @scribe.gen('pdf') + assert_equal data, 'book.pdf' + out = Dir.glob('output/**/*') + assert out.include? 'output/book.pdf' + end + end + end + + test "scribe can generate a epub" do + in_temp_dir do + @scribe.init('t') + Dir.chdir('t') do + data = @scribe.gen('epub') + assert_equal data, 'book.epub' + out = Dir.glob('output/**/*') + assert out.include? 'output/book.epub' + end + end + end + + xtest "scribe can generate docbook" do + end + + xtest "scribe can generate a mobi" do + end + + xtest "scribe can generate all" do + end + + xtest "scribe doesn't regen already generated assets" do + end +end