sup

A curses threads-with-tags style email client

sup.git

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

test/integration/test_maildir.rb (2656B) - 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     Logger.remove_sink $stderr
     45     Index.init @path
     46     Index.load
     47     SourceManager.instance.instance_eval '@sources = {}'
     48     SourceManager.instance.add_source source
     49     PollManager.poll_from source
     50     Index.save_index
     51   end
     52 
     53   # and now, let the tests begin!
     54 
     55   def test_can_index_a_maildir_directory
     56 
     57     maildir = create_a_maildir
     58     create_a_maildir_email(File.join(maildir, 'cur'), @test_message_1)
     59     start_sup_and_add_source Maildir.new "maildir:#{maildir}"
     60 
     61     messages_in_index = []
     62     Index.instance.each_message {|a| messages_in_index << a}
     63     refute_empty messages_in_index, 'There are no messages in the index'
     64     assert_equal(messages_in_index.first.raw_message, @test_message_1)
     65 
     66   end
     67 
     68   def test_can_index_a_maildir_directory_with_special_characters
     69 
     70     maildir = create_a_maildir URI_ENCODE_CHARS
     71     create_a_maildir_email(File.join(maildir, 'cur'), @test_message_1)
     72     start_sup_and_add_source Maildir.new "maildir:#{maildir}"
     73 
     74     messages_in_index = []
     75     Index.instance.each_message {|a| messages_in_index << a}
     76     refute_empty messages_in_index, 'There are no messages in the index'
     77     assert_equal(messages_in_index.first.raw_message, @test_message_1)
     78 
     79   end
     80 
     81   def test_missing_date_header
     82     ## The message is missing a Date header so we should use its modtime
     83     ## as a fallback.
     84     fallback_date = Time.new 2004, 4, 19, 11, 12, 13
     85     maildir = create_a_maildir
     86     filename = create_a_maildir_email(File.join(maildir, 'cur'), @test_message_1)
     87     File.utime fallback_date, fallback_date, filename
     88     start_sup_and_add_source Maildir.new "maildir:#{maildir}"
     89 
     90     messages_in_index = Index.instance.enum_for(:each_message).to_a
     91     assert_equal fallback_date, messages_in_index.first.date
     92   end
     93 end
     94