diff --git a/Gemfile b/Gemfile index a6c2de8..7c33060 100755 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'https://rubygems.org' gemspec diff --git a/Guardfile b/Guardfile index bc967df..fe9dfd3 100755 --- a/Guardfile +++ b/Guardfile @@ -1,10 +1,12 @@ +# frozen_string_literal: true + # A guardfile for making Danger Plugins # For more info see https://github.com/guard/guard#readme # To run, use `bundle exec guard`. -guard :rspec, cmd: "bundle exec rspec" do - require "guard/rspec/dsl" +guard :rspec, cmd: 'bundle exec rspec' do + require 'guard/rspec/dsl' dsl = Guard::RSpec::Dsl.new(self) # Feel free to open issues for suggestions and improvements diff --git a/Rakefile b/Rakefile index 1f5d138..46e4f9e 100755 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,8 @@ +# frozen_string_literal: true + import 'ext/swiftlint/Rakefile' require 'bundler/gem_tasks' -require "rspec/core/rake_task" +require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) @@ -10,5 +12,4 @@ task :spec do Rake::Task['specs'].invoke end -task :default => :spec - +task default: :spec diff --git a/bin/danger-swiftlint b/bin/danger-swiftlint index a1bb3c5..076047b 100755 --- a/bin/danger-swiftlint +++ b/bin/danger-swiftlint @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# frozen_string_literal: true if $PROGRAM_NAME == __FILE__ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) diff --git a/danger-swiftlint.gemspec b/danger-swiftlint.gemspec index ab115b6..a4c39aa 100755 --- a/danger-swiftlint.gemspec +++ b/danger-swiftlint.gemspec @@ -1,4 +1,6 @@ -# coding: utf-8 + +# frozen_string_literal: true + lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'version' @@ -8,15 +10,15 @@ Gem::Specification.new do |spec| spec.version = DangerSwiftlint::VERSION spec.authors = ['Ash Furrow', 'David Grandinetti', 'Orta Therox', 'Thiago Felix', 'Giovanni Lodi'] spec.email = ['ash@ashfurrow.com', 'dbgrandi@gmail.com', 'orta.therox@gmail.com', 'thiago@thiagofelix.com', 'gio@mokacoding.com'] - spec.description = %q{A Danger plugin for linting Swift with SwiftLint.} - spec.summary = %q{A Danger plugin for linting Swift with SwiftLint.} + spec.description = 'A Danger plugin for linting Swift with SwiftLint.' + spec.summary = 'A Danger plugin for linting Swift with SwiftLint.' spec.homepage = 'https://github.com/ashfurrow/danger-swiftlint' spec.license = 'MIT' - spec.files = `git ls-files`.split($/) + spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ['lib'] - spec.extensions = %w(ext/swiftlint/Rakefile) + spec.extensions = %w[ext/swiftlint/Rakefile] spec.executables = ['danger-swiftlint'] spec.add_dependency 'danger' @@ -27,19 +29,18 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'bundler', '~> 1.3' # Testing support - spec.add_development_dependency "rspec", '~> 3.4' + spec.add_development_dependency 'rspec', '~> 3.4' # Makes testing easy via `bundle exec guard` - spec.add_development_dependency "guard", '~> 2.14' - spec.add_development_dependency "guard-rspec", '~> 4.7' + spec.add_development_dependency 'guard', '~> 2.14' + spec.add_development_dependency 'guard-rspec', '~> 4.7' # If you want to work on older builds of ruby - spec.add_development_dependency "listen", '3.0.7' + spec.add_development_dependency 'listen', '3.0.7' # This gives you the chance to run a REPL inside your test # via # binding.pry # This will stop test execution and let you inspect the results - spec.add_development_dependency "pry" - + spec.add_development_dependency 'pry' end diff --git a/ext/swiftlint/Rakefile b/ext/swiftlint/Rakefile index 8073b84..2aae6ee 100644 --- a/ext/swiftlint/Rakefile +++ b/ext/swiftlint/Rakefile @@ -1,12 +1,13 @@ +# frozen_string_literal: true + require_relative '../../lib/version' namespace :swiftlint do - - desc "Download and install swiftlint tool" + desc 'Download and install swiftlint tool' task :install do - REPO = "https://github.com/realm/SwiftLint" - VERSION = ENV["SWIFTLINT_VERSION"] || DangerSwiftlint::SWIFTLINT_VERSION - ASSET = "portable_swiftlint.zip" + REPO = 'https://github.com/realm/SwiftLint' + VERSION = ENV['SWIFTLINT_VERSION'] || DangerSwiftlint::SWIFTLINT_VERSION + ASSET = 'portable_swiftlint.zip' URL = "#{REPO}/releases/download/#{VERSION}/#{ASSET}" DESTINATION = File.expand_path(File.join(File.dirname(__FILE__), 'bin')) @@ -16,9 +17,8 @@ namespace :swiftlint do "curl -s -L #{URL} -o #{ASSET}", "unzip -q #{ASSET} -d #{DESTINATION}", "rm #{ASSET}" - ].join(" && ") + ].join(' && ') end - end task default: 'swiftlint:install' diff --git a/ext/swiftlint/swiftlint.rb b/ext/swiftlint/swiftlint.rb index 3700df0..31a63f3 100644 --- a/ext/swiftlint/swiftlint.rb +++ b/ext/swiftlint/swiftlint.rb @@ -1,15 +1,15 @@ +# frozen_string_literal: true + class Swiftlint - def initialize(swiftlint_path=nil) + def initialize(swiftlint_path = nil) @swiftlint_path = swiftlint_path end # Runs swiftlint - def run(cmd='lint', additional_swiftlint_args='', options={}) + def run(cmd = 'lint', additional_swiftlint_args = '', options = {}) # change pwd before run swiftlint - if options.has_key? :pwd - Dir.chdir options.delete(:pwd) - end + Dir.chdir options.delete(:pwd) if options.key? :pwd # run swiftlint with provided options `#{swiftlint_path} #{cmd} #{swiftlint_arguments(options, additional_swiftlint_args)}` @@ -27,7 +27,7 @@ def is_installed? # Return swiftlint execution path def swiftlint_path - @swiftlint_path or default_swiftlint_path + @swiftlint_path || default_swiftlint_path end private @@ -35,10 +35,10 @@ def swiftlint_path # Parse options into shell arguments how swift expect it to be # more information: https://github.com/Carthage/Commandant # @param options (Hash) hash containing swiftlint options - def swiftlint_arguments (options, additional_swiftlint_args) + def swiftlint_arguments(options, additional_swiftlint_args) (options. # filter not null - select {|key, value| !value.nil?}. + reject { |_key, value| value.nil? }. # map booleans arguments equal true map { |key, value| value.is_a?(TrueClass) ? [key, ''] : [key, value] }. # map booleans arguments equal false @@ -48,7 +48,7 @@ def swiftlint_arguments (options, additional_swiftlint_args) # prepend '--' into the argument map { |key, value| ["--#{key}", value] }. # reduce everything into a single string - reduce('') { |args, option| "#{args} #{option[0]} #{option[1]}"} + + reduce('') { |args, option| "#{args} #{option[0]} #{option[1]}" } + " #{additional_swiftlint_args}"). # strip leading spaces strip diff --git a/lib/danger_plugin.rb b/lib/danger_plugin.rb index acb7269..7c01165 100755 --- a/lib/danger_plugin.rb +++ b/lib/danger_plugin.rb @@ -1,10 +1,11 @@ +# frozen_string_literal: true + require 'find' require 'yaml' require 'shellwords' require_relative '../ext/swiftlint/swiftlint' module Danger - # Lint Swift files inside your projects. # This is done using the [SwiftLint](https://github.com/realm/SwiftLint) tool. # Results are passed out as a table in markdown. @@ -39,9 +40,9 @@ class DangerSwiftlint < Plugin # if nil, modified and added files from the diff will be used. # @return [void] # - def lint_files(files=nil, inline_mode: false, fail_on_error: false, additional_swiftlint_args: "") + def lint_files(files = nil, inline_mode: false, fail_on_error: false, additional_swiftlint_args: '') # Fails if swiftlint isn't installed - raise "swiftlint is not installed" unless swiftlint.is_installed? + raise 'swiftlint is not installed' unless swiftlint.is_installed? config = if config_file File.expand_path(config_file) @@ -51,7 +52,7 @@ def lint_files(files=nil, inline_mode: false, fail_on_error: false, additional_s nil end log "Using config file: #{config}" - + dir_selected = directory ? File.expand_path(directory) : Dir.pwd log "Swiftlint will be run from #{dir_selected}" @@ -81,19 +82,19 @@ def lint_files(files=nil, inline_mode: false, fail_on_error: false, additional_s if inline_mode # Reprt with inline comment - send_inline_comment(warnings, "warn") - send_inline_comment(errors, "fail") + send_inline_comment(warnings, 'warn') + send_inline_comment(errors, 'fail') else # Report if any warning or error if warnings.count > 0 || errors.count > 0 - message = "### SwiftLint found issues\n\n" + message = +"### SwiftLint found issues\n\n" message << markdown_issues(warnings, 'Warnings') unless warnings.empty? 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" + if fail_on_error && errors.count.positive? + fail 'Failed due to SwiftLint errors' end end end @@ -104,8 +105,8 @@ def lint_files(files=nil, inline_mode: false, fail_on_error: false, additional_s # @return [Array] swiftlint issues def run_swiftlint(files, options, additional_swiftlint_args) files - .map { |file| options.merge({path: file})} - .map { |full_options| swiftlint.lint(full_options, additional_swiftlint_args)} + .map { |file| options.merge(path: file) } + .map { |full_options| swiftlint.lint(full_options, additional_swiftlint_args) } .reject { |s| s == '' } .map { |s| JSON.parse(s).flatten } .flatten @@ -115,29 +116,29 @@ def run_swiftlint(files, options, additional_swiftlint_args) # 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=[], dir_selected) + 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 # Filter files to lint - return files. + files. # Ensure only swift files are selected select { |file| file.end_with?('.swift') }. # Make sure we don't fail when paths have spaces map { |file| Shellwords.escape(file) }. # Remove dups - uniq. - map { |file| File.expand_path(file) }. + 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| - Find.find(excluded_path). - map { |excluded_file| Shellwords.escape(excluded_file) }. - include?(file) - } - } + reject do |file| + excluded_paths.any? do |excluded_path| + Find.find(excluded_path) + .map { |excluded_file| Shellwords.escape(excluded_file) } + .include?(file) + end + end end # Parses the configuration file and return the excluded files @@ -145,25 +146,25 @@ def find_swift_files(files=nil, excluded_paths=[], dir_selected) # @return [Array] list of files excluded def excluded_files_from_config(filepath) config = if filepath - YAML.load_file(filepath) - else - {"excluded" => []} + YAML.load_file(filepath) + else + { 'excluded' => [] } end excluded_paths = config['excluded'] || [] # Extract excluded paths - return excluded_paths. - map { |path| File.join(File.dirname(filepath), path) }. - map { |path| File.expand_path(path) }. - select { |path| File.exists?(path) || Dir.exists?(path) } + excluded_paths + .map { |path| File.join(File.dirname(filepath), path) } + .map { |path| File.expand_path(path) } + .select { |path| File.exist?(path) || Dir.exist?(path) } end # Create a markdown table from swiftlint issues # # @return [String] - def markdown_issues (results, heading) - message = "#### #{heading}\n\n" + def markdown_issues(results, heading) + message = +"#### #{heading}\n\n" message << "File | Line | Reason |\n" message << "| --- | ----- | ----- |\n" @@ -182,11 +183,11 @@ def markdown_issues (results, heading) # Send inline comment with danger's warn or fail method # # @return [void] - def send_inline_comment (results, method) + 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 diff --git a/lib/version.rb b/lib/version.rb index 732a6e1..fcc7bfb 100755 --- a/lib/version.rb +++ b/lib/version.rb @@ -1,4 +1,6 @@ +# frozen_string_literal: true + module DangerSwiftlint - VERSION = "0.10.1".freeze - SWIFTLINT_VERSION = "0.20.1".freeze + VERSION = '0.10.0' + SWIFTLINT_VERSION = '0.20.1' end diff --git a/spec/danger_plugin_spec.rb b/spec/danger_plugin_spec.rb index 32320ba..8acfdb0 100755 --- a/spec/danger_plugin_spec.rb +++ b/spec/danger_plugin_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require File.expand_path('../spec_helper', __FILE__) module Danger @@ -12,9 +14,9 @@ module Danger allow(@swiftlint.git).to receive(:deleted_files).and_return([]) end - it "handles swiftlint not being installed" do + it 'handles swiftlint not being installed' do allow_any_instance_of(Swiftlint).to receive(:is_installed?).and_return(false) - expect { @swiftlint.lint_files }.to raise_error("swiftlint is not installed") + expect { @swiftlint.lint_files }.to raise_error('swiftlint is not installed') end it 'does not markdown an empty message' do @@ -44,29 +46,29 @@ module Danger it 'accept files as arguments' do expect_any_instance_of(Swiftlint).to receive(:lint) - .with(hash_including(:path => File.expand_path('spec/fixtures/SwiftFile.swift')), '') + .with(hash_including(path: File.expand_path('spec/fixtures/SwiftFile.swift')), '') .and_return(@swiftlint_response) - @swiftlint.lint_files("spec/fixtures/*.swift") + @swiftlint.lint_files('spec/fixtures/*.swift') output = @swiftlint.status_report[:markdowns].first.to_s - expect(output).to include("SwiftLint found issues") - expect(output).to include("SwiftFile.swift | 13 | Force casts should be avoided.") + expect(output).to include('SwiftLint found issues') + expect(output).to include('SwiftFile.swift | 13 | Force casts should be avoided.') end it 'accepts additional cli arguments' do expect_any_instance_of(Swiftlint).to receive(:lint) - .with(hash_including(:path => File.expand_path('spec/fixtures/SwiftFile.swift')), '--lenient') + .with(hash_including(path: File.expand_path('spec/fixtures/SwiftFile.swift')), '--lenient') .and_return(@swiftlint_response) - @swiftlint.lint_files("spec/fixtures/*.swift", additional_swiftlint_args: '--lenient') + @swiftlint.lint_files('spec/fixtures/*.swift', additional_swiftlint_args: '--lenient') end it 'uses git diff when files are not provided' do allow(@swiftlint.git).to receive(:modified_files).and_return(['spec/fixtures/SwiftFile.swift']) allow(@swiftlint.git).to receive(:added_files).and_return([]) allow_any_instance_of(Swiftlint).to receive(:lint) - .with(hash_including(:path => File.expand_path('spec/fixtures/SwiftFile.swift')), '') + .with(hash_including(path: File.expand_path('spec/fixtures/SwiftFile.swift')), '') .and_return(@swiftlint_response) @swiftlint.lint_files @@ -79,29 +81,28 @@ module Danger @swiftlint.directory = 'spec/fixtures/some_dir' allow_any_instance_of(Swiftlint).to receive(:lint) - .with(hash_including(:pwd => File.expand_path(@swiftlint.directory)), '') + .with(hash_including(pwd: File.expand_path(@swiftlint.directory)), '') .and_return(@swiftlint_response) - @swiftlint.lint_files(["spec/fixtures/some_dir/SwiftFile.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' - ]) + '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) + .with(hash_including(path: File.expand_path('spec/fixtures/some_dir/SwiftFile.swift')), '') + .once + .and_return(@swiftlint_response) @swiftlint.lint_files end @@ -127,13 +128,13 @@ module Danger it 'does not lint files in the excluded paths' do allow(@swiftlint.git).to receive(:added_files).and_return([]) allow(@swiftlint.git).to receive(:modified_files).and_return([ - 'spec/fixtures/SwiftFile.swift', - 'spec/fixtures/excluded_dir/SwiftFileThatShouldNotBeIncluded.swift', - 'spec/fixtures/excluded_dir/SwiftFile WithEscaped+CharactersThatShouldNotBeIncluded.swift' - ]) + 'spec/fixtures/SwiftFile.swift', + 'spec/fixtures/excluded_dir/SwiftFileThatShouldNotBeIncluded.swift', + 'spec/fixtures/excluded_dir/SwiftFile WithEscaped+CharactersThatShouldNotBeIncluded.swift' + ]) expect_any_instance_of(Swiftlint).to receive(:lint) - .with(hash_including(:path => File.expand_path('spec/fixtures/SwiftFile.swift')), '') + .with(hash_including(path: File.expand_path('spec/fixtures/SwiftFile.swift')), '') .once .and_return(@swiftlint_response) @@ -144,11 +145,11 @@ module Danger it 'does not crash when excluded is nil' do allow(@swiftlint.git).to receive(:added_files).and_return([]) allow(@swiftlint.git).to receive(:modified_files).and_return([ - 'spec/fixtures/SwiftFile.swift', - ]) + 'spec/fixtures/SwiftFile.swift' + ]) expect_any_instance_of(Swiftlint).to receive(:lint) - .with(hash_including(:path => File.expand_path('spec/fixtures/SwiftFile.swift')), '') + .with(hash_including(path: File.expand_path('spec/fixtures/SwiftFile.swift')), '') .once .and_return(@swiftlint_response) @@ -208,15 +209,15 @@ module Danger # they'd result in file not found errors. allow(@swiftlint.git).to receive(:added_files).and_return([]) allow(@swiftlint.git).to receive(:modified_files).and_return([ - 'spec/fixtures/SwiftFile.swift', - 'spec/fixtures/DeletedFile.swift' - ]) + 'spec/fixtures/SwiftFile.swift', + 'spec/fixtures/DeletedFile.swift' + ]) allow(@swiftlint.git).to receive(:deleted_files).and_return([ - 'spec/fixtures/DeletedFile.swift' - ]) + 'spec/fixtures/DeletedFile.swift' + ]) expect_any_instance_of(Swiftlint).to receive(:lint) - .with(hash_including(:path => File.expand_path('spec/fixtures/SwiftFile.swift')), '') + .with(hash_including(path: File.expand_path('spec/fixtures/SwiftFile.swift')), '') .once .and_return(@swiftlint_response) @@ -225,10 +226,10 @@ module Danger it 'generates errors instead of markdown when use inline mode' do allow_any_instance_of(Swiftlint).to receive(:lint) - .with(hash_including(:path => File.expand_path('spec/fixtures/SwiftFile.swift')), '') + .with(hash_including(path: File.expand_path('spec/fixtures/SwiftFile.swift')), '') .and_return(@swiftlint_response) - @swiftlint.lint_files("spec/fixtures/*.swift", inline_mode: true, fail_on_error: false, additional_swiftlint_args: '') + @swiftlint.lint_files('spec/fixtures/*.swift', inline_mode: true, fail_on_error: false, additional_swiftlint_args: '') status = @swiftlint.status_report expect(status[:errors]).to_not be_empty diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bebbaf6..c7db4ec 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,16 +1,18 @@ +# frozen_string_literal: true + require 'pathname' ROOT = Pathname.new(File.expand_path('../../', __FILE__)) -$:.unshift((ROOT + 'lib').to_s) -$:.unshift((ROOT + 'spec').to_s) +$LOAD_PATH.unshift((ROOT + 'lib').to_s) +$LOAD_PATH.unshift((ROOT + 'spec').to_s) RSpec.configure do |config| - # Use color in STDOUT + # Use color in STDOUT config.color = true end RSpec::Matchers.define :including do |x| - match { |actual| actual.include? x } + match { |actual| actual.include? x } end require 'bundler/setup' @@ -32,11 +34,11 @@ def testing_ui # running a PR on TravisCI def testing_env { - "HAS_JOSH_K_SEAL_OF_APPROVAL" => "true", - "TRAVIS_PULL_REQUEST" => "800", - "TRAVIS_REPO_SLUG" => "artsy/eigen", - "TRAVIS_COMMIT_RANGE" => "759adcbd0d8f...13c4dc8bb61d", - "DANGER_GITHUB_API_TOKEN" => "123sbdq54erfsd3422gdfio" + 'HAS_JOSH_K_SEAL_OF_APPROVAL' => 'true', + 'TRAVIS_PULL_REQUEST' => '800', + 'TRAVIS_REPO_SLUG' => 'artsy/eigen', + 'TRAVIS_COMMIT_RANGE' => '759adcbd0d8f...13c4dc8bb61d', + 'DANGER_GITHUB_API_TOKEN' => '123sbdq54erfsd3422gdfio' } end @@ -44,4 +46,4 @@ def testing_env def testing_dangerfile env = Danger::EnvironmentManager.new(testing_env) Danger::Dangerfile.new(env, testing_ui) -end \ No newline at end of file +end diff --git a/spec/swiftlint_spec.rb b/spec/swiftlint_spec.rb index fef9b28..5ed5641 100644 --- a/spec/swiftlint_spec.rb +++ b/spec/swiftlint_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require File.expand_path('../spec_helper', __FILE__) require_relative '../ext/swiftlint/swiftlint' @@ -25,7 +27,7 @@ it 'runs lint by default with options being optional' do expect(swiftlint).to receive(:`).with(including('swiftlint lint')) - swiftlint.run() + swiftlint.run end it 'runs accepting symbolized options' do @@ -36,8 +38,6 @@ '', use_stdin: false, cache_path: '/path', - enable_all_rules: true - ) + enable_all_rules: true) end end -