Class: Ace::TestRunner::Atoms::TestDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/test_runner/atoms/test_detector.rb

Overview

Detects and finds test files based on patterns

Constant Summary collapse

DEFAULT_PATTERNS =
[
  "test/**/*_test.rb",
  "spec/**/*_spec.rb",
  "test/**/test_*.rb"
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(patterns: nil, root_dir: ".") ⇒ TestDetector

Returns a new instance of TestDetector.



14
15
16
17
# File 'lib/ace/test_runner/atoms/test_detector.rb', line 14

def initialize(patterns: nil, root_dir: ".")
  @patterns = patterns || DEFAULT_PATTERNS
  @root_dir = root_dir
end

Instance Method Details

#classify_file(file_path) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ace/test_runner/atoms/test_detector.rb', line 66

def classify_file(file_path)
  case file_path
  when /test\/unit\/atoms\//
    :atoms
  when /test\/unit\/molecules\//
    :molecules
  when /test\/unit\/organisms\//
    :organisms
  when /test\/unit\/models\//
    :models
  when /test\/integration\//
    :integration
  when /test\/system\//
    :system
  when /test\/unit\//
    :unit
  when /spec\/unit\/atoms\//
    :atoms
  when /spec\/unit\/molecules\//
    :molecules
  when /spec\/unit\/organisms\//
    :organisms
  when /spec\/unit\/models\//
    :models
  when /spec\/integration\//
    :integration
  when /spec\/system\//
    :system
  when /spec\/unit\//
    :unit
  else
    :other
  end
end

#filter_by_pattern(files, pattern) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/ace/test_runner/atoms/test_detector.rb', line 41

def filter_by_pattern(files, pattern)
  return files unless pattern

  regex = Regexp.new(pattern, Regexp::IGNORECASE)
  files.select do |file|
    file.match?(regex) || File.basename(file).match?(regex)
  end
end

#find_test_filesObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ace/test_runner/atoms/test_detector.rb', line 19

def find_test_files
  files = []

  # Handle both hash (new format) and array (old format) patterns
  patterns_to_search = if @patterns.is_a?(Hash)
    @patterns.values
  else
    @patterns
  end

  patterns_to_search.each do |pattern|
    full_pattern = File.join(@root_dir, pattern)
    matched_files = Dir.glob(full_pattern).select { |f| File.file?(f) }
    files.concat(matched_files)
  end

  # Filter out helper files that aren't actual test files
  files = files.reject { |f| f.end_with?("/test_helper.rb", "/spec_helper.rb") }

  files.uniq.sort
end

#group_test_files(files) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/ace/test_runner/atoms/test_detector.rb', line 101

def group_test_files(files)
  grouped = Hash.new { |h, k| h[k] = [] }

  files.each do |file|
    group = classify_file(file)
    grouped[group] << file
  end

  grouped
end

#test_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ace/test_runner/atoms/test_detector.rb', line 50

def test_file?(path)
  return false unless File.exist?(path) && File.file?(path)

  # Handle both hash (new format) and array (old format) patterns
  patterns_to_check = if @patterns.is_a?(Hash)
    @patterns.values
  else
    @patterns
  end

  patterns_to_check.any? do |pattern|
    File.fnmatch?(pattern, path) ||
      File.fnmatch?(pattern, path.sub(@root_dir + "/", ""))
  end
end