Skip to content

Commit

Permalink
Merge pull request #60 from ashfurrow/cleanup
Browse files Browse the repository at this point in the history
Work for #48
  • Loading branch information
ashfurrow authored Oct 7, 2017
2 parents c79ea3b + 11f4b21 commit 5a13922
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Current Master

- Nothing yet!
- Expands config paths to be absolute when passed to `swiftlint`.

## 0.10.0

Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
danger-swiftlint (0.9.0)
danger-swiftlint (0.10.0)
danger
rake (> 10)
thor (~> 0.19)
Expand All @@ -21,7 +21,7 @@ GEM
colored2 (3.1.2)
cork (0.3.0)
colored2 (~> 3.1)
danger (5.5.1)
danger (5.5.3)
claide (~> 1.0)
claide-plugins (>= 0.9.2)
colored2 (~> 3.1)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ swiftlint.lint_files additional_swiftlint_args: '--lenient'

You can use the `SWIFTLINT_VERSION` environment variable to override the default version installed via the `rake install` task.

Finally, if something's not working correctly, you can debug this plugin by using setting `swiftlint.verbose = true`.

## Attribution

Original structure, sequence, and organization of repo taken from [danger-prose](https://github.com/dbgrandi/danger-prose) by [David Grandinetti](https://github.com/dbgrandi/).
Expand Down
28 changes: 20 additions & 8 deletions lib/danger_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class DangerSwiftlint < Plugin
# Allows you to specify a directory from where swiftlint will be run.
attr_accessor :directory

# Provides additional logging diagnostic information.
attr_accessor :verbose

# Lints Swift files. Will fail if `swiftlint` cannot be installed correctly.
# Generates a `markdown` list of warnings for the prose in a corpus of .markdown and .md files.
#
Expand All @@ -41,20 +44,23 @@ def lint_files(files=nil, inline_mode: false, fail_on_error: false, additional_s
raise "swiftlint is not installed" unless swiftlint.is_installed?

config = if config_file
config_file
File.expand_path(config_file)
elsif File.file?('.swiftlint.yml')
'.swiftlint.yml'
File.expand_path('.swiftlint.yml')
else
nil
end
log "Using config file: #{config}"

dir_selected = directory ? File.expand_path(directory) : Dir.pwd
log "Swiftlint will be run from #{dir_selected}"

# Extract excluded paths
excluded_paths = excluded_files_from_config(config)

# Extract swift files (ignoring excluded ones)
files = find_swift_files(files, excluded_paths, dir_selected)
log "Swiftlint will lint the following files: #{files.join(', ')}"

# Prepare swiftlint options
options = {
Expand All @@ -63,9 +69,11 @@ def lint_files(files=nil, inline_mode: false, fail_on_error: false, additional_s
quiet: true,
pwd: dir_selected
}
log "linting with options: #{options}"

# Lint each file and collect the results
issues = run_swiftlint(files, options, additional_swiftlint_args)
log "Received from Swiftlint: #{issues}"

# Filter warnings and errors
warnings = issues.select { |issue| issue['severity'] == 'Warning' }
Expand All @@ -83,10 +91,10 @@ def lint_files(files=nil, inline_mode: false, fail_on_error: false, additional_s
message << markdown_issues(errors, 'Errors') unless errors.empty?
markdown message

# Fail Danger on errors
if fail_on_error && errors.count > 0
fail "Failed due to SwiftLint errors"
end
# Fail Danger on errors
if fail_on_error && errors.count > 0
fail "Failed due to SwiftLint errors"
end
end
end
end
Expand Down Expand Up @@ -177,8 +185,8 @@ def markdown_issues (results, heading)
def send_inline_comment (results, method)
dir = "#{Dir.pwd}/"
results.each do |r|
filename = r['file'].gsub(dir, "")
send(method, r['reason'], file: filename, line: r['line'])
filename = r['file'].gsub(dir, "")
send(method, r['reason'], file: filename, line: r['line'])
end
end

Expand All @@ -188,5 +196,9 @@ def send_inline_comment (results, method)
def swiftlint
Swiftlint.new(binary_path)
end

def log(text)
puts(text) if @verbose
end
end
end
45 changes: 45 additions & 0 deletions spec/danger_plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,51 @@ module Danger
@swiftlint.lint_files
end

it 'default config is nil, unspecified' do
allow(@swiftlint.git).to receive(:added_files).and_return([])
allow(@swiftlint.git).to receive(:modified_files).and_return([
'spec/fixtures/SwiftFile.swift',
])

expect_any_instance_of(Swiftlint).to receive(:lint)
.with(hash_including(:config => nil), '')
.once
.and_return(@swiftlint_response)

@swiftlint.lint_files
end

it 'expands default config file (if present) to absolute path' do
allow(@swiftlint.git).to receive(:added_files).and_return([])
allow(@swiftlint.git).to receive(:modified_files).and_return([
'spec/fixtures/SwiftFile.swift',
])
expect(File).to receive(:file?).and_return(true)
expect(YAML).to receive(:load_file).and_return({})

expect_any_instance_of(Swiftlint).to receive(:lint)
.with(hash_including(:config => File.expand_path('.swiftlint.yml')), '')
.once
.and_return(@swiftlint_response)

@swiftlint.lint_files
end

it 'expands specified config file to absolute path' do
allow(@swiftlint.git).to receive(:added_files).and_return([])
allow(@swiftlint.git).to receive(:modified_files).and_return([
'spec/fixtures/SwiftFile.swift',
])

expect_any_instance_of(Swiftlint).to receive(:lint)
.with(hash_including(:config => File.expand_path('spec/fixtures/some_config.yml')), '')
.once
.and_return(@swiftlint_response)

@swiftlint.config_file = 'spec/fixtures/some_config.yml'
@swiftlint.lint_files
end

it 'does not lint deleted files paths' do
# Danger (4.3.0 at the time of writing) returns deleted files in the
# modified fiels array, which kinda makes sense.
Expand Down

0 comments on commit 5a13922

Please sign in to comment.