Class: Polyamory::TestUnit

Inherits:
Object
  • Object
show all
Defined in:
lib/polyamory/test_unit.rb

Overview

Internal: Deals with finding Test::Unit or MiniTest files to test.

Defined Under Namespace

Classes: FocusedTestFinder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ TestUnit

Returns a new instance of TestUnit.



9
10
11
12
# File 'lib/polyamory/test_unit.rb', line 9

def initialize context
  @context = context
  @test_filters = context.name_filters.dup
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/polyamory/test_unit.rb', line 7

def context
  @context
end

#test_filtersObject (readonly)

Returns the value of attribute test_filters.



7
8
9
# File 'lib/polyamory/test_unit.rb', line 7

def test_filters
  @test_filters
end

Instance Method Details

#add_ruby_options(cmd) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/polyamory/test_unit.rb', line 133

def add_ruby_options cmd
  opts = []
  opts << '-w' if context.warnings?
  opts << "-Ilib:#{test_dir.basename}" unless rails?
  for path in context.load_paths
    opts << "-I#{path}"
  end
  opts << '%'
  cmd.env['RUBYOPT'] = opts.join(' ') if opts.size > 1
end

#add_test_filter_for_line(file, linenum) ⇒ Object



160
161
162
# File 'lib/polyamory/test_unit.rb', line 160

def add_test_filter_for_line file, linenum
  @test_filters << find_test_filter_for_line(file, linenum)
end

#all_matching_filesObject

Internal: Memoized find_files in primary dir



35
36
37
# File 'lib/polyamory/test_unit.rb', line 35

def all_matching_files
  @all_matching_files ||= find_files
end

#file_pattern(dir) ⇒ Object



18
19
20
# File 'lib/polyamory/test_unit.rb', line 18

def file_pattern dir
  "#{dir}/**/*_test.rb"
end

#file_pattern_alt(dir) ⇒ Object



22
23
24
# File 'lib/polyamory/test_unit.rb', line 22

def file_pattern_alt dir
  "#{dir}/**/test*.rb"
end

#find_files(dir = test_dir) ⇒ Object

Internal: Finds test files with one glob pattern or the other. The pattern that matches the most files wins.



28
29
30
31
32
# File 'lib/polyamory/test_unit.rb', line 28

def find_files dir = test_dir
  paths = glob file_pattern(dir)
  paths_alt = glob file_pattern_alt(dir)
  paths.size > paths_alt.size ? paths : paths_alt
end

#find_test_filter_for_line(file, linenum) ⇒ Object



164
165
166
167
# File 'lib/polyamory/test_unit.rb', line 164

def find_test_filter_for_line file, linenum
  focused_test_finder.call(file, linenum) or
    raise "test method not found (#{file.relative}:#{linenum})"
end

#focused_test_finderObject



169
# File 'lib/polyamory/test_unit.rb', line 169

def focused_test_finder() FocusedTestFinder end

#glob(pattern) ⇒ Object



125
126
127
# File 'lib/polyamory/test_unit.rb', line 125

def glob pattern
  RootedPathname.glob pattern, context.root
end

#handle?(path) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/polyamory/test_unit.rb', line 56

def handle? path
  path.in_dir? test_dir
end

#pick_jobs(paths) ⇒ Object

Public: From a list of paths, yank the ones that this knows how to handle, and build test jobs from it.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/polyamory/test_unit.rb', line 96

def pick_jobs paths
  to_test = []
  paths.reject! do |path|
    if handle? path
      to_test << path
      true
    end
  end

  unless to_test.empty?
    [test_command(to_test)]
  else
    []
  end
end

#rails?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/polyamory/test_unit.rb', line 129

def rails?
  (context.root + 'config/environment.rb').exist?
end

#resolve_as_directory(name) ⇒ Object

“functional” => “test/functional/**” “test/functional” => “test/functional/**”



69
70
71
72
73
74
# File 'lib/polyamory/test_unit.rb', line 69

def resolve_as_directory name
  dir = [test_dir + name, context.root + name].detect { |dir|
    dir.directory? and handle? dir
  }
  find_files(dir) if dir
end

#resolve_as_file_pattern(name) ⇒ Object

“word” => “test/**” that match “word”



89
90
91
92
# File 'lib/polyamory/test_unit.rb', line 89

def resolve_as_file_pattern name
  pattern = /(?:\b|_)#{Regexp.escape name}(?:\b|_)/
  all_matching_files.select {|p| p =~ pattern }
end

#resolve_as_filename(name) ⇒ Object

“test/unit/test_user.rb:42”



77
78
79
80
81
82
83
84
85
86
# File 'lib/polyamory/test_unit.rb', line 77

def resolve_as_filename name
  filename = name.sub(/:(\d+)$/, '')
  line_number = $1
  file = context.root + filename

  if file.file? and handle? file
    add_test_filter_for_line(file, line_number) if line_number
    file
  end
end

#resolve_name(name) ⇒ Object



60
61
62
63
64
65
# File 'lib/polyamory/test_unit.rb', line 60

def resolve_name name
  resolve_as_directory(name) or
    resolve_as_filename(name) or
    resolve_as_file_pattern(name) or
    raise "nothing resolved from #{name}"
end

#resolve_paths(names) ⇒ Object

Public: Resolve a set of files, directories, and patterns to a list of paths to test.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/polyamory/test_unit.rb', line 41

def resolve_paths names
  if context.tag_filters.any?
    # test/unit and minitest don't support tags
    []
  elsif names.any?
    paths = []
    for name in names
      paths.concat Array(resolve_name name)
    end
    paths
  else
    all_matching_files
  end
end

#test_command(paths) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/polyamory/test_unit.rb', line 112

def test_command paths
  Command.new %w'polyamory -t' do |test_job|
    add_ruby_options test_job
    test_job.concat paths.map {|p| p.relative }

    tunit_opts = testunit_options
    if tunit_opts.any?
      test_job << '--'
      test_job.concat tunit_opts
    end
  end
end

#test_dirObject



14
15
16
# File 'lib/polyamory/test_unit.rb', line 14

def test_dir
  @test_dir ||= context.root + 'test'
end

#test_filter_regexp(filters) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/polyamory/test_unit.rb', line 152

def test_filter_regexp filters
  if filters.size == 1
    '/%s/' % filters.first
  else
    '/(%s)/' % filters.join('|')
  end
end

#testunit_optionsObject



144
145
146
147
148
149
150
# File 'lib/polyamory/test_unit.rb', line 144

def testunit_options
  opts = []
  opts << '-n' << test_filter_regexp(test_filters) if test_filters.any?
  opts << '-s' << context.test_seed if context.test_seed
  opts << '-v' if context.verbose?
  opts
end