danger-swiftlint 0.21.1 → 0.22.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
- SHA256:
3
- metadata.gz: 6e30637e9b9981efedc2a18d8acdf8f9957a0f813f714b131601f43dce07ec0d
4
- data.tar.gz: b0c6033a1291893cc3b871cfd3f7d6fb631d89431ca3724d0f187e5ef69bdfc0
2
+ SHA1:
3
+ metadata.gz: c86940adc167f7b4863e444266b96770155c2ab8
4
+ data.tar.gz: c398255b1c603ed1e979426afba4a9a4c73f3a40
5
5
  SHA512:
6
- metadata.gz: 1417540992b6d0b2ccd7a1b9dacd57235f66e3e495ef826af54168e0686434efaf80eb552a35f212091cd5f8d297b6252810c0be1074214d4b19c2c7c680a992
7
- data.tar.gz: aef461f9368041b4c0f8e9d30d64e3eb42e4110578cf009660234d05e6c1e026d1a3c09f1a80fa88e96bee7602f0cb5d9e9c2822add4c4dea554c04e8bbf67c5
6
+ metadata.gz: 71f4b0e1f6e4fb1238d3934d58feefe9f1a113bcb640bc2a8f3fd6089565c73cb6f8d23e2023e0af1efdfb4fb59d996ac14cd329c5f3cccbca5e84186868ef65
7
+ data.tar.gz: 18ecfda87f5bc8cc1cd3bb2670530505550b95a938d5e4c62d2566838a3c114cde4f4c6651a73a97b309402feef4ff457275484ab3a5f5f415dd86d0d7057c55
@@ -50,6 +50,9 @@ module Danger
50
50
  # All issues found
51
51
  attr_accessor :issues
52
52
 
53
+ # Whether all issues or ones in PR Diff to be reported
54
+ attr_accessor :filter_issues_in_diff
55
+
53
56
  # Lints Swift files. Will fail if `swiftlint` cannot be installed correctly.
54
57
  # Generates a `markdown` list of warnings for the prose in a corpus of
55
58
  # .markdown and .md files.
@@ -109,6 +112,11 @@ module Danger
109
112
  issues = run_swiftlint_for_each(files, options, additional_swiftlint_args)
110
113
  end
111
114
 
115
+ if filter_issues_in_diff
116
+ # Filter issues related to changes in PR Diff
117
+ issues = filter_git_diff_issues(issues)
118
+ end
119
+
112
120
  @issues = issues
113
121
  other_issues_count = 0
114
122
  unless @max_num_violations.nil? || no_comment
@@ -308,5 +316,53 @@ module Danger
308
316
  def log(text)
309
317
  puts(text) if @verbose
310
318
  end
319
+
320
+ # Filters issues reported against changes in the modified files
321
+ #
322
+ # @return [Array] swiftlint issues
323
+ def filter_git_diff_issues(issues)
324
+ modified_files_info = git_modified_files_info()
325
+ return issues.select { |i|
326
+ modified_files_info["#{i['file']}"] != nil && modified_files_info["#{i['file']}"].include?(i['line'].to_i)
327
+ }
328
+ end
329
+
330
+ # Finds modified files and added files, creates array of files with modified line numbers
331
+ #
332
+ # @return [Array] Git diff changes for each file
333
+ def git_modified_files_info()
334
+ modified_files_info = Hash.new
335
+ updated_files = (git.modified_files - git.deleted_files) + git.added_files
336
+ updated_files.each {|file|
337
+ modified_lines = git_modified_lines(file)
338
+ modified_files_info[File.expand_path(file)] = modified_lines
339
+ }
340
+ modified_files_info
341
+ end
342
+
343
+ # Gets git patch info and finds modified line numbers, excludes removed lines
344
+ #
345
+ # @return [Array] Modified line numbers i
346
+ def git_modified_lines(file)
347
+ git_range_info_line_regex = /^@@ .+\+(?<line_number>\d+),/
348
+ git_modified_line_regex = /^\+(?!\+|\+)/
349
+ git_removed_line_regex = /^[-]/
350
+ git_not_removed_line_regex = /^[^-]/
351
+ file_info = git.diff_for_file(file)
352
+ line_number = 0
353
+ lines = []
354
+ file_info.patch.split("\n").each do |line|
355
+ starting_line_number = 0
356
+ case line
357
+ when git_range_info_line_regex
358
+ starting_line_number = Regexp.last_match[:line_number].to_i
359
+ when git_modified_line_regex
360
+ lines << line_number
361
+ end
362
+ line_number += 1 if line_number > 0
363
+ line_number = starting_line_number if line_number == 0 && starting_line_number > 0
364
+ end
365
+ lines
366
+ end
311
367
  end
312
368
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DangerSwiftlint
4
- VERSION = '0.21.1'
5
- SWIFTLINT_VERSION = '0.31.0'
4
+ VERSION = '0.22.0'
5
+ SWIFTLINT_VERSION = '0.33.1'
6
6
  end
@@ -348,6 +348,63 @@ module Danger
348
348
  expect(status[:errors]).to be_empty
349
349
  end
350
350
 
351
+ it 'Get git modified file line numbers' do
352
+ git_diff = File.read("spec/fixtures/SwiftFile.diff")
353
+ allow(@swiftlint.git).to receive(:diff_for_file).and_return(git_diff)
354
+ allow(@swiftlint.git.diff_for_file).to receive(:patch).and_return(git_diff)
355
+ modified_lines = @swiftlint.git_modified_lines("spec/fixtures/SwiftFile.swift")
356
+ expect(modified_lines).to_not be_empty
357
+ expect(modified_lines.length).to eql(23)
358
+ end
359
+
360
+ it 'Get git modified files info' do
361
+ allow(@swiftlint.git).to receive(:added_files).and_return([])
362
+ allow(@swiftlint.git).to receive(:modified_files).and_return([
363
+ 'spec/fixtures/SwiftFile.swift',
364
+ 'spec/fixtures/DeletedFile.swift'
365
+ ])
366
+ allow(@swiftlint.git).to receive(:deleted_files).and_return([
367
+ 'spec/fixtures/DeletedFile.swift'
368
+ ])
369
+ git_diff = File.read("spec/fixtures/SwiftFile.diff")
370
+ allow(@swiftlint.git).to receive(:diff_for_file).and_return(git_diff)
371
+ allow(@swiftlint.git.diff_for_file).to receive(:patch).and_return(git_diff)
372
+ modified_files_info = @swiftlint.git_modified_files_info
373
+ expect(modified_files_info).to_not be_empty
374
+ expect(modified_files_info.length).to eql(1)
375
+ end
376
+
377
+ it 'filters lint issues to return issues in modified files based on git diff patch info' do
378
+ allow(@swiftlint.git).to receive(:added_files).and_return([])
379
+ allow(@swiftlint.git).to receive(:modified_files).and_return([
380
+ 'spec/fixtures/SwiftFile.swift',
381
+ 'spec/fixtures/DeletedFile.swift'
382
+ ])
383
+ allow(@swiftlint.git).to receive(:deleted_files).and_return([
384
+ 'spec/fixtures/DeletedFile.swift'
385
+ ])
386
+ git_diff = File.read("spec/fixtures/SwiftFile.diff")
387
+ allow(@swiftlint.git).to receive(:diff_for_file).and_return(git_diff)
388
+ allow(@swiftlint.git.diff_for_file).to receive(:patch).and_return(git_diff)
389
+
390
+ swiftlint_violations_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" : 14 },
391
+ { "rule_id" : "force_cast", "reason" : "Force casts should be avoided.", "character" : 10, "file" : "/Users/me/this_repo/spec/fixtures/SwiftFile.swift", "severity" : "Error", "type" : "Force Cast", "line" : 16 }]'
392
+
393
+ violations_json = JSON.parse(swiftlint_violations_response)
394
+ violations_json[0][:file] = File.expand_path('spec/fixtures/SwiftFile.swift')
395
+ violations_json[1][:file] = File.expand_path('spec/fixtures/SwiftFile.swift')
396
+ swiftlint_violations_response= violations_json.to_json
397
+ allow_any_instance_of(Swiftlint).to receive(:lint)
398
+ .with(hash_including(path: File.expand_path('spec/fixtures/SwiftFile.swift')), '')
399
+ .and_return(swiftlint_violations_response)
400
+
401
+ @swiftlint.filter_issues_in_diff = true
402
+ @swiftlint.lint_files('spec/fixtures/*.swift', inline_mode: true, fail_on_error: false, additional_swiftlint_args: '')
403
+
404
+ status = @swiftlint.status_report
405
+ expect(status[:warnings]).to eql(["Force casts should be avoided.\n`force_cast` `SwiftFile.swift:16`"])
406
+ end
407
+
351
408
  context '#strict' do
352
409
  before(:each) do
353
410
  allow_any_instance_of(Swiftlint).to receive(:lint)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danger-swiftlint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.1
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ash Furrow
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2019-05-21 00:00:00.000000000 Z
15
+ date: 2019-07-12 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: danger
@@ -181,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
181
  version: '0'
182
182
  requirements: []
183
183
  rubyforge_project:
184
- rubygems_version: 2.7.6
184
+ rubygems_version: 2.6.13
185
185
  signing_key:
186
186
  specification_version: 4
187
187
  summary: A Danger plugin for linting Swift with SwiftLint.