File: tc_dhelpdocumentpool.rb

package info (click to toggle)
dhelp 0.6.27
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 580 kB
  • sloc: ruby: 1,193; sh: 551; perl: 372; makefile: 77
file content (173 lines) | stat: -rw-r--r-- 6,760 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
require 'test/unit'
require 'dhelp'
require 'fileutils'
require 'set'

class TC_DhelpDocumentPool < Test::Unit::TestCase

  TEST_DOC_BASE_DIR     = 'test/doc-base-pool'
  TEST_DOC_DIR_DATABASE = 'test/dddb'
  TEST_INDEX_FILE       = 'test/index.swish++'
  TEST_PENDING_FILE     = 'test/pending.list'
  TEST_INDEXER_CONFIG   = 'config/swish++.conf'

  def doc_base_document(path)
    Dhelp::DocBaseDocument.new(TEST_DOC_BASE_DIR + "/#{path}")
  end

  def doc_id_set_in_pool(pool)
    doc_id_set = Set.new
    pool.each do |doc|
      doc_id_set << doc.document
    end
    doc_id_set
  end

  def setup
    @pool = Dhelp::DhelpDocumentPool.new(
              :doc_base_dir         => [TEST_DOC_BASE_DIR],
              :doc_dir_database     => TEST_DOC_DIR_DATABASE,
              :index_file           => TEST_INDEX_FILE,
              :indexer_config_file  => TEST_INDEXER_CONFIG,
              :pending_file         => TEST_PENDING_FILE
              )
    @doc_base_id_set = Set.new(['docbook-xsl-doc-html',
                                'pica-manual',
                                'pica-manual-2'])
  end

  def test_each
    assert_equal @doc_base_id_set, doc_id_set_in_pool(@pool)
  end

  def test_deregistration
    deregistered_docs = ['pica-manual']
    deregistered_docs.each do |doc|
      @pool.deregister(TEST_DOC_BASE_DIR + "/#{doc}")
    end
    assert_equal @doc_base_id_set - deregistered_docs,
      doc_id_set_in_pool(@pool)
  end

  def test_registration
    index_file = 'test/share-doc/pica/manual.html/index.html'
    doc_id     = 'pica-manual'
    doc        = doc_base_document(doc_id)
    # Make sure we're expecting the correct directory
    assert_equal index_file,
      doc.formats.find {|f| f.format.downcase == 'html'}.index
    # Register document, see if the containing directory is added
    @pool.register(doc)
    ddd = Dhelp::DocDirDatabase.open(DBM::READER, TEST_DOC_DIR_DATABASE)
    assert_equal doc_id, ddd.info_for_path(File.dirname(index_file)).first
    ddd.close
  end

  def test_section_tree
    expected_section_tree = {
      'Apps'  => {:documents   => [],
                  :subsections => {
                     'Text' => {:documents => [doc_base_document('docbook-xsl-doc-html')], :subsections => {}}}},
      'Admin' => {:documents   => [doc_base_document('pica-manual'),
                                   doc_base_document('pica-manual-2')],
                  :subsections => {}}}
    actual_section_tree   = @pool.section_tree
    # Can't compare the whole thing because the memory addresses for the
    # DocBaseDocument objects are different :-(
    assert_equal expected_section_tree.keys, actual_section_tree.keys
    assert_equal expected_section_tree['Apps'][:subsections].keys,
                 actual_section_tree['Apps'][:subsections].keys
    assert_equal expected_section_tree['Apps'][:subsections]['Text'][:documents].map {|d| d.document},
                 actual_section_tree['Apps'][:subsections]['Text'][:documents].map {|d| d.document}
    assert_equal expected_section_tree['Admin'][:documents].map {|d| d.document},
                 actual_section_tree['Admin'][:documents].map {|d| d.document}
  end

  def test_doc_base_dirs
    @pool.register(doc_base_document('docbook-xsl-doc-html'))
    dddbh1 = Dhelp::DocDirDatabase.open(DBM::READER, TEST_DOC_DIR_DATABASE)
    assert dddbh1.include?('test/share-doc/docbook-xsl-doc-html/doc'),
           "The docbook-xsl-doc-html directory should be registered"
    assert !dddbh1.include?('test/share-doc/pica/manual.html'),
           "The pica-manual directory should NOT be registered"
    dddbh1.close

    @pool.register(doc_base_document('pica-manual'))
    dddbh2 = Dhelp::DocDirDatabase.open(DBM::READER, TEST_DOC_DIR_DATABASE)
    assert dddbh2.include?('test/share-doc/docbook-xsl-doc-html/doc'),
           "The docbook-xsl-doc-html directory should still be registered"
    assert dddbh2.include?('test/share-doc/pica/manual.html'),
           "The pica-manual directory should be registered"
    dddbh2.close
  end

  def test_rebuild_indexing
    # Register all documents, all will be indexed
    @pool.rebuild
    # Now, once everything is indexed, force a reindexing (like the cron job)
    FileUtils.rm_f TEST_INDEX_FILE
    @pool.rebuild
    assert File.exist?(TEST_INDEX_FILE),
           "Index file should exist after rebuilding"
    assert(File.size(TEST_INDEX_FILE) > 0,
           "Index file should have non-zero size after rebuilding")
  end

  def test_rebuild_dirs
    # 1) Create a temporary directory with some documents
    tmp_dir     = 'test/tmp/doc-base-pool'
    dddb        = 'test/tmp/doc-base_dirs'
    index_file  = 'test/tmp/index'
    config_file = TEST_INDEXER_CONFIG
    FileUtils.mkdir_p tmp_dir
    FileUtils.cp 'test/doc-base/dir-test-1', tmp_dir
    FileUtils.cp 'test/doc-base/dir-test-2', tmp_dir
    # Create directories for documentation
    FileUtils.mkdir_p 'test/tmp/share-doc/dir-test-1/manual.html'
    FileUtils.mkdir_p 'test/tmp/share-doc/dir-test-2/manual.html'

    tmp_pool = Dhelp::DhelpDocumentPool.new(
                  :doc_base_dir         => [tmp_dir],
                  :doc_dir_database     => dddb,
                  :index_file           => index_file,
                  :indexer_config_file  => config_file
                  )
    # 2) Register everything
    tmp_pool.rebuild
    # 3) Deregister one of those documents
    path_to_deregister = "test/tmp/doc-base-pool/dir-test-1"
    tmp_pool.deregister(path_to_deregister)
    # 4) Delete that document
    FileUtils.rm path_to_deregister
    # 5) Rebuild, check that the directory database doesn't include the deleted
    #    document
    tmp_pool.rebuild
    doc_dir_db = Dhelp::DocDirDatabase.open(DBM::READER, dddb)
    assert !doc_dir_db.include?('test/tmp/share-doc/dir-test-1/manual.html'),
           "The directory for dir-test-1 should NOT exist"
    assert doc_dir_db.include?('test/tmp/share-doc/dir-test-2/manual.html'),
           "The directory for dir-test-2 should still exist"
    doc_dir_db.close

    # Cleanup
    FileUtils.rm_rf 'test/tmp'
  end

  def test_register_dirs
    doc    = Dhelp::DocBaseDocument.new('test/doc-base/ghc6-users-guide')
    @pool.register(doc)
    ddd = Dhelp::DocDirDatabase.open(DBM::READER, TEST_DOC_DIR_DATABASE)
    assert ddd.include?('test/share-doc/ghc6-doc/html/libraries'),
           "Docdir should include the libraries subdir"
    assert ddd.include?('test/share-doc/ghc6-doc/html'),
           "Docdir should include the index file subdir"
  end

  def teardown
    @pool = nil
    @doc_base_id_set = nil
    FileUtils.rm_f Dir.glob("#{TEST_DOC_DIR_DATABASE}.*")
    FileUtils.rm_f TEST_INDEX_FILE
    FileUtils.rm_f TEST_PENDING_FILE
  end
end