olag 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ # Extend the core String class.
2
+ class String
3
+
4
+ # Strip away common indentation from the beginning of each line in this
5
+ # String. By default, detects the indentation from the first line. This can
6
+ # be overriden to the exact (String) indentation to strip, or to the (Fixnum)
7
+ # number of spaces the first line is further-indented from the rest of the
8
+ # text.
9
+ def unindent(unindentation = 0)
10
+ unindentation = " " * (indentation.length - unindentation) if Fixnum === unindentation
11
+ return gsub(/^#{unindentation}/, "")
12
+ end
13
+
14
+ # Extract the indentation from the beginning of this String.
15
+ def indentation
16
+ return sub(/[^ ].*$/m, "")
17
+ end
18
+
19
+ end
data/lib/olag/test.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "olag/test/with_errors"
2
+ require "olag/test/with_fakefs"
3
+ require "olag/test/with_rake"
4
+ require "olag/test/with_tempfile"
5
+
6
+ # Enhance the global test module with additional mix-in modules.
7
+ module Test
8
+ end
@@ -0,0 +1,26 @@
1
+ require "olag/errors"
2
+
3
+ module Test
4
+
5
+ # Mix-in for tests that collect Errors.
6
+ module WithErrors
7
+
8
+ # Aliasing methods needs to be deferred to when the module is included
9
+ # and be executed in the context of the class.
10
+ def self.included(base)
11
+ base.class_eval do
12
+
13
+ alias_method :errors_original_setup, :setup
14
+
15
+ # Automatically create an fresh +@errors+ data member for each test.
16
+ def setup
17
+ errors_original_setup
18
+ @errors = Olag::Errors.new
19
+ end
20
+
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,36 @@
1
+ require "fakefs/safe"
2
+
3
+ module Test
4
+
5
+ # Mix-in for tests that use the FakeFS fake file system.
6
+ module WithFakeFS
7
+
8
+ # Aliasing methods needs to be deferred to when the module is included and
9
+ # be executed in the context of the class.
10
+ def self.included(base)
11
+ base.class_eval do
12
+
13
+ alias_method :fakefs_original_setup, :setup
14
+
15
+ # Automatically create an fresh fake file system for each test.
16
+ def setup
17
+ fakefs_original_setup
18
+ FakeFS.activate!
19
+ FakeFS::FileSystem.clear
20
+ end
21
+
22
+ alias_method :fakefs_original_teardown, :teardown
23
+
24
+ # Automatically clean up the fake file system at the end of each test.
25
+ def teardown
26
+ fakefs_original_teardown
27
+ FakeFS.deactivate!
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,34 @@
1
+ module Test
2
+
3
+ # Mix-in for tests that use Rake.
4
+ module WithRake
5
+
6
+ # Aliasing methods needs to be deferred to when the module is included
7
+ # and be executed in the context of the class.
8
+ def self.included(base)
9
+ base.class_eval do
10
+
11
+ alias_method :rake_original_setup, :setup
12
+
13
+ # Automatically create a fresh Rake application.
14
+ def setup
15
+ rake_original_setup
16
+ @original_rake = Rake.application
17
+ @rake = Rake::Application.new
18
+ Rake.application = @rake
19
+ end
20
+
21
+ alias_method :rake_original_teardown, :teardown
22
+
23
+ # Automatically restore the original Rake application.
24
+ def teardown
25
+ rake_original_teardown
26
+ Rake.application = @original_rake
27
+ end
28
+
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,49 @@
1
+ require "fileutils"
2
+
3
+ module Test
4
+
5
+ # Mix-in for tests that write a temporary disk file.
6
+ module WithTempfile
7
+
8
+ # Create a temporary file on the disk. The file will be automatically
9
+ # removed when the test is done.
10
+ def write_tempfile(path, content, directory = ".")
11
+ file = Tempfile.open(path, directory)
12
+ file.write(content)
13
+ file.close(false)
14
+ (@tempfiles ||= []) << (path = file.path)
15
+ return path
16
+ end
17
+
18
+ # Create a temporary directory on the disk. The directory will be
19
+ # automatically removed when the test is done. This is very useful for
20
+ # complex file tests that can't use FakeFS.
21
+ def create_tempdir(directory = ".")
22
+ file = Tempfile.open("dir", directory)
23
+ (@tempfiles ||= []) << (path = file.path)
24
+ File.delete(path)
25
+ Dir.mkdir(path)
26
+ return path
27
+ end
28
+
29
+ # Aliasing methods needs to be deferred to when the module is included and
30
+ # be executed in the context of the class.
31
+ def self.included(base)
32
+ base.class_eval do
33
+
34
+ alias_method :tempfile_original_teardown, :teardown
35
+
36
+ # Automatically clean up the temporary files when the test is done.
37
+ def teardown
38
+ tempfile_original_teardown
39
+ (@tempfiles || []).each do |tempfile|
40
+ FileUtils.rm_rf(tempfile) if File.exist?(tempfile)
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,53 @@
1
+ module Olag
2
+
3
+ module Version
4
+
5
+ # Update the file containing the gem's version. The file is expected to
6
+ # contain a line in the format: <tt>VERSION =
7
+ # "_major_._minor_._commits_"</tt>. The third number is updated according
8
+ # to the number of Git commits. This works well as long as we are working
9
+ # in the master branch.
10
+ def self.update(path)
11
+ current_file_contents, current_version, correct_version = current_status(path)
12
+ if current_version != correct_version
13
+ correct_file_contents = current_file_contents.sub(current_version, correct_version)
14
+ File.open(path, "w") { |file| file.write(correct_file_contents) }
15
+ end
16
+ return correct_version
17
+ end
18
+
19
+ protected
20
+
21
+ # Return the current version file contents, the current version, and the
22
+ # correct version.
23
+ def self.current_status(path)
24
+ prefix, current_suffix = extract_version(path, current_file_contents = File.read(path))
25
+ correct_suffix = count_git_commits.to_s
26
+ current_version = prefix + current_suffix
27
+ correct_version = prefix + correct_suffix
28
+ return current_file_contents, current_version, correct_version
29
+ end
30
+
31
+ # Extract the version number from the contents of the version file. This is
32
+ # an array of two strings - the prefix containing the major and minor
33
+ # numbers, and the suffix containing the commits number.
34
+ def self.extract_version(path, file_contents)
35
+ abort("#{path}: Does not contain a valid VERSION line.") unless file_contents =~ /VERSION\s+=\s+["'](\d+\.\d+\.)(\d+)["']/
36
+ return [ $1, $2 ]
37
+ end
38
+
39
+ # Return the total number of Git commits that apply to the current state of
40
+ # the working directory. This means we add one to the actual number of
41
+ # commits if there are uncommitted changes; this way the version number
42
+ # does not change after doing a commit - it only changes after we make
43
+ # changes following a commit.
44
+ def self.count_git_commits
45
+ git_commits = IO.popen("git rev-list --all | wc -l").read.chomp.to_i
46
+ git_status = IO.popen("git status").read
47
+ git_commits += 1 unless git_status.include?("working directory clean")
48
+ return git_commits
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,8 @@
1
+ # This module contains all the Olag code.
2
+ module Olag
3
+
4
+ # This version number. The third number is automatically updated to track the
5
+ # number of Git commits by running <tt>rake version</tt>.
6
+ VERSION = "0.1.10"
7
+
8
+ end
@@ -0,0 +1,15 @@
1
+ require "olag/data_files"
2
+ require "test/spec"
3
+
4
+ # Test accessing data files packages with the gem.
5
+ class TestAccessDataFiles < Test::Unit::TestCase
6
+
7
+ def test_access_data_file
8
+ File.exist?(Olag::DataFiles.expand_path("olag/data_files.rb")).should == true
9
+ end
10
+
11
+ def test_access_missing_file
12
+ Olag::DataFiles.expand_path("no-such-file").should == "no-such-file"
13
+ end
14
+
15
+ end
@@ -0,0 +1,33 @@
1
+ require "olag/errors"
2
+ require "olag/test"
3
+ require "test/spec"
4
+
5
+ # Test collecting errors.
6
+ class TestCollectErrors < Test::Unit::TestCase
7
+
8
+ include Test::WithErrors
9
+
10
+ def test_one_error
11
+ @errors << "Oops"
12
+ @errors.should == [ "#{$0}: Oops" ]
13
+ end
14
+
15
+ def test_path_error
16
+ @errors.in_path("foo") do
17
+ @errors << "Eeek"
18
+ "result"
19
+ end.should == "result"
20
+ @errors << "Oops"
21
+ @errors.should == [ "#{$0}: Eeek in file: foo", "#{$0}: Oops" ]
22
+ end
23
+
24
+ def test_line_error
25
+ @errors.in_path("foo") do
26
+ @errors.at_line(1)
27
+ @errors << "Eeek"
28
+ end
29
+ @errors << "Oops"
30
+ @errors.should == [ "#{$0}: Eeek in file: foo at line: 1", "#{$0}: Oops" ]
31
+ end
32
+
33
+ end
@@ -0,0 +1,17 @@
1
+ require "olag/hash_struct"
2
+ require "test/spec"
3
+
4
+ # Test accessing missing keys as members.
5
+ class TestMissingKeys < ::Test::Unit::TestCase
6
+
7
+ def test_read_missing_key
8
+ {}.missing.should == nil
9
+ end
10
+
11
+ def test_set_missing_key
12
+ hash = {}
13
+ hash.missing = "value"
14
+ hash.missing.should == "value"
15
+ end
16
+
17
+ end
@@ -0,0 +1,44 @@
1
+ require "olag/application"
2
+ require "olag/test"
3
+ require "test/spec"
4
+
5
+ # An application that emits an error when run.
6
+ class ErrorApplication < Olag::Application
7
+
8
+ # Run the error application.
9
+ def run
10
+ super { @errors << "Oops!" }
11
+ end
12
+
13
+ end
14
+
15
+ # Test running a Olag Application.
16
+ class TestRunApplication < Test::Unit::TestCase
17
+
18
+ include Test::WithFakeFS
19
+
20
+ def test_do_nothing
21
+ Olag::Application.with_argv([]) { Olag::Application.new(true).run }.should == 0
22
+ end
23
+
24
+ def test_extra_arguments
25
+ Olag::Application.with_argv(%w(-e stderr dummy)) { Olag::Application.new(true).run }.should == 1
26
+ File.read("stderr").should.include?("Expects no command line file arguments")
27
+ end
28
+
29
+ def test_print_version
30
+ Olag::Application.with_argv(%w(-o nested/stdout -v -h)) { Olag::Application.new(true).run }.should == 0
31
+ File.read("nested/stdout").should == "#{$0}: Version: #{Olag::VERSION}\n"
32
+ end
33
+
34
+ def test_print_help
35
+ Olag::Application.with_argv(%w(-o stdout -h -v)) { Olag::Application.new(true).run }.should == 0
36
+ File.read("stdout").should.include?("DESCRIPTION:")
37
+ end
38
+
39
+ def test_print_errors
40
+ Olag::Application.with_argv(%w(-e stderr)) { ErrorApplication.new(true).run }.should == 1
41
+ File.read("stderr").should.include?("Oops!")
42
+ end
43
+
44
+ end
@@ -0,0 +1,26 @@
1
+ require "olag/string_unindent"
2
+ require "test/spec"
3
+
4
+ # Test unindenting a multi-line text.
5
+ class TestUnindentText < ::Test::Unit::TestCase
6
+
7
+ def test_automatic_unindent
8
+ <<-EOF.unindent.should == "a\n b\n"
9
+ a
10
+ b
11
+ EOF
12
+ end
13
+
14
+ def test_invalid_unindent
15
+ " a\n b\n".unindent.should == "a\n b\n"
16
+ end
17
+
18
+ def test_integer_unindent
19
+ " a\n b\n".unindent(1).should == " a\n b\n"
20
+ end
21
+
22
+ def test_string_unindent
23
+ " a\n b\n".unindent(" ").should == " a\n b\n"
24
+ end
25
+
26
+ end
metadata ADDED
@@ -0,0 +1,243 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: olag
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 10
10
+ version: 0.1.10
11
+ platform: ruby
12
+ authors:
13
+ - Oren Ben-Kiki
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-24 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: Saikuro
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: codnar
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: fakefs
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: flay
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :development
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: rake
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ type: :development
89
+ version_requirements: *id005
90
+ - !ruby/object:Gem::Dependency
91
+ name: rcov
92
+ prerelease: false
93
+ requirement: &id006 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ type: :development
103
+ version_requirements: *id006
104
+ - !ruby/object:Gem::Dependency
105
+ name: rdoc
106
+ prerelease: false
107
+ requirement: &id007 !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ type: :development
117
+ version_requirements: *id007
118
+ - !ruby/object:Gem::Dependency
119
+ name: reek
120
+ prerelease: false
121
+ requirement: &id008 !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ type: :development
131
+ version_requirements: *id008
132
+ - !ruby/object:Gem::Dependency
133
+ name: roodi
134
+ prerelease: false
135
+ requirement: &id009 !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ hash: 3
141
+ segments:
142
+ - 0
143
+ version: "0"
144
+ type: :development
145
+ version_requirements: *id009
146
+ - !ruby/object:Gem::Dependency
147
+ name: test-spec
148
+ prerelease: false
149
+ requirement: &id010 !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ hash: 3
155
+ segments:
156
+ - 0
157
+ version: "0"
158
+ type: :development
159
+ version_requirements: *id010
160
+ description: Olag is Oren's set of utilities for creating a well-behaved gem. This is very opinionated software; it eliminates a lot of the boilerplate, at the cost of making many decisions which may not be suitable for everyone (directory structure, code verification, codnar for documentation, etc.).
161
+ email: rubygems-oren@ben-kiki.org
162
+ executables: []
163
+
164
+ extensions: []
165
+
166
+ extra_rdoc_files:
167
+ - README.rdoc
168
+ - LICENSE
169
+ - ChangeLog
170
+ files:
171
+ - lib/olag/application.rb
172
+ - lib/olag/change_log.rb
173
+ - lib/olag/data_files.rb
174
+ - lib/olag/errors.rb
175
+ - lib/olag/gem_specification.rb
176
+ - lib/olag/globals.rb
177
+ - lib/olag/hash_struct.rb
178
+ - lib/olag/rake.rb
179
+ - lib/olag/string_unindent.rb
180
+ - lib/olag/test/with_errors.rb
181
+ - lib/olag/test/with_fakefs.rb
182
+ - lib/olag/test/with_rake.rb
183
+ - lib/olag/test/with_tempfile.rb
184
+ - lib/olag/test.rb
185
+ - lib/olag/update_version.rb
186
+ - lib/olag/version.rb
187
+ - doc/root.html
188
+ - doc/system.markdown
189
+ - Rakefile
190
+ - codnar.html
191
+ - test/access_data_files.rb
192
+ - test/collect_errors.rb
193
+ - test/missing_keys.rb
194
+ - test/run_application.rb
195
+ - test/unindent_text.rb
196
+ - README.rdoc
197
+ - LICENSE
198
+ - ChangeLog
199
+ homepage: https://rubygems.org/gems/olag
200
+ licenses: []
201
+
202
+ post_install_message:
203
+ rdoc_options:
204
+ - --title
205
+ - Olag 0.1.10
206
+ - --main
207
+ - README.rdoc
208
+ - --line-numbers
209
+ - --all
210
+ - --quiet
211
+ require_paths:
212
+ - lib
213
+ required_ruby_version: !ruby/object:Gem::Requirement
214
+ none: false
215
+ requirements:
216
+ - - ">="
217
+ - !ruby/object:Gem::Version
218
+ hash: 3
219
+ segments:
220
+ - 0
221
+ version: "0"
222
+ required_rubygems_version: !ruby/object:Gem::Requirement
223
+ none: false
224
+ requirements:
225
+ - - ">="
226
+ - !ruby/object:Gem::Version
227
+ hash: 3
228
+ segments:
229
+ - 0
230
+ version: "0"
231
+ requirements: []
232
+
233
+ rubyforge_project:
234
+ rubygems_version: 1.7.2
235
+ signing_key:
236
+ specification_version: 3
237
+ summary: Olag - Oren's Library/Application Gem framework
238
+ test_files:
239
+ - test/access_data_files.rb
240
+ - test/collect_errors.rb
241
+ - test/missing_keys.rb
242
+ - test/run_application.rb
243
+ - test/unindent_text.rb