test/integration/test_mbox.rb (2306B) - raw
1 require "sup"
2 require "test_helper"
3
4 class TestMbox < Minitest::Test
5
6 include Redwood
7
8 def setup
9 @path = Dir.mktmpdir
10
11 @test_message_1 = <<EOS
12 From sup-talk-bounces@rubyforge.org Mon Apr 27 12:56:18 2009
13 From: Bob <bob@bob.com>
14 To: Joe <joe@joe.com>
15
16 Hello there friend. How are you? Blah is blah blah.
17 I like mboxes, don't you?
18 EOS
19
20 end
21
22 def teardown
23 ObjectSpace.each_object(Class).select {|a| a < Redwood::Singleton}.each do |klass|
24 klass.deinstantiate! unless klass == Redwood::Logger
25 end
26 FileUtils.rm_r @path
27 end
28
29 def create_a_mbox(extra='')
30 mbox = File.join(@path, "test_mbox#{extra}.mbox")
31 File.write(mbox, @test_message_1)
32 mbox
33 end
34
35 def start_sup_and_add_source(source)
36 start
37 Index.init @path
38 Index.load
39 SourceManager.instance.instance_eval '@sources = {}'
40 SourceManager.instance.add_source source
41 PollManager.poll_from source
42 end
43
44 # and now, let the tests begin!
45
46 def test_can_index_a_mbox_directory
47
48 mbox = create_a_mbox
49 start_sup_and_add_source MBox.new "mbox:#{mbox}"
50
51 messages_in_index = []
52 Index.instance.each_message {|a| messages_in_index << a}
53 refute_empty messages_in_index, 'There are no messages in the index'
54 test_message_without_first_line = @test_message_1.sub(/^.*\n/,'')
55 assert_equal(messages_in_index.first.raw_message, test_message_without_first_line)
56
57 end
58
59 def test_can_index_a_mbox_directory_with_special_characters
60
61 mbox = create_a_mbox URI_ENCODE_CHARS
62 start_sup_and_add_source MBox.new "mbox:#{mbox}"
63
64 messages_in_index = []
65 Index.instance.each_message {|a| messages_in_index << a}
66 refute_empty messages_in_index, 'There are no messages in the index'
67 test_message_without_first_line = @test_message_1.sub(/^.*\n/,'')
68 assert_equal(messages_in_index.first.raw_message, test_message_without_first_line)
69
70 end
71
72 def test_missing_date_header
73 ## The message is missing a Date header so we should use envelope date
74 ## stored in the From line as a fallback.
75 fallback_date = Time.new 2009, 4, 27, 12, 56, 18
76 mbox = create_a_mbox
77 start_sup_and_add_source MBox.new "mbox:#{mbox}"
78
79 messages_in_index = Index.instance.enum_for(:each_message).to_a
80 assert_equal fallback_date, messages_in_index.first.date
81 end
82 end