Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When directory is specified, danger-swiftlint is still linting files outside the specified directory #53

Merged
merged 3 commits into from
Sep 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/danger_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,21 @@ def lint_files(files=nil, inline_mode: false)
else
nil
end

dir_selected = directory ? File.expand_path(directory) : Dir.pwd

# Extract excluded paths
excluded_paths = excluded_files_from_config(config)

# Extract swift files (ignoring excluded ones)
files = find_swift_files(files, excluded_paths)
files = find_swift_files(files, excluded_paths, dir_selected)

# Prepare swiftlint options
options = {
config: config,
reporter: 'json',
quiet: true,
pwd: directory ? File.expand_path(directory) : Dir.pwd
pwd: dir_selected
}

# Lint each file and collect the results
Expand Down Expand Up @@ -100,7 +102,7 @@ def run_swiftlint(files, options)
# If files are not provided it will use git modifield and added files
#
# @return [Array] swift files
def find_swift_files(files=nil, excluded_paths=[])
def find_swift_files(files=nil, excluded_paths=[], dir_selected)
# Assign files to lint
files = files ? Dir.glob(files) : (git.modified_files - git.deleted_files) + git.added_files

Expand All @@ -113,6 +115,8 @@ def find_swift_files(files=nil, excluded_paths=[])
# Remove dups
uniq.
map { |file| File.expand_path(file) }.
# Ensure only files in the selected directory
select { |file| file.start_with?(dir_selected) }.
# Reject files excluded on configuration
reject { |file|
excluded_paths.any? { |excluded_path|
Expand Down
22 changes: 20 additions & 2 deletions spec/danger_plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,34 @@ module Danger
end

it 'uses a custom directory' do
@swiftlint.directory = 'some_dir'
@swiftlint.directory = 'spec/fixtures/some_dir'

allow_any_instance_of(Swiftlint).to receive(:lint)
.with(hash_including(:pwd => File.expand_path(@swiftlint.directory)))
.and_return(@swiftlint_response)

@swiftlint.lint_files("spec/fixtures/*.swift")
@swiftlint.lint_files(["spec/fixtures/some_dir/SwiftFile.swift"])

output = @swiftlint.status_report[:markdowns].first.to_s
expect(output).to_not be_empty

end

it 'only lint files specified in custom dir' do
@swiftlint.directory = 'spec/fixtures/some_dir'

allow(@swiftlint.git).to receive(:added_files).and_return([])
allow(@swiftlint.git).to receive(:modified_files).and_return([
'spec/fixtures/some_dir/SwiftFile.swift',
'spec/fixtures/SwiftFile.swift'
])

expect_any_instance_of(Swiftlint).to receive(:lint)
.with(hash_including(:path => File.expand_path('spec/fixtures/some_dir/SwiftFile.swift')))
.once
.and_return(@swiftlint_response)

@swiftlint.lint_files
end

it 'does not crash if JSON reporter returns an empty string rather than an object' do
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/some_dir/SwiftFile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This file intentional left blank-ish.