Class: Polyamory::RSpec

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

Overview

Internal: Deals with finding specs to test

Direct Known Subclasses

Cucumber

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ RSpec

Returns a new instance of RSpec.



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

def initialize context
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

Instance Method Details

#add_ruby_options(cmd) ⇒ Object



114
115
116
117
118
119
# File 'lib/polyamory/rspec.rb', line 114

def add_ruby_options cmd
  opts = []
  opts << '-w' if context.warnings?
  opts << '%'
  cmd.env['RUBYOPT'] = opts.join(' ') if opts.size > 1
end

#all_matching_filesObject

Internal: Memoized find_files in primary dir



31
32
33
# File 'lib/polyamory/rspec.rb', line 31

def all_matching_files
  @all_matching_files ||= find_files
end

#file_pattern(dir) ⇒ Object



21
22
23
# File 'lib/polyamory/rspec.rb', line 21

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

#find_files(dir = test_dir) ⇒ Object

Internal: Finds test files matching glob pattern



26
27
28
# File 'lib/polyamory/rspec.rb', line 26

def find_files dir = test_dir
  glob file_pattern(dir)
end

#glob(pattern) ⇒ Object



110
111
112
# File 'lib/polyamory/rspec.rb', line 110

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

#handle?(path) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/polyamory/rspec.rb', line 49

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.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/polyamory/rspec.rb', line 86

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

#resolve_as_directory(name) ⇒ Object

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



62
63
64
65
66
# File 'lib/polyamory/rspec.rb', line 62

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

#resolve_as_file_pattern(name) ⇒ Object

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



79
80
81
82
# File 'lib/polyamory/rspec.rb', line 79

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”



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

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

  if file.file? and handle? file
    context.root + name
  end
end

#resolve_name(name) ⇒ Object



53
54
55
56
57
58
# File 'lib/polyamory/rspec.rb', line 53

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.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/polyamory/rspec.rb', line 37

def resolve_paths names
  if names.any?
    paths = []
    for name in names
      paths.concat Array(resolve_name name)
    end
    paths
  else
    Array(resolve_as_directory test_dir_name)
  end
end

#rspec_optionsObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/polyamory/rspec.rb', line 121

def rspec_options
  opts = []
  opts << '-b' if context.full_backtrace?
  opts << '--seed' << context.test_seed if context.test_seed
  for path in context.load_paths
    opts << "-I#{path}"
  end
  for filter in context.name_filters
    opts << '-e' << filter
  end
  for tag in context.tag_filters
    opts << '-t' << tag
  end
  opts
end

#test_command(paths) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/polyamory/rspec.rb', line 102

def test_command paths
  Command.new 'rspec' do |test_job|
    add_ruby_options test_job
    test_job.concat rspec_options
    test_job.concat paths.map {|p| p.relative }
  end
end

#test_dirObject



17
18
19
# File 'lib/polyamory/rspec.rb', line 17

def test_dir
  @test_dir ||= context.root + test_dir_name
end

#test_dir_nameObject



13
14
15
# File 'lib/polyamory/rspec.rb', line 13

def test_dir_name
  'spec'
end