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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/barthes/runner.rb', line 106

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



99
100
101
102
103
104
# File 'lib/barthes/runner.rb', line 99

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)


72
73
74
75
76
# File 'lib/barthes/runner.rb', line 72

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
# File 'lib/barthes/runner.rb', line 51

def run(paths)
  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
  if !Barthes::Cache.empty?
    File.write Barthes::Config[:cache], JSON.pretty_generate(Barthes::Cache) + "\n"
  end
  exit @failed ? 1 : 0
end

#tagged?(env) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
# File 'lib/barthes/runner.rb', line 123

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/barthes/runner.rb', line 78

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