Skip to content
This repository was archived by the owner on Mar 7, 2018. It is now read-only.

Commit e975ce7

Browse files
committed
light refactoring for better test coverage
1 parent fbc9497 commit e975ce7

File tree

4 files changed

+118
-102
lines changed

4 files changed

+118
-102
lines changed

bin/dashing

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ bin_file = Pathname.new(__FILE__).realpath
44
$:.unshift File.expand_path("../../lib", bin_file)
55

66
require 'dashing'
7+
Dashing::CLI.source_root(File.expand_path('../../templates', bin_file))
78
Dashing::CLI.start(ARGV)

lib/dashing/app.rb

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,59 @@
99

1010
SCHEDULER = Rufus::Scheduler.new
1111

12-
set :root, Dir.pwd
13-
14-
set :sprockets, Sprockets::Environment.new(settings.root)
15-
set :assets_prefix, '/assets'
16-
set :digest_assets, false
17-
['assets/javascripts', 'assets/stylesheets', 'assets/fonts', 'assets/images', 'widgets', File.expand_path('../../javascripts', __FILE__)]. each do |path|
18-
settings.sprockets.append_path path
12+
def development?
13+
ENV['RACK_ENV'] == 'development'
1914
end
2015

21-
set server: 'thin', connections: [], history_file: 'history.yml'
16+
def production?
17+
ENV['RACK_ENV'] == 'production'
18+
end
2219

23-
# Persist history in tmp file at exit
24-
at_exit do
25-
File.open(settings.history_file, 'w') do |f|
26-
f.puts settings.history.to_yaml
20+
helpers Sinatra::ContentFor
21+
helpers do
22+
def protected!
23+
# override with auth logic
2724
end
2825
end
2926

27+
set :root, Dir.pwd
28+
set :sprockets, Sprockets::Environment.new(settings.root)
29+
set :assets_prefix, '/assets'
30+
set :digest_assets, false
31+
set server: 'thin', connections: [], history_file: 'history.yml'
32+
set :public_folder, File.join(settings.root, 'public')
33+
set :views, File.join(settings.root, 'dashboards')
34+
set :default_dashboard, nil
35+
set :auth_token, nil
36+
3037
if File.exists?(settings.history_file)
3138
set history: YAML.load_file(settings.history_file)
3239
else
3340
set history: {}
3441
end
3542

36-
set :public_folder, File.join(settings.root, 'public')
37-
set :views, File.join(settings.root, 'dashboards')
38-
set :default_dashboard, nil
39-
set :auth_token, nil
43+
%w(javascripts stylesheets fonts images).each do |path|
44+
settings.sprockets.append_path("assets/#{path}")
45+
end
4046

41-
helpers Sinatra::ContentFor
42-
helpers do
43-
def protected!
44-
# override with auth logic
45-
end
47+
['widgets', File.expand_path('../../javascripts', __FILE__)]. each do |path|
48+
settings.sprockets.append_path(path)
49+
end
50+
51+
not_found do
52+
send_file File.join(settings.public_folder, '404.html')
53+
end
54+
55+
at_exit do
56+
File.write(settings.history_file, settings.history.to_yaml)
57+
end
58+
59+
get '/' do
60+
protected!
61+
dashboard = settings.default_dashboard || first_dashboard
62+
raise Exception.new('There are no dashboards available') if not dashboard
63+
64+
redirect "/" + dashboard
4665
end
4766

4867
get '/events', provides: 'text/event-stream' do
@@ -55,15 +74,6 @@ def protected!
5574
end
5675
end
5776

58-
get '/' do
59-
protected!
60-
begin
61-
redirect "/" + (settings.default_dashboard || first_dashboard).to_s
62-
rescue NoMethodError => e
63-
raise Exception.new("There are no dashboards in your dashboard directory.")
64-
end
65-
end
66-
6777
get '/:dashboard' do
6878
protected!
6979
tilt_html_engines.each do |suffix, _|
@@ -74,14 +84,6 @@ def protected!
7484
halt 404
7585
end
7686

77-
get '/views/:widget?.html' do
78-
protected!
79-
tilt_html_engines.each do |suffix, engines|
80-
file = File.join(settings.root, "widgets", params[:widget], "#{params[:widget]}.#{suffix}")
81-
return engines.first.new(file).render if File.exist? file
82-
end
83-
end
84-
8587
post '/dashboards/:id' do
8688
request.body.rewind
8789
body = JSON.parse(request.body.read)
@@ -109,16 +111,12 @@ def protected!
109111
end
110112
end
111113

112-
not_found do
113-
send_file File.join(settings.public_folder, '404.html')
114-
end
115-
116-
def development?
117-
ENV['RACK_ENV'] == 'development'
118-
end
119-
120-
def production?
121-
ENV['RACK_ENV'] == 'production'
114+
get '/views/:widget?.html' do
115+
protected!
116+
tilt_html_engines.each do |suffix, engines|
117+
file = File.join(settings.root, "widgets", params[:widget], "#{params[:widget]}.#{suffix}")
118+
return engines.first.new(file).render if File.exist? file
119+
end
122120
end
123121

124122
def send_event(id, body, target=nil)
@@ -154,14 +152,16 @@ def tilt_html_engines
154152
end
155153
end
156154

157-
settings_file = File.join(settings.root, 'config/settings.rb')
158-
if (File.exists?(settings_file))
159-
require settings_file
155+
def require_glob(relative_glob)
156+
Dir[File.join(settings.root, relative_glob)].each do |file|
157+
require file
158+
end
160159
end
161160

162-
Dir[File.join(settings.root, 'lib', '**', '*.rb')].each {|file| require file }
163-
{}.to_json # Forces your json codec to initialize (in the event that it is lazily loaded). Does this before job threads start.
161+
settings_file = File.join(settings.root, 'config/settings.rb')
162+
require settings_file if File.exists?(settings_file)
164163

164+
{}.to_json # Forces your json codec to initialize (in the event that it is lazily loaded). Does this before job threads start.
165165
job_path = ENV["JOB_PATH"] || 'jobs'
166-
files = Dir[File.join(settings.root, job_path, '**', '/*.rb')]
167-
files.each { |job| require(job) }
166+
require_glob(File.join('lib', '**', '*.rb'))
167+
require_glob(File.join(job_path, '**', '*.rb'))

lib/dashing/cli.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class CLI < Thor
1010
class << self
1111
attr_accessor :auth_token
1212

13-
def CLI.hyphenate(str)
13+
def hyphenate(str)
1414
return str.downcase if str =~ /^[A-Z-]+$/
1515
str.gsub('_', '-').gsub(/\B[A-Z]/, '-\&').squeeze('-').downcase
1616
end
@@ -43,16 +43,7 @@ def install(gist_id)
4343
gist = Downloader.get_gist(gist_id)
4444
public_url = "https://gist.github.com/#{gist_id}"
4545

46-
gist['files'].each do |file, details|
47-
if file =~ /\.(html|coffee|scss)\z/
48-
widget_name = File.basename(file, '.*')
49-
new_path = File.join(Dir.pwd, 'widgets', widget_name, file)
50-
create_file(new_path, details['content'])
51-
elsif file.end_with?('.rb')
52-
new_path = File.join(Dir.pwd, 'jobs', file)
53-
create_file(new_path, details['content'])
54-
end
55-
end
46+
install_widget_from_gist(gist)
5647

5748
print set_color("Don't forget to edit the ", :yellow)
5849
print set_color("Gemfile ", :yellow, :bold)
@@ -98,6 +89,19 @@ def run_command(command)
9889
system(command)
9990
end
10091

92+
def install_widget_from_gist(gist)
93+
gist['files'].each do |file, details|
94+
if file =~ /\.(html|coffee|scss)\z/
95+
widget_name = File.basename(file, '.*')
96+
new_path = File.join(Dir.pwd, 'widgets', widget_name, file)
97+
create_file(new_path, details['content'])
98+
elsif file.end_with?('.rb')
99+
new_path = File.join(Dir.pwd, 'jobs', file)
100+
create_file(new_path, details['content'])
101+
end
102+
end
103+
end
104+
101105
def require_file(file)
102106
require file
103107
end

test/app_test.rb

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,45 @@
44
class AppTest < Dashing::Test
55
def setup
66
@connection = []
7-
Sinatra::Application.settings.history_file = File.join(Dir.tmpdir, 'history.yml')
8-
Sinatra::Application.settings.connections = [@connection]
9-
Sinatra::Application.settings.auth_token = nil
10-
Sinatra::Application.settings.default_dashboard = nil
7+
app.settings.connections = [@connection]
8+
app.settings.auth_token = nil
9+
app.settings.default_dashboard = nil
10+
app.settings.history_file = File.join(Dir.tmpdir, 'history.yml')
11+
end
12+
13+
def test_redirect_to_first_dashboard
14+
with_generated_project do
15+
get '/'
16+
assert_equal 302, last_response.status
17+
assert_equal 'http://example.org/sample', last_response.location
18+
end
19+
end
20+
21+
def test_redirect_to_first_dashboard_without_erb
22+
with_generated_project do |dir|
23+
FileUtils.touch(File.join(dir, "dashboards/htmltest.html"))
24+
get '/'
25+
assert_equal 302, last_response.status
26+
assert_equal 'http://example.org/htmltest', last_response.location
27+
end
28+
end
29+
30+
def test_redirect_to_default_dashboard
31+
with_generated_project do
32+
app.settings.default_dashboard = 'test1'
33+
get '/'
34+
assert_equal 302, last_response.status
35+
assert_equal 'http://example.org/test1', last_response.location
36+
end
37+
end
38+
39+
def test_errors_out_when_no_dashboards_available
40+
with_generated_project do
41+
app.settings.views = File.join(app.settings.root, 'lib')
42+
43+
get '/'
44+
assert_equal 500, last_response.status
45+
end
1146
end
1247

1348
def test_post_widgets_without_auth_token
@@ -22,13 +57,13 @@ def test_post_widgets_without_auth_token
2257
end
2358

2459
def test_post_widgets_with_invalid_auth_token
25-
Sinatra::Application.settings.auth_token = 'sekrit'
60+
app.settings.auth_token = 'sekrit'
2661
post '/widgets/some_widget', JSON.generate({value: 9})
2762
assert_equal 401, last_response.status
2863
end
2964

3065
def test_post_widgets_with_valid_auth_token
31-
Sinatra::Application.settings.auth_token = 'sekrit'
66+
app.settings.auth_token = 'sekrit'
3267
post '/widgets/some_widget', JSON.generate({value: 9, auth_token: 'sekrit'})
3368
assert_equal 204, last_response.status
3469
end
@@ -52,32 +87,6 @@ def test_dashboard_events
5287
assert_equal 'reload', parse_data(@connection[0])['event']
5388
end
5489

55-
def test_redirect_to_default_dashboard
56-
with_generated_project do
57-
Sinatra::Application.settings.default_dashboard = 'test1'
58-
get '/'
59-
assert_equal 302, last_response.status
60-
assert_equal 'http://example.org/test1', last_response.location
61-
end
62-
end
63-
64-
def test_redirect_to_first_dashboard
65-
with_generated_project do
66-
get '/'
67-
assert_equal 302, last_response.status
68-
assert_equal 'http://example.org/sample', last_response.location
69-
end
70-
end
71-
72-
def test_redirect_to_first_dashboard_without_erb
73-
with_generated_project do |dir|
74-
FileUtils.touch(File.join(dir, "dashboards/htmltest.html"))
75-
get '/'
76-
assert_equal 302, last_response.status
77-
assert_equal 'http://example.org/htmltest', last_response.location
78-
end
79-
end
80-
8190
def test_get_dashboard
8291
with_generated_project do
8392
get '/sampletv'
@@ -129,14 +138,16 @@ def test_get_widget
129138
end
130139

131140
def with_generated_project
141+
source_path = File.expand_path('../../templates', __FILE__)
142+
132143
temp do |dir|
133144
cli = Dashing::CLI.new
134-
cli.stubs(:source_paths).returns([File.expand_path('../../templates', __FILE__)])
145+
cli.stubs(:source_paths).returns([source_path])
135146
silent { cli.new 'new_project' }
136147

137-
Sinatra::Application.settings.views = File.join(dir, 'new_project/dashboards')
138-
Sinatra::Application.settings.root = File.join(dir, 'new_project')
139-
yield Sinatra::Application.settings.root
148+
app.settings.views = File.join(dir, 'new_project/dashboards')
149+
app.settings.root = File.join(dir, 'new_project')
150+
yield app.settings.root
140151
end
141152
end
142153

0 commit comments

Comments
 (0)