Module: Seaweed

Defined in:
lib/seaweed.rb,
lib/seaweed/runner.rb,
lib/seaweed/server.rb,
lib/seaweed/version.rb

Defined Under Namespace

Classes: Runner, Server

Constant Summary collapse

ROOT =
File.expand_path File.join(File.dirname(__FILE__), '..')
PROJECT_ROOT =
File.expand_path "."
CONFIG_PATHS =
[
  File.join(PROJECT_ROOT, 'seaweed.yml'),
  File.join(PROJECT_ROOT, 'config', 'seaweed.yml')
]
VERSION =
"0.1.4"

Class Method Summary collapse

Class Method Details

.all_pathsObject



57
58
59
# File 'lib/seaweed.rb', line 57

def self.all_paths
  libs + specs
end

.close_browserObject



109
110
111
112
# File 'lib/seaweed.rb', line 109

def self.close_browser
  @browser.close
  @browser = nil
end

.libsObject



49
50
51
# File 'lib/seaweed.rb', line 49

def self.libs
  @configuration['libs']
end

.load_configurationObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/seaweed.rb', line 22

def self.load_configuration
  # Set configuration defaults
  @configuration['port']    = 4567
  @configuration['libs']    = ['lib']
  @configuration['specs']   = ['spec']
  
  # Load custom configuration file
  CONFIG_PATHS.each do |path|
    if File.exists? path
      @configuration.merge! YAML.load(File.read(path))
      puts "Loaded configuration from “#{path}"
    end
  end
end

.portObject



37
38
39
# File 'lib/seaweed.rb', line 37

def self.port
  @configuration['port']
end

.port=(value) ⇒ Object



41
42
43
# File 'lib/seaweed.rb', line 41

def self.port= value
  @configuration['port'] = value
end

.root_urlObject



45
46
47
# File 'lib/seaweed.rb', line 45

def self.root_url
  "http://localhost:#{port}/"
end

.run_suiteObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/seaweed.rb', line 97

def self.run_suite
  if @browser
    @browser.navigate.refresh
  else
    @browser = Selenium::WebDriver.for :firefox, profile: Selenium::WebDriver::Firefox::Profile.new
    @browser.get "#{root_url}#terminal"
  end
  result = @browser[css: '.results'].text
  puts result
  !!result.match('passed, 0 failed')
end

.spawn_serverObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/seaweed.rb', line 84

def self.spawn_server
  # Start server in its own thread
  server = Thread.new &method(:start_server)
  
  # Keep trying to connect to server until we succeed
  begin
    page = Net::HTTP.get URI.parse(root_url)
  rescue Errno::ECONNREFUSED
    sleep 1
    retry
  end
end

.specsObject



53
54
55
# File 'lib/seaweed.rb', line 53

def self.specs
  @configuration['specs']
end

.sprockets_environmentObject

Prepares a Sprockets::Environment object to serve coffeescript assets



62
63
64
65
66
67
68
69
# File 'lib/seaweed.rb', line 62

def self.sprockets_environment
  @environment ||= Sprockets::Environment.new.tap do |environment|
    environment.append_path File.join(Seaweed::ROOT, 'lib')
    all_paths.each do |path|
      environment.append_path path
    end
  end
end

.start_serverObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/seaweed.rb', line 71

def self.start_server
  app = Rack::Builder.app do
    map '/assets' do
      run Seaweed.sprockets_environment
    end
    
    map '/' do
      run Seaweed::Server
    end
  end
  Rack::Handler.default.run app, :Port => port
end

.watch_for_changesObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/seaweed.rb', line 114

def self.watch_for_changes
  require 'watchr'
  
  # Build a regexp to match .coffee files in any project paths
  path_matcher = Regexp.new('^(' + all_paths.map{ |s| Regexp.escape s}.join('|') + ')\/.*\.coffee$')
  
  script = Watchr::Script.new
  script.watch(path_matcher) { run_suite }
  controller = Watchr::Controller.new(script, Watchr.handler.new)
  controller.run
end