Skip to content

Commit

Permalink
Adds overriding configuration for start and break commands #30
Browse files Browse the repository at this point in the history
* Adds specs for Configuration
* Adds fakefs gem for specs
  • Loading branch information
stephenmckinney committed Jan 18, 2013
1 parent f6db5b5 commit 8a800c8
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 23 deletions.
18 changes: 9 additions & 9 deletions bin/pomo
Expand Up @@ -73,13 +73,13 @@ command :start do |c|
c.example 'Start the first task' , 'pomo start first'
c.example 'Start the fifth task' , 'pomo start 5'

c.option '-p', '--progress', 'Run with progress bar in foreground'
c.option '--notifier <lib>', String, 'Specify notificaiton library: `notification_center`, `libnotify`, `growl`, `quicksilver`'
c.option '--[no-]progress' , 'Run with progress bar'
c.option '--[no-]tmux' , 'Refresh tmux status bar on timer change'
c.action do |args, options|
abort 'a task is already running' if list.running

config = Pomo::Configuration.load

options.default :progress => false
config = Pomo::Configuration.load(options.__hash__)
args = ['incomplete'] if args.empty?
list.find(*args) do |task|
abort 'task already completed' if task.complete?
Expand Down Expand Up @@ -162,13 +162,13 @@ command :break do |c|
c.example 'Take a 30 minute break' , 'pomo break --length 30'

c.option '-l', '--length minutes', Integer, 'Change the default length in minutes'
c.option '-p', '--progress', 'Run with progress bar in foreground'
c.option '--notifier <lib>', String, 'Specify notificaiton library: `notification_center`, `libnotify`, `growl`, `quicksilver`'
c.option '--[no-]progress' , 'Run with progress bar'
c.option '--[no-]tmux' , 'Refresh tmux status bar on timer change'
c.action do |args, options|
options.default :progress => false
options.default :length => 5
options.default :length => args.first ? args.first.to_i : 5

config = Pomo::Configuration.load
#options.default :length => args.first ? args.first.to_i : 5
config = Pomo::Configuration.load(options.__hash__)
task = Pomo::Break.new('Break time', options.__hash__)
task.start(config, :progress => options.progress)
end
Expand Down
23 changes: 15 additions & 8 deletions lib/pomo/configuration.rb
@@ -1,4 +1,6 @@
require 'yaml'
require 'commander'
include Commander::UI

module Pomo
class Configuration
Expand Down Expand Up @@ -33,33 +35,38 @@ def initialize(options = {})
end

##
# Load configuration or default_options.
# Load configuration file or default_options. Passed options take precedence.

def self.load(options = {})
options.select!{|k,v| [:notifier, :progress, :tmux].include? k}

def self.load
if !(File.exists? config_file)
File.open(config_file, 'w') { |file| YAML::dump(default_options, file) }
say "Initialized default config file in #{config_file}. See 'pomo help initconfig' for options."
end

options = YAML.load_file(config_file)
new(options)
config_file_options = YAML.load_file(config_file)
new(config_file_options.merge(options))
end

##
# Save configuration.
# Save configuration. Passed options take precendence over default_options.

def self.save(options = {})
force_save = options.delete :force
options.select!{|k,v| [:notifier, :progress, :tmux].include? k}

options = default_options.merge(options)

if !(File.exists? config_file) || options[:force]
File.open(config_file, 'w') { |file| YAML::dump(options.reject{|k,v| k==:force}, file) }
if !(File.exists? config_file) || force_save
File.open(config_file, 'w') { |file| YAML::dump(options, file) }
say "Initialized config file in #{config_file}"
else
say_error "Not overwriting existing config file #{config_file}, use --force to override. See 'pomo help initconfig'."
end
end

private
# Helpers

def self.config_file
File.join(ENV['HOME'],'.pomorc')
Expand Down
4 changes: 2 additions & 2 deletions lib/pomo/task.rb
Expand Up @@ -107,7 +107,7 @@ def foreground_progress(config)
elsif remaining == 5
notifier.notify 'Almost there!', :header => '5 minutes remaining'
end
sleep 60
sleep 60 unless ENV['POMO_ENV']=='test'
{ :remaining => remaining }
end

Expand Down Expand Up @@ -135,7 +135,7 @@ def background_progress(config)
elsif remaining == 5
notifier.notify 'Almost there!', :header => '5 minutes remaining'
end
sleep 60
sleep 60 unless ENV['POMO_ENV']=='test'
end

write_tmux_time(0) if config.tmux
Expand Down
1 change: 1 addition & 0 deletions pomo.gemspec
Expand Up @@ -29,5 +29,6 @@ Gem::Specification.new do |spec|

spec.add_development_dependency('aruba', '~> 0.5.1')
spec.add_development_dependency('rspec', '~> 2.12')
spec.add_development_dependency('fakefs', '~> 0.4')
spec.add_development_dependency('yard')
end
101 changes: 100 additions & 1 deletion spec/pomo/configuration_spec.rb
Expand Up @@ -3,7 +3,106 @@
describe Pomo::Configuration do

describe '#initialize' do
it 'does nada'
it 'instantiates object' do
options = {
:notifier => 'foo',
:progress => 'bar',
:tmux => 'baz'
}
config = Pomo::Configuration.new(options)
expect(config.notifier).to eq 'foo'
expect(config.progress).to eq 'bar'
expect(config.tmux).to eq 'baz'
end
end

describe '.load' do
context 'not given a configuration file' do
it 'returns Configuration object with default options' do
config = Pomo::Configuration.load
expect(config.notifier).to eq Pomo::Configuration.default_notifier
expect(config.progress).to be false
expect(config.tmux).to be false
end

it 'writes a configuration file with default options' do
Pomo::Configuration.load
expect(File.read(Pomo::Configuration.config_file)).to eq \
YAML::dump(Pomo::Configuration.default_options)
end
end

context 'given a configuration file' do
before(:each) do
opts = {
:notifier => 'foo',
:progress => 'bar',
:tmux => 'baz'
}
File.open(Pomo::Configuration.config_file, 'w') do |file|
YAML::dump(opts, file)
end
end

it 'returns Configuration object with options in file' do
config = Pomo::Configuration.load
expect(config.notifier).to eq 'foo'
expect(config.progress).to eq 'bar'
expect(config.tmux).to eq 'baz'
end

context 'given options' do
it 'overrides options in configuration file' do
options = {
:notifier => 'goo',
:progress => 'car',
:tmux => 'caz'
}
config = Pomo::Configuration.load(options)
expect(config.notifier).to eq 'goo'
expect(config.progress).to eq 'car'
expect(config.tmux).to eq 'caz'
end
end

end
end

describe '.save' do
let(:options) { {:notifier => 'foo', :progress => 'bar', :tmux => 'baz'} }

context 'not given a configuration file' do
it 'writes a configuration file with options' do
Pomo::Configuration.save(options)
expect(File.read(Pomo::Configuration.config_file)).to eq \
YAML::dump(options)
end
end

context 'given a configuration file' do
before(:each) do
opts = {
:notifier => 'goo',
:progress => 'car',
:tmux => 'caz'
}
File.open(Pomo::Configuration.config_file, 'w') do |file|
YAML::dump(opts, file)
end
end

it 'does not overwrite' do
Pomo::Configuration.save(options)
expect(File.read(Pomo::Configuration.config_file)).to_not eq \
YAML::dump(options)
end

it 'does overwrite if passed `force` option' do
Pomo::Configuration.save(options.merge(:force => true))
expect(File.read(Pomo::Configuration.config_file)).to eq \
YAML::dump(options)
end
end
end

end
11 changes: 8 additions & 3 deletions spec/spec_helper.rb
@@ -1,4 +1,5 @@
require 'pomo'
require 'fakefs/safe'

# Taken from Aruba::Api
class Pomo::RSpecHelper
Expand All @@ -22,12 +23,16 @@ def self.restore_env

config.before(:suite) do
Pomo::RSpecHelper.set_env('POMO_ENV', 'test')
Pomo::RSpecHelper.set_env('HOME', '/tmp/home')
end

config.before(:each) do
FileUtils.rm_rf '/tmp/home'
FileUtils.mkdir '/tmp/home'
FakeFS.activate!
FileUtils.mkdir_p ENV['HOME']
end

config.after(:each) do
FakeFS.deactivate!
FakeFS::FileSystem.clear
end

config.after(:suite) do
Expand Down

0 comments on commit 8a800c8

Please sign in to comment.