Skip to content

Commit 5494f52

Browse files
committed
Stop polluting the global namespace with test code and add shared,
checkout-specific tmpdir support in spec helper.
1 parent 186fdd3 commit 5494f52

File tree

3 files changed

+70
-36
lines changed

3 files changed

+70
-36
lines changed

spec/bcdatabase/commands_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ module Bcdatabase::Commands
88
before do
99
pending('Issue #7') if RUBY_VERSION > '1.9'
1010

11-
ENV["BCDATABASE_PATH"] = "/tmp/bcdb_specs"
11+
ENV["BCDATABASE_PATH"] = tmpdir + 'bcdb_specs'
1212
FileUtils.mkdir_p ENV["BCDATABASE_PATH"]
1313
end
1414

1515
after(:each) do
1616
if RUBY_VERSION < '1.9'
17-
FileUtils.rm_rf ENV["BCDATABASE_PATH"]
1817
ENV["BCDATABASE_PATH"] = nil
1918
end
2019
end

spec/bcdatabase_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
describe Bcdatabase do
44
before(:each) do
5-
ENV["BCDATABASE_PATH"] = "/tmp/bcdb_specs"
5+
ENV["BCDATABASE_PATH"] = tmpdir + 'bcdb_specs'
66
FileUtils.mkdir_p ENV["BCDATABASE_PATH"]
77
end
88

@@ -13,13 +13,12 @@
1313

1414
describe "cipherment" do
1515
before(:all) do
16-
keyfile = "/tmp/bcdb-spec-key"
16+
keyfile = tmpdir + 'bcdb-spec-key'
1717
open(keyfile, 'w') { |f| f.write "01234567890123456789012345678901" }
1818
ENV["BCDATABASE_PASS"] = keyfile
1919
end
2020

2121
after(:all) do
22-
FileUtils.rm ENV["BCDATABASE_PASS"]
2322
ENV["BCDATABASE_PASS"] = nil
2423
end
2524

spec/spec_helper.rb

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,78 @@
22

33
require 'bcdatabase'
44
require 'fileutils'
5+
require 'pathname'
56

6-
def temporary_yaml(name, hash)
7-
filename = "/#{ENV['BCDATABASE_PATH']}/#{name}.yaml"
8-
open(filename, "w") { |f| YAML.dump(hash, f) }
9-
filename
10-
end
7+
module Bcdatabase::Spec
8+
module Helpers
9+
def self.use_in(rspec_config)
10+
rspec_config.include self
11+
rspec_config.after do
12+
clear_tmpdir
13+
end
14+
end
1115

12-
def capture_std
13-
so = StringIO.new
14-
se = StringIO.new
15-
$stdout = so
16-
$stderr = se
17-
yield
18-
{ :out => so.string, :err => se.string }
19-
ensure
20-
$stdout = STDOUT
21-
$stderr = STDERR
22-
end
16+
def tmpdir
17+
@tmpdir ||= Pathname.new(File.expand_path('../tmp', __FILE__)).tap do |tmp|
18+
tmp.mkpath
19+
end
20+
end
2321

24-
def enable_fake_cipherment
25-
# replace real encryption methods with something predictable
26-
Bcdatabase.module_eval do
27-
class << self
28-
alias :encrypt_original :encrypt
29-
alias :decrypt_original :decrypt
30-
def encrypt(s); s.reverse; end
31-
def decrypt(s); s.reverse; end
22+
def clear_tmpdir
23+
if @tmpdir
24+
@tmpdir.rmtree
25+
end
26+
end
27+
module_function :clear_tmpdir
28+
29+
def temporary_yaml(name, hash)
30+
filename = "/#{ENV['BCDATABASE_PATH']}/#{name}.yaml"
31+
open(filename, "w") { |f| YAML.dump(hash, f) }
32+
filename
3233
end
33-
end
34-
end
3534

36-
def disable_fake_cipherment
37-
Bcdatabase.module_eval do
38-
class << self
39-
alias :encrypt :encrypt_original
40-
alias :decrypt :decrypt_original
35+
def capture_std
36+
so = StringIO.new
37+
se = StringIO.new
38+
$stdout = so
39+
$stderr = se
40+
yield
41+
{ :out => so.string, :err => se.string }
42+
ensure
43+
$stdout = STDOUT
44+
$stderr = STDERR
45+
end
46+
47+
def replace_stdin(*lines)
48+
$stdin = StringIO.new(lines.join("\n"))
49+
yield
50+
ensure
51+
$stdin = STDIN
52+
end
53+
54+
def enable_fake_cipherment
55+
# replace real encryption methods with something predictable
56+
Bcdatabase.module_eval do
57+
class << self
58+
alias :encrypt_original :encrypt
59+
alias :decrypt_original :decrypt
60+
def encrypt(s); s.reverse; end
61+
def decrypt(s); s.reverse; end
62+
end
63+
end
64+
end
65+
66+
def disable_fake_cipherment
67+
Bcdatabase.module_eval do
68+
class << self
69+
alias :encrypt :encrypt_original
70+
alias :decrypt :decrypt_original
71+
end
72+
end
4173
end
4274
end
4375
end
76+
77+
RSpec.configure do |config|
78+
Bcdatabase::Spec::Helpers.use_in(config)
79+
end

0 commit comments

Comments
 (0)