Skip to content

Commit

Permalink
update to metaheader v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cfillion committed Feb 20, 2020
1 parent 2238146 commit 7a6259e
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 23 deletions.
14 changes: 11 additions & 3 deletions lib/reapack/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,16 @@ def scan(path, contents)
type = self.class.type_of path
return unless type

mh = MetaHeader.parse contents
mh.strict = @strict
mh = if contents.is_a? MetaHeader
contents
else
mh = MetaHeader.parse contents
unless mh.has? :changelog
wp = WordpressChangelog.new mh
wp.parse contents
end
mh
end

if mh[:noindex]
remove path
Expand All @@ -139,7 +147,7 @@ def scan(path, contents)
scanner = Scanner.new cat, pkg, mh, self

begin
scanner.run
scanner.run @strict
rescue Error
backups.each {|var, value| instance_variable_set var, value }
raise
Expand Down
2 changes: 1 addition & 1 deletion lib/reapack/index/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def new_content
end

def new_header
@new_header ||= MetaHeader.new @new_content if new_content
@new_header ||= MetaHeader.parse @new_content if new_content
end
end
end
19 changes: 11 additions & 8 deletions lib/reapack/index/parsers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class WordpressChangelog < MetaHeader::Parser
class WordpressChangelog
CHANGELOG = /
Changelog\s*:\n
(.+?)\n\s*
Expand All @@ -7,27 +7,30 @@ class WordpressChangelog < MetaHeader::Parser

VERSION = /\A[\s\*]*v([\d\.]+)(?:\s+(.+))?\Z/.freeze

def initialize(mh)
@header = mh
end

def parse(input)
input = input.read
input.encode! Encoding::UTF_8, invalid: :replace

ver, changes = header[:version], header[:changelog]
ver, changes = @header[:version], @header[:changelog]
return if ver.nil? || changes || CHANGELOG.match(input).nil?

$1.lines.each {|line| read line.lstrip }
end

def read(line)
if line =~ VERSION
@current = $1 == header[:version]
@current = $1 == @header[:version]
elsif @current
if header[:changelog]
header[:changelog] += "\n"
if @header[:changelog]
@header[:changelog] += "\n"
else
header[:changelog] = String.new
@header[:changelog] = String.new
end

header[:changelog] += line.chomp
@header[:changelog] += line.chomp
end
end
end
6 changes: 4 additions & 2 deletions lib/reapack/index/scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ def initialize(cat, pkg, mh, index)
@self_overriden = false
end

def run
def run(strict = false)
@mh.alias HEADER_ALIASES

if errors = @mh.validate(HEADER_RULES)
errors = @mh.validate(HEADER_RULES, strict)

unless errors.empty?
raise Error, errors.join("\n")
end

Expand Down
2 changes: 1 addition & 1 deletion reapack-index.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'addressable', '~> 2.4'
spec.add_runtime_dependency 'colorize', '~> 0.7'
spec.add_runtime_dependency 'gitable', '~> 0.3'
spec.add_runtime_dependency 'metaheader', '~> 1.3'
spec.add_runtime_dependency 'metaheader', '~> 2.0'
spec.add_runtime_dependency 'nokogiri', '~> 1.7'
spec.add_runtime_dependency 'pandoc-ruby', '~> 2.0'
spec.add_runtime_dependency 'rugged', '~> 0.25'
Expand Down
4 changes: 2 additions & 2 deletions test/scanner/test_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def setup
@pkg.expect :type, :script
@pkg.expect :path, 'cat/test'

@mh = MetaHeader.new String.new
@mh = MetaHeader.new
@mh[:version] = '1.0'

@index = MiniTest::Mock.new
Expand All @@ -20,7 +20,7 @@ def setup
def test_validation
mh_mock = MiniTest::Mock.new
mh_mock.expect :alias, nil, [Hash]
mh_mock.expect :validate, ['first', 'second'], [Hash]
mh_mock.expect :validate, ['first', 'second'], [Hash, false]

@scanner.instance_variable_set :@mh, mh_mock

Expand Down
19 changes: 14 additions & 5 deletions test/test_parsers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class TestParsers < MiniTest::Test
def test_wordpress
mh = MetaHeader.new <<-IN
input = <<-IN
/**
* Version: 1.1
*/
Expand All @@ -23,12 +23,17 @@ def test_wordpress
Test\x97
IN

mh = MetaHeader.parse input
assert_equal '1.1', mh[:version]
refute mh.has?(:changelog)

parser = WordpressChangelog.new mh
parser.parse input
assert_equal "+ Line 3\n+ Line 4", mh[:changelog]
end

def test_wordpress_no_date
mh = MetaHeader.new <<-IN
input = <<-IN
/**
* Version: 1.1
*/
Expand All @@ -49,12 +54,14 @@ def test_wordpress_no_date
Test\x97
IN

assert_equal '1.1', mh[:version]
mh = MetaHeader.parse input
parser = WordpressChangelog.new mh
parser.parse input
assert_equal "+ Line 3\n+ Line 4", mh[:changelog]
end

def test_wordpress_noprefix
mh = MetaHeader.new <<-IN
input = <<-IN
--[[
Version: 1.1
--]]
Expand All @@ -76,7 +83,9 @@ def test_wordpress_noprefix
Test\x97
IN

assert_equal '1.1', mh[:version]
mh = MetaHeader.parse input
parser = WordpressChangelog.new mh
parser.parse input
assert_equal "+ Line 3\n+ Line 4", mh[:changelog]
end
end
2 changes: 1 addition & 1 deletion test/test_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TestScanner < MiniTest::Test
def setup
@mock = MiniTest::Mock.new

@mh = MetaHeader.new String.new
@mh = MetaHeader.new
@mh[:version] = '1.0'

@doc = Nokogiri::XML <<-XML
Expand Down

0 comments on commit 7a6259e

Please sign in to comment.