svutil 0.1.8 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/svutil.rb CHANGED
@@ -6,5 +6,5 @@ require 'svutil/log'
6
6
  require 'svutil/process_manager'
7
7
 
8
8
  module SVUtil
9
- VERSION = '0.1.8'
9
+ VERSION = '0.9.0'
10
10
  end
data/lib/svutil/config.rb CHANGED
@@ -27,6 +27,7 @@ module SVUtil
27
27
  end
28
28
 
29
29
  class Config
30
+
30
31
  class << self
31
32
  attr_writer :config_file
32
33
  attr_reader :attrs
@@ -34,12 +35,11 @@ module SVUtil
34
35
  # Used mainly for testing
35
36
  attr_accessor :option_source
36
37
 
37
- def config_class
38
- subclasses_of(self).first || self
39
- end
38
+ attr_accessor :cli_options
39
+ attr_accessor :cli_option_handlers
40
40
 
41
- def config_file
42
- @config_file || 'settings'
41
+ def self.config_class
42
+ subclasses_of(self).first || self
43
43
  end
44
44
 
45
45
  def set(options = {})
@@ -55,55 +55,75 @@ module SVUtil
55
55
  yield @defaults
56
56
  end
57
57
 
58
+ def config_file
59
+ @config_file ||= "settings"
60
+ end
61
+
62
+ def set_cli_option(option, value)
63
+ @cli_options ||= {}
64
+ @cli_options[option.to_s] = value
65
+ end
66
+
67
+ def handle_options(&block)
68
+ @cli_option_handlers ||= []
69
+ @cli_option_handlers << block
70
+ end
71
+
72
+ def validator(&block)
73
+ @validate_block = block
74
+ end
75
+
58
76
  def validate
59
77
  # TODO: Check file perms
60
78
  if ((pid_file.nil? or pid_file.empty? or File.directory?(pid_file)) && self.daemon)
61
- STDERR.puts "PID file must be a writable file"
62
- exit 1
79
+ err("PID file must be a writable file")
63
80
  end
81
+ # TODO: Validate the writability of the log file
82
+ @validate_block.call(self) unless @validate_block.nil?
64
83
  true
65
84
  end
66
85
 
67
86
  def init
68
- self.set do |c|
69
- process_options
70
- # If there is no config file and we haven't specified one then ignore
71
- if File.exists?(self.config_file) || self.config_provided_on_cli
72
- load_config_file
73
- end
74
- end
75
- end
76
-
77
- protected
78
- # Overide in subclasses for additional options
79
- def process_options
80
- parse_options
81
- end
82
-
83
- def parse_options(&block)
87
+ set do |c|
88
+ self.config_provided_on_cli = false
84
89
  OptionParser.new do |opts|
85
90
  opts.on("-f", "--config [filename]", "Config file to use (default 'settings')") do |filename|
86
91
  self.config_file = filename.strip
87
92
  self.config_provided_on_cli = true
88
93
  end
89
94
  opts.on("-d", "--daemon", "Run in the background as a daemon") do
90
- self.daemon = true
95
+ set_cli_option(:daemon, true)
91
96
  end
92
97
  opts.on("-l", "--debug-log [log-file]", "Debug Log File") do |log|
93
- self.log_file = log
98
+ set_cli_option(:log_file, log)
94
99
  end
95
100
  opts.on("-T", "--trace", "Display backtrace on errors") do
96
- self.trace = true
101
+ set_cli_option(:trace, true)
97
102
  end
98
103
  opts.on("-P", "--pid [pid-file]", "PID File") do |pid|
99
- self.pid_file = pid
104
+ set_cli_option(:pid_file, pid)
105
+ end
106
+ # Handle CLI passed options
107
+ (cli_option_handlers || []).each do |handler|
108
+ handler.call(opts)
100
109
  end
101
- yield opts if block_given?
102
110
  end.parse!(self.option_source || ARGV)
111
+
112
+ # Process the config file
113
+ if (self.config_file && File.exists?(self.config_file)) || self.config_provided_on_cli
114
+ load_config_file
115
+ end
116
+
117
+ # Finally apply any CLI options
118
+ (cli_options || {}).each do |(k,v)|
119
+ c.send("#{k}=", v)
120
+ end
103
121
  end
122
+ end
104
123
 
105
124
  private
106
125
  def method_missing(method_id, *args)
126
+ @defaults ||= Defaults.new
107
127
  @attrs ||= {}
108
128
  if method_id.to_s =~ /=$/
109
129
  @attrs[method_id.to_s[0...-1]] = args.first
@@ -123,13 +143,18 @@ module SVUtil
123
143
  contents.split("\n").each_with_index do |line, index|
124
144
  pair = line.split("=")
125
145
  if pair.size != 2
126
- STDERR.puts "Invalid config file '#{config_file}'. Syntax error at line #{index + 1}"
127
- exit 1
146
+ err("Invalid config file '#{config_file}'. Syntax error at line #{index + 1}")
128
147
  end
129
148
  self.send("#{pair[0].strip}=", pair[1].strip)
130
149
  end
131
150
  end
132
151
 
152
+ # TODO: This should be moved out to a Validator class
153
+ def err(message)
154
+ STDERR.puts(message)
155
+ exit 1
156
+ end
157
+
133
158
  def subclasses_of(*superclasses) #:nodoc:
134
159
  subclasses = []
135
160
  superclasses.each do |sup|
data/test/test_config.rb CHANGED
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb')
2
2
 
3
3
  class CustomConfig < SVUtil::Config
4
4
  defaults do |c|
@@ -13,69 +13,71 @@ class TestConfig < Test::Unit::TestCase
13
13
  end
14
14
 
15
15
  def test_set
16
- assert !Config.my_symbol
17
- Config.my_symbol = 'abc'
18
- assert_equal 'abc', Config.my_symbol
19
- Config.my_var = 10
20
- assert_equal 10, Config.my_var
16
+ assert !CustomConfig.my_symbol
17
+ CustomConfig.my_symbol = 'abc'
18
+ assert_equal 'abc', CustomConfig.my_symbol
19
+ CustomConfig.my_var = 10
20
+ assert_equal 10, CustomConfig.my_var
21
21
  end
22
22
 
23
23
  def test_set_with_block
24
- Config.expects(:validate).times(1)
25
- Config.set do |c|
24
+ CustomConfig.expects(:validate).times(1)
25
+ CustomConfig.set do |c|
26
26
  c.another = 'abc'
27
27
  c.some_var = 123
28
28
  c.foobar = true
29
29
  end
30
- assert_equal 'abc', Config.another
31
- assert_equal 123, Config.some_var
32
- assert Config.foobar
30
+ assert_equal 'abc', CustomConfig.another
31
+ assert_equal 123, CustomConfig.some_var
32
+ assert CustomConfig.foobar
33
33
  end
34
34
 
35
35
  def test_standard_cli_options
36
- Config.expects(:validate).times(1)
37
- Config.option_source = [ "-f", "test/settings", "-d", "-l", "debug", "-T" ]
38
- Config.init
39
- assert_equal "test/settings", Config.config_file
40
- assert_equal "debug", Config.log_file
41
- assert Config.daemon
42
- assert Config.trace
36
+ CustomConfig.expects(:validate).times(1)
37
+ CustomConfig.option_source = [ "-f", "test/settings", "-d", "-l", "debug", "-T" ]
38
+ CustomConfig.init
39
+ assert_equal "test/settings", CustomConfig.config_file
40
+ assert_equal "debug", CustomConfig.log_file
41
+ assert CustomConfig.daemon
42
+ assert CustomConfig.trace
43
43
  end
44
44
 
45
45
  def test_config_file
46
- Config.expects(:validate).times(1)
47
- Config.config_file = "test/settings"
48
- Config.init
49
- assert_equal 'bar', Config.foo
46
+ CustomConfig.expects(:validate).times(1)
47
+ CustomConfig.config_file = "test/settings"
48
+ CustomConfig.init
49
+ assert_equal 'bar', CustomConfig.foo
50
50
  end
51
51
 
52
52
  def test_load_config_file
53
- Config.expects(:load_config_file).times(1)
54
- Config.config_file = "test/settings"
55
- Config.init
56
- assert_equal 'bar', Config.foo
53
+ CustomConfig.expects(:load_config_file).times(1)
54
+ CustomConfig.config_file = "test/settings"
55
+ CustomConfig.init
56
+ assert_equal 'bar', CustomConfig.foo
57
+ end
58
+
59
+ def test_validations_on_cli_with_no_config_file
60
+ CustomConfig.expects(:validate).times(1)
61
+ CustomConfig.config_file = nil
62
+ CustomConfig.init
57
63
  end
58
64
 
59
- # Command Line loses!
65
+ # Command Line wins!
60
66
  def test_file_overide
61
- Config.expects(:validate).times(1)
62
- Config.config_file = "test/settings"
63
- Config.option_source = [ "-l", "logfile_set_on_cli" ]
64
- Config.init
65
- assert_equal "logfile_set_in_config_file", Config.log_file
67
+ CustomConfig.expects(:validate).times(1)
68
+ CustomConfig.config_file = "test/settings"
69
+ CustomConfig.option_source = [ "-l", "logfile_set_on_cli" ]
70
+ CustomConfig.init
71
+ assert_equal "logfile_set_on_cli", CustomConfig.log_file
66
72
  end
67
73
 
68
74
  def test_defaults
69
- Config.defaults do |c|
75
+ CustomConfig.defaults do |c|
70
76
  c.manager = 'daniel'
71
77
  end
72
- assert_equal 'daniel', Config.manager
73
- Config.manager = 'james'
74
- assert_equal 'james', Config.manager
75
- end
76
-
77
- def test_validations
78
-
78
+ assert_equal 'daniel', CustomConfig.manager
79
+ CustomConfig.manager = 'james'
80
+ assert_equal 'james', CustomConfig.manager
79
81
  end
80
82
 
81
83
  def test_custom_defaults
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb')
2
+
3
+ class MyConfig < SVUtil::Config
4
+ handle_options do |opts|
5
+ opts.on("-Q", "--restless", "Feeling restless?") do
6
+ set_cli_option(:restless, true)
7
+ end
8
+ opts.on("-x", "--x-value [value]", "X Value") do |x|
9
+ set_cli_option(:x, x)
10
+ end
11
+ end
12
+ end
13
+
14
+ class TestConfigHandleOptions < Test::Unit::TestCase
15
+ include SVUtil
16
+
17
+ def setup
18
+ end
19
+
20
+ def test_handle_options
21
+ MyConfig.option_source = [ "-P", "foo", "-x", "12345", "-Q" ]
22
+ MyConfig.init
23
+ assert MyConfig.pid_file = "foo"
24
+ assert MyConfig.x = '1234'
25
+ assert MyConfig.restless
26
+ end
27
+
28
+ def test_built_in_options
29
+ MyConfig.option_source = [ "-d", "-P", "foo" ]
30
+ MyConfig.init
31
+ assert MyConfig.daemon
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb')
2
+
3
+ class ValidatedConfig < SVUtil::Config
4
+ validator do |c|
5
+ err("Output dir must be provided") unless c.output_dir
6
+ end
7
+ end
8
+
9
+ class TestValidations < Test::Unit::TestCase
10
+ include SVUtil
11
+
12
+ def setup
13
+ end
14
+
15
+ def test_custom_validation_error
16
+ ValidatedConfig.expects(:err).times(1)
17
+ ValidatedConfig.output_dir = nil
18
+ ValidatedConfig.validate
19
+ end
20
+
21
+ def test_custom_validation
22
+ ValidatedConfig.expects(:err).never
23
+ ValidatedConfig.output_dir = "foo"
24
+ assert ValidatedConfig.validate
25
+ end
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svutil
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.9.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-21 00:00:00.000000000Z
12
+ date: 2012-01-03 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: newgem
16
- requirement: &70318178637000 !ruby/object:Gem::Requirement
16
+ requirement: &70175710061260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.5.3
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70318178637000
24
+ version_requirements: *70175710061260
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hoe
27
- requirement: &70318178636200 !ruby/object:Gem::Requirement
27
+ requirement: &70175710060100 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.12'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70318178636200
35
+ version_requirements: *70175710060100
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rdoc
38
- requirement: &70318173938520 !ruby/object:Gem::Requirement
38
+ requirement: &70175705942700 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '3.10'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70318173938520
46
+ version_requirements: *70175705942700
47
47
  description: A simple managed process builder for Ruby projects
48
48
  email:
49
49
  - daniel@netfox.com
@@ -66,7 +66,9 @@ files:
66
66
  - test/test_helper.rb
67
67
  - test/test_svutil.rb
68
68
  - test/test_config.rb
69
+ - test/test_config_handle_options.rb
69
70
  - test/test_defaults.rb
71
+ - test/test_validations.rb
70
72
  - .gemtest
71
73
  homepage: http://svutil.rubyforge.org
72
74
  licenses: []
@@ -96,6 +98,8 @@ specification_version: 3
96
98
  summary: Simple Process Builder for Ruby
97
99
  test_files:
98
100
  - test/test_config.rb
101
+ - test/test_config_handle_options.rb
99
102
  - test/test_defaults.rb
100
103
  - test/test_helper.rb
101
104
  - test/test_svutil.rb
105
+ - test/test_validations.rb