Class: Barthes::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/barthes/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
13
14
15
16
# File 'lib/barthes/runner.rb', line 9

def initialize(options)
	Barthes::Config.update(options)
	load_cache
	load_libraries
	@env = {}
	load_envs(options[:env]) if options[:env]
	@reporter = Reporter.new
end

Instance Method Details

#expand_paths(paths, suffix) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/barthes/runner.rb', line 35

def expand_paths(paths, suffix)
	files = []
	if paths.empty?
		files += Dir["./**/*#{suffix}"]
	else
		paths.each do |path|
			if FileTest.directory?(path)
				files += Dir["#{path}/**/*#{suffix}"]
			elsif FileTest.file?(path)
				files << path
			end
		end
	end
	files
end

#handle_action(action, scenarios) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/barthes/runner.rb', line 111

def handle_action(action, scenarios)
	return if @failed
	name, content = action[1], action.last
	env = @env.dup
	env.update(content['env']) if content['env']
	@reporter.report(:action, name, action.last, scenarios) do
		if Barthes::Config[:dryrun] == 0 && !@failed && tagged?(env)
			content = Action.new(env).action(content)
			@failed = true if %w(failure error).include?(content['status'])
		end
		if !tagged?(env)
			content['status'] = 'skipped'
		end
		content
	end
end

#handle_scenario(scenario, scenarios) ⇒ Object



104
105
106
107
108
109
# File 'lib/barthes/runner.rb', line 104

def handle_scenario(scenario, scenarios)
	return if @failed
	@reporter.report(:scenario, scenario[1], scenario.last, scenarios) do
		scenario.last
	end
end

#in_range?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/barthes/runner.rb', line 77

def in_range?
	flag = @num >= Barthes::Config[:from]
	flag = flag && (@num <= Barthes::Config[:to]) if Barthes::Config[:to]
	flag
end

#load_cacheObject



18
19
20
21
22
# File 'lib/barthes/runner.rb', line 18

def load_cache
	if File.exists? Barthes::Config[:cache]
		Barthes::Cache.update JSON.parse File.read Barthes::Config[:cache]
	end
end

#load_envs(env_paths) ⇒ Object



29
30
31
32
33
# File 'lib/barthes/runner.rb', line 29

def load_envs(env_paths)
	env_paths.each do |path|
		@env.update JSON.parse File.read(path)
	end
end

#load_librariesObject



24
25
26
27
# File 'lib/barthes/runner.rb', line 24

def load_libraries
	path = Dir.pwd + '/.barthes'
	load path if File.exists?(path)
end

#run(paths) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/barthes/runner.rb', line 51

def run(paths)
	begin
		files = expand_paths(paths, '_spec.json')
		results = []
		@reporter.report(:run, results) do
			@num = 1
			files.each do |file|
				json = JSON.parse File.read(file)
				@reporter.report(:feature, json[1]) do
					walk_json(json.last, [file])
					results << json
					json
				end
			end
			results
		end
	rescue StandardError => e
		raise e
	ensure
		if !Barthes::Cache.empty?
			File.write Barthes::Config[:cache], JSON.pretty_generate(Barthes::Cache) + "\n"
		end
	end
	exit @failed ? 1 : 0
end

#tagged?(env) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
# File 'lib/barthes/runner.rb', line 128

def tagged?(env)
	return true if Barthes::Config[:tags].nil?
	tags = env['tags'] || []
	flag = (Barthes::Config[:tags] & tags).size > 0
end

#walk_json(json, scenarios) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/barthes/runner.rb', line 83

def walk_json(json, scenarios)
	if json.class == Array
		case json.first
		when 'scenario'
			handle_scenario(json, scenarios)
			scenarios.push(json.first)
			walk_json(json.last, scenarios)
			scenarios.pop
		when 'action'
			json.last['status'] = 'skipped' if Barthes::Config[:dryrun] > 0
			json.last['number'] = @num
			handle_action(json, scenarios) if in_range?
			@num += 1
		else
			json.each do |element|
				walk_json(element, scenarios)
			end
		end
	end
end