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
|
#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = PostRunner_spec.rb -- PostRunner - Manage the data from your Garmin sport devices.
#
# Copyright (c) 2014 by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
require 'fileutils'
require 'postrunner/Main'
require 'spec_helper'
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
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 the other FIT file' do
postrunner([ 'import', '.' ])
list = postrunner(%w( list ))
list.index('FILE1.FIT').should be_a(Fixnum)
list.index('FILE2.FIT').should be_a(Fixnum)
rc = YAML::load_file(File.join(@db_dir, 'config.yml'))
rc[:import_dir].should == '.'
template = "<a href=\"%s.html\"><img src=\"icons/%s.png\" " +
"class=\"active_button\">"
html1 = File.read(File.join(@db_dir, 'html', 'FILE1.html'))
html1.include?(template % ['FILE2', 'forward']).should be_true
html2 = File.read(File.join(@db_dir, 'html', 'FILE2.html'))
html2.include?(template % ['FILE1', 'back']).should be_true
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 foobar :1 ))
list = postrunner(%w( list ))
list.index('FILE2.FIT').should be_nil
list.index('foobar').should be_a(Fixnum)
end
it 'should fail when setting bad attribute' do
lambda { postrunner(%w( set foo bar :1)) }.should raise_error SystemExit
end
it 'should set name for FILE2.FIT activity' do
postrunner(%w( set name foobar :1 ))
list = postrunner(%w( list ))
list.index('FILE2.FIT').should be_nil
list.index('foobar').should be_a(Fixnum)
end
it 'should set activity type for FILE2.FIT activity' do
postrunner(%w( set type Cycling :1 ))
list = postrunner(%w( summary :1 ))
list.index('Running').should be_nil
list.index('Cycling').should be_a(Fixnum)
end
it 'should fail when setting bad activity type' do
lambda { postrunner(%w( set type foobar :1)) }.should raise_error SystemExit
end
it 'should set activity subtype for FILE2.FIT activity' do
postrunner(%w( set subtype Road :1 ))
list = postrunner(%w( summary :1 ))
list.index('Generic').should be_nil
list.index('Road').should be_a(Fixnum)
end
it 'should fail when setting bad activity subtype' do
lambda { postrunner(%w( set subtype foobar :1)) }.should raise_error SystemExit
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
it 'should switch to statute units' do
postrunner(%w( units statute ))
end
it 'should switch back to metric units' do
postrunner(%w( units metric ))
end
it 'should properly upgrade to a new version' do
# Change version in config file to 0.0.0.
rc = PostRunner::RuntimeConfig.new(@db_dir)
rc.set_option(:version, '0.0.0')
# Check that the config file really was changed.
rc = PostRunner::RuntimeConfig.new(@db_dir)
rc.get_option(:version).should == '0.0.0'
archive_file = File.join(@db_dir, 'archive.yml')
archive = YAML.load_file(archive_file)
archive.each { |a| a.remove_instance_variable:@sport }
File.write(archive_file, archive.to_yaml)
# Run some command.
postrunner(%w( list ))
# Check that version matches the current version again.
rc = PostRunner::RuntimeConfig.new(@db_dir)
rc.get_option(:version).should == PostRunner::VERSION
end
end
|