danger-swiftformat 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 129504b9697fb37eb0dc46c0f2f2852667119e7f
4
+ data.tar.gz: bc8d5744ddb99d28de270ebfee1244b58e9dcd44
5
+ SHA512:
6
+ metadata.gz: eab4ef630332a2551cb8d75b61a9d60bb57b5e8d1f9d93e7f68d32841b9581aacfa93072cf672c9f35d3e0ed6782ca9ab9d90bfc732cff364d0eead0d76603b3
7
+ data.tar.gz: 79038b5585e8150badcc6a4f46e1b758cbb4217e61e5de0308650c97d2d78a6d2cb9a451f913b58ae1ffb1e22dd17c622cc0e2653edb0ae64da57cb1adeba66c
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ pkg
3
+ .idea/
4
+ .yardoc
data/.rubocop.yml ADDED
@@ -0,0 +1,152 @@
1
+ # Defaults can be found here: https://github.com/bbatsov/rubocop/blob/master/config/default.yml
2
+
3
+ # If you don't like these settings, just delete this file :)
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.1
7
+
8
+ Style/StringLiterals:
9
+ EnforcedStyle: double_quotes
10
+ Enabled: true
11
+
12
+ # kind_of? is a good way to check a type
13
+ Style/ClassCheck:
14
+ EnforcedStyle: kind_of?
15
+
16
+ # It's better to be more explicit about the type
17
+ Style/BracesAroundHashParameters:
18
+ Enabled: false
19
+
20
+ # specs sometimes have useless assignments, which is fine
21
+ Lint/UselessAssignment:
22
+ Exclude:
23
+ - '**/spec/**/*'
24
+
25
+ # We could potentially enable the 2 below:
26
+ Layout/IndentHash:
27
+ Enabled: false
28
+
29
+ Layout/AlignHash:
30
+ Enabled: false
31
+
32
+ # HoundCI doesn't like this rule
33
+ Layout/DotPosition:
34
+ Enabled: false
35
+
36
+ # We allow !! as it's an easy way to convert ot boolean
37
+ Style/DoubleNegation:
38
+ Enabled: false
39
+
40
+ # Cop supports --auto-correct.
41
+ Lint/UnusedBlockArgument:
42
+ Enabled: false
43
+
44
+ # We want to allow class Fastlane::Class
45
+ Style/ClassAndModuleChildren:
46
+ Enabled: false
47
+
48
+ Metrics/AbcSize:
49
+ Max: 60
50
+
51
+ # The %w might be confusing for new users
52
+ Style/WordArray:
53
+ MinSize: 19
54
+
55
+ # raise and fail are both okay
56
+ Style/SignalException:
57
+ Enabled: false
58
+
59
+ # Better too much 'return' than one missing
60
+ Style/RedundantReturn:
61
+ Enabled: false
62
+
63
+ # Having if in the same line might not always be good
64
+ Style/IfUnlessModifier:
65
+ Enabled: false
66
+
67
+ # and and or is okay
68
+ Style/AndOr:
69
+ Enabled: false
70
+
71
+ # Configuration parameters: CountComments.
72
+ Metrics/ClassLength:
73
+ Max: 350
74
+
75
+ Metrics/CyclomaticComplexity:
76
+ Max: 17
77
+
78
+ # Configuration parameters: AllowURI, URISchemes.
79
+ Metrics/LineLength:
80
+ Max: 370
81
+
82
+ # Configuration parameters: CountKeywordArgs.
83
+ Metrics/ParameterLists:
84
+ Max: 10
85
+
86
+ Metrics/PerceivedComplexity:
87
+ Max: 18
88
+
89
+ # Sometimes it's easier to read without guards
90
+ Style/GuardClause:
91
+ Enabled: false
92
+
93
+ # something = if something_else
94
+ # that's confusing
95
+ Style/ConditionalAssignment:
96
+ Enabled: false
97
+
98
+ # Better to have too much self than missing a self
99
+ Style/RedundantSelf:
100
+ Enabled: false
101
+
102
+ Metrics/MethodLength:
103
+ Max: 60
104
+
105
+ # We're not there yet
106
+ Style/Documentation:
107
+ Enabled: false
108
+
109
+ # Adds complexity
110
+ Style/IfInsideElse:
111
+ Enabled: false
112
+
113
+ # danger specific
114
+
115
+ Style/BlockComments:
116
+ Enabled: false
117
+
118
+ Layout/MultilineMethodCallIndentation:
119
+ EnforcedStyle: indented
120
+
121
+ # FIXME: 25
122
+ Metrics/BlockLength:
123
+ Max: 345
124
+ Exclude:
125
+ - "**/*_spec.rb"
126
+
127
+ Style/MixinGrouping:
128
+ Enabled: false
129
+
130
+ Style/FileName:
131
+ Enabled: false
132
+
133
+ Layout/IndentHeredoc:
134
+ Enabled: false
135
+
136
+ Style/SpecialGlobalVars:
137
+ Enabled: false
138
+
139
+ PercentLiteralDelimiters:
140
+ PreferredDelimiters:
141
+ "%": ()
142
+ "%i": ()
143
+ "%q": ()
144
+ "%Q": ()
145
+ "%r": "{}"
146
+ "%s": ()
147
+ "%w": ()
148
+ "%W": ()
149
+ "%x": ()
150
+
151
+ Security/YAMLLoad:
152
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ cache:
3
+ directories:
4
+ - bundle
5
+
6
+ rvm:
7
+ - 2.2.8
8
+ - 2.3.5
9
+ - 2.4.2
10
+
11
+ script:
12
+ - bundle exec rake spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in danger-swiftformat.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,136 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ danger-swiftformat (0.0.1)
5
+ danger-plugin-api (~> 1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.5.2)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ ast (2.3.0)
13
+ claide (1.0.2)
14
+ claide-plugins (0.9.2)
15
+ cork
16
+ nap
17
+ open4 (~> 1.3)
18
+ coderay (1.1.2)
19
+ colored2 (3.1.2)
20
+ cork (0.3.0)
21
+ colored2 (~> 3.1)
22
+ danger (5.5.5)
23
+ claide (~> 1.0)
24
+ claide-plugins (>= 0.9.2)
25
+ colored2 (~> 3.1)
26
+ cork (~> 0.1)
27
+ faraday (~> 0.9)
28
+ faraday-http-cache (~> 1.0)
29
+ git (~> 1)
30
+ kramdown (~> 1.5)
31
+ no_proxy_fix
32
+ octokit (~> 4.7)
33
+ terminal-table (~> 1)
34
+ danger-plugin-api (1.0.0)
35
+ danger (> 2.0)
36
+ diff-lcs (1.3)
37
+ faraday (0.13.1)
38
+ multipart-post (>= 1.2, < 3)
39
+ faraday-http-cache (1.3.1)
40
+ faraday (~> 0.8)
41
+ ffi (1.9.18)
42
+ formatador (0.2.5)
43
+ git (1.3.0)
44
+ guard (2.14.1)
45
+ formatador (>= 0.2.4)
46
+ listen (>= 2.7, < 4.0)
47
+ lumberjack (~> 1.0)
48
+ nenv (~> 0.1)
49
+ notiffany (~> 0.0)
50
+ pry (>= 0.9.12)
51
+ shellany (~> 0.0)
52
+ thor (>= 0.18.1)
53
+ guard-compat (1.2.1)
54
+ guard-rspec (4.7.3)
55
+ guard (~> 2.1)
56
+ guard-compat (~> 1.1)
57
+ rspec (>= 2.99.0, < 4.0)
58
+ kramdown (1.16.2)
59
+ listen (3.1.5)
60
+ rb-fsevent (~> 0.9, >= 0.9.4)
61
+ rb-inotify (~> 0.9, >= 0.9.7)
62
+ ruby_dep (~> 1.2)
63
+ lumberjack (1.0.12)
64
+ method_source (0.9.0)
65
+ multipart-post (2.0.0)
66
+ nap (1.1.0)
67
+ nenv (0.3.0)
68
+ no_proxy_fix (0.1.2)
69
+ notiffany (0.1.1)
70
+ nenv (~> 0.1)
71
+ shellany (~> 0.0)
72
+ octokit (4.7.0)
73
+ sawyer (~> 0.8.0, >= 0.5.3)
74
+ open4 (1.3.4)
75
+ parallel (1.12.1)
76
+ parser (2.4.0.2)
77
+ ast (~> 2.3)
78
+ powerpack (0.1.1)
79
+ pry (0.11.3)
80
+ coderay (~> 1.1.0)
81
+ method_source (~> 0.9.0)
82
+ public_suffix (3.0.1)
83
+ rainbow (3.0.0)
84
+ rake (12.3.0)
85
+ rb-fsevent (0.10.2)
86
+ rb-inotify (0.9.10)
87
+ ffi (>= 0.5.0, < 2)
88
+ rspec (3.7.0)
89
+ rspec-core (~> 3.7.0)
90
+ rspec-expectations (~> 3.7.0)
91
+ rspec-mocks (~> 3.7.0)
92
+ rspec-core (3.7.0)
93
+ rspec-support (~> 3.7.0)
94
+ rspec-expectations (3.7.0)
95
+ diff-lcs (>= 1.2.0, < 2.0)
96
+ rspec-support (~> 3.7.0)
97
+ rspec-mocks (3.7.0)
98
+ diff-lcs (>= 1.2.0, < 2.0)
99
+ rspec-support (~> 3.7.0)
100
+ rspec-support (3.7.0)
101
+ rubocop (0.52.0)
102
+ parallel (~> 1.10)
103
+ parser (>= 2.4.0.2, < 3.0)
104
+ powerpack (~> 0.1)
105
+ rainbow (>= 2.2.2, < 4.0)
106
+ ruby-progressbar (~> 1.7)
107
+ unicode-display_width (~> 1.0, >= 1.0.1)
108
+ ruby-progressbar (1.9.0)
109
+ ruby_dep (1.5.0)
110
+ sawyer (0.8.1)
111
+ addressable (>= 2.3.5, < 2.6)
112
+ faraday (~> 0.8, < 1.0)
113
+ shellany (0.0.1)
114
+ terminal-table (1.8.0)
115
+ unicode-display_width (~> 1.1, >= 1.1.1)
116
+ thor (0.20.0)
117
+ unicode-display_width (1.3.0)
118
+ yard (0.9.12)
119
+
120
+ PLATFORMS
121
+ ruby
122
+
123
+ DEPENDENCIES
124
+ bundler (~> 1.16)
125
+ danger-swiftformat!
126
+ guard (~> 2.14)
127
+ guard-rspec (~> 4.7)
128
+ listen (~> 3.1)
129
+ pry
130
+ rake (~> 12.3)
131
+ rspec (~> 3.7)
132
+ rubocop (~> 0.52)
133
+ yard (~> 0.9)
134
+
135
+ BUNDLED WITH
136
+ 1.16.0
data/Guardfile ADDED
@@ -0,0 +1,19 @@
1
+ # A guardfile for making Danger Plugins
2
+ # For more info see https://github.com/guard/guard#readme
3
+
4
+ # To run, use `bundle exec guard`.
5
+
6
+ guard :rspec, cmd: 'bundle exec rspec' do
7
+ require 'guard/rspec/dsl'
8
+ dsl = Guard::RSpec::Dsl.new(self)
9
+
10
+ # RSpec files
11
+ rspec = dsl.rspec
12
+ watch(rspec.spec_helper) { rspec.spec_dir }
13
+ watch(rspec.spec_support) { rspec.spec_dir }
14
+ watch(rspec.spec_files)
15
+
16
+ # Ruby files
17
+ ruby = dsl.ruby
18
+ dsl.watch_spec_files_for(ruby.lib_files)
19
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2017 Vincent Garrigues <vincent.garrigues@gmail.com>
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # danger-swiftformat [![Build Status](https://travis-ci.org/garriguv/danger-ruby-swiftformat.svg?branch=master)](https://travis-ci.org/garriguv/danger-ruby-swiftformat)
2
+
3
+ A [danger] plugin to check Swift formatting using [SwiftFormat].
4
+
5
+ This plugin is heavily inspired by [danger-swiftlint].
6
+
7
+ ## Installation
8
+
9
+ $ gem install danger-swiftformat
10
+
11
+ ## Usage
12
+
13
+ Add the following to your `Gemfile`
14
+
15
+ require 'danger-swiftformat'
16
+
17
+ In your `Dangerfile`
18
+
19
+ ```ruby
20
+ swiftformat.binary_path = "/path/to/swiftformat" # optional, but recommended ;)
21
+ swiftformat.check_format(fail_on_error: true)
22
+ ```
23
+
24
+ ## Development
25
+
26
+ 1. Clone this repo
27
+ 2. Run `bundle install` to setup dependencies.
28
+ 3. Run `bundle exec rake spec` to run the tests.
29
+ 4. Use `bundle exec guard` to automatically have tests run as you make changes.
30
+ 5. Make your changes.
31
+
32
+ [danger]: https://danger.systems/ruby/
33
+ [SwiftFormat]: https://github.com/nicklockwood/SwiftFormat
34
+ [danger-swiftlint]: https://github.com/ashfurrow/danger-ruby-swiftlint
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "rubocop/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:specs)
6
+
7
+ task default: :spec
8
+
9
+ task :spec do
10
+ Rake::Task["specs"].invoke
11
+ Rake::Task["rubocop"].invoke
12
+ Rake::Task["spec_docs"].invoke
13
+ end
14
+
15
+ desc "Run RuboCop on the lib/specs directory"
16
+ RuboCop::RakeTask.new(:rubocop) do |task|
17
+ task.patterns = %w(lib/**/*.rb spec/**/*.rb)
18
+ end
19
+
20
+ desc "Ensure that the plugin passes `danger plugins lint`"
21
+ task :spec_docs do
22
+ sh "bundle exec danger plugins lint"
23
+ end
@@ -0,0 +1,49 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'swiftformat/gem_version.rb'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'danger-swiftformat'
8
+ spec.version = Swiftformat::VERSION
9
+ spec.authors = ['Vincent Garrigues']
10
+ spec.email = ['vincent.garrigues@gmail.com']
11
+ spec.description = %q{A danger plugin for checking Swift formatting using SwiftFormat.}
12
+ spec.summary = %q{A danger plugin for checking Swift formatting using SwiftFormat.}
13
+ spec.homepage = 'https://github.com/garriguv/danger-swiftformat'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'danger-plugin-api', '~> 1.0'
22
+
23
+ # General ruby development
24
+ spec.add_development_dependency 'bundler', '~> 1.16'
25
+ spec.add_development_dependency 'rake', '~> 12.3'
26
+
27
+ # Testing support
28
+ spec.add_development_dependency 'rspec', '~> 3.7'
29
+
30
+ # Linting code and docs
31
+ spec.add_development_dependency "rubocop", "~> 0.52"
32
+ spec.add_development_dependency "yard", "~> 0.9"
33
+
34
+ # Makes testing easy via `bundle exec guard`
35
+ spec.add_development_dependency 'guard', '~> 2.14'
36
+ spec.add_development_dependency 'guard-rspec', '~> 4.7'
37
+
38
+ # If you want to work on older builds of ruby
39
+ spec.add_development_dependency 'listen', '~> 3.1'
40
+
41
+ # This gives you the chance to run a REPL inside your tests
42
+ # via:
43
+ #
44
+ # require 'pry'
45
+ # binding.pry
46
+ #
47
+ # This will stop test execution and let you inspect the results
48
+ spec.add_development_dependency 'pry'
49
+ end
@@ -0,0 +1,3 @@
1
+ require "swiftformat/plugin"
2
+ require "swiftformat/swiftformat"
3
+ require "swiftformat/cmd"
@@ -0,0 +1 @@
1
+ require "swiftformat/gem_version"
@@ -0,0 +1,9 @@
1
+ module Danger
2
+ class Cmd
3
+ def self.run(cmd)
4
+ stdout, _stderr, _status = Open3.capture3(*cmd)
5
+
6
+ stdout.strip
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Swiftformat
2
+ VERSION = "0.0.1".freeze
3
+ end
@@ -0,0 +1,70 @@
1
+ module Danger
2
+ # A danger plugin to check Swift formatting using SwiftFormat.
3
+ #
4
+ # @example Check that the added and modified files are properly formatted:
5
+ #
6
+ # swiftformat.check_format
7
+ #
8
+ # @see garriguv/danger-swiftformat
9
+ # @tags swiftformat
10
+ #
11
+ class DangerSwiftformat < Plugin
12
+ # The path to SwiftFormat's executable
13
+ #
14
+ # @return [String]
15
+ attr_accessor :binary_path
16
+
17
+ # Runs swiftformat
18
+ #
19
+ # @param [Boolean] fail_on_error
20
+ #
21
+ # @return [void]
22
+ #
23
+ def check_format(fail_on_error: false)
24
+ # Check if SwiftFormat is installed
25
+ raise "Could not find SwiftFormat executable" unless swiftformat.installed?
26
+
27
+ # Find Swift files
28
+ swift_files = find_swift_files
29
+
30
+ # Run swiftformat
31
+ results = swiftformat.check_format(swift_files)
32
+
33
+ # Stop processing if the errors array is empty
34
+ return if results[:errors].empty?
35
+
36
+ # Process the errors
37
+ message = "### SwiftFormat found issues:\n\n"
38
+ message << "| File | Rules |\n"
39
+ message << "| ---- | ----- |\n"
40
+ results[:errors].each do |error|
41
+ message << "| #{error[:file]} | #{error[:rules].join(', ')} |\n"
42
+ end
43
+ markdown message
44
+
45
+ if fail_on_error
46
+ fail "SwiftFormat found issues"
47
+ end
48
+ end
49
+
50
+ # Find the files on which SwiftFormat should be run
51
+ #
52
+ # @return [Array<String]
53
+ def find_swift_files
54
+ files = (git.modified_files - git.deleted_files) + git.added_files
55
+
56
+ files
57
+ .select { |file| file.end_with?(".swift") }
58
+ .map { |file| Shellwords.escape(file) }
59
+ .uniq
60
+ .sort
61
+ end
62
+
63
+ # Constructs the SwiftFormat class
64
+ #
65
+ # @return [SwiftFormat]
66
+ def swiftformat
67
+ @swiftformat ||= SwiftFormat.new(binary_path)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,48 @@
1
+ module Danger
2
+ class SwiftFormat
3
+ def initialize(path = nil)
4
+ @path = path || "swiftformat"
5
+ end
6
+
7
+ def installed?
8
+ Cmd.run([@path, "--version"])
9
+ end
10
+
11
+ def check_format(files)
12
+ output = Cmd.run([@path] + files + %w(--dryrun --verbose))
13
+ raise "error running swiftformat: empty output" if output.empty?
14
+ process(output)
15
+ end
16
+
17
+ private
18
+
19
+ def process(output)
20
+ {
21
+ errors: errors(output),
22
+ stats: {
23
+ run_time: run_time(output)
24
+ }
25
+ }
26
+ end
27
+
28
+ ERRORS_REGEX = /rules applied:(.*)\n.*updated (.*)$/
29
+
30
+ def errors(output)
31
+ errors = []
32
+ output.scan(ERRORS_REGEX) do |match|
33
+ next if match.count < 2
34
+ errors << {
35
+ file: match[1],
36
+ rules: match[0].split(",").map(&:strip)
37
+ }
38
+ end
39
+ errors
40
+ end
41
+
42
+ RUNTIME_REGEX = /.*swiftformat completed.*(.+\..+)s/
43
+
44
+ def run_time(output)
45
+ RUNTIME_REGEX.match(output)[1]
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,9 @@
1
+ running swiftformat...
2
+ formatting /Users/garriguv/FirstClass.swift
3
+ -- no changes
4
+ formatting /Users/garriguv/FileWithErrors.swift
5
+ -- no changes
6
+ formatting /Users/garriguv/OtherFile.swift
7
+ -- no changes
8
+
9
+ swiftformat completed. 0/3 files would have been updated in 0.08s
@@ -0,0 +1,10 @@
1
+ running swiftformat...
2
+ formatting /Users/garriguv/FirstClass.swift
3
+ -- no changes
4
+ formatting /Users/garriguv/FileWithErrors.swift
5
+ -- rules applied: consecutiveBlankLines, spaceAroundParens, trailingSpace
6
+ would have updated /Users/garriguv/FileWithErrors.swift
7
+ formatting /Users/garriguv/OtherFile.swift
8
+ -- no changes
9
+
10
+ swiftformat completed. 1/3 files would have been updated in 0.08s
@@ -0,0 +1,69 @@
1
+ require "pathname"
2
+ ROOT = Pathname.new(File.expand_path("../../", __FILE__))
3
+ $:.unshift((ROOT + "lib").to_s)
4
+ $:.unshift((ROOT + "spec").to_s)
5
+
6
+ require "bundler/setup"
7
+ require "pry"
8
+
9
+ require "rspec"
10
+ require "danger"
11
+
12
+ if `git remote -v` == ""
13
+ puts "You cannot run tests without setting a local git remote on this repo"
14
+ puts "It's a weird side-effect of Danger's internals."
15
+ exit(0)
16
+ end
17
+
18
+ # Use coloured output, it's the best.
19
+ RSpec.configure do |config|
20
+ config.filter_gems_from_backtrace "bundler"
21
+ config.color = true
22
+ config.tty = true
23
+ end
24
+
25
+ require "danger_plugin"
26
+
27
+ # These functions are a subset of https://github.com/danger/danger/blob/master/spec/spec_helper.rb
28
+ # If you are expanding these files, see if it's already been done ^.
29
+
30
+ # A silent version of the user interface,
31
+ # it comes with an extra function `.string` which will
32
+ # strip all ANSI colours from the string.
33
+
34
+ # rubocop:disable Lint/NestedMethodDefinition
35
+ def testing_ui
36
+ @output = StringIO.new
37
+ def @output.winsize
38
+ [20, 9999]
39
+ end
40
+
41
+ cork = Cork::Board.new(out: @output)
42
+ def cork.string
43
+ out.string.gsub(/\e\[([;\d]+)?m/, "")
44
+ end
45
+ cork
46
+ end
47
+ # rubocop:enable Lint/NestedMethodDefinition
48
+
49
+ # Example environment (ENV) that would come from
50
+ # running a PR on TravisCI
51
+ def testing_env
52
+ {
53
+ "HAS_JOSH_K_SEAL_OF_APPROVAL" => "true",
54
+ "TRAVIS_PULL_REQUEST" => "800",
55
+ "TRAVIS_REPO_SLUG" => "artsy/eigen",
56
+ "TRAVIS_COMMIT_RANGE" => "759adcbd0d8f...13c4dc8bb61d",
57
+ "DANGER_GITHUB_API_TOKEN" => "123sbdq54erfsd3422gdfio"
58
+ }
59
+ end
60
+
61
+ # A stubbed out Dangerfile for use in tests
62
+ def testing_dangerfile
63
+ env = Danger::EnvironmentManager.new(testing_env)
64
+ Danger::Dangerfile.new(env, testing_ui)
65
+ end
66
+
67
+ def fixture(name)
68
+ File.open("spec/fixtures/#{name}", "r", &:read)
69
+ end
@@ -0,0 +1,83 @@
1
+ require File.expand_path("../../spec_helper", __FILE__)
2
+
3
+ module Danger
4
+ describe Danger::DangerSwiftformat do
5
+ it "should be a plugin" do
6
+ expect(Danger::DangerSwiftformat.new(nil)).to be_a Danger::Plugin
7
+ end
8
+
9
+ describe "with Dangerfile" do
10
+ before do
11
+ @dangerfile = testing_dangerfile
12
+ @sut = @dangerfile.swiftformat
13
+ end
14
+
15
+ it "fails if SwiftFormat is not installed" do
16
+ allow_any_instance_of(SwiftFormat).to receive(:installed?).and_return(false)
17
+
18
+ expect { @sut.check_format }.to raise_error("Could not find SwiftFormat executable")
19
+ end
20
+
21
+ context "with binary_path" do
22
+ let(:binary_path) { "path/to/swiftformat" }
23
+
24
+ it "passes the binary_path to the constructor" do
25
+ swiftformat = double("swiftformat")
26
+ allow(SwiftFormat).to receive(:new).with(binary_path).and_return(swiftformat)
27
+
28
+ @sut.binary_path = binary_path
29
+
30
+ expect(@sut.swiftformat).to eq(swiftformat)
31
+ end
32
+ end
33
+
34
+ describe "#check_format" do
35
+ let(:success_output) { { errors: [], stats: { run_time: "0.08s" } } }
36
+ let(:error_output) { { errors: [{ file: "Modified.swift", rules: %w(firstRule secondRule) }], stats: { run_time: "0.16s" } } }
37
+
38
+ before do
39
+ allow_any_instance_of(SwiftFormat).to receive(:installed?).and_return(true)
40
+ allow(@sut.git).to receive(:added_files).and_return(["Added.swift"])
41
+ allow(@sut.git).to receive(:modified_files).and_return(["Modified.swift"])
42
+ allow(@sut.git).to receive(:deleted_files).and_return(["Deleted.swift"])
43
+ end
44
+
45
+ context "when swiftformat does not find any errors" do
46
+ before do
47
+ allow_any_instance_of(SwiftFormat).to receive(:check_format).with(%w(Added.swift Modified.swift)).and_return(success_output)
48
+ end
49
+
50
+ it "should not do anything" do
51
+ @sut.check_format(fail_on_error: true)
52
+
53
+ status = @sut.status_report
54
+ expect(status[:errors]).to be_empty
55
+ expect(status[:markdowns]).to be_empty
56
+ end
57
+ end
58
+
59
+ context "when swiftformat finds errors" do
60
+ before do
61
+ allow_any_instance_of(SwiftFormat).to receive(:check_format).with(%w(Added.swift Modified.swift)).and_return(error_output)
62
+ end
63
+
64
+ it "should output some markdown and error if fail_on_error is true" do
65
+ @sut.check_format(fail_on_error: true)
66
+
67
+ status = @sut.status_report
68
+ expect(status[:errors]).to_not be_empty
69
+ expect(status[:markdowns]).to_not be_empty
70
+ end
71
+
72
+ it "should output some markdown and not error if fail_on_error is false" do
73
+ @sut.check_format(fail_on_error: false)
74
+
75
+ status = @sut.status_report
76
+ expect(status[:errors]).to be_empty
77
+ expect(status[:markdowns]).to_not be_empty
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,87 @@
1
+ require File.expand_path("../../spec_helper", __FILE__)
2
+
3
+ RSpec.describe Danger::SwiftFormat do
4
+ describe "#installed?" do
5
+ context "when no path is given" do
6
+ it "should use the default path" do
7
+ sut = Danger::SwiftFormat.new
8
+ cmd = class_double("Danger::Cmd").as_stubbed_const(transfer_nested_constants: true)
9
+
10
+ expect(cmd).to receive(:run).with(%w(swiftformat --version))
11
+
12
+ sut.installed?
13
+ end
14
+ end
15
+
16
+ context "when path provided" do
17
+ let(:path) { "/path/to/swiftformat" }
18
+
19
+ it "should use the provided path" do
20
+ sut = Danger::SwiftFormat.new(path)
21
+ cmd = class_double("Danger::Cmd").as_stubbed_const(transfer_nested_constants: true)
22
+
23
+ expect(cmd).to receive(:run).with(%w(/path/to/swiftformat --version))
24
+
25
+ sut.installed?
26
+ end
27
+ end
28
+ end
29
+
30
+ describe "#check_format" do
31
+ before do
32
+ @sut = Danger::SwiftFormat.new
33
+ @cmd = class_double("Danger::Cmd").as_stubbed_const(transfer_nested_constants: true)
34
+ end
35
+
36
+ it "should run swiftformat on the specified files" do
37
+ files = %w(/path/to/file.swift /path/to/directory/)
38
+ expect(@cmd).to receive(:run)
39
+ .with(%w(swiftformat /path/to/file.swift /path/to/directory/ --dryrun --verbose))
40
+ .and_return(fixture("swiftformat_output.txt"))
41
+
42
+ @sut.check_format(files)
43
+ end
44
+
45
+ it "should return a formatted output including rules when there are errors" do
46
+ expect(@cmd).to receive(:run)
47
+ .with(%w(swiftformat . --dryrun --verbose))
48
+ .and_return(fixture("swiftformat_output_with_errors.txt"))
49
+
50
+ output = {
51
+ errors: [
52
+ {
53
+ file: "/Users/garriguv/FileWithErrors.swift",
54
+ rules: %w(consecutiveBlankLines spaceAroundParens trailingSpace)
55
+ }
56
+ ],
57
+ stats: {
58
+ run_time: "0.08"
59
+ }
60
+ }
61
+ expect(@sut.check_format(%w(.))).to eq(output)
62
+ end
63
+
64
+ it "should also return a formatted output if there were no errors" do
65
+ expect(@cmd).to receive(:run)
66
+ .with(%w(swiftformat . --dryrun --verbose))
67
+ .and_return(fixture("swiftformat_output.txt"))
68
+
69
+ output = {
70
+ errors: [],
71
+ stats: {
72
+ run_time: "0.08"
73
+ }
74
+ }
75
+
76
+ expect(@sut.check_format(%w(.))).to eq(output)
77
+ end
78
+
79
+ it "should raise an error if the output is empty" do
80
+ expect(@cmd).to receive(:run)
81
+ .with(%w(swiftformat . --dryrun --verbose))
82
+ .and_return("")
83
+
84
+ expect { @sut.check_format(%w(.)) }.to raise_error("error running swiftformat: empty output")
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,210 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: danger-swiftformat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vincent Garrigues
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-12-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: danger-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.52'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.52'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.9'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.14'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.14'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '4.7'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4.7'
125
+ - !ruby/object:Gem::Dependency
126
+ name: listen
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.1'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.1'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: A danger plugin for checking Swift formatting using SwiftFormat.
154
+ email:
155
+ - vincent.garrigues@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".rubocop.yml"
162
+ - ".travis.yml"
163
+ - Gemfile
164
+ - Gemfile.lock
165
+ - Guardfile
166
+ - LICENSE.txt
167
+ - README.md
168
+ - Rakefile
169
+ - danger-swiftformat.gemspec
170
+ - lib/danger_plugin.rb
171
+ - lib/danger_swiftformat.rb
172
+ - lib/swiftformat/cmd.rb
173
+ - lib/swiftformat/gem_version.rb
174
+ - lib/swiftformat/plugin.rb
175
+ - lib/swiftformat/swiftformat.rb
176
+ - spec/fixtures/swiftformat_output.txt
177
+ - spec/fixtures/swiftformat_output_with_errors.txt
178
+ - spec/spec_helper.rb
179
+ - spec/swiftformat/plugin_spec.rb
180
+ - spec/swiftformat/swiftformat_spec.rb
181
+ homepage: https://github.com/garriguv/danger-swiftformat
182
+ licenses:
183
+ - MIT
184
+ metadata: {}
185
+ post_install_message:
186
+ rdoc_options: []
187
+ require_paths:
188
+ - lib
189
+ required_ruby_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ required_rubygems_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ requirements: []
200
+ rubyforge_project:
201
+ rubygems_version: 2.6.13
202
+ signing_key:
203
+ specification_version: 4
204
+ summary: A danger plugin for checking Swift formatting using SwiftFormat.
205
+ test_files:
206
+ - spec/fixtures/swiftformat_output.txt
207
+ - spec/fixtures/swiftformat_output_with_errors.txt
208
+ - spec/spec_helper.rb
209
+ - spec/swiftformat/plugin_spec.rb
210
+ - spec/swiftformat/swiftformat_spec.rb