Module: Transpec::SpecFileFinder

Defined in:
lib/transpec/spec_file_finder.rb

Class Method Summary collapse

Class Method Details

.base_paths(paths) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/transpec/spec_file_finder.rb', line 21

def base_paths(paths)
  if paths.empty?
    if Dir.exist?('spec')
      ['spec']
    else
      fail ArgumentError, 'Specify target files or directories.'
    end
  else
    if paths.all? { |path| inside_of_current_working_directory?(path) }
      paths
    else
      fail ArgumentError, 'Target path must be inside of the current working directory.'
    end
  end
end

.find(paths) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/transpec/spec_file_finder.rb', line 9

def find(paths)
  base_paths(paths).reduce([]) do |file_paths, path|
    if File.directory?(path)
      file_paths.concat(ruby_files_in_directory(path))
    elsif File.file?(path)
      file_paths << path
    elsif !File.exist?(path)
      fail ArgumentError, "No such file or directory #{path.inspect}"
    end
  end
end

.inside_of_current_working_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/transpec/spec_file_finder.rb', line 37

def inside_of_current_working_directory?(path)
  File.expand_path(path).start_with?(Dir.pwd)
end

.ruby_files_in_directory(directory_path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/transpec/spec_file_finder.rb', line 41

def ruby_files_in_directory(directory_path)
  ruby_file_paths = []

  Find.find(directory_path) do |path|
    next unless File.file?(path)
    next unless File.extname(path) == '.rb'
    ruby_file_paths << path
  end

  ruby_file_paths
end