lib/opal/rspec/locator.rb in opal-rspec-1.0.0 vs lib/opal/rspec/locator.rb in opal-rspec-1.1.0.alpha1
- old
+ new
@@ -1,38 +1,62 @@
require 'pathname'
require 'rake'
# require the bundled RSpec's file and don't rely on the load path in case opal-rspec is included in a project's
# Gemfile without rspec also being in the Gemfile
-require_relative '../../../rspec-core/upstream/lib/rspec/core/ruby_project'
+load __dir__+'/../../../rspec-core/upstream/lib/rspec/core/ruby_project.rb', ::Opal
module Opal
module RSpec
class Locator
- include ::RSpec::Core::RubyProject
+ include ::Opal::RSpec::Core::RubyProject
DEFAULT_GLOB = '**{,/*/**}/*_spec{.js,}.{rb,opal}'
- DEFAULT_PATTERN = "spec-opal/#{DEFAULT_GLOB}"
DEFAULT_DEFAULT_PATH = 'spec-opal'
attr_accessor :spec_pattern, :spec_exclude_pattern, :spec_files, :default_path
def initialize(pattern: nil, exclude_pattern: nil, files: nil, default_path: nil)
- @spec_pattern = pattern || DEFAULT_PATTERN
- @spec_exclude_pattern = exclude_pattern
+ @spec_exclude_pattern = Array(exclude_pattern)
@spec_files = files
@default_path = default_path || DEFAULT_DEFAULT_PATH
+ @spec_pattern = Array(pattern || DEFAULT_GLOB)
+
+ @spec_pattern = @spec_pattern.map do |pattern|
+ pattern.sub(/\A#{Regexp.escape(@default_path)}/, '')
+ end
+
+ @spec_exclude_pattern = @spec_exclude_pattern.map do |pattern|
+ pattern.sub(/\A#{Regexp.escape(@default_path)}/, '')
+ end
end
def determine_root
find_first_parent_containing(@default_path) || '.'
end
def get_spec_load_paths
- [@default_path].map { |dir| File.join(root, dir) }
+ [File.join(root, @default_path)]
end
+ def get_matching_files_under(path: )
+ FileList[*@spec_pattern.map { |i| "#{path}/#{i}" }]
+ .exclude(*@spec_exclude_pattern.map { |i| "#{path}/#{i}" })
+ end
+
def get_opal_spec_requires
- files = @spec_files || FileList[*@spec_pattern].exclude(*@spec_exclude_pattern)
+ if !@spec_files || @spec_files.empty?
+ files = get_matching_files_under(path: @default_path)
+ else
+ files = @spec_files.map do |file|
+ file = file.split(/[\[:]/).first
+ if File.directory?(file)
+ get_matching_files_under(path: file).to_a
+ else
+ file
+ end
+ end.flatten
+ files = FileList[*files]
+ end
files.uniq.map { |file| File.expand_path file }
end
end
end
end