Skip to content

Commit

Permalink
Git conflicts validator, Fixes #36
Browse files Browse the repository at this point in the history
  • Loading branch information
agwozdowski committed Mar 5, 2016
1 parent 9ae3440 commit 472ee21
Show file tree
Hide file tree
Showing 14 changed files with 272 additions and 115 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- #21: Replace readme validator with required files validators, which allows us to
define what files you want to require in your project
- #35: Added gemfile validator which checks if Gemfile contains gems from local path
- #36: Added git conflicts validator which checks if there are files with git conflict marker

If you had readme validator turned off like this
```ruby
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ determine, which you can use in your project:
| rubycritic | - | Generates a RubyCritic for given project |
| allowed_extensions | - | Checks that all the files in ./config directory have an allowed extension |
| yml_parser | - | Checks that parameters of all yaml files in ./config directory have some value |
| git_conflicts | - | Checks if there are files with git conflict marker |
| bundler_audit | - | Checks for vulnerable versions of gems in Gemfile.lock |
| gemfile | - | Checks if Gemfile contains gems from local path |

Expand Down
14 changes: 2 additions & 12 deletions lib/polish_geeks/dev_tools/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Commands
# Base class for all the commands
# @abstract Subclass and use
class Base
include PolishGeeks::DevTools::Helpers::FileHelper

# Output string that we get after executing this command
attr_reader :output
# stored_output [PolishGeeks::DevTools::OutputStorer] storer with results of previous
Expand Down Expand Up @@ -60,18 +62,6 @@ def ensure_executable!
validator_class.new(stored_output).validate!
end
end

private

# @param [String] path from which we want take files
# @return [Array<String>] list of files in path with app prefix path
# @note if path is a file return array with file path with app prefix path
def files_from_path(path)
full_path = "#{::PolishGeeks::DevTools.app_root}/#{path}"
return [full_path] if File.file?(full_path)

Dir.glob(full_path).select { |f| File.file? f }
end
end
end
end
Expand Down
10 changes: 0 additions & 10 deletions lib/polish_geeks/dev_tools/commands/empty_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,6 @@ def excludes
def config_excludes
DevTools.config.empty_methods_ignored || []
end

# @param [String] file name that we want to sanitize
# @return [String] sanitized file name
# @example
# file = /home/something/app/lib/lib.rb,
# where /home/something/app/ is a app root path, then
# sanitize(file) #=> lib/lib.rb
def sanitize(file)
file.gsub("#{PolishGeeks::DevTools.app_root}/", '')
end
end
end
end
Expand Down
10 changes: 0 additions & 10 deletions lib/polish_geeks/dev_tools/commands/final_blank_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,6 @@ def config_excludes
DevTools.config.final_blank_line_ignored || []
end

# @param [String] file name that we want to sanitize
# @return [String] sanitized file name
# @example
# file = /home/something/app/lib/lib.rb,
# where /home/something/app/ is a app root path, then
# sanitize(file) #=> lib/lib.rb
def sanitize(file)
file.gsub("#{PolishGeeks::DevTools.app_root}/", '')
end

# @param [String] file name which we want validate
# @return [Boolean] true if file is empty or has final blank line.
# Otherwise return false.
Expand Down
44 changes: 44 additions & 0 deletions lib/polish_geeks/dev_tools/commands/git_conflicts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module PolishGeeks
module DevTools
module Commands
# Validator used to check if all files don't contain merge conflict
# @example: <<<<<<< HEAD
class GitConflicts < Base
self.type = :validator

attr_reader :counter

# Regexp that we want to use to catch files with git conflicts like <<<<<<< HEAD
CHECKED_REGEXP = '^<<<<<<<[ \t]'.freeze

# Executes this command and set output variable
def execute
@output = invalid_files
end

# @return [Boolean] true if all files don't contain git conflict
def valid?
output.empty?
end

# @return [String] default label for this command
def label
'Git conflicts'
end

# @return [String] message that should be printed when some files has git conflict
def error_message
"Following files have git conflicts: \n#{output.join("\n")}\n"
end

private

# @return [Array<String>] list of files which contain merge conflict marker
def invalid_files
cmd = "grep -r -l '#{CHECKED_REGEXP}' . | xargs -n1 basename"
PolishGeeks::DevTools::Shell.new.execute(cmd).split("\n")
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/polish_geeks/dev_tools/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def config
haml_lint
allowed_extensions
yml_parser
git_conflicts
rspec_files_names
rspec_files_structure
tasks_files_names
Expand Down
29 changes: 29 additions & 0 deletions lib/polish_geeks/dev_tools/helpers/file_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module PolishGeeks
module DevTools
# Helpers wrapper
module Helpers
# Helper useful to manage paths and files
module FileHelper
# @param [String] file name that we want to sanitize
# @return [String] sanitized file name
# @example
# file = /home/something/app/lib/lib.rb,
# where /home/something/app/ is a app root path, then
# sanitize(file) #=> lib/lib.rb
def sanitize(file)
file.gsub("#{PolishGeeks::DevTools.app_root}/", '')
end

# @param [String] path from which we want take files
# @return [Array<String>] list of files in path with app prefix path
# @note if path is a file return array with file path with app prefix path
def files_from_path(path)
full_path = "#{::PolishGeeks::DevTools.app_root}/#{path}"
return [full_path] if File.file?(full_path)

Dir.glob(full_path).select { |f| File.file? f }
end
end
end
end
end
77 changes: 53 additions & 24 deletions lib/polishgeeks-dev-tools.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,58 @@
%w(
yaml
yard
pry
fileutils
timecop
faker
ostruct
).each { |lib| require lib }

%w(
validators/base
commands/base
commands/empty_methods
commands/empty_methods/string_refinements
).each { |lib| require "polish_geeks/dev_tools/#{lib}" }
# Require all from files array. If prefix is set method paste it before every file
# @param [Array<String>] files to require
# @param [String] prefix path
def require_files(files, prefix = nil)
files.each do |file|
require prefix ? File.join(prefix, file) : file
end
end

%w(
*.rb
validators/*.rb
commands/*.rb
commands/**/*.rb
).each do |path|
base_path = File.dirname(__FILE__) + "/polish_geeks/dev_tools/#{path}"
Dir[base_path].each { |file| require file }
# Require all file from dirs array. If prefix is set method paste it before every dir
# @param [Array<String>] dirs from whic we want require files
# @param [String] prefix path
def require_dirs(dirs, prefix = 'polish_geeks/dev_tools')
dirs.each do |path|
base_path = File.join(File.dirname(__FILE__), prefix, path)
Dir[base_path].each { |file| require file }
end
end

require_files(
%w(
yaml
yard
pry
fileutils
timecop
faker
ostruct
)
)

require_dirs(
%w(
helpers/**/*.rb
)
)

require_files(
%w(
validators/base
commands/base
commands/empty_methods
commands/empty_methods/string_refinements
),
'polish_geeks/dev_tools'
)

require_dirs(
%w(
*.rb
validators/*.rb
commands/*.rb
commands/**/*.rb
)
)

require 'polish_geeks/dev_tools'
load 'polish_geeks/dev_tools/tasks/dev-tools.rake'
43 changes: 0 additions & 43 deletions spec/lib/polish_geeks/dev_tools/commands/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,47 +44,4 @@
it { expect { subject.ensure_executable! }.not_to raise_error }
end
end

describe '#files_from_path' do
let(:app_root) { PolishGeeks::DevTools.app_root }

context 'path is a directory' do
let(:path) { rand.to_s }
let(:file_in_path) { "#{app_root}/#{rand}" }
let(:dir_in_path) { "#{app_root}/#{rand}" }
before do
expect(File)
.to receive(:file?)
.with("#{app_root}/#{path}")
.and_return(false)

expect(Dir)
.to receive(:glob)
.with("#{app_root}/#{path}")
.and_return([file_in_path, dir_in_path])

expect(File)
.to receive(:file?)
.with(file_in_path)
.and_return(true)

expect(File)
.to receive(:file?)
.with(dir_in_path)
.and_return(false)
end
it { expect(subject.send(:files_from_path, path)).to eq [file_in_path] }
end

context 'path is a file' do
let(:path) { rand.to_s }
before do
expect(File)
.to receive(:file?)
.with("#{app_root}/#{path}")
.and_return(true)
end
it { expect(subject.send(:files_from_path, path)).to eq ["#{app_root}/#{path}"] }
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,4 @@
it { expect(subject.send(:config_excludes)).to eq [] }
end
end

describe '#sanitize' do
let(:file) { rand.to_s }
let(:app_root) { PolishGeeks::DevTools.app_root }
let(:path) { "#{app_root}/#{file}" }

it { expect(subject.send(:sanitize, "#{app_root}/#{path}")).to eq file }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,6 @@
end
end

describe '#sanitize' do
let(:file) { rand.to_s }
let(:app_root) { PolishGeeks::DevTools.app_root }
let(:path) { "#{app_root}/#{file}" }

it { expect(subject.send(:sanitize, "#{app_root}/#{path}")).to eq file }
end

describe '#file_valid?' do
let(:file) { rand.to_s }

Expand Down
Loading

0 comments on commit 472ee21

Please sign in to comment.