roger 0.13.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,51 +7,52 @@ require 'webrick/https'
7
7
 
8
8
  module Roger
9
9
  class Server
10
-
11
- attr_reader :options, :server_options
12
-
10
+
11
+ attr_reader :server_options
12
+
13
13
  attr_reader :project
14
-
15
- attr_accessor :port, :handler
16
-
14
+
15
+ attr_accessor :port, :handler, :host
16
+
17
17
  def initialize(project, options={})
18
18
  @stack = initialize_rack_builder
19
-
19
+
20
20
  @project = project
21
-
21
+
22
22
  @server_options = {}
23
-
23
+
24
+ # Defaults
25
+ self.port = 9000
26
+ self.handler = nil
27
+ self.host = "0.0.0.0"
28
+
24
29
  set_options(options)
25
30
  end
26
-
31
+
27
32
  # Sets the options, this is a separate method as we want to override certain
28
33
  # things set in the mockupfile from the commandline
29
34
  def set_options(options)
30
- @options = {
31
- :handler => nil, # Autodetect
32
- :port => 9000
33
- }.update(options)
34
-
35
- self.port = @options[:port]
36
- self.handler = @options[:handler]
35
+ self.port = options[:port] if options.has_key?(:port)
36
+ self.handler = options[:handler] if options.has_key?(:handler)
37
+ self.host = options[:host] if options.has_key?(:host)
37
38
  end
38
-
39
+
39
40
  # Use the specified Rack middleware
40
41
  #
41
42
  # @see ::Rack::Builder#use
42
43
  def use(*args, &block)
43
44
  @stack.use *args, &block
44
45
  end
45
-
46
+
46
47
  # Use the map handler to map endpoints to certain urls
47
48
  #
48
- # @see ::Rack::Builder#map
49
+ # @see ::Rack::Builder#map
49
50
  def map(*args, &block)
50
51
  @stack.map *args, &block
51
52
  end
52
-
53
+
53
54
  def run!
54
- self.get_handler(self.handler).run self.application, self.server_options do |server|
55
+ self.handler.run self.application, self.server_options do |server|
55
56
  trap(:INT) do
56
57
  ## Use thins' hard #stop! if available, otherwise just #stop
57
58
  server.respond_to?(:stop!) ? server.stop! : server.stop
@@ -60,55 +61,67 @@ module Roger
60
61
  end
61
62
  end
62
63
  alias :run :run!
63
-
64
+
64
65
  def port=(p)
65
- self.server_options[:Port] = p
66
+ @port = self.server_options[:Port] = p
67
+ end
68
+
69
+ def host=(h)
70
+ @host = self.server_options[:Host] = h
66
71
  end
67
-
72
+
73
+ def handler=(h)
74
+ if h.respond_to?(:run)
75
+ @handler = h
76
+ else
77
+ @handler = self.get_handler(h)
78
+ end
79
+ end
80
+
68
81
  protected
69
-
82
+
70
83
  # Build the final application that get's run by the Rack Handler
71
84
  def application
72
85
  return @app if @app
73
-
86
+
74
87
  @stack.run Rack::Roger.new(self.project)
75
-
88
+
76
89
  @app = @stack
77
- end
78
-
90
+ end
91
+
79
92
  # Initialize the Rack builder instance for this server
80
93
  #
81
94
  # @return ::Rack::Builder instance
82
95
  def initialize_rack_builder
83
- builder = ::Rack::Builder.new
96
+ builder = ::Rack::Builder.new
84
97
  builder.use ::Rack::ShowExceptions
85
98
  builder.use ::Rack::Lint
86
99
  builder.use ::Rack::ConditionalGet
87
- builder.use ::Rack::Head
88
-
89
- builder
100
+ builder.use ::Rack::Head
101
+
102
+ builder
90
103
  end
91
-
104
+
92
105
  # Get the actual handler for use in the server
93
106
  # Will always return a handler, it will try to use the fallbacks
94
107
  def get_handler(preferred_handler_name = nil)
95
108
  servers = %w[puma mongrel thin webrick]
96
109
  servers.unshift(preferred_handler_name) if preferred_handler_name
97
-
110
+
98
111
  handler = nil
99
- while((server_name = servers.shift) && handler === nil) do
112
+ while((server_name = servers.shift) && handler === nil) do
100
113
  begin
101
114
  handler = ::Rack::Handler.get(server_name)
102
115
  rescue LoadError
103
116
  rescue NameError
104
117
  end
105
118
  end
106
-
119
+
107
120
  if preferred_handler_name && server_name != preferred_handler_name
108
- puts "Handler '#{preferred_handler_name}' not found, using fallback ('#{server_name}')."
121
+ puts "Handler '#{preferred_handler_name}' not found, using fallback ('#{handler.inspect}')."
109
122
  end
110
123
  handler
111
124
  end
112
-
125
+
113
126
  end
114
127
  end
@@ -1,4 +1,4 @@
1
- require 'thor'
1
+ require "thor"
2
2
  require File.dirname(__FILE__) + "/helpers/get_callable"
3
3
  require File.dirname(__FILE__) + "/helpers/logging"
4
4
 
@@ -91,12 +91,16 @@ module Roger
91
91
 
92
92
  # Get files from the project path
93
93
  #
94
- # @param [Array] globs an array of file path globs that will be globbed against the project path
95
- # @param [Array] excludes an array of regexps that will be excluded from the result
94
+ # @param [Array] globs an array of file path globs that will be globbed
95
+ # against the project path
96
+ # @param [Array] excludes an array of regexps[!] that will be excluded
97
+ # from the result. N.b. please add an exclamation mark
98
+ # if you wasted more then an hour of your life because the
99
+ # shell glob didn't 'work'.
96
100
  def get_files(globs, excludes = [])
97
101
  files = globs.map{|g| Dir.glob(self.project.path + g) }.flatten
98
102
  if excludes.any?
99
- files.reject{|c| excludes.detect{|e| e.match(c) } }
103
+ files.reject { |c| excludes.detect { |e| c.match(e) } }
100
104
  else
101
105
  files
102
106
  end
@@ -2,25 +2,25 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "roger"
5
- s.version = "0.13.0"
6
-
5
+ s.version = "1.0.0"
6
+
7
7
  s.authors = ["Flurin Egger", "Edwin van der Graaf", "Joran Kapteijns"]
8
- s.email = ["info@digitpaint.nl", "flurin@digitpaint.nl"]
8
+ s.email = ["info@digitpaint.nl", "flurin@digitpaint.nl"]
9
9
  s.homepage = "http://github.com/digitpaint/roger"
10
10
  s.summary = "Roger is a set of tools to create self-containing HTML mockups."
11
11
  s.licenses = ["MIT"]
12
12
 
13
13
  s.date = Time.now.strftime("%Y-%m-%d")
14
-
14
+
15
15
  s.files = `git ls-files`.split("\n")
16
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
- s.require_paths = ["lib"]
19
-
18
+ s.require_paths = ["lib"]
19
+
20
20
  s.extra_rdoc_files = [
21
21
  "README.md"
22
22
  ] + `git ls-files -- {doc}/*`.split("\n")
23
-
23
+
24
24
  s.rdoc_options = ["--charset=UTF-8"]
25
25
 
26
26
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
@@ -34,4 +34,5 @@ Gem::Specification.new do |s|
34
34
 
35
35
  s.add_development_dependency("test-unit", "~> 3.0.0")
36
36
  s.add_development_dependency("mocha", "~> 1.1.0")
37
+ s.add_development_dependency("puma", "~> 2.10.0")
37
38
  end
@@ -0,0 +1,47 @@
1
+ require "stringio"
2
+
3
+ module Roger
4
+ module TestCli
5
+ # Capture stdout/stderr output
6
+ def capture
7
+ @_orig_stdout, @_orig_stderr = $stdout, $stderr
8
+
9
+ $stdout = StringIO.new
10
+ $stderr = StringIO.new
11
+
12
+ yield
13
+
14
+ return [$stdout.string, $stderr.string]
15
+ ensure
16
+ $stdout, $stderr = @_orig_stdout, @_orig_stderr
17
+ end
18
+
19
+ def run_command(args, &block)
20
+ out, err = capture do
21
+ Cli::Base.start(args, :debug => true)
22
+ end
23
+ [out,err]
24
+ end
25
+
26
+
27
+ def run_command_with_mockupfile(args, &block)
28
+ project = Project.new(@base_path || File.dirname(__FILE__) + "/../../project", :mockupfile_path => false)
29
+
30
+ mockupfile = Roger::Mockupfile.new(project)
31
+
32
+ if block_given?
33
+ yield(mockupfile)
34
+ end
35
+
36
+ project.mockupfile = mockupfile
37
+
38
+ Cli::Base.project = project
39
+
40
+ out, err = capture do
41
+ Cli::Base.start(args, :debug => true)
42
+ end
43
+ [out,err]
44
+ end
45
+
46
+ end
47
+ end
@@ -3,15 +3,12 @@ require File.dirname(__FILE__) + "/lib/tests/noop/noop"
3
3
  require File.dirname(__FILE__) + "/lib/tests/fail/fail"
4
4
  require File.dirname(__FILE__) + "/lib/tests/succeed/succeed"
5
5
 
6
- mockup.project.options[:verbose] = true;
7
6
 
8
7
  mockup.project.partial_path = [mockup.project.path + "partials", mockup.project.path + "partials2"]
9
8
 
10
9
  mockup.serve do |s|
11
10
  end
12
11
 
13
-
14
-
15
12
  mockup.test do |t|
16
13
  t.use :noop
17
14
  t.use :fail
@@ -3,11 +3,12 @@ module RogerNoopTest
3
3
 
4
4
  def initialize(options={})
5
5
  @options = {}
6
- @options.update(options) if options
6
+ @options.update(options) if options
7
7
  end
8
8
 
9
9
  def call(test, options={})
10
10
  test.log(self, "NOOP")
11
+ test.debug(self, "NOOP DEBUG")
11
12
  true
12
13
  end
13
14
 
@@ -0,0 +1,48 @@
1
+ require "./lib/roger/cli.rb"
2
+ require "test/unit"
3
+
4
+ require File.dirname(__FILE__) + "/../../helpers/cli"
5
+
6
+ # These tests ar for the roger generate command
7
+
8
+ module CustomGens
9
+ module Generators
10
+
11
+ class MockedGenerator < Roger::Generators::Base
12
+
13
+ desc "@mocked description"
14
+ argument :path, :type => :string, :required => false, :desc => "Path to generate mockup into"
15
+ argument :another_arg, :type => :string, :required => false, :desc => "Mocked or what?!"
16
+
17
+ def test
18
+ # Somewhat ugly way of checking
19
+ raise NotImplementedError
20
+ end
21
+ end
22
+
23
+ Roger::Generators.register :mocked, MockedGenerator
24
+ end
25
+ end
26
+
27
+ module Roger
28
+ class CliGenerateTest < ::Test::Unit::TestCase
29
+ include TestCli
30
+
31
+ def setup
32
+ @base_path = File.dirname(__FILE__) + "/../../project"
33
+ end
34
+
35
+ # roger generate
36
+ def test_has_generate_command
37
+ assert_includes Cli::Base.tasks.keys, "generate"
38
+ end
39
+
40
+ def test_help_shows_available_generators
41
+ out, err = run_command %w{help generate}
42
+
43
+ assert_includes out, "generate new"
44
+ assert_includes out, "generate mock"
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,142 @@
1
+ require "./lib/roger/cli.rb"
2
+ require "test/unit"
3
+
4
+ require File.dirname(__FILE__) + "/../../helpers/cli"
5
+
6
+ # These tests ar for the roger serve command
7
+
8
+ class Roger::Cli::Serve
9
+ def start
10
+ # Let's not start it.
11
+ end
12
+ end
13
+
14
+
15
+ module Roger
16
+ class CliServeTest < ::Test::Unit::TestCase
17
+ include TestCli
18
+
19
+ def setup
20
+ @base_path = File.dirname(__FILE__) + "/../../project"
21
+ end
22
+
23
+ def teardown
24
+ Cli::Base.project = nil
25
+ end
26
+
27
+ # roger serve
28
+ def test_has_serve_command
29
+ assert_includes Cli::Base.tasks.keys, "serve"
30
+ end
31
+
32
+ # roger server
33
+ def test_serve_default_options
34
+ out, err = run_command(%w{serve})
35
+
36
+ assert_includes out, "9000"
37
+ assert_includes out, "0.0.0.0"
38
+ assert_includes out, "Puma"
39
+ end
40
+
41
+ def test_serve_with_custom_host
42
+ out, err = run_command(%w{serve --host=localhost})
43
+
44
+ assert_includes out, "9000"
45
+ assert_includes out, "localhost"
46
+ assert_includes out, "Puma"
47
+ end
48
+
49
+ def test_serve_with_custom_port
50
+ out, err = run_command(%w{serve --port=8888})
51
+
52
+ assert_includes out, "8888"
53
+ assert_includes out, "0.0.0.0"
54
+ assert_includes out, "Puma"
55
+ end
56
+
57
+ def test_serve_with_custom_handler
58
+ out, err = run_command(%w{serve --handler=webrick})
59
+
60
+ assert_includes out, "9000"
61
+ assert_includes out, "0.0.0.0"
62
+ assert_includes out, "WEBrick"
63
+ end
64
+
65
+ end
66
+
67
+ class CliServeWithMockupfileTest < ::Test::Unit::TestCase
68
+ include TestCli
69
+
70
+ def test_serve_with_port_in_mockupfile
71
+ out, err = run_command_with_mockupfile(%w{serve}) do |m|
72
+ m.serve do |s|
73
+ s.port = 9001
74
+ end
75
+ end
76
+
77
+ assert_includes out, "9001"
78
+ assert_includes out, "0.0.0.0"
79
+ assert_includes out, "Puma"
80
+ end
81
+
82
+ def test_serve_with_host_in_mockupfile
83
+ out, err = run_command_with_mockupfile(%w{serve}) do |m|
84
+ m.serve do |s|
85
+ s.host = "127.0.0.1"
86
+ end
87
+ end
88
+
89
+ assert_includes out, "9000"
90
+ assert_includes out, "127.0.0.1"
91
+ assert_includes out, "Puma"
92
+ end
93
+
94
+ def test_serve_with_handler_in_mockupfile
95
+ out, err = run_command_with_mockupfile(%w{serve}) do |m|
96
+ m.serve do |s|
97
+ s.handler = "webrick"
98
+ end
99
+ end
100
+
101
+ assert_includes out, "9000"
102
+ assert_includes out, "0.0.0.0"
103
+ assert_includes out, "WEBrick"
104
+ end
105
+
106
+ def test_serve_with_custom_port_should_override_mockupfile
107
+ out, err = run_command_with_mockupfile(%w{serve --port=9002}) do |m|
108
+ m.serve do |s|
109
+ s.port = 9001
110
+ end
111
+ end
112
+
113
+ assert_includes out, "9002"
114
+ assert_includes out, "0.0.0.0"
115
+ assert_includes out, "Puma"
116
+ end
117
+
118
+ def test_serve_with_custom_host_should_override_mockupfile
119
+ out, err = run_command_with_mockupfile(%w{serve --host=localhost}) do |m|
120
+ m.serve do |s|
121
+ s.host = "127.0.0.1"
122
+ end
123
+ end
124
+
125
+ assert_includes out, "9000"
126
+ assert_includes out, "localhost"
127
+ assert_includes out, "Puma"
128
+ end
129
+
130
+ def test_serve_with_custom_handler_should_override_mockupfile
131
+ out, err = run_command_with_mockupfile(%w{serve --handler=webrick}) do |m|
132
+ m.serve do |s|
133
+ s.handler = "puma"
134
+ end
135
+ end
136
+
137
+ assert_includes out, "9000"
138
+ assert_includes out, "0.0.0.0"
139
+ assert_includes out, "WEBrick"
140
+ end
141
+ end
142
+ end