<commit_before>require 'helper'

require 'tmpdir'
require 'fileutils'

#
# End-to-end test for mktoc
#
module ManBookTest
  class TestMkToc < ManBookTest::TestCase
    #
    # command under test
    #
    APP_SCRIPT = 'bin/mktoc'

    #
    # generator identification
    #
    GENERATOR = "mktoc v#{ManBook::VERSION}"

    #
    # generated work products (in addition to HTML input files that were created externally)
    #
    WORKPRODUCTS = {:html  => 'index.html',
                    :ncx   => 'index.ncx',
                    :opf   => 'index.opf',
                    :about => 'about.html'}

    #
    # which method tests what work product
    #
    WORK_PRODUCT_TESTS = {:html  => 'test_workproduct_html',
                          :ncx   => 'test_workproduct_ncx',
                          :opf   => 'test_workproduct_opf',
                          :about => 'test_workproduct_about',
                          :cover => 'test_workproduct_cover'}

    COVER_IMAGE_ALT = File.join(FIXTURES_DIR, 'alt-cover.jpg')

    def setup
      super
      FileUtils.cp_r(File.join(FIXTURES_DIR, '.'), output_dir)
      @fixtures = Dir.glob(File.join(FIXTURES_DIR, '*.html')).map{|f| File.basename(f)}
    end

    def test_no_args
      assert_exec("#{app_script}", false, nil, /ERROR: Directory argument missing/)
    end

    def test_defaults
      assert_exec("#{app_script} #{output_dir}")
      test_all_workproducts(ManBook::TITLE_DEFAULT, ManBook::AUTHOR_DEFAULT, File.basename(ManBook::COVER_IMAGE_DEFAULT))
    end

    def test_overridden_title
      title = "Foo42Bar"
      assert_exec("#{app_script} #{output_dir} --title \"#{title}\"")
      test_all_workproducts(title, ManBook::AUTHOR_DEFAULT, File.basename(ManBook::COVER_IMAGE_DEFAULT))
    end

    def test_overridden_author
      author = "<NAME>"
      assert_exec("#{app_script} #{output_dir} --author \"#{author}\"")
      test_all_workproducts(ManBook::TITLE_DEFAULT, author, File.basename(ManBook::COVER_IMAGE_DEFAULT))
    end

    def test_overridden_title_and_author
      title = "Foo42Bar42"
      author = "<NAME>"
      assert_exec("#{app_script} #{output_dir} --title \"#{title}\" --author \"#{author}\"")
      test_all_workproducts(title, author, File.basename(ManBook::COVER_IMAGE_DEFAULT))
    end

    def test_no_cover_image
      assert_exec("#{app_script} #{output_dir} --no-cover-image")
      test_all_workproducts(ManBook::TITLE_DEFAULT, ManBook::AUTHOR_DEFAULT, nil)
    end

    def test_alt_cover_image
      cover_image = COVER_IMAGE_ALT
      assert(File.exist?(cover_image))
      assert_exec("#{app_script} #{output_dir} --cover-image #{cover_image}")
      test_all_workproducts(ManBook::TITLE_DEFAULT, ManBook::AUTHOR_DEFAULT, File.basename(cover_image))
    end

    def test_alt_cover_image_not_found
      cover_image = "DOES_NOT_EXIST"
      assert(!File.exist?(cover_image))
      assert_exec("#{app_script} #{output_dir} --cover-image #{cover_image}", false, nil, /ERROR: Could not find cover image/)
    end

  private
    def test_all_workproducts(title, author, cover_image = nil)
      if cover_image.nil?
        workproducts = WORKPRODUCTS
      else
        workproducts = WORKPRODUCTS.merge({:cover => cover_image})
      end

      assert_equal(@fixtures.size + workproducts.size, Dir.glob(File.join(output_dir, '*')).size)

      workproducts.each{|k,v|
        vf = File.join(output_dir, v)
        assert(File.exist?(vf), "Expect #{vf} to exist")

        # dispatch to test that is specific to the work product
        wp_test = WORK_PRODUCT_TESTS[k]
        raise "No test defined for work product #{k}" if wp_test.nil?
        send(wp_test, title, cover_image)
      }
    end

    def assert_exec(cmd, assert_status_success = true, re_stdout = nil, re_stderr = nil)
      status = Open4::popen4(cmd){|pid, stdin, stdout, stderr|
        if re_stdout.nil?
          assert_empty(stdout.read)
        else
          assert_match(re_stdout, stdout.read)
        end

        if re_stderr.nil?
          assert_empty(stderr.read)
        else
          assert_match(re_stderr, stderr.read)
        end
      }

      if assert_status_success<commit_msg>Add missing author to the concrete workproduct test call<commit_after>require 'helper'

require 'tmpdir'
require 'fileutils'

#
# End-to-end test for mktoc
#
module ManBookTest
  class TestMkToc < ManBookTest::TestCase
    #
    # command under test
    #
    APP_SCRIPT = 'bin/mktoc'

    #
    # generator identification
    #
    GENERATOR = "mktoc v#{ManBook::VERSION}"

    #
    # generated work products (in addition to HTML input files that were created externally)
    #
    WORKPRODUCTS = {:html  => 'index.html',
                    :ncx   => 'index.ncx',
                    :opf   => 'index.opf',
                    :about => 'about.html'}

    #
    # which method tests what work product
    #
    WORK_PRODUCT_TESTS = {:html  => 'test_workproduct_html',
                          :ncx   => 'test_workproduct_ncx',
                          :opf   => 'test_workproduct_opf',
                          :about => 'test_workproduct_about',
                          :cover => 'test_workproduct_cover'}

    COVER_IMAGE_ALT = File.join(FIXTURES_DIR, 'alt-cover.jpg')

    def setup
      super
      FileUtils.cp_r(File.join(FIXTURES_DIR, '.'), output_dir)
      @fixtures = Dir.glob(File.join(FIXTURES_DIR, '*.html')).map{|f| File.basename(f)}
    end

    def test_no_args
      assert_exec("#{app_script}", false, nil, /ERROR: Directory argument missing/)
    end

    def test_defaults
      assert_exec("#{app_script} #{output_dir}")
      test_all_workproducts(ManBook::TITLE_DEFAULT, ManBook::AUTHOR_DEFAULT, File.basename(ManBook::COVER_IMAGE_DEFAULT))
    end

    def test_overridden_title
      title = "Foo42Bar"
      assert_exec("#{app_script} #{output_dir} --title \"#{title}\"")
      test_all_workproducts(title, ManBook::AUTHOR_DEFAULT, File.basename(ManBook::COVER_IMAGE_DEFAULT))
    end

    def test_overridden_author
      author = "<NAME>"
      assert_exec("#{app_script} #{output_dir} --author \"#{author}\"")
      test_all_workproducts(ManBook::TITLE_DEFAULT, author, File.basename(ManBook::COVER_IMAGE_DEFAULT))
    end

    def test_overridden_title_and_author
      title = "Foo42Bar42"
      author = "<NAME>"
      assert_exec("#{app_script} #{output_dir} --title \"#{title}\" --author \"#{author}\"")
      test_all_workproducts(title, author, File.basename(ManBook::COVER_IMAGE_DEFAULT))
    end

    def test_no_cover_image
      assert_exec("#{app_script} #{output_dir} --no-cover-image")
      test_all_workproducts(ManBook::TITLE_DEFAULT, ManBook::AUTHOR_DEFAULT, nil)
    end

    def test_alt_cover_image
      cover_image = COVER_IMAGE_ALT
      assert(File.exist?(cover_image))
      assert_exec("#{app_script} #{output_dir} --cover-image #{cover_image}")
      test_all_workproducts(ManBook::TITLE_DEFAULT, ManBook::AUTHOR_DEFAULT, File.basename(cover_image))
    end

    def test_alt_cover_image_not_found
      cover_image = "DOES_NOT_EXIST"
      assert(!File.exist?(cover_image))
      assert_exec("#{app_script} #{output_dir} --cover-image #{cover_image}", false, nil, /ERROR: Could not find cover image/)
    end

  private
    def test_all_workproducts(title, author, cover_image = nil)
      if cover_image.nil?
        workproducts = WORKPRODUCTS
      else
        workproducts = WORKPRODUCTS.merge({:cover => cover_image})
      end

      assert_equal(@fixtures.size + workproducts.size, Dir.glob(File.join(output_dir, '*')).size)

      workproducts.each{|k,v|
        vf = File.join(output_dir, v)
        assert(File.exist?(vf), "Expect #{vf} to exist")

        # dispatch to test that is specific to the work product
        wp_test = WORK_PRODUCT_TESTS[k]
        raise "No test defined for work product #{k}" if wp_test.nil?
        send(wp_test, title, author, cover_image)
      }
    end

    def assert_exec(cmd, assert_status_success = true, re_stdout = nil, re_stderr = nil)
      status = Open4::popen4(cmd){|pid, stdin, stdout, stderr|
        if re_stdout.nil?
          assert_empty(stdout.read)
        else
          assert_match(re_stdout, stdout.read)
        end

        if re_stderr.nil?
          assert_empty(stderr.read)
        else
          assert_match(re_stderr, stderr.read)
        end
      }

      if assert_status_success
{'fraction_non_alphanumeric': 0.11766805472932779, 'fraction_numerical': 0.0019036287923854848, 'mean_word_length': 2.841864716636197, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1516438', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2689, 'n_words': 524}