Skip to content
Browse files

Adding configuration support.

  • Loading branch information...
1 parent 858053b commit 855597312ca43f6e7c820bed731beb5e8e33a289 @JEG2 committed Jun 13, 2008
Showing with 114 additions and 0 deletions.
  1. +15 −0 Rakefile
  2. +26 −0 lib/rrdb.rb
  3. +40 −0 test/tc_config.rb
  4. +15 −0 test/tc_run_command.rb
  5. +11 −0 test/tc_version.rb
  6. +7 −0 test/ts_all.rb
View
15 Rakefile
@@ -0,0 +1,15 @@
+#!/usr/bin/env rake
+
+require "rake/testtask"
+
+dir = File.dirname(__FILE__)
+lib = File.join(dir, "lib", "rrdb.rb")
+version = File.read(lib)[/^\s*VERSION\s*=\s*(['"])(\d\.\d\.\d)\1/, 2]
+
+task :default => [:test]
+
+Rake::TestTask.new do |test|
+ test.libs << "test"
+ test.test_files = %w[test/ts_all.rb]
+ test.verbose = true
+end
View
26 lib/rrdb.rb
@@ -0,0 +1,26 @@
+#!/usr/bin/env ruby -wKU
+
+class RRDB
+ VERSION = "0.0.1"
+
+ def self.run_command(command)
+ output = `#{command} 2>&1`
+ $?.success? ? output : nil
+ rescue
+ nil
+ end
+
+ def self.config(hash_or_key = nil)
+ case hash_or_key
+ when nil
+ @config ||= Hash.new
+ when Hash
+ config.merge!(hash_or_key)
+ else
+ config[hash_or_key]
+ end
+ end
+
+ config :rrdtool_path => (run_command("which rrdtool") || "rrdtool").strip,
+ :reserve_fields => 10
+end
View
40 test/tc_config.rb
@@ -0,0 +1,40 @@
+#!/usr/bin/env ruby -wKU
+
+require "test/unit"
+
+require "rrdb"
+
+class TestConfig < Test::Unit::TestCase
+ def setup
+ @saved_config = RRDB.config.dup
+ end
+
+ def teardown
+ RRDB.config.replace(@saved_config)
+ end
+
+ def test_config_is_a_hash_with_defaults
+ assert_instance_of(Hash, RRDB.config)
+ assert_not_nil(RRDB.config[:reserve_fields])
+ end
+
+ def test_alternative_interface_to_access_config_values
+ assert_equal(RRDB.config[:reserve_fields], RRDB.config(:reserve_fields))
+ end
+
+ def test_adding_new_values_to_the_config
+ assert_nil(RRDB.config[:config_value_from_tests])
+ RRDB.config(:config_value_from_tests => true)
+ assert_not_nil(RRDB.config[:config_value_from_tests])
+ end
+
+ def test_updating_the_config
+ old_value = RRDB.config[:reserve_fields]
+ RRDB.config(:reserve_fields => old_value.to_i + 1)
+ assert_not_equal(old_value, RRDB.config[:reserve_fields])
+ end
+
+ def test_config_tries_to_locate_rrdtool
+ assert_match(/\brrdtool\z/, RRDB.config[:rrdtool_path])
+ end
+end
View
15 test/tc_run_command.rb
@@ -0,0 +1,15 @@
+#!/usr/bin/env ruby -wKU
+
+require "test/unit"
+
+require "rrdb"
+
+class TestRunCommand < Test::Unit::TestCase
+ def test_run_command_returns_the_output_of_a_shell_command_on_success
+ assert_instance_of(String, RRDB.run_command("rrdtool -v"))
+ end
+
+ def test_run_command_returns_nil_on_failure_or_error
+ assert_nil(RRDB.run_command("BROKENrrdtool -v"))
+ end
+end
View
11 test/tc_version.rb
@@ -0,0 +1,11 @@
+#!/usr/bin/env ruby -wKU
+
+require "test/unit"
+
+require "rrdb"
+
+class TestVersion < Test::Unit::TestCase
+ def test_version_includes_major_minor_and_tiny
+ assert_match(/\A\d\.\d\.\d\z/, RRDB::VERSION)
+ end
+end
View
7 test/ts_all.rb
@@ -0,0 +1,7 @@
+#!/usr/bin/env ruby -wKU
+
+require "test/unit"
+
+require "tc_version"
+require "tc_run_command"
+require "tc_config"

0 comments on commit 8555973

Please sign in to comment.
Something went wrong with that request. Please try again.