Module: Bryton::Lite::Runner

Defined in:
lib/bryton/lite.rb

Overview

Runs tests in the current directory tree.

Defined Under Namespace

Classes: Capture

Class Method Summary collapse

Class Method Details

.dir(path, &block) ⇒ Object

Runs the tests in a directory. Don’t call this method directly.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bryton/lite.rb', line 54

def self.dir(path, &block)
  entries = Dir.entries('./')
  entries = entries.reject {|entry| entry.match(/\A\.+\z/mu)}
  
  # note file and path
  Bryton::Lite::Tests['file'] = {}
  Bryton::Lite::Tests['file']['path'] = path
  Bryton::Lite::Tests['file']['dir'] = true
  
  # loop through entries
  entries.each do |entry|
    entry_path = "#{path}/#{entry}"
    
    # if directory, recurse into it
    if File.directory?(entry)
      Dir.chdir(entry) do
        Bryton::Lite::Tests.nest do
          self.dir(entry_path, &block)
        end
      end
    
    # elsif the file is executable then execute it
    elsif File.executable?(entry)
      results = execute(entry, entry_path)
      Bryton::Lite::Tests.add_to_nest results
      
      if block_given?
        yield entry_path, results
      end
    end
  end
end

.execute(name, path) ⇒ Object

Executes a file, parses the results, and returns the results. Don’t call this method directly.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/bryton/lite.rb', line 98

def self.execute(name, path)
  # init
  rv = {}
  rv['file'] = {}
  rv['file']['path'] = path
  
  # execute script
  cap = Bryton::Lite::Runner::Capture.new("./#{name}")
  
  # if script crashed
  if not cap.success?
    rv['success'] = false
    rv['errors'] ||= []
    
    # build error
    error = {'id'=>'execution-failed'}
    error['id'] = 'execution-failed'
    error['stderr'] = cap.stderr
    
    # add error and return
    rv['errors'].push(error)
    return rv
  
  # if we get a non-blank line, attempt to parse it as JSON
  elsif non_blank = self.last_non_blank(cap.stdout)
    begin
      # retrieve and parse results
      results = JSON.parse( non_blank )
      rv = rv.merge(results)
      
      # ensure that if there are errors, success is false
      if rv['errors'] and rv['errors'].any?
        rv['success'] = false
      end
      
      # return
      return rv
    rescue
      rv['success'] = false
      rv['errors'] = []
      rv['errors'].push({'id'=>'invalid-json'})
      return rv
    end
  
  # did not get non-blank line
  else
    rv['success'] = false
    rv['errors'] = []
    rv['errors'].push({'id'=>'no-results'})
    return rv
  end
end

.last_non_blank(str) ⇒ Object

Finds the last non-blank line in STDOUT from the file execution. Don’t call this method directly.



162
163
164
165
166
167
168
169
170
171
# File 'lib/bryton/lite.rb', line 162

def self.last_non_blank(str)
  str.lines.reverse.each do |line|
    if line.match(/\S/mu)
      return line
    end
  end
  
  # didn't find non-blank
  return nil
end

.run(&block) ⇒ Object

Run the tests. If you pass in a block, each test result is yielded with the path to the file and the results hash.



37
38
39
40
41
42
# File 'lib/bryton/lite.rb', line 37

def self.run(&block)
  Bryton::Lite::Tests.reset
  dir('.', &block)
  Bryton::Lite::Tests.try_succeed
  Bryton::Lite::Tests.reorder_results
end

.success?Boolean

Returns the success or failure of the test run.

Returns:

  • (Boolean)


183
184
185
# File 'lib/bryton/lite.rb', line 183

def self.success?
  return Bryton::Lite::Tests.success?
end