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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fb09ff0d1d4f8ff28d3f2f0ccaedc6b1e018a082
4
- data.tar.gz: b8351decbe16e2cc8db540cf3962477f782aaf5c
3
+ metadata.gz: c3b67fdc37db666afbd54a7c894889412530d1fb
4
+ data.tar.gz: b5c833f2a4a439ed751a84862577d3cd0f955e36
5
5
  SHA512:
6
- metadata.gz: 64508f23d0fbf7b091d06fc8a54c63f6111b1e92a1a9acfc41b5222006c0d2f026895590c4f675511d87b0171fdb5d73badff31c5238859b83806875536d3d28
7
- data.tar.gz: 4cd03d9ffc5584605d87a66a75ee60bdded4fde0c24746ef009413d5bd51fe327b18862cf17d1162e32567a4cf69a4587e897f63ca8f528cb5ad18ab4fc0ba8a
6
+ metadata.gz: 903d1596af3e61c7eea9c33a6c0d4df519f7781a8c9013f01ce03a8c1e698c6d31e861da5b4662ccede4472618d1e4c3f1dc83e96c3e8d5673db8104b5896bfa
7
+ data.tar.gz: 2ba85e0319b65ac9f6c03583ea7573dde044686baa941f7eb0447febc19257da11570fd7617efe902e005a489b9981a68108083718c7d7693c0a3e77a56b4e4b
data/.simplecov CHANGED
@@ -35,6 +35,6 @@ else
35
35
  SimpleCov.start do
36
36
  add_filter "/test/"
37
37
  add_filter "/features/"
38
- minimum_coverage 90
38
+ minimum_coverage 80
39
39
  end
40
40
  end
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
- begin
54
- Xcop::CLI.new(opts.arguments, license).run
55
- rescue StandardError => e
56
- puts e.message
57
- exit -1
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
@@ -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
+
@@ -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
@@ -25,5 +25,5 @@
25
25
  # Copyright:: Copyright (c) 2017 Yegor Bugayenko
26
26
  # License:: MIT
27
27
  module Xcop
28
- VERSION = '0.4'.freeze
28
+ VERSION = '0.5'.freeze
29
29
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xcop
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.4'
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko