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.0.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)

  • :reset_schemas tells earth to recreate tables for each model (default: false)

  • :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



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

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.



33
34
35
# File 'lib/sniff.rb', line 33

def fixtures_path
  @fixtures_path
end

#loggerObject

Returns the value of attribute logger.



33
34
35
# File 'lib/sniff.rb', line 33

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



33
34
35
# File 'lib/sniff.rb', line 33

def options
  @options
end

#projectObject

Returns the value of attribute project.



33
34
35
# File 'lib/sniff.rb', line 33

def project
  @project
end

#rootObject

Returns the value of attribute root.



33
34
35
# File 'lib/sniff.rb', line 33

def root
  @root
end

#test_support_pathObject

Returns the value of attribute test_support_path.



33
34
35
# File 'lib/sniff.rb', line 33

def test_support_path
  @test_support_path
end

Class Method Details

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



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

def Sniff.init(local_root, options = {})
  sniff = new local_root, options
  sniff.connect
  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



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

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

#emitter_classObject



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

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



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

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



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

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



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

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

#migrate!Object



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

def migrate!
  emitter_class.auto_upgrade! if emitter_class
end

#seed!Object



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

def seed!
  Fixture.load_fixtures fixtures_path
end