Class: Sus::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/sus/config.rb

Overview

Represents the configuration for running tests.

Constant Summary collapse

PATH =

The default path to the configuration file.

"config/sus.rb"
DEFAULT_TEST_PATTERN =

The default pattern for finding test files.

"test/**/*.rb"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, paths, verbose: false) ⇒ Config

Initialize a new Config instance.



51
52
53
54
55
56
57
58
59
# File 'lib/sus/config.rb', line 51

def initialize(root, paths, verbose: false)
	@root = root
	@paths = paths
	@verbose = verbose
	
	@clock = Clock.new
	
	self.add_default_load_paths
end

Instance Attribute Details

#Optional paths to specific test files.(pathstospecifictestfiles.) ⇒ Object (readonly)



81
# File 'lib/sus/config.rb', line 81

attr :paths

#pathsObject (readonly)

Returns the value of attribute paths.



81
82
83
# File 'lib/sus/config.rb', line 81

def paths
  @paths
end

#rootObject (readonly)

Returns the value of attribute root.



78
79
80
# File 'lib/sus/config.rb', line 78

def root
  @root
end

#The root directory for the project.(rootdirectory) ⇒ Object (readonly)



78
# File 'lib/sus/config.rb', line 78

attr :root

Class Method Details

.load(root: Dir.pwd, arguments: ARGV) ⇒ Object

Load configuration from the given root directory.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sus/config.rb', line 31

def self.load(root: Dir.pwd, arguments: ARGV)
	derived = Class.new(self)
	
	if path = self.path(root)
		config = Module.new
		config.module_eval(::File.read(path), path)
		derived.prepend(config)
	end
	
	options = {
		verbose: !!arguments.delete("--verbose")
	}
	
	return derived.new(root, arguments, **options)
end

.path(root) ⇒ Object

Find the configuration file path for the given root directory.



19
20
21
22
23
24
25
# File 'lib/sus/config.rb', line 19

def self.path(root)
	path = ::File.join(root, PATH)
	
	if ::File.exist?(path)
		return path
	end
end

Instance Method Details

#add_default_load_pathsObject

Add default load paths (lib and fixtures).



72
73
74
75
# File 'lib/sus/config.rb', line 72

def add_default_load_paths
	add_load_path("lib")
	add_load_path("fixtures")
end

#add_load_path(path) ⇒ Object

Add a directory to the load path.



63
64
65
66
67
68
69
# File 'lib/sus/config.rb', line 63

def add_load_path(path)
	path = ::File.expand_path(path, @root)
	
	if ::File.directory?(path)
		$LOAD_PATH.unshift(path)
	end
end

#after_tests(assertions, output: self.output) ⇒ Object

Called after tests are run.



155
156
157
158
159
# File 'lib/sus/config.rb', line 155

def after_tests(assertions, output: self.output)
	@clock.stop!
	
	self.print_summary(output, assertions)
end

#before_tests(assertions, output: self.output) ⇒ Object

Called before tests are run.



145
146
147
148
149
150
# File 'lib/sus/config.rb', line 145

def before_tests(assertions, output: self.output)
	@clock.reset!
	@clock.start!
	
	prepare_warnings!
end

#load_registry(paths = @paths) ⇒ Object

Load the test registry, optionally filtering by paths.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sus/config.rb', line 115

def load_registry(paths = @paths)
	registry = make_registry
	
	if paths&.any?
		registry = Sus::Filter.new(registry)
		paths.each do |path|
			registry.load(path)
		end
	else
		test_paths.each do |path|
			registry.load(path)
		end
	end
	
	return registry
end

#make_registryObject

Create a new registry instance.



108
109
110
# File 'lib/sus/config.rb', line 108

def make_registry
	Sus::Registry.new(root: @root)
end

#outputObject



94
95
96
# File 'lib/sus/config.rb', line 94

def output
	@output ||= Sus::Output.default
end

#partial?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/sus/config.rb', line 89

def partial?
	@paths.any?
end

#prepare_warnings!Object

Prepare Ruby warnings for deprecated features.



138
139
140
# File 'lib/sus/config.rb', line 138

def prepare_warnings!
	Warning[:deprecated] = true
end

Print feedback about the test suite.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/sus/config.rb', line 203

def print_test_feedback(output, assertions = nil,
	duration: @clock.duration,
	count: assertions.count,
	total: assertions.total
)
	rate = count / duration
	
	if total < 10 or count < 10
		output.puts "😭 You should write more tests and assertions!"
		
		# Statistics will be less meaningful with such a small amount of data, so give up:
		return
	end
	
	# Check whether there is at least, on average, one assertion (or more) per test:
	assertions_per_test = count / total
	if assertions_per_test < 1.0
		output.puts "😩 Your tests don't have enough assertions (#{assertions_per_test.round(1)} < 1.0)!"
	end
	
	# Give some feedback about the number of tests:
	if total < 20
		output.puts "🥲 You should write more tests (#{total}/20)!"
	elsif total < 50
		output.puts "🙂 Your test suite is starting to shape up, keep on at it (#{total}/50)!"
	elsif total < 100
		output.puts "😀 Your test suite is maturing, keep on at it (#{total}/100)!"
	else
		output.puts "🤩 Your test suite is amazing!"
	end
	
	# Give some feedback about the performance of the tests:
	if rate < 10.0
		output.puts "💔 Ouch! Your test suite performance is painful (#{rate.round(1)} < 10)!"
	elsif rate < 100.0
		output.puts "💩 Oops! Your test suite performance could be better (#{rate.round(1)} < 100)!"
	elsif rate < 1_000.0
		output.puts "💪 Good job! Your test suite has good performance (#{rate.round(1)} < 1000)!"
	elsif rate < 10_000.0
		output.puts "🎉 Great job! Your test suite has excellent performance (#{rate.round(1)} < 10000)!"
	else
		output.puts "🔥 Wow! Your test suite has outstanding performance (#{rate.round(1)} >= 10000.0)!"
	end
end

#registryObject



133
134
135
# File 'lib/sus/config.rb', line 133

def registry
	@registry ||= self.load_registry
end

#test_pathsObject



102
103
104
# File 'lib/sus/config.rb', line 102

def test_paths
	return Dir.glob(DEFAULT_TEST_PATTERN, base: @root)
end

#verbose?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/sus/config.rb', line 84

def verbose?
	@verbose
end