From 73fdb8aa8f25c8ab4084a66f4c27ab92f8d23c8c Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Sun, 17 Apr 2022 18:08:29 +1000 Subject: [PATCH] fix old malformed tag URI syntax before loading YAML Fixes #577. --- lib/sup.rb | 9 +++-- test/integration/test_sup-add.rb | 60 ++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 test/integration/test_sup-add.rb diff --git a/lib/sup.rb b/lib/sup.rb index 209ddea99..b1eb8791e 100644 --- a/lib/sup.rb +++ b/lib/sup.rb @@ -139,11 +139,14 @@ def save_yaml_obj o, fn, safe=false, backup=false 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| "!" } + 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 new file mode 100644 index 000000000..291f0aaaf --- /dev/null +++ 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 < + 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", < @path}, "bin/sup-add", "maildir:///other/path") + + generated_sources_yaml = File.read "#{@path}/sources.yaml" + assert_equal < + uri: maildir:/some/path + usual: true + archived: false + sync_back: true + id: 1 + labels: [] +- ! + uri: maildir:///other/path + usual: true + archived: false + sync_back: true + id: 2 + labels: [] +EOS + end + +end