Class: Ace::TestRunner::Models::TestTarget

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/test_runner/models/test_target.rb

Overview

Represents a target of tests to be executed together

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, patterns: [], files: [], options: {}) ⇒ TestTarget

Returns a new instance of TestTarget.



10
11
12
13
14
15
# File 'lib/ace/test_runner/models/test_target.rb', line 10

def initialize(name:, patterns: [], files: [], options: {})
  @name = name
  @patterns = Array(patterns)
  @files = Array(files)
  @options = options
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



8
9
10
# File 'lib/ace/test_runner/models/test_target.rb', line 8

def files
  @files
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/ace/test_runner/models/test_target.rb', line 8

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/ace/test_runner/models/test_target.rb', line 8

def options
  @options
end

#patternsObject (readonly)

Returns the value of attribute patterns.



8
9
10
# File 'lib/ace/test_runner/models/test_target.rb', line 8

def patterns
  @patterns
end

Class Method Details

.default_targetsObject

Default test targets for common Ruby project structures



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ace/test_runner/models/test_target.rb', line 67

def self.default_targets
  {
    "fast" => {
      "patterns" => ["test/fast/**/*_test.rb", "test/*_test.rb", "test/unit/**/*_test.rb"],
      "options" => {}
    },
    "feat" => {
      "patterns" => ["test/feat/**/*_test.rb", "test/edge/**/*_test.rb"],
      "options" => {}
    },
    "all" => {
      "patterns" => ["test/fast/**/*_test.rb", "test/*_test.rb", "test/feat/**/*_test.rb", "test/edge/**/*_test.rb"],
      "options" => {}
    }
  }
end

.from_config(config) ⇒ Object

Load targets from configuration



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ace/test_runner/models/test_target.rb', line 53

def self.from_config(config)
  targets = config.fetch("targets", default_targets)

  targets.map do |name, definition|
    new(
      name: name,
      patterns: definition["patterns"] || [],
      files: definition["files"] || [],
      options: definition.fetch("options", {}).transform_keys(&:to_sym)
    )
  end
end

Instance Method Details

#execute(executor, formatter_options = {}) ⇒ Object

Execute this target’s tests



26
27
28
29
30
31
32
# File 'lib/ace/test_runner/models/test_target.rb', line 26

def execute(executor, formatter_options = {})
  test_files = find_files
  return empty_result if test_files.empty?

  options = @options.merge(formatter_options)
  executor.execute_tests(test_files, options)
end

#find_filesObject

Find all test files matching this target’s patterns



18
19
20
21
22
23
# File 'lib/ace/test_runner/models/test_target.rb', line 18

def find_files
  return @files unless @files.empty?

  detector = Atoms::TestDetector.new(patterns: @patterns)
  detector.find_test_files
end

#includes_file?(file_path) ⇒ Boolean

Check if a file belongs to this target

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/ace/test_runner/models/test_target.rb', line 35

def includes_file?(file_path)
  return true if @files.include?(file_path)

  @patterns.any? do |pattern|
    File.fnmatch(pattern, file_path, File::FNM_PATHNAME)
  end
end

#to_hObject



43
44
45
46
47
48
49
50
# File 'lib/ace/test_runner/models/test_target.rb', line 43

def to_h
  {
    name: @name,
    patterns: @patterns,
    files: @files,
    options: @options
  }
end