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

#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