From 70b3632fd9e6ea2522ef86ff4eefecc49e449b84 Mon Sep 17 00:00:00 2001 From: felix Date: Mon, 16 May 2011 15:45:39 +0700 Subject: start some tests --- Gemfile.lock | 20 ++++++++++++++++++++ Rakefile | 2 +- bin/timetrackr | 16 ++++++++-------- lib/timetrackr.rb | 10 +++++----- lib/timetrackr/yaml.rb | 22 +++++++++++++--------- test/helper.rb | 3 --- test/test_timetrackr.rb | 50 +++++++++++++++++++++++++++++++++++++++++++++++-- timetrackr.gemspec | 12 ++++++------ 8 files changed, 101 insertions(+), 34 deletions(-) create mode 100644 Gemfile.lock diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..07df601 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,20 @@ +GEM + remote: http://rubygems.org/ + specs: + git (1.2.5) + jeweler (1.5.2) + bundler (~> 1.0.0) + git (>= 1.2.5) + rake + rake (0.8.7) + rcov (0.9.9) + shoulda (2.11.3) + +PLATFORMS + ruby + +DEPENDENCIES + bundler (~> 1.0.0) + jeweler (~> 1.5.2) + rcov + shoulda diff --git a/Rakefile b/Rakefile index 505e695..bafc719 100644 --- a/Rakefile +++ b/Rakefile @@ -18,7 +18,7 @@ Jeweler::Tasks.new do |gem| gem.summary = "A simple time tracking utility" gem.description = "A simple time tracking utility" gem.email = "felix@seconddrawer.com.au" - gem.authors = ["felix"] + gem.authors = ["Felix Hanley"] # Include your dependencies below. Runtime dependencies are required when using your gem, # and development dependencies are only needed for development (ie running rake tasks, tests, etc) # gem.add_runtime_dependency 'jabber4r', '> 0.1' diff --git a/bin/timetrackr b/bin/timetrackr index 919e714..6d42dac 100755 --- a/bin/timetrackr +++ b/bin/timetrackr @@ -8,6 +8,7 @@ DEFAULTS = { :backend => 'yaml', :verbose => false, :single_task => false, + :path => File.join(ENV['HOME'],'.timetrackr.db'), :time_format => "% -30s %2dh %2dm %2ds" } @@ -62,15 +63,16 @@ end config = DEFAULTS.merge(config || {}) $verbose = config[:verbose] -trackr = TimeTracker.create(config[:backend]) +trackr = TimeTrackr.create(config[:backend], config) # # commands # case cmd -when 'start','s' +when 'start','in','s' task = ARGV.shift details = {:action => 'start', :notes => ARGV.join(' ')} + # switch tasks if config says so if config[:single_task] && trackr.current != task trackr.current.each do |t| trackr.event(t,Time.now,details.merge(:action => 'stop')) unless t == task @@ -82,7 +84,7 @@ when 'start','s' puts "Started task '#{task}'" if $verbose end -when 'stop','k' +when 'stop','out','kill','k' task = ARGV.shift tasks = [] details = {:action => 'stop', :notes => ARGV.join(' ')} @@ -102,7 +104,7 @@ when 'switch','sw' trackr.event(task, Time.now, details.merge(:action => 'start')) puts "Switched to task '#{task}'" if $verbose -when 'time', nil +when 'time','status',nil task = ARGV.shift if task && trackr.tasks.include?(task) tasks = [*task] @@ -114,10 +116,8 @@ when 'time', nil puts format_time(name, trackr.time(task),config[:time_format]) end -when 'clear' - task = ARGV.shift - tasks = [] - tasks = [*task] if trackr.current.include?(task) +when 'clear','delete','del' + tasks = ARGV tasks = trackr.tasks if task == 'all' tasks.each do |task| trackr.clear(task) diff --git a/lib/timetrackr.rb b/lib/timetrackr.rb index cf64d5c..75bf520 100644 --- a/lib/timetrackr.rb +++ b/lib/timetrackr.rb @@ -1,13 +1,13 @@ -autoload 'YamlTimeTracker', 'timetrackr/yaml' -autoload 'SqliteTimeTracker', 'timetrackr/sqlite' +autoload 'YamlTimeTrackr', 'timetrackr/yaml' +autoload 'SqliteTimeTrackr', 'timetrackr/sqlite' -class TimeTracker +class TimeTrackr def self.create(type,options={}) case type.to_s when 'yaml' begin require 'yaml' - log = YamlTimeTracker.new(options) + log = YamlTimeTrackr.new(options[:path]) puts 'Loaded yaml tracker' if $verbose rescue LoadError puts 'Yaml not found' @@ -15,7 +15,7 @@ class TimeTracker when 'sqlite' begin require 'sqlite3' - log = SqliteTimeTracker.new(options) + log = SqliteTimeTrackr.new(options[:path]) puts 'Loaded sqlite tracker' if $verbose rescue LoadError puts 'Sqlite not found' diff --git a/lib/timetrackr/yaml.rb b/lib/timetrackr/yaml.rb index f9dc6cb..ac5e299 100644 --- a/lib/timetrackr/yaml.rb +++ b/lib/timetrackr/yaml.rb @@ -1,12 +1,12 @@ -class YamlTimeTracker < TimeTracker +class YamlTimeTrackr < TimeTrackr - def initialize(options) - @log_path = options[:log_path] || File.join(ENV['HOME'],'.timetrackr.db') - if File.exist? @log_path - @db = YAML.load_file(@log_path) - else + def initialize(path) + @log_path = path + if !File.exist? @log_path @db = {:current => [], :tasks => {}} + write_file end + @db = YAML.load_file(@log_path) puts "Using log file '#{@log_path}'" if $verbose end @@ -48,9 +48,7 @@ class YamlTimeTracker < TimeTracker end def close - File.open(@log_path,'w') do |fh| - YAML.dump(@db,fh) - end + write_file end def clear(task) @@ -68,4 +66,10 @@ class YamlTimeTracker < TimeTracker @db[:tasks][task] = Array[details] end end + + def write_file + File.open(@log_path,'w') do |fh| + YAML.dump(@db,fh) + end + end end diff --git a/test/helper.rb b/test/helper.rb index 0eda669..d29cdb1 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -13,6 +13,3 @@ require 'shoulda' $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'timetrackr' - -class Test::Unit::TestCase -end diff --git a/test/test_timetrackr.rb b/test/test_timetrackr.rb index b259c11..0c8a526 100644 --- a/test/test_timetrackr.rb +++ b/test/test_timetrackr.rb @@ -1,7 +1,53 @@ require 'helper' class TestTimetrackr < Test::Unit::TestCase - should "probably rename this file and start testing for real" do - flunk "hey buddy, you should probably rename this file and start testing for real" + context 'a YAML based tracker' do + setup do + @config = {:path => '/tmp/timetracker.test'} + @t = TimeTrackr.create('yaml',@config) + end + + def teardown + File.unlink(@config[:path]) rescue nil + end + + should 'initialise a log file' do + assert File.exist?(@config[:path]) + end + + context 'with empty db' do + setup do + File.open(@config[:path]) do |fh| + @db = YAML.load(fh) + end + end + + should 'create basic structure' do + File.open(@config[:path]) do |fh| + @db = YAML.load(fh) + end + assert @db[:current].class == Array + assert @db[:tasks].class == Hash + end + + should 'not fail on current command' do + assert_nothing_raised Exception do + @t.current + end + end + + should 'not fail on tasks command' do + assert_nothing_raised Exception do + @t.tasks + end + end + + should 'not fail on close command' do + assert_nothing_raised Exception do + @t.close + end + end + + end end end diff --git a/timetrackr.gemspec b/timetrackr.gemspec index 653359b..c055b73 100644 --- a/timetrackr.gemspec +++ b/timetrackr.gemspec @@ -8,8 +8,8 @@ Gem::Specification.new do |s| s.version = "0.1.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= - s.authors = ["felix"] - s.date = %q{2011-05-14} + s.authors = ["Felix Hanley"] + s.date = %q{2011-05-16} s.default_executable = %q{timetrackr} s.description = %q{A simple time tracking utility} s.email = %q{felix@seconddrawer.com.au} @@ -26,12 +26,12 @@ Gem::Specification.new do |s| "VERSION", "bin/timetrackr", "lib/timetrackr.rb", - "lib/timetrackr/eventlog.rb", - "lib/timetrackr/eventlog/yaml.rb", + "lib/timetrackr/yaml.rb", "test/helper.rb", - "test/test_timetrackr.rb" + "test/test_timetrackr.rb", + "timetrackr.gemspec" ] - s.homepage = %q{http://github.com/felix/timetrackr} + s.homepage = %q{http://felixhanley.info/projects/timetrackr} s.licenses = ["MIT"] s.require_paths = ["lib"] s.rubygems_version = %q{1.6.2} -- cgit v1.2.3