test/integration/test_mbox.rb (2358B) - 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 Logger.remove_sink $stderr
38 Index.init @path
39 Index.load
40 SourceManager.instance.instance_eval '@sources = {}'
41 SourceManager.instance.add_source source
42 PollManager.poll_from source
43 Index.save_index
44 end
45
46 # and now, let the tests begin!
47
48 def test_can_index_a_mbox_directory
49
50 mbox = create_a_mbox
51 start_sup_and_add_source MBox.new "mbox:#{mbox}"
52
53 messages_in_index = []
54 Index.instance.each_message {|a| messages_in_index << a}
55 refute_empty messages_in_index, 'There are no messages in the index'
56 test_message_without_first_line = @test_message_1.sub(/^.*\n/,'')
57 assert_equal(messages_in_index.first.raw_message, test_message_without_first_line)
58
59 end
60
61 def test_can_index_a_mbox_directory_with_special_characters
62
63 mbox = create_a_mbox URI_ENCODE_CHARS
64 start_sup_and_add_source MBox.new "mbox:#{mbox}"
65
66 messages_in_index = []
67 Index.instance.each_message {|a| messages_in_index << a}
68 refute_empty messages_in_index, 'There are no messages in the index'
69 test_message_without_first_line = @test_message_1.sub(/^.*\n/,'')
70 assert_equal(messages_in_index.first.raw_message, test_message_without_first_line)
71
72 end
73
74 def test_missing_date_header
75 ## The message is missing a Date header so we should use envelope date
76 ## stored in the From line as a fallback.
77 fallback_date = Time.new 2009, 4, 27, 12, 56, 18
78 mbox = create_a_mbox
79 start_sup_and_add_source MBox.new "mbox:#{mbox}"
80
81 messages_in_index = Index.instance.enum_for(:each_message).to_a
82 assert_equal fallback_date, messages_in_index.first.date
83 end
84 end