silk 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,7 +24,9 @@ for full argument descriptions.
24
24
 
25
25
  The system automatically converts the rest URL (only GETs at the moment) into a rake task name - if the name doesn't exist, you'll get a 404. Any query params get passed into the task
26
26
 
27
- So http://localhost:8888/users/get?login=myles
27
+ So http://localhost:8888/users/get?login=myles (Returns Plain text)
28
+ So http://localhost:8888/users/get.json?login=myles (Returns JSON)
29
+ So http://localhost:8888/users/get.xml?login=myles (Returns XML)
28
30
 
29
31
  would run the users:get task. do something like this in the rake task:
30
32
 
@@ -34,9 +36,17 @@ would run the users:get task. do something like this in the rake task:
34
36
  task :get do |t, args|
35
37
  # args[:login] will be populated with myles
36
38
  if args[:login]
37
- puts "Success".to_json
39
+ respond_to do |format|
40
+ format.text { "Success" }
41
+ format.xml { "<body>Success</body>" }
42
+ format.json { "Success".to_json }
43
+ end
38
44
  else
39
- $stderr.puts "Fail".to_json
45
+ error_respond_to do |format|
46
+ format.text { "Fail" }
47
+ format.xml { "<error>Fail</error>" }
48
+ format.json { "Fail".to_json }
49
+ end
40
50
  exit(-1)
41
51
  end
42
52
  end
@@ -54,8 +64,6 @@ There is a test suite. It uses shoulda + mocha + test::rack
54
64
 
55
65
  If you are going to submit patches, please try to make sure the tests pass, and that you have created a test covering the changes you have made
56
66
 
57
- (Please note: I haven't got complete coverage yet, so I'm not really practicing what I preach. I'm working on it - feel free to help out!)
58
-
59
67
  == Status
60
68
 
61
69
  This is a really early release, that still needs more testing (This is so far more proof of concept at this point), and may blow up at any given moment. Feel free to fork and help out.
data/Rakefile CHANGED
@@ -22,6 +22,7 @@ begin
22
22
  gem.add_development_dependency "redgreen"
23
23
  gem.add_development_dependency "mocha"
24
24
  end
25
+ Jeweler::GemcutterTasks.new
25
26
  rescue LoadError
26
27
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
27
28
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -0,0 +1,29 @@
1
+ require 'fileutils'
2
+
3
+ class FileExists < StandardError;end
4
+
5
+ # Create the default recipe container for the gem
6
+ def write_to(path)
7
+ if !File.exists?(path)
8
+ FileUtils.mkdir_p(path)
9
+ else
10
+ raise FileExists.new("#{path} already exists")
11
+ end
12
+ end
13
+
14
+ begin
15
+ write_to('/etc/silk')
16
+ exit(0)
17
+ rescue FileExists => e
18
+ puts e
19
+ exit(0)
20
+ rescue
21
+ # Just drop through...
22
+ end
23
+
24
+ begin
25
+ write_to(File.join(ENV['HOME'], '.silk'))
26
+ rescue => e
27
+ puts e
28
+ exit(-1)
29
+ end
@@ -7,6 +7,7 @@ require 'daemons'
7
7
  require 'options'
8
8
  require 'server'
9
9
  require 'tasks'
10
+ require 'dsl'
10
11
 
11
12
  module Silk
12
13
  def self.run
@@ -0,0 +1,57 @@
1
+ class FormattedResponse
2
+ def method_missing(symbol, *args, &blk)
3
+ if [ :json, :xml, :text ].include?(symbol)
4
+ if block_given?
5
+ instance_variable_set("@#{symbol.to_s}", yield)
6
+ else
7
+ instance_variable_get("@#{symbol.to_s}")
8
+ end
9
+ else
10
+ super
11
+ end
12
+ end
13
+ end
14
+
15
+ # Allows you to set different outputs based on the requested format.
16
+ # SUpports xml, json and text
17
+ #
18
+ # respond_to do |format|
19
+ # format.xml { '<xml>xml</xml>' }
20
+ # format.json { '{ json }' }
21
+ # format.text { 'text' }
22
+ # end
23
+ #
24
+ def respond_to(&blk)
25
+ response = FormattedResponse.new
26
+ yield(response)
27
+ case(ENV['format'])
28
+ when 'xml'
29
+ puts response.xml
30
+ when 'json'
31
+ puts response.json
32
+ else
33
+ puts response.text
34
+ end
35
+ end
36
+
37
+ # Allows you to set different error outputs based on the requested format.
38
+ # SUpports xml, json and text
39
+ #
40
+ # error_respond_to do |format|
41
+ # format.xml { '<xml>xml</xml>' }
42
+ # format.json { '{ json }' }
43
+ # format.text { 'text' }
44
+ # end
45
+ #
46
+ def error_respond_to(&blk)
47
+ response = FormattedResponse.new
48
+ yield(response)
49
+ case(ENV['format'])
50
+ when 'xml'
51
+ $stderr.puts response.xml
52
+ when 'json'
53
+ $stderr.puts response.json
54
+ else
55
+ $stderr.puts response.text
56
+ end
57
+ end
@@ -16,7 +16,7 @@ class Options
16
16
 
17
17
  opts.on('-h', '--help', "You're looking at it") do
18
18
  puts opts
19
- exit(-1)
19
+ exit(1)
20
20
  end
21
21
 
22
22
  options[:ontop] = false
@@ -24,11 +24,11 @@ class Options
24
24
  options[:ontop] = true
25
25
  end
26
26
 
27
- options[:filter_paths] = [ File.join('', 'etc', 'silk') ]
28
- options[:filter_paths] << File.join(ENV['HOME'], '.silk') if ENV['HOME']
27
+ options[:recipe_paths] = [ File.join('', 'etc', 'silk') ]
28
+ options[:recipe_paths] << File.join(ENV['HOME'], '.silk') if ENV['HOME']
29
29
 
30
30
  opts.on('-r [recipe]', '--recipes [recipe]', /.+/, 'Reads in additional recipes') do |recipes|
31
- options[:filter_paths] << recipes
31
+ options[:recipe_paths] << recipes
32
32
  end
33
33
 
34
34
  options[:port] = 8888
@@ -42,13 +42,13 @@ class Options
42
42
  end
43
43
 
44
44
  options[:bind] = '0.0.0.0'
45
- opts.on('-b', '--bind', /.+/, 'Set the IP address to listen to') do |host|
45
+ opts.on('-b [address]', '--bind', /.+/, 'Set the IP address to listen to') do |host|
46
46
  options[:bind] = host
47
47
  end
48
48
 
49
49
  options[:server] = %w[thin mongrel webrick]
50
- opts.on('-s', '--server', /.+/, 'handler used for built-in web server') do |server|
51
- options[:server] = server
50
+ opts.on('-s [server list]', '--server', /.+/, 'handler used for built-in web server') do |server|
51
+ options[:server] = [ server ]
52
52
  end
53
53
 
54
54
  opts.on('-v', '--version') do
@@ -4,19 +4,32 @@ require 'sinatra/base'
4
4
  module Silk
5
5
  class Server < Sinatra::Base
6
6
  get %r{\/(.+)} do |c|
7
- content_type('application/json')
8
-
9
7
  options = Silk.options
10
- task = c.gsub("/", ":")
8
+ task = nil
9
+ format = 'text'
11
10
 
11
+ query = c.split('.')
12
+ format = query.pop if query.size > 1
13
+ task = query.join('.').gsub("/", ":")
14
+
15
+ case(format)
16
+ when 'json'
17
+ content_type('application/json')
18
+ when 'xml'
19
+ content_type('application/xml')
20
+ else
21
+ content_type('text/plain')
22
+ end
23
+
12
24
  tasks = Silk::Tasks.new
13
25
  unless tasks.list.include?(task)
14
- not_found("Not Found".to_json)
26
+ not_found("Not Found")
15
27
  end
16
28
 
17
29
  results = { :stdout => '', :stderr => '' }
18
30
  params.delete("captures")
19
-
31
+ ENV['format'] = format
32
+
20
33
  stdout_read, stdout_write = IO.pipe
21
34
  stderr_read, stderr_write = IO.pipe
22
35
  pid = Process.fork do
@@ -35,11 +48,11 @@ module Silk
35
48
  stderr_read.each do |line|
36
49
  results[:stderr] += line
37
50
  end
38
- Process.waitpid(pid)
51
+ pid, status = Process.waitpid2(pid)
39
52
 
40
- headers('X_PROCESS_EXIT_STATUS' => $?.exitstatus.to_s)
41
-
42
- if $?.exitstatus != 0
53
+ headers('X_PROCESS_EXIT_STATUS' => status.exitstatus.to_s)
54
+
55
+ if status.exitstatus != 0
43
56
  error(500, results[:stderr].strip)
44
57
  else
45
58
  results[:stdout].strip
@@ -6,7 +6,7 @@ module Silk
6
6
  Rake::Task.clear
7
7
  @app = Rake::Application.new
8
8
  @app.init
9
- Silk.options[:filter_paths].each do |path|
9
+ Silk.options[:recipe_paths].each do |path|
10
10
  FileList.new("#{path}/*.rake").each do |file|
11
11
  @app.add_import(file)
12
12
  end
@@ -18,7 +18,7 @@ module Silk
18
18
  return Rake::Task.tasks.map { |task| task.name }
19
19
  end
20
20
 
21
- def run(task, arguments)
21
+ def run(task, arguments = {})
22
22
  Rake::Task[task].execute(arguments)
23
23
  end
24
24
  end
@@ -0,0 +1,94 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{silk}
8
+ s.version = "0.0.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Myles Eftos"]
12
+ s.date = %q{2010-08-28}
13
+ s.default_executable = %q{silk}
14
+ s.description = %q{It allows you to write rake tasks to do common tasks, such as creating email addresses, adding users etc. Silk provides a HTTP wrapper the the rake tasks, and allows communication via JSON objects, which makes it dead easy for them to be called from a web app.}
15
+ s.email = %q{myles@madpilot.com.au}
16
+ s.executables = ["silk"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/silk",
29
+ "install.rb",
30
+ "lib/silk.rb",
31
+ "lib/silk/dsl.rb",
32
+ "lib/silk/options.rb",
33
+ "lib/silk/server.rb",
34
+ "lib/silk/tasks.rb",
35
+ "silk.gemspec",
36
+ "test/helper.rb",
37
+ "test/rakefiles/test.rake",
38
+ "test/test_dsl.rb",
39
+ "test/test_helper.rb",
40
+ "test/test_options.rb",
41
+ "test/test_server.rb",
42
+ "test/test_silk.rb",
43
+ "test/test_tasks.rb"
44
+ ]
45
+ s.homepage = %q{http://github.com/madpilot/silk}
46
+ s.rdoc_options = ["--charset=UTF-8"]
47
+ s.require_paths = ["lib"]
48
+ s.rubygems_version = %q{1.3.5}
49
+ s.summary = %q{A framework for creating a hosting console}
50
+ s.test_files = [
51
+ "test/test_server.rb",
52
+ "test/test_silk.rb",
53
+ "test/test_options.rb",
54
+ "test/test_dsl.rb",
55
+ "test/test_tasks.rb",
56
+ "test/test_helper.rb",
57
+ "test/helper.rb"
58
+ ]
59
+
60
+ if s.respond_to? :specification_version then
61
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
62
+ s.specification_version = 3
63
+
64
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
65
+ s.add_runtime_dependency(%q<daemons>, [">= 0"])
66
+ s.add_runtime_dependency(%q<json>, [">= 0"])
67
+ s.add_runtime_dependency(%q<sinatra>, [">= 0"])
68
+ s.add_runtime_dependency(%q<SyslogLogger>, [">= 0"])
69
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
70
+ s.add_development_dependency(%q<rack-test>, [">= 0"])
71
+ s.add_development_dependency(%q<redgreen>, [">= 0"])
72
+ s.add_development_dependency(%q<mocha>, [">= 0"])
73
+ else
74
+ s.add_dependency(%q<daemons>, [">= 0"])
75
+ s.add_dependency(%q<json>, [">= 0"])
76
+ s.add_dependency(%q<sinatra>, [">= 0"])
77
+ s.add_dependency(%q<SyslogLogger>, [">= 0"])
78
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
79
+ s.add_dependency(%q<rack-test>, [">= 0"])
80
+ s.add_dependency(%q<redgreen>, [">= 0"])
81
+ s.add_dependency(%q<mocha>, [">= 0"])
82
+ end
83
+ else
84
+ s.add_dependency(%q<daemons>, [">= 0"])
85
+ s.add_dependency(%q<json>, [">= 0"])
86
+ s.add_dependency(%q<sinatra>, [">= 0"])
87
+ s.add_dependency(%q<SyslogLogger>, [">= 0"])
88
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
89
+ s.add_dependency(%q<rack-test>, [">= 0"])
90
+ s.add_dependency(%q<redgreen>, [">= 0"])
91
+ s.add_dependency(%q<mocha>, [">= 0"])
92
+ end
93
+ end
94
+
@@ -2,23 +2,27 @@ require 'json'
2
2
 
3
3
  desc "Level 1"
4
4
  task :level_1 do
5
- puts "Level 1".to_json
5
+ puts "Level 1"
6
6
  end
7
7
 
8
8
  namespace :level_1 do
9
9
  desc "Level 2"
10
10
  task :level_2 do
11
- puts "Level 2".to_json
11
+ respond_to do |format|
12
+ format.text { "text" }
13
+ format.xml { "<xml>xml</xml>" }
14
+ format.json { "json".to_json }
15
+ end
12
16
  end
13
17
 
14
18
  desc "Level 2 with args"
15
19
  task :level_2_with_args do |t, args|
16
- puts(("Level 2: " + args.inspect).to_json)
20
+ puts(("Level 2: " + args.inspect))
17
21
  end
18
22
 
19
23
  namespace :level_2 do
20
24
  task :level_3 do
21
- puts "Level 3".to_json
25
+ puts "Level 3"
22
26
  end
23
27
  end
24
28
  end
@@ -26,7 +30,7 @@ end
26
30
  namespace :errors do
27
31
  desc "Returns an error"
28
32
  task :return do
29
- $stderr.puts "Error".to_json
30
- exit(-1)
33
+ $stderr.puts "Error"
34
+ exit(1)
31
35
  end
32
36
  end
@@ -0,0 +1,153 @@
1
+ require 'test_helper'
2
+
3
+ class TestDSL < Test::Unit::TestCase
4
+ context 'TestDSL' do
5
+ context 'respond_to' do
6
+ should 'call the text block if ENV[format] == text' do
7
+ ENV['format'] = 'text'
8
+
9
+ stdout_read, stdout_write = IO.pipe
10
+ pid = Process.fork do
11
+ $stdout.reopen stdout_write
12
+ stdout_read.close
13
+
14
+ respond_to do |format|
15
+ format.text { 'text' }
16
+ format.json { 'json'.to_json }
17
+ format.xml { '<xml>xml</xml>' }
18
+ end
19
+ end
20
+ stdout_write.close
21
+
22
+ stdout = ''
23
+ stdout_read.each do |line|
24
+ stdout += line
25
+ end
26
+ Process.waitpid(pid)
27
+ assert_equal 'text', stdout.strip
28
+ end
29
+
30
+ should 'call the text block if ENV[format] == xml' do
31
+ ENV['format'] = 'xml'
32
+
33
+ stdout_read, stdout_write = IO.pipe
34
+ pid = Process.fork do
35
+ $stdout.reopen stdout_write
36
+ stdout_read.close
37
+
38
+ respond_to do |format|
39
+ format.text { 'text' }
40
+ format.json { 'json'.to_json }
41
+ format.xml { '<xml>xml</xml>' }
42
+ end
43
+ end
44
+ stdout_write.close
45
+
46
+ stdout = ''
47
+ stdout_read.each do |line|
48
+ stdout += line
49
+ end
50
+ Process.waitpid(pid)
51
+ assert_equal '<xml>xml</xml>', stdout.strip
52
+ end
53
+
54
+ should 'call the text block if ENV[format] == json' do
55
+ ENV['format'] = 'json'
56
+
57
+ stdout_read, stdout_write = IO.pipe
58
+ pid = Process.fork do
59
+ $stdout.reopen stdout_write
60
+ stdout_read.close
61
+
62
+ respond_to do |format|
63
+ format.text { 'text' }
64
+ format.json { 'json'.to_json }
65
+ format.xml { '<xml>xml</xml>' }
66
+ end
67
+ end
68
+ stdout_write.close
69
+
70
+ stdout = ''
71
+ stdout_read.each do |line|
72
+ stdout += line
73
+ end
74
+ Process.waitpid(pid)
75
+ assert_equal 'json'.to_json, stdout.strip
76
+ end
77
+ end
78
+
79
+ context 'error_respond_to' do
80
+ should 'call the text block if ENV[format] == text' do
81
+ ENV['format'] = 'text'
82
+
83
+ stderr_read, stderr_write = IO.pipe
84
+ pid = Process.fork do
85
+ $stderr.reopen stderr_write
86
+ stderr_read.close
87
+
88
+ error_respond_to do |format|
89
+ format.text { 'text' }
90
+ format.json { 'json'.to_json }
91
+ format.xml { '<xml>xml</xml>' }
92
+ end
93
+ end
94
+ stderr_write.close
95
+
96
+ stderr = ''
97
+ stderr_read.each do |line|
98
+ stderr += line
99
+ end
100
+ Process.waitpid(pid)
101
+ assert_equal 'text', stderr.strip
102
+ end
103
+
104
+ should 'call the text block if ENV[format] == xml' do
105
+ ENV['format'] = 'xml'
106
+
107
+ stderr_read, stderr_write = IO.pipe
108
+ pid = Process.fork do
109
+ $stderr.reopen stderr_write
110
+ stderr_read.close
111
+
112
+ error_respond_to do |format|
113
+ format.text { 'text' }
114
+ format.json { 'json'.to_json }
115
+ format.xml { '<xml>xml</xml>' }
116
+ end
117
+ end
118
+ stderr_write.close
119
+
120
+ stderr = ''
121
+ stderr_read.each do |line|
122
+ stderr += line
123
+ end
124
+ Process.waitpid(pid)
125
+ assert_equal '<xml>xml</xml>', stderr.strip
126
+ end
127
+
128
+ should 'call the text block if ENV[format] == json' do
129
+ ENV['format'] = 'json'
130
+
131
+ stderr_read, stderr_write = IO.pipe
132
+ pid = Process.fork do
133
+ $stderr.reopen stderr_write
134
+ stderr_read.close
135
+
136
+ error_respond_to do |format|
137
+ format.text { 'text' }
138
+ format.json { 'json'.to_json }
139
+ format.xml { '<xml>xml</xml>' }
140
+ end
141
+ end
142
+ stderr_write.close
143
+
144
+ stderr = ''
145
+ stderr_read.each do |line|
146
+ stderr += line
147
+ end
148
+ Process.waitpid(pid)
149
+ assert_equal 'json'.to_json, stderr.strip
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,202 @@
1
+ require 'test_helper'
2
+
3
+ class TestOptions < Test::Unit::TestCase
4
+ context 'TestOptions' do
5
+ setup do
6
+ ENV['HOME'] = '/home/john'
7
+ ARGV.delete_if { |el| true }
8
+ end
9
+
10
+ should 'set defaults' do
11
+ options = Options.parse
12
+ assert_equal false, options[:verbose]
13
+ assert_equal false, options[:ontop]
14
+ assert_equal [ '/etc/silk', '/home/john/.silk' ], options[:recipe_paths]
15
+ assert_equal 8888, options[:port]
16
+ assert_equal false, options[:lock]
17
+ assert_equal '0.0.0.0', options[:bind]
18
+ assert_equal [ 'thin', 'mongrel', 'webrick' ], options[:server]
19
+ end
20
+
21
+ should 'set verbose mode if -V is set' do
22
+ ARGV << '-V'
23
+ options = Options.parse
24
+ assert_equal true, options[:verbose]
25
+ end
26
+
27
+ should 'set verbose mode if --verbose is set' do
28
+ ARGV << '--verbose'
29
+ options = Options.parse
30
+ assert_equal true, options[:verbose]
31
+ end
32
+
33
+ should 'display help on -h' do
34
+ ARGV << '-h'
35
+
36
+ stdout_read, stdout_write = IO.pipe
37
+
38
+ pid = Process.fork do
39
+ $stdout.reopen stdout_write
40
+ stdout_read.close
41
+ options = Options.parse
42
+ end
43
+
44
+ stdout_write.close
45
+ pid, status = Process.waitpid2(pid)
46
+ assert_equal 1, status.exitstatus
47
+ end
48
+
49
+ should 'display help on --help' do
50
+ ARGV << '--help'
51
+
52
+ stdout_read, stdout_write = IO.pipe
53
+
54
+ pid = Process.fork do
55
+ $stdout.reopen stdout_write
56
+ stdout_read.close
57
+ options = Options.parse
58
+ end
59
+
60
+ stdout_write.close
61
+ pid, status = Process.waitpid2(pid)
62
+ assert_equal 1, status.exitstatus
63
+ end
64
+
65
+ should 'set ontop to true if -f' do
66
+ ARGV << '-f'
67
+ options = Options.parse
68
+ assert_equal true, options[:ontop]
69
+ end
70
+
71
+ should 'set ontop to true if --foreground' do
72
+ ARGV << '--foreground'
73
+ options = Options.parse
74
+ assert_equal true, options[:ontop]
75
+ end
76
+
77
+ should 'add recipes to the recipe list for each -r' do
78
+ ARGV << '-r'
79
+ ARGV << '/tmp/recipes'
80
+ options = Options.parse
81
+ assert_equal [ '/etc/silk', '/home/john/.silk', '/tmp/recipes' ], options[:recipe_paths]
82
+
83
+ ARGV << '-r'
84
+ ARGV << '/tmp/recipes'
85
+ ARGV << '-r'
86
+ ARGV << '/tmp/recipes_2'
87
+
88
+ options = Options.parse
89
+ assert_equal [ '/etc/silk', '/home/john/.silk', '/tmp/recipes', '/tmp/recipes_2' ], options[:recipe_paths]
90
+ end
91
+
92
+ should 'add recipes to the recipe list for each -r' do
93
+ ARGV << '--recipes'
94
+ ARGV << '/tmp/recipes'
95
+ options = Options.parse
96
+ assert_equal [ '/etc/silk', '/home/john/.silk', '/tmp/recipes' ], options[:recipe_paths]
97
+
98
+ ARGV << '--recipes'
99
+ ARGV << '/tmp/recipes'
100
+ ARGV << '--recipes'
101
+ ARGV << '/tmp/recipes_2'
102
+
103
+ options = Options.parse
104
+ assert_equal [ '/etc/silk', '/home/john/.silk', '/tmp/recipes', '/tmp/recipes_2' ], options[:recipe_paths]
105
+ end
106
+
107
+ should 'set port if -p is set' do
108
+ ARGV << '-p'
109
+ ARGV << '1234'
110
+ options = Options.parse
111
+ assert_equal 1234, options[:port]
112
+ end
113
+
114
+ should 'set port if --port is set' do
115
+ ARGV << '--port'
116
+ ARGV << '1234'
117
+ options = Options.parse
118
+ assert_equal 1234, options[:port]
119
+ end
120
+
121
+ should 'set lock if -x is set' do
122
+ ARGV << '-x'
123
+ options = Options.parse
124
+ assert_equal true, options[:lock]
125
+ end
126
+
127
+ should 'set lock if --lock is set' do
128
+ ARGV << '-x'
129
+ options = Options.parse
130
+ assert_equal true, options[:lock]
131
+ end
132
+
133
+ should 'set bind address if -b is set' do
134
+ ARGV << '-b'
135
+ ARGV << '1.2.3.4'
136
+ options = Options.parse
137
+ assert_equal '1.2.3.4', options[:bind]
138
+ end
139
+
140
+ should 'set bind address if --bind is set' do
141
+ ARGV << '--bind'
142
+ ARGV << '1.2.3.4'
143
+ options = Options.parse
144
+ assert_equal '1.2.3.4', options[:bind]
145
+ end
146
+
147
+ should 'set the server array if -s is set' do
148
+ ARGV << '-s'
149
+ ARGV << 'iis'
150
+ options = Options.parse
151
+ assert_equal [ 'iis' ], options[:server]
152
+ end
153
+
154
+ should 'set the server array if -s is set' do
155
+ ARGV << '--server'
156
+ ARGV << 'iis'
157
+ options = Options.parse
158
+ assert_equal [ 'iis' ], options[:server]
159
+ end
160
+
161
+ should 'display the VERSION of -v is set' do
162
+ ARGV << '-v'
163
+
164
+ stdout_read, stdout_write = IO.pipe
165
+
166
+ pid = Process.fork do
167
+ $stdout.reopen stdout_write
168
+ stdout_read.close
169
+ options = Options.parse
170
+ end
171
+
172
+ stdout_write.close
173
+ pid, status = Process.waitpid2(pid)
174
+ assert_equal -1, status.exitstatus
175
+ end
176
+
177
+ should 'display the VERSION of -v is set' do
178
+ ARGV << '--version'
179
+
180
+ stdout_read, stdout_write = IO.pipe
181
+
182
+ pid = Process.fork do
183
+ $stdout.reopen stdout_write
184
+ stdout_read.close
185
+ options = Options.parse
186
+ end
187
+
188
+ result = ''
189
+ stdout_write.close
190
+ stdout_read.each do |line|
191
+ result += line
192
+ end
193
+ pid, status = Process.waitpid2(pid)
194
+ assert_equal 0, status.exitstatus
195
+ version = ''
196
+ File.open(File.join(File.dirname(__FILE__), '..', 'VERSION')) do |fh|
197
+ version = fh.read
198
+ end
199
+ assert_equal result, version
200
+ end
201
+ end
202
+ end
@@ -1,53 +1,78 @@
1
1
  require 'test_helper'
2
2
 
3
- class TestSilk < Test::Unit::TestCase
3
+ class TestServer < Test::Unit::TestCase
4
4
  include Rack::Test::Methods
5
5
 
6
6
  def app
7
7
  Silk::Server
8
8
  end
9
9
 
10
- context '' do
10
+ context 'TestServer' do
11
11
  setup do
12
- Silk.options = { :filter_paths => File.join(File.dirname(File.expand_path(__FILE__)), 'rakefiles') }
12
+ Silk.options = { :recipe_paths => File.join(File.dirname(File.expand_path(__FILE__)), 'rakefiles') }
13
+ end
14
+
15
+ context 'mime_types' do
16
+ should "return 'text/plain' if there is no format" do
17
+ get '/level_1/level_2'
18
+ assert last_response.ok?
19
+ assert_equal "text/plain", last_response.headers['Content-Type']
20
+ assert_equal "text", last_response.body
21
+ end
22
+
23
+ should "return 'text/plain' if the format isn't recognised" do
24
+ get '/level_1/level_2.csv'
25
+ assert last_response.ok?
26
+ assert_equal "text/plain", last_response.headers['Content-Type']
27
+ assert_equal "text", last_response.body
28
+ end
29
+
30
+ should "return 'application/json' if there is no format" do
31
+ get '/level_1/level_2.json'
32
+ assert last_response.ok?
33
+ assert_equal "application/json", last_response.headers['Content-Type']
34
+ assert_equal "json".to_json, last_response.body
35
+ end
36
+
37
+ should "return 'application/xml' if there is no format" do
38
+ get '/level_1/level_2.xml'
39
+ assert last_response.ok?
40
+ assert_equal "application/xml", last_response.headers['Content-Type']
41
+ assert_equal "<xml>xml</xml>", last_response.body
42
+ end
13
43
  end
14
44
 
15
45
  should "return a 404 if the rake task is not found" do
16
46
  get '/'
17
- assert "application/json", last_response.headers['Content-Type']
18
47
  assert last_response.not_found?
19
48
  end
20
49
 
21
50
  should "return a success if the rake task exists and is successfully run" do
22
51
  get '/level_1'
23
52
  assert last_response.ok?
24
- assert "application/json", last_response.headers['Content-Type']
25
53
  assert_equal "0", last_response.headers['X_PROCESS_EXIT_STATUS']
26
- assert_equal "Level 1".to_json, last_response.body.to_s
54
+ assert_equal "Level 1", last_response.body.to_s
27
55
  end
28
56
 
29
57
  should "return a success if the rake task exists at a second level and is successfully run" do
30
58
  get '/level_1/level_2'
31
59
  assert last_response.ok?
32
- assert "application/json", last_response.headers['Content-Type']
33
60
  assert_equal "0", last_response.headers['X_PROCESS_EXIT_STATUS']
34
- assert_equal "Level 2".to_json, last_response.body
61
+ assert_equal "text", last_response.body
35
62
  end
36
63
 
37
64
  should "return a success if the rake task exists an process arguments if present" do
38
65
  get '/level_1/level_2_with_args?argument_1=1&argument_2=2'
39
66
  assert last_response.ok?
40
- assert "application/json", last_response.headers['Content-Type']
41
67
  assert_equal "0", last_response.headers['X_PROCESS_EXIT_STATUS']
42
- assert_equal ("Level 2: " + { "argument_1" => "1", "argument_2" => "2" }.inspect).to_json, last_response.body
68
+ assert_equal ("Level 2: " + { "argument_1" => "1", "argument_2" => "2" }.inspect), last_response.body
43
69
  end
44
70
 
45
71
  should "return read stderr, and set the exit status, and return 500 if the rake task exists, but returns a non-zero exit code" do
46
72
  get '/errors/return'
47
73
  assert_equal 500, last_response.status
48
- assert "application/json", last_response.headers['Content-Type']
49
- assert_equal "255", last_response.headers['X_PROCESS_EXIT_STATUS']
50
- assert_equal "Error".to_json, last_response.body
74
+ assert_equal "1", last_response.headers['X_PROCESS_EXIT_STATUS']
75
+ assert_equal "Error", last_response.body
51
76
  end
52
77
  end
53
78
  end
@@ -1,7 +1,49 @@
1
- require 'helper'
1
+ require 'test_helper'
2
2
 
3
3
  class TestSilk < Test::Unit::TestCase
4
- context '' do
5
-
4
+ context 'TestSilk' do
5
+ context 'run' do
6
+ should 'use SyslogLogger' do
7
+ logger_mock = mock
8
+ options = { :ontop => false }
9
+ Options.stubs(:parse).returns(options)
10
+ Daemons.stubs(:call).with(options)
11
+ SyslogLogger.expects(:new).with('silk').returns(logger_mock)
12
+ Silk.run
13
+ assert_equal logger_mock, options[:logger]
14
+ assert_equal options, Silk.options
15
+ end
16
+
17
+ should 'use STDOUT for logging if ontop is true' do
18
+ logger_mock = mock
19
+ options = { :ontop => true }
20
+ Options.stubs(:parse).returns(options)
21
+ Daemons.stubs(:call).with(options)
22
+ Logger.expects(:new).with(STDOUT).returns(logger_mock)
23
+ Silk.run
24
+ assert_equal logger_mock, options[:logger]
25
+ assert_equal options, Silk.options
26
+ end
27
+
28
+ should 'change the directory in the child to match the parent' do
29
+ logger_mock = mock
30
+ options = { :ontop => true, :multiple => true, :bind => '1.2.3.4', :port => 80 }
31
+ Options.stubs(:parse).returns(options)
32
+ Logger.stubs(:new).returns(logger_mock)
33
+ Dir.expects(:pwd).returns('/tmp/dir')
34
+ Dir.expects(:chdir).with('/tmp/dir')
35
+ Silk::Server.stubs(:run!)
36
+ Silk.run
37
+ end
38
+
39
+ should 'should start the webserver with the supplied options' do
40
+ logger_mock = mock
41
+ options = { :ontop => true, :multiple => true, :bind => '1.2.3.4', :port => 80 }
42
+ Options.stubs(:parse).returns(options)
43
+ Logger.stubs(:new).returns(logger_mock)
44
+ Silk::Server.expects(:run!).with({ :host => '1.2.3.4', :port => 80, :environment => :production })
45
+ Silk.run
46
+ end
47
+ end
6
48
  end
7
49
  end
@@ -1,9 +1,9 @@
1
1
  require 'test_helper'
2
2
 
3
- class TestSilk < Test::Unit::TestCase
4
- context '' do
3
+ class TestTasks < Test::Unit::TestCase
4
+ context 'TestTasks' do
5
5
  setup do
6
- Silk.options = { :filter_paths => File.join(File.dirname(File.expand_path(__FILE__)), 'rakefiles') }
6
+ Silk.options = { :recipe_paths => File.join(File.dirname(File.expand_path(__FILE__)), 'rakefiles') }
7
7
  @task = Silk::Tasks.new
8
8
  end
9
9
 
@@ -16,6 +16,7 @@ class TestSilk < Test::Unit::TestCase
16
16
  end
17
17
 
18
18
  should "run a task if it exists" do
19
+ # I'm guessing this test fails because STDOUT and STDERR are already being captured
19
20
  stdout_read, stdout_write = IO.pipe
20
21
  stderr_read, stderr_write = IO.pipe
21
22
  pid = Process.fork do
@@ -39,11 +40,11 @@ class TestSilk < Test::Unit::TestCase
39
40
  end
40
41
  Process.waitpid(pid)
41
42
 
42
- assert_equal "Level_1".to_json, stdout.strip
43
- assert_equal "".to_json, stderr.strip
43
+ assert_equal "Level 1", stdout.strip
44
+ assert_equal "", stderr.strip
44
45
  end
45
46
 
46
- should "run a task and pass in arrguments if it exists" do
47
+ should "run a task and pass in arguments if it exists" do
47
48
  # I'm guessing this test fails because STDOUT and STDERR are already being captured
48
49
  stdout_read, stdout_write = IO.pipe
49
50
  stderr_read, stderr_write = IO.pipe
@@ -52,7 +53,7 @@ class TestSilk < Test::Unit::TestCase
52
53
  $stderr.reopen stderr_write
53
54
  stdout_read.close
54
55
  stderr_read.close
55
- @task.run("level_2_with_args", { 'param_1' => '1' })
56
+ @task.run("level_1:level_2_with_args", { 'param_1' => '1' })
56
57
  end
57
58
 
58
59
  stdout_write.close
@@ -68,10 +69,8 @@ class TestSilk < Test::Unit::TestCase
68
69
  end
69
70
  Process.waitpid(pid)
70
71
 
71
- assert_equal(("Level 2: " + { 'param_1' => '1' }.inspect).to_json, stdout.strip)
72
- assert_equal "".to_json, stderr.strip
72
+ assert_equal(("Level 2: " + { 'param_1' => '1' }.inspect), stdout.strip)
73
+ assert_equal "", stderr.strip
73
74
  end
74
-
75
- should "run the task only once if a new Application is built"
76
75
  end
77
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: silk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Myles Eftos
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-18 00:00:00 +08:00
12
+ date: 2010-08-28 00:00:00 +08:00
13
13
  default_executable: silk
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -109,13 +109,18 @@ files:
109
109
  - Rakefile
110
110
  - VERSION
111
111
  - bin/silk
112
+ - install.rb
112
113
  - lib/silk.rb
114
+ - lib/silk/dsl.rb
113
115
  - lib/silk/options.rb
114
116
  - lib/silk/server.rb
115
117
  - lib/silk/tasks.rb
118
+ - silk.gemspec
116
119
  - test/helper.rb
117
120
  - test/rakefiles/test.rake
121
+ - test/test_dsl.rb
118
122
  - test/test_helper.rb
123
+ - test/test_options.rb
119
124
  - test/test_server.rb
120
125
  - test/test_silk.rb
121
126
  - test/test_tasks.rb
@@ -150,6 +155,8 @@ summary: A framework for creating a hosting console
150
155
  test_files:
151
156
  - test/test_server.rb
152
157
  - test/test_silk.rb
158
+ - test/test_options.rb
159
+ - test/test_dsl.rb
153
160
  - test/test_tasks.rb
154
161
  - test/test_helper.rb
155
162
  - test/helper.rb