forked from samg/timetrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimetrap_spec.rb
1436 lines (1244 loc) · 49.8 KB
/
timetrap_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
TEST_MODE = true
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'timetrap'))
require 'rspec'
require 'fakefs/safe'
def local_time(str)
Timetrap::Timer.process_time(str)
end
def local_time_cli(str)
local_time(str).strftime('%Y-%m-%d %H:%M:%S')
end
module Timetrap::StubConfig
def with_stubbed_config options = {}
defaults = Timetrap::Config.defaults.dup
Timetrap::Config.stub(:[]).and_return do |k|
defaults.merge(options)[k]
end
yield if block_given?
end
end
describe Timetrap do
include Timetrap::StubConfig
before do
with_stubbed_config
end
def create_entry atts = {}
Timetrap::Entry.create({
:sheet => 'default',
:start => Time.now,
:end => Time.now,
:note => 'note'}.merge(atts))
end
before :each do
Timetrap::Entry.create_table!
Timetrap::Meta.create_table!
$stdout = StringIO.new
$stdin = StringIO.new
$stderr = StringIO.new
end
describe 'CLI' do
describe "COMMANDS" do
def invoke command
Timetrap::CLI.parse command
Timetrap::CLI.invoke
end
describe 'with no command' do
it "should invoke --help" do
with_stubbed_config('default_command' => nil) do
invoke ''
$stdout.string.should include "Usage"
end
end
end
describe 'with default command configured' do
it "should invoke the default command" do
with_stubbed_config('default_command' => 'n') do
invoke ''
$stderr.string.should include('*default: not running')
end
end
it "should allow a complicated default command" do
with_stubbed_config('default_command' => 'display -f csv', 'formatter_search_paths' => '/tmp') do
invoke 'in foo bar'
invoke 'out'
invoke ''
$stdout.string.should include(',"foo bar"')
end
end
end
describe 'with an invalid command' do
it "should tell me I'm wrong" do
invoke 'poo'
$stderr.string.should include 'Invalid command: "poo"'
end
end
describe 'archive' do
before do
3.times do |i|
create_entry
end
end
it "should put the entries in a hidden sheet" do
$stdin.string = "yes\n"
invoke 'archive'
Timetrap::Entry.each do |e|
e.sheet.should == '_default'
end
end
it "should leave the running entry alone" do
invoke "in"
$stdin.string = "yes\n"
invoke 'archive'
Timetrap::Entry.order(:id).last.sheet.should == 'default'
end
end
describe 'config' do
it "should write a config file" do
FakeFS do
FileUtils.mkdir_p(ENV['HOME'])
config_file = ENV['HOME'] + '/.timetrap.yml'
FileUtils.rm(config_file) if File.exist? config_file
File.exist?(config_file).should be_false
invoke "configure"
File.exist?(config_file).should be_true
end
end
it "should describe config file" do
FakeFS do
invoke "configure"
$stdout.string.should == "Config file is at \"#{ENV['HOME']}/.timetrap.yml\"\n"
end
end
end
describe 'edit' do
before do
Timetrap::Timer.start "running entry", nil
end
it "should edit the description of the active period" do
Timetrap::Timer.active_entry.note.should == 'running entry'
invoke 'edit new description'
Timetrap::Timer.active_entry.note.should == 'new description'
end
it "should allow you to move an entry to another sheet" do
invoke 'edit --move blahblah'
Timetrap::Timer.active_entry[:sheet].should == 'blahblah'
invoke 'edit -m blahblahblah'
Timetrap::Timer.active_entry[:sheet].should == 'blahblahblah'
end
it "should change the current sheet if the current entry's sheet is changed" do
Timetrap::Timer.current_sheet.should_not == 'blahblahblah'
invoke 'edit -m blahblahblah'
Timetrap::Timer.active_entry[:sheet].should == 'blahblahblah'
Timetrap::Timer.current_sheet.should == 'blahblahblah'
end
it "should change the current sheet if a non current entry's sheet is changed" do
sheet = Timetrap::Timer.current_sheet
id = Timetrap::Timer.active_entry[:id]
invoke 'out'
invoke "edit -m blahblahblah -i #{id}"
Timetrap::Timer.current_sheet.should == sheet
Timetrap::Entry[id][:sheet].should == 'blahblahblah'
end
it "should allow appending to the description of the active period" do
with_stubbed_config('append_notes_delimiter' => '//')
Timetrap::Timer.active_entry.note.should == 'running entry'
invoke 'edit --append new'
Timetrap::Timer.active_entry.note.should == 'running entry//new'
invoke 'edit -z more'
Timetrap::Timer.active_entry.note.should == 'running entry//new//more'
end
it "should edit the start time of the active period" do
invoke 'edit --start "yesterday 10am"'
Timetrap::Timer.active_entry.start.should == Chronic.parse("yesterday 10am")
Timetrap::Timer.active_entry.note.should == 'running entry'
end
it "should edit the end time of the active period" do
entry = Timetrap::Timer.active_entry
invoke 'edit --end "yesterday 10am"'
entry.refresh.end.should == Chronic.parse("yesterday 10am")
entry.refresh.note.should == 'running entry'
end
it "should edit a non running entry based on id" do
not_running = Timetrap::Timer.active_entry
Timetrap::Timer.stop(Timetrap::Timer.current_sheet)
Timetrap::Timer.start "another entry", nil
# create a few more entries to ensure we're not falling back on "last
# checked out of" feature.
Timetrap::Timer.stop(Timetrap::Timer.current_sheet)
Timetrap::Timer.start "another entry", nil
Timetrap::Timer.stop(Timetrap::Timer.current_sheet)
Timetrap::Timer.start "another entry", nil
invoke "edit --id #{not_running.id} a new description"
not_running.refresh.note.should == 'a new description'
end
it "should edit the entry last checked out of if none is running" do
not_running = Timetrap::Timer.active_entry
Timetrap::Timer.stop(Timetrap::Timer.current_sheet)
invoke "edit -z 'a new description'"
not_running.refresh.note.should include 'a new description'
end
it "should edit the entry last checked out of if none is running even if the sheet is changed" do
not_running = Timetrap::Timer.active_entry
Timetrap::Timer.stop(Timetrap::Timer.current_sheet)
invoke "edit -z 'a new description'"
invoke "sheet another second sheet"
not_running.refresh.note.should include 'a new description'
not_running.refresh.sheet.should == 'default'
Timetrap::Timer.current_sheet.should == 'another second sheet'
end
end
describe 'auto_sheet' do
describe "using dotfiles auto_sheet" do
describe 'with a .timetrap-sheet in cwd' do
it 'should use sheet defined in dorfile' do
Dir.chdir('spec/dotfile') do
with_stubbed_config('auto_sheet' => 'dotfiles')
Timetrap::Timer.current_sheet.should == 'dotfile-sheet'
end
end
end
end
describe "using YamlCwd autosheet" do
describe 'with cwd in auto_sheet_paths' do
it 'should use sheet defined in config' do
with_stubbed_config(
'auto_sheet_paths' => {
'a sheet' => ['/not/cwd/', Dir.getwd]
}, 'auto_sheet' => 'yaml_cwd')
Timetrap::Timer.current_sheet.should == 'a sheet'
end
end
describe 'with ancestor of cwd in auto_sheet_paths' do
it 'should use sheet defined in config' do
with_stubbed_config(
'auto_sheet_paths' => {'a sheet' => '/'},
'auto_sheet' => 'yaml_cwd'
)
Timetrap::Timer.current_sheet.should == 'a sheet'
end
end
describe 'with cwd not in auto_sheet_paths' do
it 'should not use sheet defined in config' do
with_stubbed_config(
'auto_sheet_paths' => {
'a sheet' => '/not/the/current/working/directory/'
},'auto_sheet' => 'yaml_cwd')
Timetrap::Timer.current_sheet.should == 'default'
end
end
describe 'with cwd and ancestor in auto_sheet_paths' do
it 'should use the most specific config' do
with_stubbed_config(
'auto_sheet_paths' => {
'general sheet' => '/', 'more specific sheet' => Dir.getwd
}, 'auto_sheet' => 'yaml_cwd')
Timetrap::Timer.current_sheet.should == 'more specific sheet'
with_stubbed_config(
'auto_sheet_paths' => {
'more specific sheet' => Dir.getwd, 'general sheet' => '/'
}, 'auto_sheet' => 'yaml_cwd')
Timetrap::Timer.current_sheet.should == 'more specific sheet'
end
end
end
end
describe "backend" do
it "should open an sqlite console to the db" do
Timetrap::CLI.should_receive(:exec).with("psql #{Timetrap::DB_URL}")
invoke 'backend'
end
end
describe "format" do
before do
create_entry
end
it "should be deprecated" do
invoke 'format'
$stderr.string.should == <<-WARN
The "format" command is deprecated in favor of "display". Sorry for the inconvenience.
WARN
end
end
describe "display" do
describe "text" do
before do
Timetrap::Entry.create( :sheet => 'another',
:note => 'a long entry note', :start => '2008-10-05 18:00:00'
)
Timetrap::Entry.create( :sheet => 'SpecSheet',
:note => 'entry 2', :start => '2008-10-03 16:00:00', :end => '2008-10-03 18:00:00'
)
Timetrap::Entry.create( :sheet => 'SpecSheet',
:note => 'entry 1', :start => '2008-10-03 12:00:00', :end => '2008-10-03 14:00:00'
)
Timetrap::Entry.create( :sheet => 'SpecSheet',
:note => 'entry 3', :start => '2008-10-05 16:00:00', :end => '2008-10-05 18:00:00'
)
Timetrap::Entry.create( :sheet => 'SpecSheet',
:note => 'entry 4', :start => '2008-10-05 18:00:00'
)
Time.stub(:now).and_return local_time('2008-10-05 20:00:00')
@desired_output = <<-OUTPUT
Timesheet: SpecSheet
Day Start End Duration Notes
Fri Oct 03, 2008 12:00:00 - 14:00:00 2:00:00 entry 1
16:00:00 - 18:00:00 2:00:00 entry 2
4:00:00
Sun Oct 05, 2008 16:00:00 - 18:00:00 2:00:00 entry 3
18:00:00 - 2:00:00 entry 4
4:00:00
-----------------------------------------------------------
Total 8:00:00
OUTPUT
@desired_output_with_ids = <<-OUTPUT
Timesheet: SpecSheet
Id Day Start End Duration Notes
3 Fri Oct 03, 2008 12:00:00 - 14:00:00 2:00:00 entry 1
2 16:00:00 - 18:00:00 2:00:00 entry 2
4:00:00
4 Sun Oct 05, 2008 16:00:00 - 18:00:00 2:00:00 entry 3
5 18:00:00 - 2:00:00 entry 4
4:00:00
-----------------------------------------------------------
Total 8:00:00
OUTPUT
@desired_output_with_long_ids = <<-OUTPUT
Timesheet: SpecSheet
Id Day Start End Duration Notes
3 Fri Oct 03, 2008 12:00:00 - 14:00:00 2:00:00 entry 1
2 16:00:00 - 18:00:00 2:00:00 entry 2
4:00:00
40000 Sun Oct 05, 2008 16:00:00 - 18:00:00 2:00:00 entry 3
5 18:00:00 - 2:00:00 entry 4
4:00:00
-----------------------------------------------------------
Total 8:00:00
OUTPUT
@desired_output_for_all = <<-OUTPUT
Timesheet: SpecSheet
Day Start End Duration Notes
Fri Oct 03, 2008 12:00:00 - 14:00:00 2:00:00 entry 1
16:00:00 - 18:00:00 2:00:00 entry 2
4:00:00
Sun Oct 05, 2008 16:00:00 - 18:00:00 2:00:00 entry 3
18:00:00 - 2:00:00 entry 4
4:00:00
---------------------------------------------------------------------
Total 8:00:00
Timesheet: another
Day Start End Duration Notes
Sun Oct 05, 2008 18:00:00 - 2:00:00 a long entry note
2:00:00
---------------------------------------------------------------------
Total 2:00:00
-------------------------------------------------------------------------
Grand Total 10:00:00
OUTPUT
end
it "should display the current timesheet" do
Timetrap::Timer.current_sheet = 'SpecSheet'
invoke 'display'
$stdout.string.should == @desired_output
end
it "should display a non current timesheet" do
Timetrap::Timer.current_sheet = 'another'
invoke 'display SpecSheet'
$stdout.string.should == @desired_output
end
it "should display a non current timesheet based on a partial name match" do
Timetrap::Timer.current_sheet = 'another'
invoke 'display S'
$stdout.string.should == @desired_output
end
it "should prefer an exact match of a named sheet to a partial match" do
Timetrap::Timer.current_sheet = 'Spec'
Timetrap::Entry.create( :sheet => 'Spec',
:note => 'entry 5', :start => '2008-10-05 18:00:00'
)
invoke 'display Spec'
$stdout.string.should include("entry 5")
end
it "should display a timesheet with ids" do
invoke 'display S --ids'
$stdout.string.should == @desired_output_with_ids
end
it "should properly format a timesheet with long ids" do
Timetrap::DB["UPDATE entries SET id = 40000 WHERE id = 4"].all
invoke 'display S --ids'
$stdout.string.should == @desired_output_with_long_ids
end
it "should properly format a timesheet with no ids even if long ids are in the db" do
Timetrap::DB["UPDATE entries SET id = 40000 WHERE id = 4"].all
invoke 'display S'
$stdout.string.should == @desired_output
end
it "should display all timesheets" do
Timetrap::Timer.current_sheet = 'another'
invoke 'display all'
$stdout.string.should == @desired_output_for_all
end
it "should not display archived for all timesheets" do
$stdin.string = "yes\n"
invoke 'archive SpecSheet'
$stdout.string = ''
invoke 'display all'
$stdout.string.should_not =~ /_SpecSheet/
end
it "it should find a user provided formatter class and require it" do
create_entry
create_entry
dir = '/tmp/timetrap/foo/bar'
with_stubbed_config('formatter_search_paths' => dir)
FileUtils.mkdir_p(dir)
File.open(dir + '/baz.rb', 'w') do |f|
f.puts <<-RUBY
class Timetrap::Formatters::Baz
def initialize(entries); end
def output
"yeah I did it"
end
end
RUBY
end
invoke 'd -fbaz'
$stdout.string.should == "yeah I did it\n"
FileUtils.rm_r dir
end
it "should work when there's no note" do
Timetrap::Entry.create( :sheet => 'SpecSheet',
:note => nil
)
invoke 'd SpecSheet'
# check it doesn't error and produces valid looking output
$stdout.string.should include('Timesheet: SpecSheet')
end
end
describe "default" do
before do
create_entry(:start => '2008-10-03 12:00:00', :end => '2008-10-03 14:00:00')
create_entry(:start => '2008-10-05 12:00:00', :end => '2008-10-05 14:00:00')
end
it "should allow another formatter to be set as the default" do
with_stubbed_config 'default_formatter' => 'ids',
'formatter_search_paths' => nil
invoke 'd'
$stdout.string.should == Timetrap::Entry.all.map(&:id).join(" ") + "\n"
end
end
describe 'ids' do
before do
create_entry(:start => '2008-10-03 12:00:00', :end => '2008-10-03 14:00:00')
create_entry(:start => '2008-10-05 12:00:00', :end => '2008-10-05 14:00:00')
end
it "should not export running items" do
invoke 'in'
invoke 'display --format ids'
$stdout.string.should == Timetrap::Entry.all.map(&:id).join(" ") + "\n"
end
end
describe 'csv' do
before do
create_entry(:start => '2008-10-03 12:00:00', :end => '2008-10-03 14:00:00')
create_entry(:start => '2008-10-05 12:00:00', :end => '2008-10-05 14:00:00')
end
it "should not export running items" do
invoke 'in'
invoke 'display --format csv'
$stdout.string.should == <<-EOF
start,end,note,sheet
"2008-10-03 12:00:00","2008-10-03 14:00:00","note","default"
"2008-10-05 12:00:00","2008-10-05 14:00:00","note","default"
EOF
end
it "should filter events by the passed dates" do
invoke 'display --format csv --start 2008-10-03 --end 2008-10-03'
$stdout.string.should == <<-EOF
start,end,note,sheet
"2008-10-03 12:00:00","2008-10-03 14:00:00","note","default"
EOF
end
it "should not filter events by date when none are passed" do
invoke 'display --format csv'
$stdout.string.should == <<-EOF
start,end,note,sheet
"2008-10-03 12:00:00","2008-10-03 14:00:00","note","default"
"2008-10-05 12:00:00","2008-10-05 14:00:00","note","default"
EOF
end
end
describe 'json' do
before do
create_entry(:start => local_time_cli('2008-10-03 12:00:00'), :end => local_time_cli('2008-10-03 14:00:00'))
create_entry(:start => local_time_cli('2008-10-05 12:00:00'), :end => local_time_cli('2008-10-05 14:00:00'))
end
it "should export to json not including running items" do
invoke 'in'
invoke 'display -f json'
JSON.parse($stdout.string).should == JSON.parse(<<-EOF)
[{\"sheet\":\"default\",\"end\":\"#{local_time('2008-10-03 14:00:00')}\",\"start\":\"#{local_time('2008-10-03 12:00:00')}\",\"note\":\"note\",\"id\":1},{\"sheet\":\"default\",\"end\":\"#{local_time('2008-10-05 14:00:00')}\",\"start\":\"#{local_time('2008-10-05 12:00:00')}\",\"note\":\"note\",\"id\":2}]
EOF
end
end
describe 'ical' do
before do
create_entry(:start => local_time_cli('2008-10-03 12:00:00'), :end => local_time_cli('2008-10-03 14:00:00'))
create_entry(:start => local_time_cli('2008-10-05 12:00:00'), :end => local_time_cli('2008-10-05 14:00:00'))
end
it "should not export running items" do
invoke 'in'
invoke 'display --format ical'
$stdout.string.scan(/BEGIN:VEVENT/).should have(2).item
end
it "should filter events by the passed dates" do
invoke 'display --format ical --start 2008-10-03 --end 2008-10-03'
$stdout.string.scan(/BEGIN:VEVENT/).should have(1).item
end
it "should not filter events by date when none are passed" do
invoke 'display --format ical'
$stdout.string.scan(/BEGIN:VEVENT/).should have(2).item
end
it "should export a sheet to an ical format" do
invoke 'display --format ical --start 2008-10-03 --end 2008-10-03'
desired = <<-EOF
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
PRODID:iCalendar-Ruby
BEGIN:VEVENT
SEQUENCE:0
DTEND:20081003T140000
SUMMARY:note
DTSTART:20081003T120000
END:VEVENT
END:VCALENDAR
EOF
desired.each_line do |line|
$stdout.string.should =~ /#{line.chomp}/
end
end
end
end
describe "in" do
it "should start the time for the current timesheet" do
lambda do
invoke 'in'
end.should change(Timetrap::Entry, :count).by(1)
end
it "should set the note when starting a new entry" do
invoke 'in working on something'
Timetrap::Entry.order_by(:id).last.note.should == 'working on something'
end
it "should set the start when starting a new entry" do
@time = Time.now
Time.stub(:now).and_return @time
invoke 'in working on something'
Timetrap::Entry.order_by(:id).last.start.to_i.should == @time.to_i
end
it "should not start the time if the timetrap is running" do
Timetrap::Timer.stub(:running?).and_return true
lambda do
invoke 'in'
end.should_not change(Timetrap::Entry, :count)
end
it "should allow the sheet to be started at a certain time" do
invoke 'in work --at "10am 2008-10-03"'
Timetrap::Entry.order_by(:id).last.start.should == Time.parse('2008-10-03 10:00')
end
it "should fail with a warning for misformatted cli options it can't parse" do
now = Time.now
Time.stub(:now).and_return now
invoke 'in work --at="18 minutes ago"'
Timetrap::Entry.order_by(:id).last.should be_nil
$stderr.string.should =~ /\w+/
end
it "should fail with a time argurment of total garbage" do
now = Time.now
Time.stub(:now).and_return now
invoke 'in work --at "total garbage"'
Timetrap::Entry.order_by(:id).last.should be_nil
$stderr.string.should =~ /\w+/
end
describe "with require_note config option set" do
before do
with_stubbed_config 'require_note' => true
end
it "should prompt for a note if one isn't passed" do
$stdin.string = "an interactive note\n"
invoke "in"
$stderr.string.should include('enter a note')
Timetrap::Timer.active_entry.note.should == "an interactive note"
end
it "should not prompt for a note if one is passed" do
$stdin.string = "an interactive note\n"
invoke "in a normal note"
Timetrap::Timer.active_entry.note.should == "a normal note"
end
it "should not stop the running entry or prompt" do
invoke "in a normal note"
$stdin.string = "an interactive note\n"
invoke "in"
Timetrap::Timer.active_entry.note.should == "a normal note"
end
end
describe "with auto_checkout config option set" do
before do
with_stubbed_config 'auto_checkout' => true
end
it "should check in normally if nothing else is running" do
Timetrap::Timer.should_not be_running #precondition
invoke 'in'
Timetrap::Timer.should be_running
end
describe "with a running entry on current sheet" do
before do
invoke 'sheet sheet1'
invoke 'in first task'
end
it "should check out and back in" do
entry = Timetrap::Timer.active_entry('sheet1')
invoke 'in second task'
Timetrap::Timer.active_entry('sheet1').note.should == 'second task'
end
it "should tell me what it's doing" do
invoke 'in second task'
$stderr.string.should include "Checked out"
end
end
describe "with a running entry on another sheet" do
before do
invoke 'sheet sheet1'
invoke 'in first task'
invoke 'sheet sheet2'
end
it "should check out of the running entry" do
Timetrap::Timer.active_entry('sheet1').should be_a(Timetrap::Entry)
invoke 'in second task'
Timetrap::Timer.active_entry('sheet1').should be nil
end
it "should check out of the running entry at another time" do
now = Time.at(Time.now - 5 * 60) # 5 minutes ago
entry = Timetrap::Timer.active_entry('sheet1')
entry.should be_a(Timetrap::Entry)
invoke "in -a '#{now}' second task"
entry.reload.end.to_s.should == now.to_s
end
it "should check out of the running entry without having to start a new entry" do
entry = Timetrap::Timer.active_entry('sheet1')
entry.should be_a(Timetrap::Entry)
entry.end.should be_nil
invoke "out"
entry.reload.end.should_not be_nil
end
end
end
end
describe "today" do
it "should only show entries for today" do
yesterday = Time.now - (24 * 60 * 60)
create_entry(
:start => yesterday,
:end => yesterday
)
create_entry
invoke 'today'
$stdout.string.should include Time.now.strftime('%a %b %d, %Y')
$stdout.string.should_not include yesterday.strftime('%a %b %d, %Y')
end
end
describe "yesterday" do
it "should only show entries for yesterday" do
yesterday = Time.now - (24 * 60 * 60)
create_entry(
:start => yesterday,
:end => yesterday
)
create_entry
invoke 'yesterday'
$stdout.string.should include yesterday.strftime('%a %b %d, %Y')
$stdout.string.should_not include Time.now.strftime('%a %b %d, %Y')
end
end
describe "week" do
it "should only show entries from this week" do
create_entry(
:start => Time.local(2012, 2, 1, 1, 2, 3),
:end => Time.local(2012, 2, 1, 2, 2, 3)
)
create_entry
invoke 'week'
$stdout.string.should include Time.now.strftime('%a %b %d, %Y')
$stdout.string.should_not include 'Feb 01, 2012'
end
end
describe "month" do
it "should display all entries for the month" do
create_entry(
:start => Time.local(2012, 2, 5, 1, 2, 3),
:end => Time.local(2012, 2, 5, 2, 2, 3)
)
create_entry(
:start => Time.local(2012, 2, 6, 1, 2, 3),
:end => Time.local(2012, 2, 6, 2, 2, 3)
)
create_entry(
:start => Time.local(2012, 1, 5, 1, 2, 3),
:end => Time.local(2012, 1, 5, 2, 2, 3)
)
Date.should_receive(:today).and_return(Date.new(2012, 2, 5))
invoke "month"
$stdout.string.should include 'Feb 05, 2012'
$stdout.string.should include 'Feb 06, 2012'
$stdout.string.should_not include 'Jan'
end
it "should work in December" do
create_entry(
:start => Time.local(2012, 12, 5, 1, 2, 3),
:end => Time.local(2012, 12, 5, 2, 2, 3)
)
Date.should_receive(:today).and_return(Date.new(2012, 12, 5))
invoke "month"
$stdout.string.should include 'Wed Dec 05, 2012 01:02:03 - 02:02:03'
end
end
describe "kill" do
it "should give me a chance not to fuck up" do
entry = create_entry
lambda do
$stdin.string = ""
invoke "kill #{entry.sheet}"
end.should_not change(Timetrap::Entry, :count).by(-1)
end
it "should delete a timesheet" do
create_entry
entry = create_entry
lambda do
$stdin.string = "yes\n"
invoke "kill #{entry.sheet}"
end.should change(Timetrap::Entry, :count).by(-2)
end
it "should delete an entry" do
create_entry
entry = create_entry
lambda do
$stdin.string = "yes\n"
invoke "kill --id #{entry.id}"
end.should change(Timetrap::Entry, :count).by(-1)
end
it "should not prompt the user if the --yes flag is passed" do
create_entry
entry = create_entry
lambda do
invoke "kill --id #{entry.id} --yes"
end.should change(Timetrap::Entry, :count).by(-1)
end
describe "with a numeric sheet name" do
before do
Time.stub(:now).and_return local_time("2008-10-05 18:00:00")
create_entry( :sheet => 1234, :start => local_time_cli('2008-10-03 12:00:00'),
:end => local_time_cli('2008-10-03 14:00:00'))
end
it "should kill the sheet" do
lambda do
invoke 'kill -y 1234'
end.should change(Timetrap::Entry, :count).by(-1)
end
end
end
describe "list" do
describe "with no sheets defined" do
it "should list the default sheet" do
invoke 'list'
$stdout.string.chomp.should == " Timesheet Running Today Total Time\n*default 0:00:00 0:00:00 0:00:00"
end
end
describe "with a numeric sheet name" do
before do
Time.stub(:now).and_return local_time("2008-10-05 18:00:00")
create_entry( :sheet => '1234', :start => local_time_cli('2008-10-03 12:00:00'),
:end => local_time_cli('2008-10-03 14:00:00'))
end
it "should list the sheet" do
invoke 'list'
$stdout.string.should == " Timesheet Running Today Total Time\n 1234 0:00:00 0:00:00 2:00:00\n*default 0:00:00 0:00:00 0:00:00\n"
end
end
describe "with a numeric current_sheet" do
before do
Timetrap::Timer.current_sheet = '1234'
end
it "should list the sheet" do
invoke 'list'
$stdout.string.should == " Timesheet Running Today Total Time\n*1234 0:00:00 0:00:00 0:00:00\n"
end
end
describe "with sheets defined" do
before :each do
Time.stub(:now).and_return local_time("2008-10-05 18:00:00")
create_entry( :sheet => 'A Longly Named Sheet 2', :start => local_time_cli('2008-10-03 12:00:00'),
:end => local_time_cli('2008-10-03 14:00:00'))
create_entry( :sheet => 'A Longly Named Sheet 2', :start => local_time_cli('2008-10-03 12:00:00'),
:end => local_time_cli('2008-10-03 14:00:00'))
create_entry( :sheet => 'A Longly Named Sheet 2', :start => local_time_cli('2008-10-05 12:00:00'),
:end => local_time_cli('2008-10-05 14:00:00'))
create_entry( :sheet => 'A Longly Named Sheet 2', :start => local_time_cli('2008-10-05 14:00:00'),
:end => nil)
create_entry( :sheet => 'Sheet 1', :start => local_time_cli('2008-10-03 16:00:00'),
:end => local_time_cli('2008-10-03 18:00:00'))
Timetrap::Timer.current_sheet = 'A Longly Named Sheet 2'
end
it "should list available timesheets" do
invoke 'list'
$stdout.string.should == <<-OUTPUT
Timesheet Running Today Total Time
*A Longly Named Sheet 2 4:00:00 6:00:00 10:00:00
Sheet 1 0:00:00 0:00:00 2:00:00
OUTPUT
end
it "should mark the last sheet with '-' if it exists" do
invoke 'sheet Sheet 1'
$stdout.string = ''
invoke 'list'
$stdout.string.should == <<-OUTPUT
Timesheet Running Today Total Time
-A Longly Named Sheet 2 4:00:00 6:00:00 10:00:00
*Sheet 1 0:00:00 0:00:00 2:00:00
OUTPUT
end
it "should not mark the last sheet with '-' if it doesn't exist" do
invoke 'sheet Non-existent'
invoke 'sheet Sheet 1'
$stdout.string = ''
invoke 'list'
$stdout.string.should == <<-OUTPUT
Timesheet Running Today Total Time
A Longly Named Sheet 2 4:00:00 6:00:00 10:00:00
*Sheet 1 0:00:00 0:00:00 2:00:00
OUTPUT
end
it "should include the active timesheet even if it has no entries" do
invoke 'sheet empty sheet'
$stdout.string = ''
invoke 'list'
$stdout.string.should == <<-OUTPUT
Timesheet Running Today Total Time
-A Longly Named Sheet 2 4:00:00 6:00:00 10:00:00
*empty sheet 0:00:00 0:00:00 0:00:00
Sheet 1 0:00:00 0:00:00 2:00:00
OUTPUT
end
end
end
describe "now" do
before do
Timetrap::Timer.current_sheet = 'current sheet'
end
describe "when the current timesheet isn't running" do
it "should show that it isn't running" do
invoke 'now'
$stderr.string.should == <<-OUTPUT
*current sheet: not running
OUTPUT
end
end
describe "when the current timesheet is running" do
before do
invoke 'in a timesheet that is running'
@entry = Timetrap::Timer.active_entry
@entry.start = Time.at(0)
@entry.save
Time.stub(:now).and_return Time.at(60)
end
it "should show how long the current item is running for" do
invoke 'now'
$stdout.string.should == <<-OUTPUT
*current sheet: 0:01:00 (a timesheet that is running)
OUTPUT
end
describe "and another timesheet is running too" do
before do
invoke 'sheet another-sheet'
invoke 'in also running'
@entry = Timetrap::Timer.active_entry
@entry.start = Time.at(0)
@entry.save
Time.stub(:now).and_return Time.at(60)
end
it "should show both entries" do
invoke 'now'
$stdout.string.should == <<-OUTPUT
current sheet: 0:01:00 (a timesheet that is running)
*another-sheet: 0:01:00 (also running)
OUTPUT
end
end
end