oughtve 110.e2f5bb28

Sign up to get free protection for your applications and to get access to all the features.
data/spec/spec.rb ADDED
@@ -0,0 +1,87 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ # The spec files are designed to be run one at a time
4
+ # because it removes the necessity of completely tearing
5
+ # down the DB bootstrap which is *exceedingly* difficult in
6
+ # any ORM.
7
+
8
+ require "fileutils"
9
+ require "tmpdir"
10
+
11
+ specdir = File.expand_path "#{File.dirname __FILE__}"
12
+ dummy_dir = File.join specdir, "/dummy_home_dir_#{$$}"
13
+
14
+ FileUtils.rm_r dummy_dir, :secure => true rescue nil
15
+
16
+
17
+ # Raw output?
18
+ raw = if ARGV.first == "--raw"
19
+ ARGV.shift
20
+ true
21
+ end
22
+
23
+ # Split any files from options to pass to the spec program
24
+ split = ARGV.index "--"
25
+
26
+ opts = if split
27
+ ARGV.slice!((split + 1)..-1).join ' '
28
+ else
29
+ '-f s'
30
+ end
31
+
32
+ files = if ARGV.empty?
33
+ Dir.glob "#{specdir}/*_spec.rb"
34
+ else
35
+ ARGV.pop if split
36
+ ARGV.map {|f| "#{specdir}/#{File.basename(f)}" }
37
+ end
38
+
39
+ begin
40
+ real_home = ENV['HOME']
41
+ ENV['HOME'] = dummy_dir
42
+
43
+ start = Time.now
44
+ examples, fails, pending = 0, 0, 0
45
+
46
+ files.each do |spec|
47
+ FileUtils.mkdir dummy_dir
48
+
49
+ puts "\n\n ***********************************"
50
+ puts "\n #{spec}:"
51
+ puts "\n ***********************************\n"
52
+
53
+ if raw
54
+ pid = fork {
55
+ exec "spec", opts, File.expand_path(spec)
56
+ }
57
+
58
+ Process.waitpid pid
59
+ else
60
+ output = `spec #{opts} #{File.expand_path(spec)}`
61
+
62
+ stats = output.scan(/(\d+) examples?, (\d+) failures?(?:, (\d+) pending)?/).first
63
+
64
+ examples += stats[0].to_i
65
+ fails += stats[1].to_i
66
+ pending += stats[2].to_i if stats[2]
67
+
68
+
69
+ puts output
70
+ end
71
+
72
+ FileUtils.rm_r dummy_dir, :secure => true rescue nil
73
+ end
74
+
75
+ unless raw
76
+ puts "\n\n========================\n"
77
+ puts " Totals: #{fails} failures out of #{examples} examples."
78
+ puts " #{pending} marked pending."
79
+ puts " Time: #{Time.now - start}s\n"
80
+ puts
81
+ end
82
+
83
+ ensure
84
+ FileUtils.rm_r dummy_dir, :secure => true rescue nil
85
+ ENV['HOME'] = real_home
86
+ end
87
+
@@ -0,0 +1,9 @@
1
+ # Ensure this current set is before any installed ones
2
+ #
3
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
4
+
5
+ require "oughtve"
6
+
7
+ # Sandbox, see spec.rb.
8
+ Oughtve.run %w[ --bootstrap ]
9
+
@@ -0,0 +1,150 @@
1
+ require File.join File.dirname(__FILE__), "spec_helper"
2
+
3
+ describe Oughtve, "marking notes done or closed with --strike using the note's ID" do
4
+
5
+ before :each do
6
+ Oughtve.run %w[ --new --tangent tangy --directory /tmp ]
7
+ Oughtve.run %w[ --scribe --tangent tangy Hi bob! ]
8
+ Oughtve.run %w[ --scribe --tangent tangy Hi Mike! ]
9
+ Oughtve.run %w[ --scribe --tangent tangy Hi james! ]
10
+ end
11
+
12
+ after :each do
13
+ Oughtve::Tangent.all(:name.not => "default").each {|t| t.destroy }
14
+ end
15
+
16
+ it "it sets the 'stricken' field to the time stricken" do
17
+ verse = Oughtve::Tangent.first(:name => "tangy").current_chapter.verses.last
18
+
19
+ (!!verse.struck).should == false
20
+
21
+ before = Time.now.to_f
22
+ Oughtve.run "--strike --id #{verse.id}".split(" ")
23
+ after = Time.now.to_f
24
+
25
+ verse = Oughtve::Tangent.first(:name => "tangy").current_chapter.verses.last
26
+
27
+ (before..after).should include(verse.struck.to_f)
28
+ end
29
+
30
+ it "raises if the ID does not exist" do
31
+ lambda { Oughtve.run %w[ --strike --id 100 ] }.should raise_error
32
+ end
33
+
34
+ end
35
+
36
+ describe Oughtve, "marking notes done or closed with --strike using text matching" do
37
+
38
+ before :each do
39
+ Oughtve.run %w[ --new --tangent tangy --directory /tmp ]
40
+ Oughtve.run %w[ --scribe --tangent tangy Hi bob! ]
41
+ Oughtve.run %w[ --scribe --tangent tangy Hi Mike! ]
42
+ Oughtve.run %w[ --scribe --tangent tangy Hi james! ]
43
+ end
44
+
45
+ after :each do
46
+ Oughtve::Tangent.all(:name.not => "default").each {|t| t.destroy }
47
+ end
48
+
49
+ it "sets the stricken field if given text is an unambiguous regexp match to an open note in current tangent" do
50
+ verse = Oughtve::Tangent.first(:name => "tangy").current_chapter.verses.last
51
+
52
+ (!!verse.struck).should == false
53
+
54
+ before = Time.now.to_f
55
+ Oughtve.run %w[--strike --tangent tangy --match Hi\ j ]
56
+ after = Time.now.to_f
57
+
58
+ verse = Oughtve::Tangent.first(:name => "tangy").current_chapter.verses.last
59
+
60
+ (before..after).should include(verse.struck.to_f)
61
+ end
62
+
63
+ it "raises if Note cannot be matched" do
64
+ lambda {
65
+ Oughtve.run %w[--strike --tangent tangy --match hi\ james ]
66
+ }.should raise_error
67
+ end
68
+
69
+ it "raises if Note cannot be matched unambiguously" do
70
+ lambda {
71
+ Oughtve.run %w[--strike --tangent tangy --match Hi ]
72
+ }.should raise_error
73
+ end
74
+
75
+ end
76
+
77
+ describe Oughtve, "striking notes with parameter directly to --strike" do
78
+
79
+ before :each do
80
+ Oughtve.run %w[ --new --tangent tangy --directory /tmp ]
81
+ Oughtve.run %w[ --scribe --tangent tangy Hi bob! ]
82
+ Oughtve.run %w[ --scribe --tangent tangy Hi Mike! ]
83
+ Oughtve.run %w[ --scribe --tangent tangy Hi james! ]
84
+ end
85
+
86
+ after :each do
87
+ Oughtve::Tangent.all(:name.not => "default").each {|t| t.destroy }
88
+ end
89
+
90
+ it "uses parameter as Integer when possible" do
91
+ verse = Oughtve::Tangent.first(:name => "tangy").current_chapter.verses.last
92
+
93
+ (!!verse.struck).should == false
94
+
95
+ before = Time.now.to_f
96
+ Oughtve.run %W[--strike #{verse.id} --tangent tangy ]
97
+ after = Time.now.to_f
98
+
99
+ verse = Oughtve::Tangent.first(:name => "tangy").current_chapter.verses.last
100
+
101
+ (before..after).should include(verse.struck.to_f)
102
+ end
103
+
104
+ it "uses parameter as regular expression if it is not an Integer" do
105
+ verse = Oughtve::Tangent.first(:name => "tangy").current_chapter.verses.last
106
+
107
+ (!!verse.struck).should == false
108
+
109
+ before = Time.now.to_f
110
+ Oughtve.run %w[ --strike Hi\ j --tangent tangy ]
111
+ after = Time.now.to_f
112
+
113
+ verse = Oughtve::Tangent.first(:name => "tangy").current_chapter.verses.last
114
+
115
+ (before..after).should include(verse.struck.to_f)
116
+ end
117
+
118
+ end
119
+
120
+ describe Oughtve, "marking notes done or closed with --strike" do
121
+
122
+ before :each do
123
+ Oughtve.run %w[ --new --tangent tangy --directory /tmp ]
124
+ Oughtve.run %w[ --scribe --tangent tangy Hi bob! ]
125
+ Oughtve.run %w[ --scribe --tangent tangy Hi Mike! ]
126
+ Oughtve.run %w[ --scribe --tangent tangy Hi james! ]
127
+ end
128
+
129
+ after :each do
130
+ Oughtve::Tangent.all(:name.not => "default").each {|t| t.destroy }
131
+ end
132
+
133
+ it "raises if note already stricken" do
134
+ verse = Oughtve::Tangent.first(:name => "tangy").current_chapter.verses.last
135
+
136
+ (!!verse.struck).should == false
137
+
138
+ before = Time.now.to_f
139
+ Oughtve.run %w[--strike --tangent tangy --match Hi\ j ]
140
+ after = Time.now.to_f
141
+
142
+ verse = Oughtve::Tangent.first(:name => "tangy").current_chapter.verses.last
143
+
144
+ (before..after).should include(verse.struck.to_f)
145
+
146
+ lambda { Oughtve.run %w[ --strike --tangent tangy --match Hi\ j ] }.should raise_error
147
+ end
148
+
149
+ end
150
+
@@ -0,0 +1,63 @@
1
+ require File.join File.dirname(__FILE__), "spec_helper"
2
+
3
+ require "fileutils"
4
+ require "ostruct"
5
+
6
+
7
+ describe Oughtve, "locating tangents" do
8
+ before :all do
9
+ @specdir = File.expand_path "#{Dir.pwd}/spec"
10
+ @one_up = File.expand_path "#{Dir.pwd}/.."
11
+ @two_up = File.expand_path "#{Dir.pwd}/../.."
12
+ @three_up = File.expand_path "#{Dir.pwd}/../../.."
13
+ @four_up = File.expand_path "#{Dir.pwd}/../../../.."
14
+
15
+ Oughtve.run "--new --tangent one_down --directory #{@one_up}".split /\s/
16
+ Oughtve.run "--new --tangent three_down --directory #{@three_up}".split /\s/
17
+ end
18
+
19
+ it "uses current directory's tangent if there is one" do
20
+ Dir.chdir @one_up do
21
+ Oughtve.tangent_for(OpenStruct.new :dir => Dir.pwd).name.should == "one_down"
22
+ end
23
+
24
+ Dir.chdir @three_up do
25
+ Oughtve.tangent_for(OpenStruct.new :dir => Dir.pwd).name.should == "three_down"
26
+ end
27
+ end
28
+
29
+ it "climbs straight up directory hierarchy until finding directory with a tangent" do
30
+ Dir.chdir @specdir do
31
+ Oughtve.tangent_for(OpenStruct.new :dir => Dir.pwd).name.should == "one_down"
32
+ end
33
+
34
+ Dir.chdir @two_up do
35
+ Oughtve.tangent_for(OpenStruct.new :dir => Dir.pwd).name.should == "three_down"
36
+ end
37
+ end
38
+
39
+ it "falls back on default at / if no others match along the way" do
40
+ Dir.chdir @four_up do
41
+ Oughtve.tangent_for(OpenStruct.new :dir => Dir.pwd).name.should == "default"
42
+ end
43
+ end
44
+
45
+ it "by default only checks by name if name given even if tangent exists for path" do
46
+ Oughtve.tangent_for(OpenStruct.new :dir => @one_up, :name => "moo").should == nil
47
+ end
48
+
49
+ it "checks path if nonexisting name given if forced with second parameter" do
50
+ Oughtve.tangent_for(OpenStruct.new(:dir => @one_up, :name => "moo"), :check_path).name.should == "one_down"
51
+ end
52
+
53
+ it "raises if both name and directory given but do not match the same one" do
54
+ lambda {
55
+ Oughtve.tangent_for(OpenStruct.new :dir => @one_up, :name => "three_down")
56
+ }.should raise_error
57
+
58
+ lambda {
59
+ Oughtve.tangent_for(OpenStruct.new(:dir => @one_up, :name => "three_down"), :check_path)
60
+ }.should raise_error
61
+ end
62
+
63
+ end
@@ -0,0 +1,211 @@
1
+ require File.join File.dirname(__FILE__), "spec_helper"
2
+
3
+
4
+ describe Oughtve, "creating a new Tangent with --tangent when directory given" do
5
+
6
+ after :each do
7
+ Oughtve::Tangent.all(:dir.not => "/").each {|t| t.destroy }
8
+ end
9
+
10
+ it "creates a new Tangent for the specified directory path" do
11
+ Oughtve::Tangent.all(:dir.not => "/").size.should == 0
12
+
13
+ Oughtve.run %w[ --new --directory /usr ]
14
+
15
+ tangents = Oughtve::Tangent.all :dir.not => "/"
16
+ tangents.size.should == 1
17
+ tangents.first.dir.should == "/usr"
18
+ end
19
+
20
+ it "does not create a Tangent for the current directory" do
21
+ Oughtve::Tangent.all(:dir.not => "/").size.should == 0
22
+
23
+ Oughtve.run %w[ --new --directory /usr ]
24
+
25
+ Oughtve::Tangent.all(:dir => File.dirname(__FILE__)).size.should == 0
26
+ end
27
+
28
+ it "expands the directory name given" do
29
+ Oughtve::Tangent.all(:dir.not => "/").size.should == 0
30
+
31
+ Oughtve.run %w[ --new --tangent two --dir ./ ]
32
+
33
+ tangents = Oughtve::Tangent.all :dir.not => "/"
34
+ tangents.size.should == 1
35
+ tangents.first.dir.should == Dir.pwd
36
+ tangents.first.name.should == "two"
37
+ end
38
+
39
+ it "raises if the directory does not exist" do
40
+ lambda { Oughtve.run %w[ --new --dir /uggabugga ] }.should raise_error(ArgumentError)
41
+ end
42
+
43
+ end
44
+
45
+ describe Oughtve, "creating a new Tangent with --tangent when name given" do
46
+
47
+ after :each do
48
+ Oughtve::Tangent.all(:dir.not => "/").each {|t| t.destroy }
49
+ end
50
+
51
+ it "stores the given name for the Tangent" do
52
+ Oughtve::Tangent.all(:dir.not => "/").size.should == 0
53
+
54
+ Oughtve.run %w[ --new --tangent tangy\ toobs --directory /tmp ]
55
+
56
+ tangents = Oughtve::Tangent.all :dir.not => "/"
57
+ tangents.size.should == 1
58
+ tangents.first.dir.should == "/tmp"
59
+ tangents.first.name.should == "tangy toobs"
60
+ end
61
+
62
+ end
63
+
64
+ describe Oughtve, "creating a new Tangent with --tangent without a name" do
65
+
66
+ after :each do
67
+ Oughtve::Tangent.all(:dir.not => "/").each {|t| t.destroy }
68
+ end
69
+
70
+ it "uses the path as the Tangent's name also" do
71
+ Oughtve::Tangent.all(:dir.not => "/").size.should == 0
72
+
73
+ Oughtve.run %w[ --new --directory /tmp ]
74
+
75
+ tangents = Oughtve::Tangent.all :dir.not => "/"
76
+ tangents.size.should == 1
77
+ tangents.first.dir.should == "/tmp"
78
+ tangents.first.name.should == "/tmp"
79
+ end
80
+
81
+ end
82
+
83
+ describe Oughtve, "creating a new Tangent" do
84
+
85
+ before :all do
86
+ @dummyroot = "/tmp/oughtve_tangent_spec_#{$$}_#{Time.now.to_i}"
87
+ @dummyhigher = File.join @dummyroot, "first"
88
+ @dummylower = File.join @dummyhigher, "second"
89
+
90
+ FileUtils.mkdir_p @dummylower
91
+ end
92
+
93
+ after :each do
94
+ Oughtve::Tangent.all(:dir.not => "/").each {|t| t.destroy }
95
+ $stderr = STDERR
96
+ end
97
+
98
+ after :all do
99
+ FileUtils.rm_r @dummyroot, :secure => true
100
+ end
101
+
102
+ it "uses current directory as the path if none is supplied" do
103
+ Oughtve::Tangent.all(:dir.not => "/").size.should == 0
104
+
105
+ Oughtve.run %w[ --new --tangent hi\ there ]
106
+
107
+ tangents = Oughtve::Tangent.all :dir.not => "/"
108
+ tangents.size.should == 1
109
+ tangents.first.dir.should == Dir.pwd
110
+ tangents.first.name.should == "hi there"
111
+ end
112
+
113
+ it "raises if a Tangent already exists for this exact path" do
114
+ Oughtve.run %w[ --new --tangent hi ]
115
+
116
+ tangents = Oughtve::Tangent.all :dir.not => "/"
117
+ tangents.size.should == 1
118
+ tangents.first.dir.should == Dir.pwd
119
+ tangents.first.name.should == "hi"
120
+
121
+ lambda { Oughtve.run %w[ --new --tangent hola ] }.should raise_error
122
+ end
123
+
124
+ it "raises if a Tangent already exists with the same name" do
125
+ Oughtve.run %w[ --new --tangent hi --directory /tmp ]
126
+
127
+ tangents = Oughtve::Tangent.all :dir.not => "/"
128
+ tangents.size.should == 1
129
+ tangents.first.dir.should == "/tmp"
130
+ tangents.first.name.should == "hi"
131
+
132
+ lambda { Oughtve.run %w[ --new --tangent hi ] }.should raise_error
133
+ end
134
+
135
+ it "can create a new Tangent below an existing one in the file hierarchy" do
136
+ Oughtve.run %W[ --new --tangent hi --directory #{@dummyhigher} ]
137
+
138
+ tangents = Oughtve::Tangent.all :dir.not => "/"
139
+ tangents.size.should == 1
140
+ tangents.first.dir.should == @dummyhigher
141
+ tangents.first.name.should == "hi"
142
+
143
+ Oughtve.run %W[ --new --tangent ho --directory #{@dummylower} ]
144
+
145
+ tangents = Oughtve::Tangent.all :dir.not => "/"
146
+ tangents.size.should == 2
147
+
148
+ tangents.first.dir.should == @dummyhigher
149
+ tangents.first.name.should == "hi"
150
+
151
+ tangents.last.dir.should == @dummylower
152
+ tangents.last.name.should == "ho"
153
+ end
154
+
155
+ it "can create a new Tangent above an existing one in the file hierarchy" do
156
+ Oughtve.run %W[ --new --tangent hi --directory #{@dummylower} ]
157
+
158
+ tangents = Oughtve::Tangent.all :dir.not => "/"
159
+ tangents.size.should == 1
160
+ tangents.first.dir.should == @dummylower
161
+ tangents.first.name.should == "hi"
162
+
163
+ Oughtve.run %W[ --new --tangent ho --directory #{@dummyhigher} ]
164
+
165
+ tangents = Oughtve::Tangent.all :dir.not => "/"
166
+ tangents.size.should == 2
167
+
168
+ tangents.first.dir.should == @dummylower
169
+ tangents.first.name.should == "hi"
170
+
171
+ tangents.last.dir.should == @dummyhigher
172
+ tangents.last.name.should == "ho"
173
+ end
174
+
175
+ # TODO: This will quite likely fail because RSpec sucks.
176
+ it "prints a warning if new Tangent being created above existing one" do
177
+ pending "RSpec does not support grabbing output."
178
+
179
+ $stdout = StringIO.new
180
+ $stderr = StringIO.new
181
+
182
+ Oughtve.run %W[ --new --tangent hi --directory #{@dummylower} ]
183
+
184
+ tangents = Oughtve::Tangent.all :dir.not => "/"
185
+ tangents.size.should == 1
186
+
187
+ Oughtve.run %W[ --new --tangent ho --directory #{@dummyhigher} ]
188
+
189
+ tangents = Oughtve::Tangent.all :dir.not => "/"
190
+ tangents.size.should == 2
191
+
192
+ out = $stdout.read.chomp
193
+ err = $stderr.read.chomp
194
+
195
+ [out, err].any? {|str| str =~ /above/ }.should == true
196
+ end
197
+
198
+ # @todo This is expected to fail because RSpec sucks. --rue
199
+ it "raises an error if the path does not exist in the filesystem" do
200
+ nonesuch = "/foobar/#{$$}/#{Time.now.to_i}/#{rand 100}"
201
+ File.exist?(nonesuch).should == false
202
+ lambda { Oughtve.run %W[ --new --tangent hi --directory #{nonesuch} ] }.should raise_error
203
+ end
204
+
205
+ it "creates the initial Chapter associated with the Tangent" do
206
+ Oughtve.run %w[ --new --tangent something --directory /tmp ]
207
+
208
+ tangent = Oughtve::Tangent.all(:dir.not => "/").first
209
+ tangent.chapters.size.should == 1
210
+ end
211
+ end