xcop 0.4 → 0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.simplecov +1 -1
- data/bin/xcop +10 -5
- data/features/cli.feature +11 -0
- data/lib/xcop.rb +23 -6
- data/lib/xcop/version.rb +1 -1
- data/test/test_xcop.rb +11 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c3b67fdc37db666afbd54a7c894889412530d1fb
|
4
|
+
data.tar.gz: b5c833f2a4a439ed751a84862577d3cd0f955e36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 903d1596af3e61c7eea9c33a6c0d4df519f7781a8c9013f01ce03a8c1e698c6d31e861da5b4662ccede4472618d1e4c3f1dc83e96c3e8d5673db8104b5896bfa
|
7
|
+
data.tar.gz: 2ba85e0319b65ac9f6c03583ea7573dde044686baa941f7eb0447febc19257da11570fd7617efe902e005a489b9981a68108083718c7d7693c0a3e77a56b4e4b
|
data/.simplecov
CHANGED
data/bin/xcop
CHANGED
@@ -32,6 +32,7 @@ opts = Slop.parse(ARGV, strict: true, help: true) do |o|
|
|
32
32
|
o.banner = "Usage (#{Xcop::VERSION}): xcop [options] [files...]"
|
33
33
|
o.bool '-h', '--help', 'Show these instructions'
|
34
34
|
o.bool '--version', 'Show current version'
|
35
|
+
o.bool '--fix', 'Fix all files instead of reporting their problems'
|
35
36
|
o.string '--license', 'File name of the boilerplate head license'
|
36
37
|
end
|
37
38
|
|
@@ -50,9 +51,13 @@ Encoding.default_internal = Encoding::UTF_8
|
|
50
51
|
|
51
52
|
license = opts.license? ? File.read(opts[:license]) : ''
|
52
53
|
|
53
|
-
|
54
|
-
Xcop::CLI.new(opts.arguments, license).
|
55
|
-
|
56
|
-
|
57
|
-
|
54
|
+
if opts.fix?
|
55
|
+
Xcop::CLI.new(opts.arguments, license).fix
|
56
|
+
else
|
57
|
+
begin
|
58
|
+
Xcop::CLI.new(opts.arguments, license).run
|
59
|
+
rescue StandardError => e
|
60
|
+
puts e.message
|
61
|
+
exit -1
|
62
|
+
end
|
58
63
|
end
|
data/features/cli.feature
CHANGED
@@ -71,3 +71,14 @@ Feature: Command Line Processing
|
|
71
71
|
And Stdout contains "Broken license"
|
72
72
|
And Exit code is not zero
|
73
73
|
|
74
|
+
Scenario: Fixing incorrect XML file
|
75
|
+
Given I have a "broken.xml" file with content:
|
76
|
+
"""
|
77
|
+
<a><b>something</b>
|
78
|
+
</a>
|
79
|
+
"""
|
80
|
+
When I run bin/xcop with "--fix broken.xml"
|
81
|
+
Then Exit code is zero
|
82
|
+
Then I run bin/xcop with "broken.xml"
|
83
|
+
Then Exit code is zero
|
84
|
+
|
data/lib/xcop.rb
CHANGED
@@ -55,6 +55,15 @@ module Xcop
|
|
55
55
|
print "OK\n"
|
56
56
|
end
|
57
57
|
end
|
58
|
+
|
59
|
+
# Fix them all.
|
60
|
+
def fix
|
61
|
+
@files.each do |f|
|
62
|
+
print "Fixing #{f}... "
|
63
|
+
Document.new(f).fix(@license)
|
64
|
+
print "done\n"
|
65
|
+
end
|
66
|
+
end
|
58
67
|
end
|
59
68
|
|
60
69
|
# One document.
|
@@ -78,15 +87,23 @@ module Xcop
|
|
78
87
|
# Return the difference for the license.
|
79
88
|
def ldiff(license)
|
80
89
|
xml = Nokogiri::XML(File.open(@path), &:noblanks)
|
81
|
-
now = xml.xpath('/comment()')[0].to_s
|
82
|
-
ideal =
|
83
|
-
'<!--',
|
84
|
-
*license.strip.split(/\n/).map(&:strip),
|
85
|
-
'-->'
|
86
|
-
].join("\n")
|
90
|
+
now = xml.xpath('/comment()')[0].text.to_s.strip
|
91
|
+
ideal = license.strip
|
87
92
|
Differ.format = :color
|
88
93
|
return Differ.diff_by_line(ideal, now).to_s unless now == ideal
|
89
94
|
''
|
90
95
|
end
|
96
|
+
|
97
|
+
# Fixes the document.
|
98
|
+
def fix(license = '')
|
99
|
+
xml = Nokogiri::XML(File.open(@path), &:noblanks)
|
100
|
+
unless license.empty?
|
101
|
+
xml.children.before(
|
102
|
+
Nokogiri::XML::Comment.new(xml, license.strip)
|
103
|
+
)
|
104
|
+
end
|
105
|
+
ideal = xml.to_xml(indent: 2)
|
106
|
+
File.write(@path, ideal)
|
107
|
+
end
|
91
108
|
end
|
92
109
|
end
|
data/lib/xcop/version.rb
CHANGED
data/test/test_xcop.rb
CHANGED
@@ -58,4 +58,15 @@ class TestXcop < Minitest::Test
|
|
58
58
|
assert_equal(Xcop::Document.new(f).ldiff(license), '')
|
59
59
|
end
|
60
60
|
end
|
61
|
+
|
62
|
+
def test_fixes_document
|
63
|
+
Dir.mktmpdir 'test3' do |dir|
|
64
|
+
f = File.join(dir, 'bad.xml')
|
65
|
+
license = "Copyright (c) Me, Myself, and I\nLike it?\n\n"
|
66
|
+
File.write(f, '<hello>My friend!</hello>')
|
67
|
+
Xcop::Document.new(f).fix(license)
|
68
|
+
assert_equal(Xcop::Document.new(f).diff, '')
|
69
|
+
assert_equal(Xcop::Document.new(f).ldiff(license), '')
|
70
|
+
end
|
71
|
+
end
|
61
72
|
end
|