Skip to content

Commit

Permalink
fix old malformed tag URI syntax before loading YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
danc86 committed Apr 17, 2022
1 parent bcbca3f commit 73fdb8a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/sup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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| "!<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!) }
Expand Down
60 changes: 60 additions & 0 deletions test/integration/test_sup-add.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 73fdb8a

Please sign in to comment.