Skip to content

Commit

Permalink
fix rubocop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
taichi-ishitani committed Sep 15, 2023
1 parent efc3bdd commit a023e6b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 115 deletions.
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in file_list_generator.gemspec
gemspec

group :developmend do
gem 'bump', '~> 0.10.0'
gem 'rake'
gem 'rspec', '~> 3.12.0'
gem 'rubocop', '~> 1.40.0'
gem 'simplecov', '~> 0.21.0'
gem 'simplecov-cobertura', '~> 2.1.0'
end
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'bundler/setup'
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'
Expand Down
8 changes: 0 additions & 8 deletions flgen.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,4 @@ Gem::Specification.new do |spec|
spec.executables = `git ls-files -- exe/*`.split($RS).map(&File.method(:basename))
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 3.0'

spec.add_development_dependency 'bump', '~> 0.10.0'
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec', '~> 3.12.0'
spec.add_development_dependency 'rubocop', '~> 1.40.0'
spec.add_development_dependency 'simplecov', '~> 0.21.0'
spec.add_development_dependency 'simplecov-cobertura', '~> 2.1.0'
end
111 changes: 42 additions & 69 deletions lib/flgen/file_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,42 @@ def reset_default_search_path(*target_types)
end

def file_list(path, from: nil, raise_error: true)
location = caller_location
load_file_list(path, from, location, raise_error)
load_file_list(path, from, raise_error, caller_location)
end

def source_file(path, from: nil, raise_error: true)
location = caller_location
add_file_entry(path, from, location, raise_error, :source_file)
add_entry(path, from, raise_error, __callee__, caller_location)
end

def library_file(path, from: nil, raise_error: true)
location = caller_location
add_file_entry(path, from, location, raise_error, :library_file)
end

def define_macro(macro, value = nil)
@context.define_macro(macro, value)
end

def macro?(macro)
@context.macros.include?(macro.to_sym)
end

alias_method :macro_defined?, :macro?

def include_directory(path, from: nil, raise_error: true)
location = caller_location
add_directory_entry(path, from, location, raise_error, :include_directory)
end

def library_directory(path, from: nil, raise_error: true)
location = caller_location
add_directory_entry(path, from, location, raise_error, :library_directory)
end
define_method(:library_file, instance_method(:source_file))
define_method(:include_directory, instance_method(:source_file))
define_method(:library_directory, instance_method(:source_file))

def find_files(*patterns, from: nil, &block)
location = caller_location
glob_files(patterns, __method__, from, location)
glob_files(patterns, from, __callee__, caller_location)
.then { |e| block ? e.each(&block) : e.to_a }
end

def find_file(*patterns, from: nil)
location = caller_location
glob_files(patterns, __method__, from, location).first
glob_files(patterns, from, __callee__, caller_location).first
end

def file?(path, from: :current)
location = caller_location
!extract_file_path(path, from, location, :file).nil?
!extract_path(path, from, __callee__, caller_location).nil?
end

def directory?(path, from: :current)
location = caller_location
!extract_directory_path(path, from, location, :directory).nil?
define_method(:directory?, instance_method(:file?))

def define_macro(macro, value = nil)
@context.define_macro(macro, value)
end

def macro?(macro)
@context.macros.include?(macro.to_sym)
end

alias_method :macro_defined?, :macro?

def env?(name)
ENV.key?(name.to_s)
end
Expand Down Expand Up @@ -111,14 +92,14 @@ def init_default_search_path
Hash.new { |_, key| key == :file_list ? :root : :current }
end

def load_file_list(path, from, location, raise_error)
unless (list_path = extract_file_path(path, from, location, :file_list))
def load_file_list(path, from, raise_error, location)
unless (extracted_path = extract_path(path, from, :file_list, location))
raise_no_entry_error(path, location, raise_error)
return
end

# Need to File.realpath to resolve symblic link
list_path = File.realpath(list_path)
list_path = File.realpath(extracted_path)
file_list_already_loaded?(list_path) && return

@context.loaded_file_lists << list_path
Expand All @@ -130,28 +111,23 @@ def file_list_already_loaded?(path)
@context.loaded_file_lists.include?(path)
end

def add_file_entry(path, from, location, raise_error, type)
unless (file_path = extract_file_path(path, from, location, type))
def add_entry(path, from, raise_error, method_name, location)
unless (extracted_path = extract_path(path, from, method_name, location))
raise_no_entry_error(path, location, raise_error)
return
end

method = "add_#{type}".to_sym
@context.__send__(method, file_path)
@context.__send__("add_#{method_name}".to_sym, extracted_path)
end

def add_directory_entry(path, from, location, raise_error, type)
unless (directory_path = extract_directory_path(path, from, location, type))
raise_no_entry_error(path, location, raise_error)
return
end
def raise_no_entry_error(path, location, raise_error)
return unless raise_error

method = "add_#{type}".to_sym
@context.__send__(method, directory_path)
raise NoEntryError.new(path, location)
end

def glob_files(patterns, method_name, from, location)
patterns.product(search_root('', from, location, method_name))
def glob_files(patterns, from, method_name, location)
patterns.product(search_root('', from, method_name, location))
.lazy.flat_map { |patten, base| do_glob_files(patten, base) }
end

Expand All @@ -165,24 +141,16 @@ def caller_location
caller_locations(2, 1).first
end

def extract_file_path(path, from, location, type)
extract_path(path, from, location, type, :file?)
end

def extract_directory_path(path, from, location, type)
extract_path(path, from, location, type, :directory?)
end

def extract_path(path, from, location, type, checker)
search_root(path, from, location, type)
def extract_path(path, from, method_name, location)
search_root(path, from, method_name, location)
.map { |root| File.expand_path(path, root) }
.find { |abs_path| File.__send__(checker, abs_path) && abs_path }
.find { |abs_path| exist_path?(abs_path, method_name) }
end

FROM_KEYWORDS = [:cwd, :current, :local_root, :root].freeze

def search_root(path, from, location, type)
search_path = from || @default_search_path[type]
def search_root(path, from, method_name, location)
search_path = from || @default_search_path[method_name]
if absolute_path?(path)
['']
elsif FROM_KEYWORDS.include?(search_path)
Expand Down Expand Up @@ -213,10 +181,15 @@ def current_directory(location)
File.dirname(path)
end

def raise_no_entry_error(path, location, raise_error)
return unless raise_error
METHODS_TARGETING_DIRECTORY =
[:include_directory, :library_directory, :directory?].freeze

raise NoEntryError.new(path, location)
def exist_path?(path, method_name)
if METHODS_TARGETING_DIRECTORY.include?(method_name)
File.directory?(path)
else
File.file?(path)
end
end
end
end
76 changes: 38 additions & 38 deletions spec/flgen/file_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -558,44 +558,6 @@ def setup_expectation(root, file_name, *invalid_roots)
end
end

describe '#define_macro' do
it 'マクロを定義する' do
file_list = described_class.new(context, path)

expect(context).to receive(:define_macro).with(:FOO, nil)
expect(context).to receive(:define_macro).with(:BAR, 1)

file_list.define_macro(:FOO)
file_list.define_macro(:BAR, 1)
end
end

describe '#macro?/#macro_defined?' do
let(:macros) do
[:FOO, :BAR]
end

it '定義済みマクロかどうかを示す' do
file_list = described_class.new(context, path)
context.macros << macros[0]
context.macros << macros[1]

expect(file_list.macro?(:FOO)).to be true
expect(file_list.macro?('FOO')).to be true
expect(file_list.macro?(:BAR)).to be true
expect(file_list.macro?('BAR')).to be true
expect(file_list.macro?(:BAZ)).to be false
expect(file_list.macro?('BAZ')).to be false

expect(file_list.macro_defined?(:FOO)).to be true
expect(file_list.macro_defined?('FOO')).to be true
expect(file_list.macro_defined?(:BAR)).to be true
expect(file_list.macro_defined?('BAR')).to be true
expect(file_list.macro_defined?(:BAZ)).to be false
expect(file_list.macro_defined?('BAZ')).to be false
end
end

describe '#include_directory' do
let(:include_directories) do
['foo', 'bar/baz']
Expand Down Expand Up @@ -1386,6 +1348,44 @@ def mock_glab(patten, results)
end
end

describe '#define_macro' do
it 'マクロを定義する' do
file_list = described_class.new(context, path)

expect(context).to receive(:define_macro).with(:FOO, nil)
expect(context).to receive(:define_macro).with(:BAR, 1)

file_list.define_macro(:FOO)
file_list.define_macro(:BAR, 1)
end
end

describe '#macro?/#macro_defined?' do
let(:macros) do
[:FOO, :BAR]
end

it '定義済みマクロかどうかを示す' do
file_list = described_class.new(context, path)
context.macros << macros[0]
context.macros << macros[1]

expect(file_list.macro?(:FOO)).to be true
expect(file_list.macro?('FOO')).to be true
expect(file_list.macro?(:BAR)).to be true
expect(file_list.macro?('BAR')).to be true
expect(file_list.macro?(:BAZ)).to be false
expect(file_list.macro?('BAZ')).to be false

expect(file_list.macro_defined?(:FOO)).to be true
expect(file_list.macro_defined?('FOO')).to be true
expect(file_list.macro_defined?(:BAR)).to be true
expect(file_list.macro_defined?('BAR')).to be true
expect(file_list.macro_defined?(:BAZ)).to be false
expect(file_list.macro_defined?('BAZ')).to be false
end
end

describe '#env?' do
it '指定された環境変数が定義されているかを返す' do
file_list = described_class.new(context, path)
Expand Down

0 comments on commit a023e6b

Please sign in to comment.