summaryrefslogtreecommitdiff
path: root/test/PostRunner_spec.rb
blob: 1c803e9681cd4b295a3ba06608bdbf6eb1379870 (plain)
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
require 'fileutils'

require 'postrunner/Main'

describe PostRunner::Main do

  def postrunner(args)
    args = [ '--dbdir', @db_dir ] + args
    old_stdout = $stdout
    $stdout = (stdout = StringIO.new)
    PostRunner::Main.new(args)
    $stdout = old_stdout
    stdout.string
  end

  def create_fit_file(name, date)
    a = Fit4Ruby::Activity.new
    a.start_time = Time.parse(date)
    a.duration = 30 * 60
    Fit4Ruby.write(name, a)
  end

  before(:all) do
    @db_dir = File.join(File.dirname(__FILE__), '.postrunner')
    FileUtils.rm_rf(@db_dir)
    FileUtils.rm_rf('FILE1.FIT')
    create_fit_file('FILE1.FIT', '2014-07-01-8:00')
    create_fit_file('FILE2.FIT', '2014-07-02-8:00')
  end

  after(:all) do
    FileUtils.rm_rf(@db_dir)
    FileUtils.rm_rf('FILE1.FIT')
    FileUtils.rm_rf('FILE2.FIT')
  end

  it 'should abort without arguments' do
    lambda { postrunner([]) }.should raise_error SystemExit
  end

  it 'should abort with bad command' do
    lambda { postrunner(%w( foobar)) }.should raise_error SystemExit
  end

  it 'should support the -v option' do
    postrunner(%w( --version ))
  end

  it 'should check a FIT file' do
    postrunner(%w( check FILE1.FIT ))
  end

  it 'should list and empty archive' do
    postrunner(%w( list ))
  end

  it 'should import a FIT file' do
    postrunner(%w( import FILE1.FIT ))
  end

  it 'should check the imported file' do
    postrunner(%w( check :1 ))
  end

  it 'should check a FIT file' do
    postrunner(%w( check FILE2.FIT ))
  end

  it 'should list the imported file' do
    postrunner(%w( list )).index('FILE1.FIT').should be_a(Fixnum)
  end

  it 'should import another FIT file' do
    postrunner(%w( import FILE2.FIT ))
    list = postrunner(%w( list ))
    list.index('FILE1.FIT').should be_a(Fixnum)
    list.index('FILE2.FIT').should be_a(Fixnum)
  end

  it 'should delete the first file' do
    postrunner(%w( delete :2 ))
    list = postrunner(%w( list ))
    list.index('FILE1.FIT').should be_nil
    list.index('FILE2.FIT').should be_a(Fixnum)
  end

  it 'should not import the deleted file again' do
    postrunner(%w( import . ))
    list = postrunner(%w( list ))
    list.index('FILE1.FIT').should be_nil
    list.index('FILE2.FIT').should be_a(Fixnum)
  end

  it 'should rename FILE2.FIT activity' do
    postrunner(%w( rename :1 --name foobar ))
    list = postrunner(%w( list ))
    list.index('FILE2.FIT').should be_nil
    list.index('foobar').should be_a(Fixnum)
  end

  it 'should dump an activity from the archive' do
    postrunner(%w( dump :1 ))
  end

  it 'should dump a FIT file' do
    postrunner(%w( dump FILE1.FIT ))
  end

end