Class: RailsAudit::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-audit/runner.rb

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



5
6
7
8
9
10
# File 'lib/rails-audit/runner.rb', line 5

def initialize
  @config = { 'Concurrency' => true, 'Rails' => true }
  if File.exist? 'config/audit.yml'
    @config = @config.merge YAML.load_file('config/audit.yml')
  end
end

Instance Method Details

#get_config(name) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/rails-audit/runner.rb', line 12

def get_config(name)
  defaults = { 'Parameters' => '', 'Enabled' => true }
  config = defaults.merge(@config[name] || {})
  {
    rails: @config['Rails'],
    params: config['Parameters'],
    enabled: config['Enabled']
  }
end

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails-audit/runner.rb', line 22

def run
  concurrency = @config['Concurrency']

  failures = concurrency && run_threads || run_sequence

  if failures.any?
    puts 'Failed tests:'
    failures.each { |f| puts f }
  end

  failures.none?
end

#run_sequenceObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/rails-audit/runner.rb', line 35

def run_sequence
  failures = []

  Audits::ALL.each do |audit|
    success = audit.run get_config(audit.get_name)
    failures << audit.get_name unless success
  end

  failures
end

#run_threadsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rails-audit/runner.rb', line 46

def run_threads
  failures = []
  mutex = Mutex.new

  threads = Audits::ALL.map do |audit|
    Thread.new do
      success = audit.run get_config(audit.get_name)
      mutex.synchronize { failures << audit.get_name unless success }
    end
  end

  threads.each { |t| t.join }

  failures
end