summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpseudomuto <david.muto@gmail.com>2013-12-19 12:08:07 -0500
committerpseudomuto <david.muto@gmail.com>2013-12-19 12:20:38 -0500
commite975ce77408f5995213cc1b85f61e806152ba3a2 (patch)
treea4a33d6c3b0cbb1bd6fbde63e18c11344f2d5562
parentfbc9497dbb8ece970f21bc67164557bba8db2db7 (diff)
light refactoring for better test coverage
-rwxr-xr-xbin/dashing1
-rw-r--r--lib/dashing/app.rb110
-rw-r--r--lib/dashing/cli.rb26
-rw-r--r--test/app_test.rb83
4 files changed, 118 insertions, 102 deletions
diff --git a/bin/dashing b/bin/dashing
index 051acba..97a7af5 100755
--- a/bin/dashing
+++ b/bin/dashing
@@ -4,4 +4,5 @@ bin_file = Pathname.new(__FILE__).realpath
$:.unshift File.expand_path("../../lib", bin_file)
require 'dashing'
+Dashing::CLI.source_root(File.expand_path('../../templates', bin_file))
Dashing::CLI.start(ARGV)
diff --git a/lib/dashing/app.rb b/lib/dashing/app.rb
index 7c4f25a..5780b92 100644
--- a/lib/dashing/app.rb
+++ b/lib/dashing/app.rb
@@ -9,40 +9,59 @@ require 'yaml'
SCHEDULER = Rufus::Scheduler.new
-set :root, Dir.pwd
-
-set :sprockets, Sprockets::Environment.new(settings.root)
-set :assets_prefix, '/assets'
-set :digest_assets, false
-['assets/javascripts', 'assets/stylesheets', 'assets/fonts', 'assets/images', 'widgets', File.expand_path('../../javascripts', __FILE__)]. each do |path|
- settings.sprockets.append_path path
+def development?
+ ENV['RACK_ENV'] == 'development'
end
-set server: 'thin', connections: [], history_file: 'history.yml'
+def production?
+ ENV['RACK_ENV'] == 'production'
+end
-# Persist history in tmp file at exit
-at_exit do
- File.open(settings.history_file, 'w') do |f|
- f.puts settings.history.to_yaml
+helpers Sinatra::ContentFor
+helpers do
+ def protected!
+ # override with auth logic
end
end
+set :root, Dir.pwd
+set :sprockets, Sprockets::Environment.new(settings.root)
+set :assets_prefix, '/assets'
+set :digest_assets, false
+set server: 'thin', connections: [], history_file: 'history.yml'
+set :public_folder, File.join(settings.root, 'public')
+set :views, File.join(settings.root, 'dashboards')
+set :default_dashboard, nil
+set :auth_token, nil
+
if File.exists?(settings.history_file)
set history: YAML.load_file(settings.history_file)
else
set history: {}
end
-set :public_folder, File.join(settings.root, 'public')
-set :views, File.join(settings.root, 'dashboards')
-set :default_dashboard, nil
-set :auth_token, nil
+%w(javascripts stylesheets fonts images).each do |path|
+ settings.sprockets.append_path("assets/#{path}")
+end
-helpers Sinatra::ContentFor
-helpers do
- def protected!
- # override with auth logic
- end
+['widgets', File.expand_path('../../javascripts', __FILE__)]. each do |path|
+ settings.sprockets.append_path(path)
+end
+
+not_found do
+ send_file File.join(settings.public_folder, '404.html')
+end
+
+at_exit do
+ File.write(settings.history_file, settings.history.to_yaml)
+end
+
+get '/' do
+ protected!
+ dashboard = settings.default_dashboard || first_dashboard
+ raise Exception.new('There are no dashboards available') if not dashboard
+
+ redirect "/" + dashboard
end
get '/events', provides: 'text/event-stream' do
@@ -55,15 +74,6 @@ get '/events', provides: 'text/event-stream' do
end
end
-get '/' do
- protected!
- begin
- redirect "/" + (settings.default_dashboard || first_dashboard).to_s
- rescue NoMethodError => e
- raise Exception.new("There are no dashboards in your dashboard directory.")
- end
-end
-
get '/:dashboard' do
protected!
tilt_html_engines.each do |suffix, _|
@@ -74,14 +84,6 @@ get '/:dashboard' do
halt 404
end
-get '/views/:widget?.html' do
- protected!
- tilt_html_engines.each do |suffix, engines|
- file = File.join(settings.root, "widgets", params[:widget], "#{params[:widget]}.#{suffix}")
- return engines.first.new(file).render if File.exist? file
- end
-end
-
post '/dashboards/:id' do
request.body.rewind
body = JSON.parse(request.body.read)
@@ -109,16 +111,12 @@ post '/widgets/:id' do
end
end
-not_found do
- send_file File.join(settings.public_folder, '404.html')
-end
-
-def development?
- ENV['RACK_ENV'] == 'development'
-end
-
-def production?
- ENV['RACK_ENV'] == 'production'
+get '/views/:widget?.html' do
+ protected!
+ tilt_html_engines.each do |suffix, engines|
+ file = File.join(settings.root, "widgets", params[:widget], "#{params[:widget]}.#{suffix}")
+ return engines.first.new(file).render if File.exist? file
+ end
end
def send_event(id, body, target=nil)
@@ -154,14 +152,16 @@ def tilt_html_engines
end
end
-settings_file = File.join(settings.root, 'config/settings.rb')
-if (File.exists?(settings_file))
- require settings_file
+def require_glob(relative_glob)
+ Dir[File.join(settings.root, relative_glob)].each do |file|
+ require file
+ end
end
-Dir[File.join(settings.root, 'lib', '**', '*.rb')].each {|file| require file }
-{}.to_json # Forces your json codec to initialize (in the event that it is lazily loaded). Does this before job threads start.
+settings_file = File.join(settings.root, 'config/settings.rb')
+require settings_file if File.exists?(settings_file)
+{}.to_json # Forces your json codec to initialize (in the event that it is lazily loaded). Does this before job threads start.
job_path = ENV["JOB_PATH"] || 'jobs'
-files = Dir[File.join(settings.root, job_path, '**', '/*.rb')]
-files.each { |job| require(job) }
+require_glob(File.join('lib', '**', '*.rb'))
+require_glob(File.join(job_path, '**', '*.rb'))
diff --git a/lib/dashing/cli.rb b/lib/dashing/cli.rb
index 001a92e..27d8f6b 100644
--- a/lib/dashing/cli.rb
+++ b/lib/dashing/cli.rb
@@ -10,7 +10,7 @@ module Dashing
class << self
attr_accessor :auth_token
- def CLI.hyphenate(str)
+ def hyphenate(str)
return str.downcase if str =~ /^[A-Z-]+$/
str.gsub('_', '-').gsub(/\B[A-Z]/, '-\&').squeeze('-').downcase
end
@@ -43,16 +43,7 @@ module Dashing
gist = Downloader.get_gist(gist_id)
public_url = "https://gist.github.com/#{gist_id}"
- gist['files'].each do |file, details|
- if file =~ /\.(html|coffee|scss)\z/
- widget_name = File.basename(file, '.*')
- new_path = File.join(Dir.pwd, 'widgets', widget_name, file)
- create_file(new_path, details['content'])
- elsif file.end_with?('.rb')
- new_path = File.join(Dir.pwd, 'jobs', file)
- create_file(new_path, details['content'])
- end
- end
+ install_widget_from_gist(gist)
print set_color("Don't forget to edit the ", :yellow)
print set_color("Gemfile ", :yellow, :bold)
@@ -98,6 +89,19 @@ module Dashing
system(command)
end
+ def install_widget_from_gist(gist)
+ gist['files'].each do |file, details|
+ if file =~ /\.(html|coffee|scss)\z/
+ widget_name = File.basename(file, '.*')
+ new_path = File.join(Dir.pwd, 'widgets', widget_name, file)
+ create_file(new_path, details['content'])
+ elsif file.end_with?('.rb')
+ new_path = File.join(Dir.pwd, 'jobs', file)
+ create_file(new_path, details['content'])
+ end
+ end
+ end
+
def require_file(file)
require file
end
diff --git a/test/app_test.rb b/test/app_test.rb
index dbb7cb5..4101e91 100644
--- a/test/app_test.rb
+++ b/test/app_test.rb
@@ -4,10 +4,45 @@ require 'haml'
class AppTest < Dashing::Test
def setup
@connection = []
- Sinatra::Application.settings.history_file = File.join(Dir.tmpdir, 'history.yml')
- Sinatra::Application.settings.connections = [@connection]
- Sinatra::Application.settings.auth_token = nil
- Sinatra::Application.settings.default_dashboard = nil
+ app.settings.connections = [@connection]
+ app.settings.auth_token = nil
+ app.settings.default_dashboard = nil
+ app.settings.history_file = File.join(Dir.tmpdir, 'history.yml')
+ end
+
+ def test_redirect_to_first_dashboard
+ with_generated_project do
+ get '/'
+ assert_equal 302, last_response.status
+ assert_equal 'http://example.org/sample', last_response.location
+ end
+ end
+
+ def test_redirect_to_first_dashboard_without_erb
+ with_generated_project do |dir|
+ FileUtils.touch(File.join(dir, "dashboards/htmltest.html"))
+ get '/'
+ assert_equal 302, last_response.status
+ assert_equal 'http://example.org/htmltest', last_response.location
+ end
+ end
+
+ def test_redirect_to_default_dashboard
+ with_generated_project do
+ app.settings.default_dashboard = 'test1'
+ get '/'
+ assert_equal 302, last_response.status
+ assert_equal 'http://example.org/test1', last_response.location
+ end
+ end
+
+ def test_errors_out_when_no_dashboards_available
+ with_generated_project do
+ app.settings.views = File.join(app.settings.root, 'lib')
+
+ get '/'
+ assert_equal 500, last_response.status
+ end
end
def test_post_widgets_without_auth_token
@@ -22,13 +57,13 @@ class AppTest < Dashing::Test
end
def test_post_widgets_with_invalid_auth_token
- Sinatra::Application.settings.auth_token = 'sekrit'
+ app.settings.auth_token = 'sekrit'
post '/widgets/some_widget', JSON.generate({value: 9})
assert_equal 401, last_response.status
end
def test_post_widgets_with_valid_auth_token
- Sinatra::Application.settings.auth_token = 'sekrit'
+ app.settings.auth_token = 'sekrit'
post '/widgets/some_widget', JSON.generate({value: 9, auth_token: 'sekrit'})
assert_equal 204, last_response.status
end
@@ -52,32 +87,6 @@ class AppTest < Dashing::Test
assert_equal 'reload', parse_data(@connection[0])['event']
end
- def test_redirect_to_default_dashboard
- with_generated_project do
- Sinatra::Application.settings.default_dashboard = 'test1'
- get '/'
- assert_equal 302, last_response.status
- assert_equal 'http://example.org/test1', last_response.location
- end
- end
-
- def test_redirect_to_first_dashboard
- with_generated_project do
- get '/'
- assert_equal 302, last_response.status
- assert_equal 'http://example.org/sample', last_response.location
- end
- end
-
- def test_redirect_to_first_dashboard_without_erb
- with_generated_project do |dir|
- FileUtils.touch(File.join(dir, "dashboards/htmltest.html"))
- get '/'
- assert_equal 302, last_response.status
- assert_equal 'http://example.org/htmltest', last_response.location
- end
- end
-
def test_get_dashboard
with_generated_project do
get '/sampletv'
@@ -129,14 +138,16 @@ class AppTest < Dashing::Test
end
def with_generated_project
+ source_path = File.expand_path('../../templates', __FILE__)
+
temp do |dir|
cli = Dashing::CLI.new
- cli.stubs(:source_paths).returns([File.expand_path('../../templates', __FILE__)])
+ cli.stubs(:source_paths).returns([source_path])
silent { cli.new 'new_project' }
- Sinatra::Application.settings.views = File.join(dir, 'new_project/dashboards')
- Sinatra::Application.settings.root = File.join(dir, 'new_project')
- yield Sinatra::Application.settings.root
+ app.settings.views = File.join(dir, 'new_project/dashboards')
+ app.settings.root = File.join(dir, 'new_project')
+ yield app.settings.root
end
end