danger-swiftlint 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ab53ea91e7386c252bd97c2ed1eb870cc15cd49
4
- data.tar.gz: 0005c7f2952722f6900127a316a2a92c57ba7aca
3
+ metadata.gz: 45256bbc01984eff36a6cf720d67107645266af9
4
+ data.tar.gz: 240ceace20e2aa1fc123af81b818843c16dcf2ed
5
5
  SHA512:
6
- metadata.gz: 07b86f7d1fcb2a48a328d79f97f22d7a8abbf20e3026b7c51704f20f8c6eddb978dea223bfbca99dbccb881deb2e829a16c3b5219ea2e9fd5f4050306c8c8ddf
7
- data.tar.gz: 1ff32813a484950a474c56e0ba786842c6e9e592eb4f233eab459fb79291942c7692476d14d1b924351a08e1f7cae43fb81e4d05f59e28936dc96c263f101c10
6
+ metadata.gz: 9b0169c3e7df727a1f5b00bc6199c95342ab4ab6aabc7662af24a3235074de8154624cf753260d9c191b2d2adc11cc0e873c6551562c54976315a53431768a46
7
+ data.tar.gz: 1471ed2849802043844808db74261b518dafe17cc76e82b240092751279b4ce9f5ca834526bc46020c790f24bf8ac1ba254670b93008643c833c74ff6bfa26cc
data/.gitignore CHANGED
@@ -1,3 +1,6 @@
1
+ # Swiftlint extension binary but keep the bin folder
2
+ ext/swiftlint/bin
3
+
1
4
  *.gem
2
5
  *.rbc
3
6
  /.config
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.0.0-p247
1
+ 2.3.0
data/Changelog.md CHANGED
@@ -2,7 +2,15 @@
2
2
 
3
3
  ## Current Master
4
4
 
5
- - Nothing yet!
5
+ ## 0.4.0
6
+
7
+ - Support for inline comments. See [#29](https://github.com/ashfurrow/danger-swiftlint/issues/28)
8
+
9
+ - Adds SwiftLint installation as part of the gem install process, should make
10
+ it easier to track which upstream fixes should or shouldn't be done by
11
+ danger-swiftlint. See [#25](https://github.com/ashfurrow/danger-swiftlint/issues/25)
12
+
13
+ - Add `danger-swiftlint` CLI, with `swiftlint_version` command to print the version of the SwiftLint binary installed by the plugin. See [#32](https://github.com/ashfurrow/danger-swiftlint/pull/32)
6
14
 
7
15
  ## 0.3.0
8
16
 
data/Gemfile.lock CHANGED
@@ -1,8 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- danger-swiftlint (0.3.0)
4
+ danger-swiftlint (0.4.0)
5
5
  danger
6
+ rake (~> 10.0)
7
+ thor (~> 0.19)
6
8
 
7
9
  GEM
8
10
  remote: https://rubygems.org/
@@ -17,12 +19,13 @@ GEM
17
19
  open4 (~> 1.3)
18
20
  coderay (1.1.1)
19
21
  colored (1.2)
22
+ colored2 (3.1.2)
20
23
  cork (0.2.0)
21
24
  colored (~> 1.2)
22
- danger (4.0.3)
25
+ danger (4.3.0)
23
26
  claide (~> 1.0)
24
27
  claide-plugins (>= 0.9.2)
25
- colored (~> 1.2)
28
+ colored2 (~> 3.1)
26
29
  cork (~> 0.1)
27
30
  faraday (~> 0.9)
28
31
  faraday-http-cache (~> 1.0)
@@ -31,7 +34,7 @@ GEM
31
34
  octokit (~> 4.2)
32
35
  terminal-table (~> 1)
33
36
  diff-lcs (1.2.5)
34
- faraday (0.10.0)
37
+ faraday (0.11.0)
35
38
  multipart-post (>= 1.2, < 3)
36
39
  faraday-http-cache (1.3.1)
37
40
  faraday (~> 0.8)
@@ -52,7 +55,7 @@ GEM
52
55
  guard (~> 2.1)
53
56
  guard-compat (~> 1.1)
54
57
  rspec (>= 2.99.0, < 4.0)
55
- kramdown (1.13.1)
58
+ kramdown (1.13.2)
56
59
  listen (3.0.7)
57
60
  rb-fsevent (>= 0.9.3)
58
61
  rb-inotify (>= 0.9.7)
@@ -78,7 +81,7 @@ GEM
78
81
  coderay (~> 1.1.0)
79
82
  method_source (~> 0.8.1)
80
83
  slop (~> 3.4)
81
- public_suffix (2.0.4)
84
+ public_suffix (2.0.5)
82
85
  rake (10.5.0)
83
86
  rb-fsevent (0.9.7)
84
87
  rb-inotify (0.9.7)
@@ -104,7 +107,7 @@ GEM
104
107
  terminal-table (1.7.3)
105
108
  unicode-display_width (~> 1.1.1)
106
109
  thor (0.19.1)
107
- unicode-display_width (1.1.2)
110
+ unicode-display_width (1.1.3)
108
111
 
109
112
  PLATFORMS
110
113
  ruby
@@ -120,8 +123,7 @@ DEPENDENCIES
120
123
  mocha-on-bacon
121
124
  prettybacon
122
125
  pry
123
- rake (~> 10.0)
124
126
  rspec (~> 3.4)
125
127
 
126
128
  BUNDLED WITH
127
- 1.13.1
129
+ 1.14.6
data/README.md CHANGED
@@ -29,6 +29,13 @@ swiftlint.config_file = '.swiftlint.yml'
29
29
  swiftlint.lint_files
30
30
  ```
31
31
 
32
+ If you want the lint result shows in diff instead of comment, you can use `inline_mode` option. Violations that out of the diff will show in danger's fail or warn section.
33
+
34
+ ```rb
35
+ swiftlint.lint_files inline_mode: true
36
+ ```
37
+
38
+
32
39
  ## Attribution
33
40
 
34
41
  Original structure, sequence, and organization of repo taken from [danger-prose](https://github.com/dbgrandi/danger-prose) by [David Grandinetti](https://github.com/dbgrandi/).
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ import 'ext/swiftlint/Rakefile'
1
2
  require 'bundler/gem_tasks'
2
3
  require "rspec/core/rake_task"
3
4
 
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ if $PROGRAM_NAME == __FILE__
4
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
5
+ end
6
+ require 'thor'
7
+ require 'version'
8
+
9
+ class DangerSwiftlintCLI < Thor
10
+ desc 'version', 'The version of the installed danger-swiftlint plugin'
11
+ def version
12
+ puts DangerSwiftlint::VERSION
13
+ end
14
+
15
+ desc 'swiftlint_version', 'The SwiftLint version used by the plugin'
16
+ def swiftlint_version
17
+ puts DangerSwiftlint::SWIFTLINT_VERSION
18
+ end
19
+ end
20
+
21
+ DangerSwiftlintCLI.start(ARGV)
@@ -6,8 +6,8 @@ require 'version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'danger-swiftlint'
8
8
  spec.version = DangerSwiftlint::VERSION
9
- spec.authors = ['Ash Furrow', 'David Grandinetti', 'Orta Therox']
10
- spec.email = ['ash@ashfurrow.com', 'dbgrandi@gmail.com', 'orta.therox@gmail.com']
9
+ spec.authors = ['Ash Furrow', 'David Grandinetti', 'Orta Therox', 'Thiago Felix', 'Giovanni Lodi']
10
+ spec.email = ['ash@ashfurrow.com', 'dbgrandi@gmail.com', 'orta.therox@gmail.com', 'thiago@thiagofelix.com', 'gio@mokacoding.com']
11
11
  spec.description = %q{A Danger plugin for linting Swift with SwiftLint.}
12
12
  spec.summary = %q{A Danger plugin for linting Swift with SwiftLint.}
13
13
  spec.homepage = 'https://github.com/ashfurrow/danger-swiftlint'
@@ -16,12 +16,15 @@ Gem::Specification.new do |spec|
16
16
  spec.files = `git ls-files`.split($/)
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ['lib']
19
+ spec.extensions = %w(ext/swiftlint/Rakefile)
20
+ spec.executables = ['danger-swiftlint']
19
21
 
20
22
  spec.add_dependency 'danger'
23
+ spec.add_dependency 'thor', '~> 0.19'
24
+ spec.add_dependency 'rake', '~> 10.0'
21
25
 
22
26
  # General ruby development
23
27
  spec.add_development_dependency 'bundler', '~> 1.3'
24
- spec.add_development_dependency 'rake', '~> 10.0'
25
28
 
26
29
  # Testing support
27
30
  spec.add_development_dependency "rspec", '~> 3.4'
@@ -29,12 +32,12 @@ Gem::Specification.new do |spec|
29
32
  # Makes testing easy via `bundle exec guard`
30
33
  spec.add_development_dependency "guard", '~> 2.14'
31
34
  spec.add_development_dependency "guard-rspec", '~> 4.7'
32
-
35
+
33
36
  # If you want to work on older builds of ruby
34
37
  spec.add_development_dependency "listen", '3.0.7'
35
38
 
36
39
  # This gives you the chance to run a REPL inside your test
37
- # via
40
+ # via
38
41
  # binding.pry
39
42
  # This will stop test execution and let you inspect the results
40
43
  spec.add_development_dependency "pry"
@@ -0,0 +1,24 @@
1
+ require_relative '../../lib/version'
2
+
3
+ namespace :swiftlint do
4
+
5
+ desc "Download and install swiftlint tool"
6
+ task :install do
7
+ REPO = "https://github.com/realm/SwiftLint"
8
+ VERSION = DangerSwiftlint::SWIFTLINT_VERSION
9
+ ASSET = "portable_swiftlint.zip"
10
+ URL = "#{REPO}/releases/download/#{VERSION}/#{ASSET}"
11
+ DESTINATION = File.expand_path(File.join(File.dirname(__FILE__), 'bin'))
12
+
13
+ puts "Downloading swiftlint@#{VERSION}"
14
+ sh [
15
+ "mkdir -p #{DESTINATION}",
16
+ "curl -s -L #{URL} -o #{ASSET}",
17
+ "unzip -q #{ASSET} -d #{DESTINATION}",
18
+ "rm #{ASSET}"
19
+ ].join(" && ")
20
+ end
21
+
22
+ end
23
+
24
+ task default: 'swiftlint:install'
@@ -0,0 +1,58 @@
1
+
2
+ class Swiftlint
3
+ class << self
4
+ # Runs swiftlint
5
+ # @param key (Symbol) the key which is used as command paramters or key in the fastlane tools
6
+ # @param env_name (String) the name of the environment variable, which is only used if no other values were found
7
+ # @param description (String) A description shown to the user
8
+ # @param sensitive (Boolean) Set if the variable is sensitive, such as a password or API token, to prevent echoing when prompted for the parameter
9
+ def run(cmd='lint', options={})
10
+
11
+ # change pwd before run swiftlint
12
+ if options.has_key? :pwd
13
+ Dir.chdir options.delete(:pwd)
14
+ end
15
+
16
+ # run swiftlint with provided options
17
+ `#{swiftlint_path} #{cmd} #{swiftlint_arguments(options)}`
18
+ end
19
+
20
+ # Shortcut for running the lint command
21
+ def lint(options)
22
+ run('lint', options)
23
+ end
24
+
25
+ # Return true if swiftlint is installed or false otherwise
26
+ def is_installed?
27
+ File.exist?(swiftlint_path)
28
+ end
29
+
30
+ private
31
+
32
+ # Parse options into shell arguments how swift expect it to be
33
+ # more information: https://github.com/Carthage/Commandant
34
+ # @param options (Hash) hash containing swiftlint options
35
+ def swiftlint_arguments options
36
+ options.
37
+ # filter not null
38
+ select {|key, value| !value.nil?}.
39
+ # map booleans arguments equal true
40
+ map { |key, value| value.is_a?(TrueClass) ? [key, ''] : [key, value] }.
41
+ # map booleans arguments equal false
42
+ map { |key, value| value.is_a?(FalseClass) ? ["no-#{key}", ''] : [key, value] }.
43
+ # replace underscore by hyphen
44
+ map { |key, value| [key.to_s.tr('_', '-'), value] }.
45
+ # prepend '--' into the argument
46
+ map { |key, value| ["--#{key}", value] }.
47
+ # reduce everything into a single string
48
+ reduce('') { |args, option| "#{args} #{option[0]} #{option[1]}" }.
49
+ # strip leading spaces
50
+ strip
51
+ end
52
+
53
+ # Path where swiftlint should be found
54
+ def swiftlint_path
55
+ File.expand_path(File.join(File.dirname(__FILE__), 'bin', 'swiftlint'))
56
+ end
57
+ end
58
+ end
data/lib/danger_plugin.rb CHANGED
@@ -1,3 +1,8 @@
1
+ require 'find'
2
+ require 'yaml'
3
+ require 'shellwords'
4
+ require_relative '../ext/swiftlint/swiftlint'
5
+
1
6
  module Danger
2
7
 
3
8
  # Lint Swift files inside your projects.
@@ -15,114 +20,115 @@ module Danger
15
20
  #
16
21
  class DangerSwiftlint < Plugin
17
22
 
18
- # Allows you to specify a config file location for swiftlint.
23
+ # The path to SwiftLint's configuration file
19
24
  attr_accessor :config_file
20
25
 
21
- # Allows you to specify a directory from where swiftlint will be run.
26
+ # Allows you to specify a directory from where swiftlint will be run.
22
27
  attr_accessor :directory
23
28
 
24
29
  # Lints Swift files. Will fail if `swiftlint` cannot be installed correctly.
25
- # Generates a `markdown` list of warnings for the prose in a corpus of .markdown and .md files.
30
+ # Generates a `markdown` list of warnings for the prose in a corpus of .markdown and .md files.
26
31
  #
27
32
  # @param [String] files
28
33
  # A globbed string which should return the files that you want to lint, defaults to nil.
29
34
  # if nil, modified and added files from the diff will be used.
30
35
  # @return [void]
31
36
  #
32
- def lint_files(files=nil)
33
- # Installs SwiftLint if needed
34
- system "brew install swiftlint" unless swiftlint_installed?
35
-
36
- # Check that this is in the user's PATH after installing
37
- unless swiftlint_installed?
38
- fail "swiftlint is not in the user's PATH, or it failed to install"
39
- return
40
- end
41
-
42
- require 'tempfile'
43
- Tempfile.open('.swiftlint_danger.yml') do |temp_config_file|
44
- on_the_fly_configuration_path = nil
45
- excluded_dirs = []
46
- if config_file
47
- require 'yaml'
48
- original_config = YAML.load_file(config_file)
49
-
50
- danger_compatible_config = original_config
51
- danger_compatible_config.tap { |hash| hash.delete('included') }
52
-
53
- excluded_dirs = danger_compatible_config['excluded']
54
- .map { |path| File.dirname(config_file) + '/' + path }
55
- .select { |path| File.exists?(File.expand_path(path)) || Dir.exists?(File.expand_path(path)) }
56
-
57
- File.write(temp_config_file.path, danger_compatible_config.to_yaml)
58
-
59
- on_the_fly_configuration_path = temp_config_file.path
60
- end
61
-
62
- swiftlint_command = "swiftlint lint --quiet --reporter json"
63
- swiftlint_command += " --config #{on_the_fly_configuration_path}" if on_the_fly_configuration_path
64
-
65
- require 'json'
66
-
67
- if directory
68
- swiftlint_command = "cd #{directory} && #{swiftlint_command}" if directory
69
-
70
- result_json = JSON.parse(`(#{swiftlint_command})`).flatten
71
- else
72
- # Either use files provided, or use the modified + added
73
- swift_files = files ? Dir.glob(files) : (git.modified_files + git.added_files)
74
-
75
- swift_files.select! do |line| line.end_with?(".swift") end
76
-
77
- require 'find'
78
- swift_files = swift_files.reject do |file|
79
- found = false
80
- excluded_dirs.each do |excluded_dir|
81
- found = Find.find(File.expand_path(excluded_dir)).include?(File.expand_path(file))
82
- break if found
83
- end
84
- found
85
- end
86
-
87
- # Make sure we don't fail when paths have spaces
88
- swift_files = swift_files.map { |file| "\"#{file}\"" }
89
-
90
- result_json = swift_files
91
- .uniq
92
- .collect { |f| `(#{swiftlint_command} --path #{f})`.strip }
93
- .reject { |s| s == '' }
94
- .map { |s| JSON.parse(s).flatten }
95
- .flatten
96
- end
97
-
98
- # Convert to swiftlint results
99
- warnings = result_json.select do |results|
100
- results['severity'] == 'Warning'
101
- end
102
- errors = result_json.select do |results|
103
- results['severity'] == 'Error'
104
- end
105
-
106
- message = ''
107
-
108
- # We got some error reports back from swiftlint
37
+ def lint_files(files=nil, inline_mode: false)
38
+ # Fails if swiftlint isn't installed
39
+ raise "swiftlint is not installed" unless Swiftlint.is_installed?
40
+
41
+ # Extract excluded paths
42
+ excluded_paths = excluded_files_from_config(config_file)
43
+
44
+ # Extract swift files (ignoring excluded ones)
45
+ files = find_swift_files(files, excluded_paths)
46
+
47
+ # Prepare swiftlint options
48
+ options = {
49
+ config: config_file,
50
+ reporter: 'json',
51
+ quiet: true,
52
+ pwd: directory || Dir.pwd
53
+ }
54
+
55
+ # Lint each file and collect the results
56
+ issues = run_swiftlint(files, options)
57
+
58
+ # Filter warnings and errors
59
+ warnings = issues.select { |issue| issue['severity'] == 'Warning' }
60
+ errors = issues.select { |issue| issue['severity'] == 'Error' }
61
+
62
+ if inline_mode
63
+ # Reprt with inline comment
64
+ send_inline_comment(warnings, "warn")
65
+ send_inline_comment(errors, "fail")
66
+ else
67
+ # Report if any warning or error
109
68
  if warnings.count > 0 || errors.count > 0
110
69
  message = "### SwiftLint found issues\n\n"
70
+ message << markdown_issues(warnings, 'Warnings') unless warnings.empty?
71
+ message << markdown_issues(errors, 'Errors') unless errors.empty?
72
+ markdown message
111
73
  end
74
+ end
75
+ end
112
76
 
113
- message << parse_results(warnings, 'Warnings') unless warnings.empty?
114
- message << parse_results(errors, 'Errors') unless errors.empty?
77
+ # Run swiftlint on each file and aggregate collect the issues
78
+ #
79
+ # @return [Array] swiftlint issues
80
+ def run_swiftlint(files, options)
81
+ files
82
+ .map { |file| options.merge({path: file})}
83
+ .map { |options| Swiftlint.lint(options)}
84
+ .reject { |s| s == '' }
85
+ .map { |s| JSON.parse(s).flatten }
86
+ .flatten
87
+ end
115
88
 
116
- markdown message unless message.empty?
89
+ # Find swift files from the files glob
90
+ # If files are not provided it will use git modifield and added files
91
+ #
92
+ # @return [Array] swift files
93
+ def find_swift_files(files=nil, excluded_files=[])
94
+ # Assign files to lint
95
+ files = files ? Dir.glob(files) : git.modified_files + git.added_files
96
+
97
+ # Filter files to lint
98
+ return files.
99
+ # Ensure only swift files are selected
100
+ select { |file| file.end_with?('.swift') }.
101
+ # Make sure we don't fail when paths have spaces
102
+ map { |file| Shellwords.escape(file) }.
103
+ # Remove dups
104
+ uniq.
105
+ # Reject files excluded on configuration
106
+ reject { |file|
107
+ excluded_files.any? { |excluded| Find.find(excluded).include?(File.expand_path(file)) }
108
+ }
109
+ end
110
+
111
+ # Parses the configuration file and return the excluded files
112
+ #
113
+ # @return [Array] list of files excluded
114
+ def excluded_files_from_config(filepath)
115
+ config = if filepath
116
+ YAML.load_file(config_file)
117
+ else
118
+ {"excluded" => []}
117
119
  end
120
+
121
+ # Extract excluded paths
122
+ return config['excluded'].
123
+ map { |path| File.join(File.dirname(config_file), path) }.
124
+ map { |path| File.expand_path(path) }.
125
+ select { |path| File.exists?(path) || Dir.exists?(path) }
118
126
  end
119
127
 
120
- # Parses swiftlint invocation results into a string
121
- # which is formatted as a markdown table.
128
+ # Create a markdown table from swiftlint issues
122
129
  #
123
130
  # @return [String]
124
- #
125
- def parse_results (results, heading)
131
+ def markdown_issues (results, heading)
126
132
  message = "#### #{heading}\n\n"
127
133
 
128
134
  message << "File | Line | Reason |\n"
@@ -139,11 +145,15 @@ module Danger
139
145
  message
140
146
  end
141
147
 
142
- # Determine if swiftlint is currently installed in the system paths.
143
- # @return [Bool]
148
+ # Send inline comment with danger's warn or fail method
144
149
  #
145
- def swiftlint_installed?
146
- `which swiftlint`.strip.empty? == false
150
+ # @return [void]
151
+ def send_inline_comment (results, method)
152
+ dir = "#{Dir.pwd}/"
153
+ results.each do |r|
154
+ filename = r['file'].gsub(dir, "")
155
+ send(method, r['reason'], file: filename, line: r['line'])
156
+ end
147
157
  end
148
158
  end
149
159
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  module DangerSwiftlint
2
- VERSION = "0.3.0".freeze
2
+ VERSION = "0.4.0".freeze
3
+ SWIFTLINT_VERSION = "0.16.1".freeze
3
4
  end
@@ -8,104 +8,64 @@ module Danger
8
8
 
9
9
  describe 'with Dangerfile' do
10
10
  before do
11
- @dangerfile = testing_dangerfile
12
11
  @swiftlint = testing_dangerfile.swiftlint
13
-
14
- @swiftlint.config_file = nil
15
12
  end
16
13
 
17
14
  it "handles swiftlint not being installed" do
18
- allow(@swiftlint).to receive(:`).with("which swiftlint").and_return("")
19
- expect(@swiftlint.swiftlint_installed?).to be_falsy
20
- end
21
-
22
- it "handles swiftlint being installed" do
23
- allow(@swiftlint).to receive(:`).with("which swiftlint").and_return("/bin/wherever/swiftlint")
24
- expect(@swiftlint.swiftlint_installed?).to be_truthy
15
+ allow(Swiftlint).to receive(:is_installed?).and_return(false)
16
+ expect { @swiftlint.lint_files }.to raise_error("swiftlint is not installed")
25
17
  end
26
18
 
27
19
  it 'does not markdown an empty message' do
28
- allow(@swiftlint).to receive(:`)
29
- .with('swiftlint lint --quiet --reporter json --path "spec/fixtures/SwiftFile.swift"')
30
- .and_return('[]')
31
-
20
+ allow(Swiftlint).to receive(:lint).and_return('[]')
32
21
  expect(@swiftlint.status_report[:markdowns].first).to be_nil
33
22
  end
34
23
 
35
24
  describe :lint_files do
36
25
  before do
37
- # So it doesn't try to install on your computer
38
- allow(@swiftlint).to receive(:`).with("which swiftlint").and_return("/bin/wheverever/swiftlint")
26
+ allow(Swiftlint).to receive(:is_installed?).and_return(true)
27
+ allow(@swiftlint.git).to receive(:added_files).and_return([])
28
+ allow(@swiftlint.git).to receive(:modified_files).and_return([])
39
29
 
40
- # Set up our stubbed JSON response
41
30
  @swiftlint_response = '[{ "rule_id" : "force_cast", "reason" : "Force casts should be avoided.", "character" : 19, "file" : "/Users/me/this_repo/spec//fixtures/SwiftFile.swift", "severity" : "Error", "type" : "Force Cast", "line" : 13 }]'
42
31
  end
43
32
 
44
- it 'handles a known SwiftLint report with give files' do
45
- allow(@swiftlint).to receive(:`).with('(swiftlint lint --quiet --reporter json --path "spec/fixtures/SwiftFile.swift")').and_return(@swiftlint_response)
33
+ it 'accept files as arguments' do
34
+ expect(Swiftlint).to receive(:lint)
35
+ .with(hash_including(:path => 'spec/fixtures/SwiftFile.swift'))
36
+ .and_return(@swiftlint_response)
46
37
 
47
- # Do it
48
38
  @swiftlint.lint_files("spec/fixtures/*.swift")
49
39
 
50
40
  output = @swiftlint.status_report[:markdowns].first.to_s
51
-
52
- expect(output).to_not be_empty
53
-
54
- # A title
55
41
  expect(output).to include("SwiftLint found issues")
56
- # A warning
57
42
  expect(output).to include("SwiftFile.swift | 13 | Force casts should be avoided.")
58
43
  end
59
44
 
60
- it 'handles no given files by looking up the git diff' do
45
+ it 'uses git diff when files are not provided' do
61
46
  allow(@swiftlint.git).to receive(:modified_files).and_return(['spec/fixtures/SwiftFile.swift'])
62
47
  allow(@swiftlint.git).to receive(:added_files).and_return([])
63
-
64
- allow(@swiftlint).to receive(:`).with('(swiftlint lint --quiet --reporter json --path "spec/fixtures/SwiftFile.swift")').and_return(@swiftlint_response)
48
+ allow(Swiftlint).to receive(:lint)
49
+ .with(hash_including(:path => 'spec/fixtures/SwiftFile.swift'))
50
+ .and_return(@swiftlint_response)
65
51
 
66
52
  @swiftlint.lint_files
67
53
 
68
- expect(@swiftlint.status_report[:markdowns].first.to_s).to_not be_empty
69
- end
70
-
71
- it 'uses a config file generated on the fly by removing the "included" values from the given one' do
72
- fake_temp_file = Tempfile.new('fake.yml')
73
-
74
- begin
75
- allow(Tempfile).to receive(:open) { |&block| block.call(fake_temp_file) }
76
-
77
- @swiftlint.config_file = 'spec/fixtures/some_config.yml'
78
-
79
- expect(YAML.load_file(@swiftlint.config_file)['included']).to_not be_nil
80
-
81
- allow(@swiftlint).to receive(:`).with('(swiftlint lint --quiet --reporter json --config ' + fake_temp_file.path + ' --path "spec/fixtures/SwiftFile.swift")') do
82
- # The tempfile lifetime is limited to the execution of the lint
83
- # command, as such if we were to assert it after the command has
84
- # run the file wouldn't exist anymore.
85
- #
86
- # By injecting the assertion here, whithin the method execution,
87
- # we access the file while it still exists.
88
- expect(YAML.load_file(fake_temp_file.path)['included']).to be_nil
89
-
90
- @swiftlint_response
91
- end
92
-
93
- @swiftlint.lint_files("spec/fixtures/*.swift")
94
-
95
- expect(@swiftlint.status_report[:markdowns].first.to_s).to_not be_empty
96
- ensure
97
- fake_temp_file.close
98
- fake_temp_file.unlink
99
- end
54
+ output = @swiftlint.status_report[:markdowns].first.to_s
55
+ expect(output).to_not be_empty
100
56
  end
101
57
 
102
58
  it 'uses a custom directory' do
103
59
  @swiftlint.directory = 'some_dir'
104
- allow(@swiftlint).to receive(:`).with('(cd some_dir && swiftlint lint --quiet --reporter json)').and_return(@swiftlint_response)
105
60
 
106
- @swiftlint.lint_files
61
+ allow(Swiftlint).to receive(:lint)
62
+ .with(hash_including(:pwd => @swiftlint.directory))
63
+ .and_return(@swiftlint_response)
64
+
65
+ @swiftlint.lint_files("spec/fixtures/*.swift")
107
66
 
108
- expect(@swiftlint.status_report[:markdowns].first.to_s).to_not be_empty
67
+ output = @swiftlint.status_report[:markdowns].first.to_s
68
+ expect(output).to_not be_empty
109
69
  end
110
70
 
111
71
  it 'does not crash if JSON reporter returns an empty string rather than an object' do
@@ -120,36 +80,38 @@ module Danger
120
80
  # To our code this would simply look like an empty result, which
121
81
  # would then become an empty string, which cannot be parsed into a
122
82
  # JSON object.
123
- response = ''
124
- allow(@swiftlint).to receive(:`).with('(swiftlint lint --quiet --reporter json --path "spec/fixtures/SwiftFile.swift")').and_return(response)
125
-
126
- @swiftlint.lint_files("spec/fixtures/*.swift")
127
83
 
128
- output = @swiftlint.status_report[:markdowns].first.to_s
84
+ allow(Swiftlint).to receive(:lint).and_return('')
129
85
 
130
- # If we get to this point then we haven't crashed, happy days
131
- expect(output).to be_empty
86
+ expect { @swiftlint.lint_files }.not_to raise_error
132
87
  end
133
88
 
134
89
  it 'does not lint files in the excluded paths' do
135
- allow(@swiftlint.git).to receive(:modified_files).and_return(['spec/fixtures/SwiftFile.swift', 'spec/fixtures/excluded_dir/SwiftFileThatShouldNotBeIncluded.swift'])
136
90
  allow(@swiftlint.git).to receive(:added_files).and_return([])
91
+ allow(@swiftlint.git).to receive(:modified_files).and_return([
92
+ 'spec/fixtures/SwiftFile.swift',
93
+ 'spec/fixtures/excluded_dir/SwiftFileThatShouldNotBeIncluded.swift'
94
+ ])
137
95
 
138
- fake_temp_file = Tempfile.new('fake.yml')
96
+ expect(Swiftlint).to receive(:lint)
97
+ .with(hash_including(:path => 'spec/fixtures/SwiftFile.swift'))
98
+ .and_return(@swiftlint_response)
99
+ .once
100
+
101
+ @swiftlint.config_file = 'spec/fixtures/some_config.yml'
102
+ @swiftlint.lint_files
103
+ end
139
104
 
140
- begin
141
- allow(Tempfile).to receive(:open) { |&block| block.call(fake_temp_file) }
105
+ it 'generates errors instead of markdown when use inline mode' do
106
+ allow(Swiftlint).to receive(:lint)
107
+ .with(hash_including(:path => 'spec/fixtures/SwiftFile.swift'))
108
+ .and_return(@swiftlint_response)
142
109
 
143
- # The only call that should be received is this one, if @swiftlint
144
- # will be called with any other param the test will fail
145
- allow(@swiftlint).to receive(:`).with('(swiftlint lint --quiet --reporter json --config ' + fake_temp_file.path + ' --path "spec/fixtures/SwiftFile.swift")').and_return(@swiftlint_response)
110
+ @swiftlint.lint_files("spec/fixtures/*.swift", inline_mode: true)
146
111
 
147
- @swiftlint.config_file = 'spec/fixtures/some_config.yml'
148
- @swiftlint.lint_files
149
- ensure
150
- fake_temp_file.close
151
- fake_temp_file.unlink
152
- end
112
+ status = @swiftlint.status_report
113
+ expect(status[:errors]).to_not be_empty
114
+ expect(status[:markdowns]).to be_empty
153
115
  end
154
116
  end
155
117
  end
data/spec/spec_helper.rb CHANGED
@@ -9,6 +9,10 @@ RSpec.configure do |config|
9
9
  config.color = true
10
10
  end
11
11
 
12
+ RSpec::Matchers.define :including do |x|
13
+ match { |actual| actual.include? x }
14
+ end
15
+
12
16
  require 'bundler/setup'
13
17
  require 'pry'
14
18
 
@@ -0,0 +1,28 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+ require_relative '../ext/swiftlint/swiftlint'
3
+
4
+ describe Swiftlint do
5
+ it 'is_installed? works based on bin/swiftlint file' do
6
+ expect(File).to receive(:exist?).with(/bin\/swiftlint/).and_return(true)
7
+ expect(Swiftlint.is_installed?).to be_truthy
8
+
9
+ expect(File).to receive(:exist?).with(/bin\/swiftlint/).and_return(false)
10
+ expect(Swiftlint.is_installed?).to be_falsy
11
+ end
12
+
13
+ it 'runs lint by default with options being optional' do
14
+ expect(Swiftlint).to receive(:`).with(including('swiftlint lint'))
15
+ Swiftlint.run()
16
+ end
17
+
18
+ it 'runs accepting symbolized options' do
19
+ cmd = 'swiftlint lint --no-use-stdin --cache-path /path --enable-all-rules'
20
+ expect(Swiftlint).to receive(:`).with(including(cmd))
21
+
22
+ Swiftlint.run('lint',
23
+ use_stdin: false,
24
+ cache_path: '/path',
25
+ enable_all_rules: true)
26
+ end
27
+ end
28
+
metadata CHANGED
@@ -1,16 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danger-swiftlint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ash Furrow
8
8
  - David Grandinetti
9
9
  - Orta Therox
10
+ - Thiago Felix
11
+ - Giovanni Lodi
10
12
  autorequire:
11
13
  bindir: bin
12
14
  cert_chain: []
13
- date: 2017-01-31 00:00:00.000000000 Z
15
+ date: 2017-03-14 00:00:00.000000000 Z
14
16
  dependencies:
15
17
  - !ruby/object:Gem::Dependency
16
18
  name: danger
@@ -27,19 +29,19 @@ dependencies:
27
29
  - !ruby/object:Gem::Version
28
30
  version: '0'
29
31
  - !ruby/object:Gem::Dependency
30
- name: bundler
32
+ name: thor
31
33
  requirement: !ruby/object:Gem::Requirement
32
34
  requirements:
33
35
  - - "~>"
34
36
  - !ruby/object:Gem::Version
35
- version: '1.3'
36
- type: :development
37
+ version: '0.19'
38
+ type: :runtime
37
39
  prerelease: false
38
40
  version_requirements: !ruby/object:Gem::Requirement
39
41
  requirements:
40
42
  - - "~>"
41
43
  - !ruby/object:Gem::Version
42
- version: '1.3'
44
+ version: '0.19'
43
45
  - !ruby/object:Gem::Dependency
44
46
  name: rake
45
47
  requirement: !ruby/object:Gem::Requirement
@@ -47,13 +49,27 @@ dependencies:
47
49
  - - "~>"
48
50
  - !ruby/object:Gem::Version
49
51
  version: '10.0'
50
- type: :development
52
+ type: :runtime
51
53
  prerelease: false
52
54
  version_requirements: !ruby/object:Gem::Requirement
53
55
  requirements:
54
56
  - - "~>"
55
57
  - !ruby/object:Gem::Version
56
58
  version: '10.0'
59
+ - !ruby/object:Gem::Dependency
60
+ name: bundler
61
+ requirement: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - "~>"
64
+ - !ruby/object:Gem::Version
65
+ version: '1.3'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: '1.3'
57
73
  - !ruby/object:Gem::Dependency
58
74
  name: rspec
59
75
  requirement: !ruby/object:Gem::Requirement
@@ -129,8 +145,12 @@ email:
129
145
  - ash@ashfurrow.com
130
146
  - dbgrandi@gmail.com
131
147
  - orta.therox@gmail.com
132
- executables: []
133
- extensions: []
148
+ - thiago@thiagofelix.com
149
+ - gio@mokacoding.com
150
+ executables:
151
+ - danger-swiftlint
152
+ extensions:
153
+ - ext/swiftlint/Rakefile
134
154
  extra_rdoc_files: []
135
155
  files:
136
156
  - ".gitignore"
@@ -142,7 +162,10 @@ files:
142
162
  - LICENSE
143
163
  - README.md
144
164
  - Rakefile
165
+ - bin/danger-swiftlint
145
166
  - danger-swiftlint.gemspec
167
+ - ext/swiftlint/Rakefile
168
+ - ext/swiftlint/swiftlint.rb
146
169
  - lib/danger_plugin.rb
147
170
  - lib/version.rb
148
171
  - spec/danger_plugin_spec.rb
@@ -150,6 +173,7 @@ files:
150
173
  - spec/fixtures/excluded_dir/SwiftFileThatShouldNotBeIncluded.swift
151
174
  - spec/fixtures/some_config.yml
152
175
  - spec/spec_helper.rb
176
+ - spec/swiftlint_spec.rb
153
177
  homepage: https://github.com/ashfurrow/danger-swiftlint
154
178
  licenses:
155
179
  - MIT
@@ -170,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
194
  version: '0'
171
195
  requirements: []
172
196
  rubyforge_project:
173
- rubygems_version: 2.5.2
197
+ rubygems_version: 2.5.1
174
198
  signing_key:
175
199
  specification_version: 4
176
200
  summary: A Danger plugin for linting Swift with SwiftLint.
@@ -180,3 +204,4 @@ test_files:
180
204
  - spec/fixtures/excluded_dir/SwiftFileThatShouldNotBeIncluded.swift
181
205
  - spec/fixtures/some_config.yml
182
206
  - spec/spec_helper.rb
207
+ - spec/swiftlint_spec.rb