sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit 1aac9d43889f0f4f4420d3632f6cc8e7ac8cd639
parent ae09300e31e5a9a22a4846a0d2ba1ec7aab0668b
Author: Dan Callaghan <djc@djc.id.au>
Date:   Sun, 17 Apr 2022 18:08:29 +1000

fix old malformed tag URI syntax before loading YAML

Fixes #577.

Diffstat:
M Manifest.txt | 1 +
M lib/sup.rb | 9 ++++++---
A test/integration/test_sup-add.rb | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 67 insertions(+), 3 deletions(-)
diff --git a/Manifest.txt b/Manifest.txt
@@ -133,6 +133,7 @@ test/gnupg_test_home/sup-test-2@foo.bar.asc
 test/integration/test_label_service.rb
 test/integration/test_maildir.rb
 test/integration/test_mbox.rb
+test/integration/test_sup-add.rb
 test/test_crypto.rb
 test/test_header_parsing.rb
 test/test_helper.rb
diff --git a/lib/sup.rb b/lib/sup.rb
@@ -139,11 +139,14 @@ module Redwood
 
   def load_yaml_obj fn, compress=false
     o = if File.exist? fn
-      if compress
-        Zlib::GzipReader.open(fn) { |f| YAML::load f }
+      raw_contents = if compress
+        Zlib::GzipReader.open(fn) { |f| f.read }
       else
-        YAML::load_file fn
+        File::open(fn) { |f| f.read }
       end
+      ## fix up malformed tag URIs created by earlier versions of sup
+      raw_contents.gsub!(/!supmua.org,2006-10-01\/(\S*)$/) { |m| "!<tag:supmua.org,2006-10-01/#{$1}>" }
+      YAML::load raw_contents
     end
     if o.is_a?(Array)
       o.each { |x| x.after_unmarshal! if x.respond_to?(:after_unmarshal!) }
diff --git a/test/integration/test_sup-add.rb b/test/integration/test_sup-add.rb
@@ -0,0 +1,60 @@
+class TestSupAdd < Minitest::Test
+
+  def setup
+    @path = Dir.mktmpdir
+  end
+
+  def teardown
+    FileUtils.rm_r @path
+  end
+
+  def test_can_add_maildir_source
+    assert system({"SUP_BASE" => @path}, "bin/sup-add", "maildir:///some/path")
+
+    generated_sources_yaml = File.read "#{@path}/sources.yaml"
+    assert_equal <<EOS, generated_sources_yaml
+---
+- !<tag:supmua.org,2006-10-01/Redwood/Maildir>
+  uri: maildir:///some/path
+  usual: true
+  archived: false
+  sync_back: true
+  id: 1
+  labels: []
+EOS
+  end
+
+  def test_fixes_old_tag_uri_syntax
+    File.write "#{@path}/sources.yaml", <<EOS
+---
+- !supmua.org,2006-10-01/Redwood/Maildir
+  uri: maildir:/some/path
+  usual: true
+  archived: false
+  sync_back: true
+  id: 1
+  labels: []
+EOS
+    assert system({"SUP_BASE" => @path}, "bin/sup-add", "maildir:///other/path")
+
+    generated_sources_yaml = File.read "#{@path}/sources.yaml"
+    assert_equal <<EOS, generated_sources_yaml
+---
+- !<tag:supmua.org,2006-10-01/Redwood/Maildir>
+  uri: maildir:/some/path
+  usual: true
+  archived: false
+  sync_back: true
+  id: 1
+  labels: []
+- !<tag:supmua.org,2006-10-01/Redwood/Maildir>
+  uri: maildir:///other/path
+  usual: true
+  archived: false
+  sync_back: true
+  id: 2
+  labels: []
+EOS
+  end
+
+end