sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/

test/integration/test_maildir.rb (2604B) - raw

      1 require "sup"
      2 require "test_helper"
      3 
      4 class TestMaildir < Minitest::Test
      5 
      6   include Redwood
      7 
      8   def setup
      9     @path = Dir.mktmpdir
     10 
     11     @test_message_1 = <<EOS
     12 From: Bob <bob@bob.com>
     13 To: a dear friend
     14 
     15 Hello there friend. How are you? Blah is blah blah.
     16 Wow. Maildir FTW, am I right?
     17 EOS
     18 
     19   end
     20 
     21   def teardown
     22     ObjectSpace.each_object(Class).select {|a| a < Redwood::Singleton}.each do |klass|
     23       klass.deinstantiate! unless klass == Redwood::Logger
     24     end
     25     FileUtils.rm_r @path
     26   end
     27 
     28   def create_a_maildir(extra='')
     29     maildir = File.join @path, "test_maildir#{extra}"
     30     ['', 'cur', 'new', 'tmp'].each do |dir|
     31       Dir.mkdir(File.join maildir, dir)
     32     end
     33     maildir
     34   end
     35 
     36   def create_a_maildir_email(folder, content)
     37     filename = File.join folder, "#{Time.now.to_f}.hostname:2,S"
     38     File.write filename, content
     39     filename
     40   end
     41 
     42   def start_sup_and_add_source(source)
     43     start
     44     Index.init @path
     45     Index.load
     46     SourceManager.instance.instance_eval '@sources = {}'
     47     SourceManager.instance.add_source source
     48     PollManager.poll_from source
     49   end
     50 
     51   # and now, let the tests begin!
     52 
     53   def test_can_index_a_maildir_directory
     54 
     55     maildir = create_a_maildir
     56     create_a_maildir_email(File.join(maildir, 'cur'), @test_message_1)
     57     start_sup_and_add_source Maildir.new "maildir:#{maildir}"
     58 
     59     messages_in_index = []
     60     Index.instance.each_message {|a| messages_in_index << a}
     61     refute_empty messages_in_index, 'There are no messages in the index'
     62     assert_equal(messages_in_index.first.raw_message, @test_message_1)
     63 
     64   end
     65 
     66   def test_can_index_a_maildir_directory_with_special_characters
     67 
     68     maildir = create_a_maildir URI_ENCODE_CHARS
     69     create_a_maildir_email(File.join(maildir, 'cur'), @test_message_1)
     70     start_sup_and_add_source Maildir.new "maildir:#{maildir}"
     71 
     72     messages_in_index = []
     73     Index.instance.each_message {|a| messages_in_index << a}
     74     refute_empty messages_in_index, 'There are no messages in the index'
     75     assert_equal(messages_in_index.first.raw_message, @test_message_1)
     76 
     77   end
     78 
     79   def test_missing_date_header
     80     ## The message is missing a Date header so we should use its modtime
     81     ## as a fallback.
     82     fallback_date = Time.new 2004, 4, 19, 11, 12, 13
     83     maildir = create_a_maildir
     84     filename = create_a_maildir_email(File.join(maildir, 'cur'), @test_message_1)
     85     File.utime fallback_date, fallback_date, filename
     86     start_sup_and_add_source Maildir.new "maildir:#{maildir}"
     87 
     88     messages_in_index = Index.instance.enum_for(:each_message).to_a
     89     assert_equal fallback_date, messages_in_index.first.date
     90   end
     91 end
     92