Class: Sniff

Inherits:
Object
  • Object
show all
Defined in:
lib/sniff.rb,
lib/sniff/fixture.rb,
lib/sniff/version.rb,
lib/sniff/rake_tasks.rb

Defined Under Namespace

Modules: Fixture Classes: RakeTasks

Constant Summary collapse

VERSION =
"1.1.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_root, options = {}) ⇒ Sniff

Prepares the environment for running tests against Earth data and emitter gems.

local_root: Root directory of the emitter gem to be tested (path to the repo)

options:

  • :logger is a Logger log device used by Sniff and ActiveRecord (default: nil)

    logger: nil = no log, string = file path, STDOUT for terminal
    
  • :fixtures_path is the path to your gem’s fixtures (default: local_root/lib/db/fixtures)

  • :cucumber tells Sniff to load cucumber test support files provided by the emitter in <emitter_root>/test_support/cucumber (default: false)

  • :project is the current project (e.g. ‘flight’). Default is guessed from CWD



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sniff.rb', line 49

def initialize(local_root, options = {})
  self.root = local_root
  self.project = options[:project] || File.basename(File.expand_path(local_root))
  self.options = options.symbolize_keys
  self.test_support_path = File.join(root, 'features', 'support')
  self.fixtures_path = options[:fixtures_path]

  ENV['DATABASE_URL'] ||= "mysql2://root:password@localhost/test_#{project}"

  load_supporting_libs

  logger = self.options[:logger] || ENV['LOGGER']
  Sniff.logger ||= Logger.new logger
  DataMiner.logger = Sniff.logger
  DataMiner.unit_converter = :conversions

  init_cucumber if self.options[:cucumber]
end

Instance Attribute Details

#fixtures_pathObject

Returns the value of attribute fixtures_path.



35
36
37
# File 'lib/sniff.rb', line 35

def fixtures_path
  @fixtures_path
end

#loggerObject

Returns the value of attribute logger.



35
36
37
# File 'lib/sniff.rb', line 35

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



35
36
37
# File 'lib/sniff.rb', line 35

def options
  @options
end

#projectObject

Returns the value of attribute project.



35
36
37
# File 'lib/sniff.rb', line 35

def project
  @project
end

#rootObject

Returns the value of attribute root.



35
36
37
# File 'lib/sniff.rb', line 35

def root
  @root
end

#test_support_pathObject

Returns the value of attribute test_support_path.



35
36
37
# File 'lib/sniff.rb', line 35

def test_support_path
  @test_support_path
end

Class Method Details

.init(local_root, options = {}) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/sniff.rb', line 27

def Sniff.init(local_root, options = {})
  sniff = new local_root, options
  sniff.connect
  sniff.migrate!
  sniff.seed!
  sniff
end

.loggerObject



20
21
22
# File 'lib/sniff.rb', line 20

def Sniff.logger
  @logger ||= Logger.new nil
end

.logger=(val) ⇒ Object



23
24
25
# File 'lib/sniff.rb', line 23

def Sniff.logger=(val)
  @logger = val
end

.path(*rest) ⇒ Object

Get a path relative to sniff’s root



16
17
18
# File 'lib/sniff.rb', line 16

def Sniff.path(*rest)
  File.join(root, *rest)
end

.rootObject

Sniff’s root directory (the gem’s location on the filesystem)



11
12
13
# File 'lib/sniff.rb', line 11

def Sniff.root 
  File.join(File.dirname(__FILE__), '..')
end

Instance Method Details

#connectObject

Connect to the database and set up an ActiveRecord logger



84
85
86
87
# File 'lib/sniff.rb', line 84

def connect
  ActiveRecord::Base.logger = Sniff.logger
  Earth.connect
end

#emitter_classObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/sniff.rb', line 89

def emitter_class
  return @emitter_class unless @emitter_class.nil?
  record_class_path = Dir.glob(File.join(test_support_path, '*_record.rb')).first
  if record_class_path
    require record_class_path
    record_class = File.read(record_class_path)
    klass = record_class.scan(/class ([^\s]*Record)/).flatten.first
    @emitter_class = klass.constantize
  end
end

#init_cucumberObject



68
69
70
71
72
73
# File 'lib/sniff.rb', line 68

def init_cucumber
  require 'cucumber'
  require 'cucumber/formatter/unicode' # Remove this line if you don't want Cucumber Unicode support
  cukes = Dir.glob File.join(File.dirname(__FILE__), 'test_support', 'cucumber', '**', '*.rb')
  cukes.each { |support_file| require support_file }
end

#load_supporting_libsObject



108
109
110
111
112
113
114
115
116
# File 'lib/sniff.rb', line 108

def load_supporting_libs
  require project

  $:.unshift File.join(root, 'lib')
  Dir[File.join(root, 'lib', 'test_support', '*.rb')].each do |lib|
    log "Loading #{lib}"
    require lib
  end
end

#log(str) ⇒ Object



75
76
77
# File 'lib/sniff.rb', line 75

def log(str)
  Sniff.logger.info str
end

#migrate!Object



100
101
102
# File 'lib/sniff.rb', line 100

def migrate!
  emitter_class.auto_upgrade! if emitter_class
end

#seed!Object



104
105
106
# File 'lib/sniff.rb', line 104

def seed!
  Fixture.load_fixtures fixtures_path
end