s4t-utils 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +8 -0
- data/Manifest.txt +2 -0
- data/Rakefile.hoe +11 -0
- data/homepage/index.html +16 -0
- data/lib/s4t-utils.rb +1 -0
- data/lib/s4t-utils/capturing-globals.rb +28 -1
- data/lib/s4t-utils/hacks.rb +4 -3
- data/lib/s4t-utils/os.rb +29 -0
- data/lib/s4t-utils/ruby-extensions.rb +67 -0
- data/lib/s4t-utils/version.rb +1 -1
- data/test/capturing-globals-tests.rb +17 -0
- data/test/hacks-tests.rb +30 -0
- metadata +5 -3
data/History.txt
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
Version 1.0.3
|
2
|
+
* Added with_stdin and capturing_stdout
|
3
|
+
* Added set_test_paths and set_script_lib_path
|
4
|
+
* pi can take symbols as captions.
|
5
|
+
* Added without_pretty_indentation and has_exact_prefix?
|
6
|
+
The names were chosen to be so bad there'd be little
|
7
|
+
chance of name clashes.
|
8
|
+
|
1
9
|
Version 1.0.2
|
2
10
|
* Added user_denies as an alias for user_disputes.
|
3
11
|
|
data/Manifest.txt
CHANGED
@@ -16,6 +16,7 @@ data/make-s4t-project/setup.rb
|
|
16
16
|
data/make-s4t-project/sub-lib-skeleton
|
17
17
|
data/make-s4t-project/test-skeleton
|
18
18
|
data/make-s4t-project/version-skeleton
|
19
|
+
homepage/index.html
|
19
20
|
lib/s4t-utils.rb
|
20
21
|
lib/s4t-utils/capturing-globals.rb
|
21
22
|
lib/s4t-utils/claims.rb
|
@@ -28,6 +29,7 @@ lib/s4t-utils/more-assertions.rb
|
|
28
29
|
lib/s4t-utils/os.rb
|
29
30
|
lib/s4t-utils/rake-task-helpers.rb
|
30
31
|
lib/s4t-utils/rakefile-common.rb
|
32
|
+
lib/s4t-utils/ruby-extensions.rb
|
31
33
|
lib/s4t-utils/svn-file-movement.rb
|
32
34
|
lib/s4t-utils/test-util.rb
|
33
35
|
lib/s4t-utils/version.rb
|
data/Rakefile.hoe
CHANGED
@@ -20,3 +20,14 @@ Hoe.new("s4t-utils", S4tUtils::Version) do |p|
|
|
20
20
|
p.url = "http://s4t-utils.rubyforge.org"
|
21
21
|
p.remote_rdoc_dir = 'rdoc'
|
22
22
|
end
|
23
|
+
|
24
|
+
desc "Tag release with current version."
|
25
|
+
task 'tag_release' do
|
26
|
+
root = "svn+ssh://marick@rubyforge.org/var/svn/s4t-utils"
|
27
|
+
from = "#{root}/trunk"
|
28
|
+
to = "#{root}/tags/rel-#{S4tUtils::Version}"
|
29
|
+
message = "Release #{S4tUtils::Version}"
|
30
|
+
exec = "svn copy -m '#{message}' #{from} #{to}"
|
31
|
+
puts exec
|
32
|
+
system(exec)
|
33
|
+
end
|
data/homepage/index.html
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
5
|
+
<title>s4t-utils</title>
|
6
|
+
|
7
|
+
</head>
|
8
|
+
<body>
|
9
|
+
<h1><i>Everyday Scripting with Ruby</i> Utilities</h1>
|
10
|
+
<a href="http://www.pragmaticprogrammer.com/titles/bmsft/"><img src="http://www.testing.com/blog/images/bookcover-small.png" style="float: left; border: 0; margin-right: 0.5in"/></a>
|
11
|
+
<p>This is the most recent version of the utilities used in <a href="http://www.exampler.com/blog/" title="Exploration Through Example">Brian Marick</a>'s <i><a href="http://www.pragmaticprogrammer.com/titles/bmsft/">Everyday Scripting with Ruby</a></i>.</p>
|
12
|
+
<p><a href="http://s4t-utils.rubyforge.org/rdoc/" title="s4t-utils's Documentation">Documentation</a> (rdoc)</p>
|
13
|
+
<p><a href="http://rubyforge.org/frs/?group_id=4026" title="RubyForge: Everyday Scripting Utilities: Project Filelist">Download</a></p>
|
14
|
+
|
15
|
+
|
16
|
+
</body>
|
data/lib/s4t-utils.rb
CHANGED
@@ -17,6 +17,23 @@ module S4tUtils
|
|
17
17
|
end
|
18
18
|
new_stderr.string
|
19
19
|
end
|
20
|
+
|
21
|
+
# Run the block, capturing output to $stdout in a string.
|
22
|
+
# That string is the method's return value.
|
23
|
+
#
|
24
|
+
# Note: this assigns to $stdout, which is deprecated in
|
25
|
+
# favor of $stdout.reopen. However, reopen can't take a
|
26
|
+
# StringIO as an argument.
|
27
|
+
def capturing_stdout
|
28
|
+
new_stdout = StringIO.new
|
29
|
+
$stdout = new_stdout
|
30
|
+
begin
|
31
|
+
yield
|
32
|
+
ensure
|
33
|
+
$stdout = STDOUT
|
34
|
+
end
|
35
|
+
new_stdout.string
|
36
|
+
end
|
20
37
|
|
21
38
|
# Run the block, replacing the values of environment variables
|
22
39
|
# with the values given in the hash _settings_. The environment
|
@@ -44,7 +61,7 @@ module S4tUtils
|
|
44
61
|
ENV['HOME'] = '.'
|
45
62
|
yield
|
46
63
|
ensure
|
47
|
-
ENV['HOME'] =
|
64
|
+
ENV['HOME'] = old_home
|
48
65
|
end
|
49
66
|
end
|
50
67
|
|
@@ -89,5 +106,15 @@ module S4tUtils
|
|
89
106
|
ARGV.replace(old_argv)
|
90
107
|
end
|
91
108
|
end
|
109
|
+
|
110
|
+
def with_stdin(string)
|
111
|
+
old_stdin = $stdin
|
112
|
+
$stdin = StringIO.new(string)
|
113
|
+
yield
|
114
|
+
ensure
|
115
|
+
$stdin = old_stdin
|
116
|
+
end
|
117
|
+
|
118
|
+
|
92
119
|
|
93
120
|
end
|
data/lib/s4t-utils/hacks.rb
CHANGED
@@ -21,13 +21,14 @@ module S4tUtils
|
|
21
21
|
# pi [1, 2, 3], 'input' # => 'input: [1, 2, 3]
|
22
22
|
#
|
23
23
|
# The _arg_ is printed using +inspect+. If _leader_ isn't given,
|
24
|
-
# nothing is printed before _arg_.
|
24
|
+
# nothing is printed before _arg_. _leader_ can be a string or symbol.
|
25
25
|
#
|
26
26
|
# pi returns its _arg_, which is occasionally useful for sticking
|
27
27
|
# debugging into the middle of complicated expressions.
|
28
28
|
def pi(arg, leader=nil)
|
29
|
-
|
30
|
-
|
29
|
+
leader = leader.to_s if leader
|
30
|
+
leader = (leader == nil) ? '' : leader + ': '
|
31
|
+
prog1(arg) { puts leader + arg.inspect }
|
31
32
|
end
|
32
33
|
|
33
34
|
|
data/lib/s4t-utils/os.rb
CHANGED
@@ -23,6 +23,35 @@ module S4tUtils
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
26
|
+
|
27
|
+
# Find and set the paths a test needs to run, given the normal
|
28
|
+
# Ruby directory structure. _given_ is something below the test directory.
|
29
|
+
# PACKAGE_ROOT becomes the root of
|
30
|
+
# the structure. PACKAGE_ROOT/lib and PACKAGE_ROOT itself are
|
31
|
+
# put on the front of the path (the latter is so that
|
32
|
+
# tests can require 'test/something'.
|
33
|
+
def set_test_paths(given)
|
34
|
+
return if S4tUtils.const_defined?("PACKAGE_ROOT")
|
26
35
|
|
36
|
+
path = File.expand_path(given)
|
37
|
+
i = path.index("/test/")
|
38
|
+
S4tUtils.const_set("PACKAGE_ROOT", path[0...i])
|
39
|
+
$:.unshift("#{PACKAGE_ROOT}/lib")
|
40
|
+
$:.unshift("#{PACKAGE_ROOT}")
|
41
|
+
end
|
42
|
+
module_function :set_test_paths
|
43
|
+
|
44
|
+
# If the _given_ file lives as a script inside a typical Ruby development
|
45
|
+
# structure, put ../lib at the front of the path. That includes files
|
46
|
+
# from there in preference to old gems lying around.
|
47
|
+
def set_script_lib_path(given)
|
48
|
+
path = File.expand_path(given)
|
49
|
+
p = lambda { | x | File.join(File.dirname(path), '..', x) }
|
50
|
+
l = p['lib']
|
51
|
+
t = p['test']
|
52
|
+
s = p['setup.rb']
|
53
|
+
$:.unshift(l) if File.exists?(l) && File.exists?(t) && File.exists?(s)
|
54
|
+
end
|
55
|
+
module_function :set_script_lib_path
|
27
56
|
|
28
57
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Created by Brian Marick on 2007-09-27.
|
4
|
+
# Copyright (c) 2007. All rights reserved.
|
5
|
+
|
6
|
+
# After some time trying to write libraries that used facets or extensions
|
7
|
+
# and didn't conflict with other libraries that used (for example) active_support,
|
8
|
+
# I give up. I'm going to copy the methods I need, rename them to something so
|
9
|
+
# bad there'll never be a conflict, and see if that's more painful.
|
10
|
+
class String
|
11
|
+
# What everyone calls starts_with?
|
12
|
+
def has_exact_prefix?(prefix)
|
13
|
+
index(prefix) == 0
|
14
|
+
end
|
15
|
+
|
16
|
+
# Copied from the extensions library (http://extensions.rubyforge.org)
|
17
|
+
#
|
18
|
+
# Trims a string:
|
19
|
+
# - removes one initial blank line
|
20
|
+
# - removes trailing spaces on each line
|
21
|
+
# - if +margin+ is given, removes initial spaces up to and including
|
22
|
+
# the margin on each line, plus one space
|
23
|
+
#
|
24
|
+
# This is designed specifically for working with inline documents.
|
25
|
+
# Here-documents are great, except they tend to go against the indentation
|
26
|
+
# of your code. This method allows a convenient way of using %{}-style
|
27
|
+
# documents. For instance:
|
28
|
+
#
|
29
|
+
# USAGE = %{
|
30
|
+
# | usage: prog [-o dir] -h file...
|
31
|
+
# | where
|
32
|
+
# | -o dir outputs to DIR
|
33
|
+
# | -h prints this message
|
34
|
+
# }.without_pretty_indentation("|")
|
35
|
+
#
|
36
|
+
# # USAGE == "usage: prog [-o dir] -h file...\n where"...
|
37
|
+
# # (note single space to right of margin is deleted)
|
38
|
+
#
|
39
|
+
# Note carefully that if no margin string is given, then there is no
|
40
|
+
# clipping at the beginning of each line and your string will remain
|
41
|
+
# indented.
|
42
|
+
def without_pretty_indentation(margin=nil)
|
43
|
+
s = self.dup
|
44
|
+
# Remove initial blank line.
|
45
|
+
s.sub!(/\A[ \t]*\n/, "")
|
46
|
+
# Get rid of the margin, if it's specified.
|
47
|
+
unless margin.nil?
|
48
|
+
margin_re = Regexp.escape(margin || "")
|
49
|
+
margin_re = /^[ \t]*#{margin_re} ?/
|
50
|
+
s.gsub!(margin_re, "")
|
51
|
+
end
|
52
|
+
# Remove trailing whitespace on each line
|
53
|
+
s.gsub!(/[ \t]+$/, "")
|
54
|
+
s
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
# Indents the string +n+ spaces.
|
59
|
+
# Taken from extensions (extensions.rubyforge.org), except that this
|
60
|
+
# version doesn't take a negative argument.
|
61
|
+
#
|
62
|
+
def indent_by(n)
|
63
|
+
n = n.to_int
|
64
|
+
gsub(/^/, " "*n)
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
data/lib/s4t-utils/version.rb
CHANGED
@@ -38,5 +38,22 @@ class CapturingGlobalsTests < Test::Unit::TestCase
|
|
38
38
|
assert_equal('steak', ARGV[1])
|
39
39
|
}
|
40
40
|
end
|
41
|
+
|
42
|
+
def test_with_stdin
|
43
|
+
with_stdin("line1\nline2") do
|
44
|
+
assert_equal("line1\n", readline)
|
45
|
+
assert_equal("line2", readline)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_with_nested_stdin
|
50
|
+
with_stdin("line1\nline2") do
|
51
|
+
assert_equal("line1\n", readline)
|
52
|
+
with_stdin("intermediate") do
|
53
|
+
assert_equal("intermediate", readline)
|
54
|
+
end
|
55
|
+
assert_equal("line2", readline)
|
56
|
+
end
|
57
|
+
end
|
41
58
|
|
42
59
|
end
|
data/test/hacks-tests.rb
CHANGED
@@ -38,5 +38,35 @@ class ClaimsTests < Test::Unit::TestCase
|
|
38
38
|
forwarder.values_at
|
39
39
|
assert_equal(["one", "two", "three"], forwarder.values_at(3))
|
40
40
|
end
|
41
|
+
|
42
|
+
def test_pi_prints_to_standard_output_using_inspect
|
43
|
+
result = capturing_stdout do
|
44
|
+
pi [1, 2, 3], "caption"
|
45
|
+
end
|
46
|
+
assert_equal("caption: [1, 2, 3]\n", result)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_pi_does_not_need_a_caption
|
50
|
+
result = capturing_stdout do
|
51
|
+
pi [1, 2, 3]
|
52
|
+
end
|
53
|
+
assert_equal("[1, 2, 3]\n", result)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_pi_can_take_a_symbol_as_a_caption
|
57
|
+
result = capturing_stdout do
|
58
|
+
pi [1, 2, 3], :caption
|
59
|
+
end
|
60
|
+
assert_equal("caption: [1, 2, 3]\n", result)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_pi_returns_a_useful_value
|
64
|
+
result = capturing_stdout do
|
65
|
+
val = pi([1, 2, 3], 'val')
|
66
|
+
assert_equal([1, 2, 3], val)
|
67
|
+
end
|
68
|
+
assert_equal("val: [1, 2, 3]\n", result)
|
69
|
+
end
|
70
|
+
|
41
71
|
|
42
72
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: s4t-utils
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0.
|
7
|
-
date: 2007-
|
6
|
+
version: 1.0.3
|
7
|
+
date: 2007-09-27 00:00:00 -05:00
|
8
8
|
summary: _Everyday Scripting with Ruby_ utilities.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -47,6 +47,7 @@ files:
|
|
47
47
|
- data/make-s4t-project/sub-lib-skeleton
|
48
48
|
- data/make-s4t-project/test-skeleton
|
49
49
|
- data/make-s4t-project/version-skeleton
|
50
|
+
- homepage/index.html
|
50
51
|
- lib/s4t-utils.rb
|
51
52
|
- lib/s4t-utils/capturing-globals.rb
|
52
53
|
- lib/s4t-utils/claims.rb
|
@@ -59,6 +60,7 @@ files:
|
|
59
60
|
- lib/s4t-utils/os.rb
|
60
61
|
- lib/s4t-utils/rake-task-helpers.rb
|
61
62
|
- lib/s4t-utils/rakefile-common.rb
|
63
|
+
- lib/s4t-utils/ruby-extensions.rb
|
62
64
|
- lib/s4t-utils/svn-file-movement.rb
|
63
65
|
- lib/s4t-utils/test-util.rb
|
64
66
|
- lib/s4t-utils/version.rb
|
@@ -114,5 +116,5 @@ dependencies:
|
|
114
116
|
requirements:
|
115
117
|
- - ">="
|
116
118
|
- !ruby/object:Gem::Version
|
117
|
-
version: 1.
|
119
|
+
version: 1.3.0
|
118
120
|
version:
|