Skip to content

Commit 496ba4d

Browse files
authored
Merge pull request #74 from tibob/72-mocha2.1.0
Fix issue #72: rake test fails with mocha > 2.1.0
2 parents 5ed6181 + c3fbf50 commit 496ba4d

File tree

2 files changed

+100
-87
lines changed

2 files changed

+100
-87
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ doc
44
*.swp
55

66
.DS_Store
7+
8+
# Bundle local files
9+
/vendor/
10+
.bundle/config
11+
Gemfile.lock

test/common.rb

Lines changed: 95 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
require 'mocha/test_unit'
33

44
begin
5-
gem 'net-ssh', ">= 2.0.0"
5+
gem 'net-ssh', '>= 2.0.0'
66
require 'net/ssh'
77
rescue LoadError
88
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../net-ssh/lib"
99

1010
begin
1111
require 'net/ssh'
1212
require 'net/ssh/version'
13-
raise LoadError, "wrong version" unless Net::SSH::Version::STRING >= '1.99.0'
13+
raise LoadError, 'wrong version' unless Net::SSH::Version::STRING >= '1.99.0'
1414
rescue LoadError => e
1515
abort "could not load net/ssh v2 (#{e.inspect})"
1616
end
@@ -42,112 +42,120 @@ def default_test
4242

4343
protected
4444

45-
def prepare_file(path, contents="", mode=0666, mtime=Time.now, atime=Time.now)
46-
entry = FileEntry.new(path, contents, mode, mtime, atime)
47-
entry.stub!
48-
entry
49-
end
45+
def prepare_file(path, contents = '', mode = 0o0666, mtime = Time.now, atime = Time.now)
46+
entry = FileEntry.new(path, contents, mode, mtime, atime)
47+
entry.stub!
48+
entry
49+
end
5050

51-
def prepare_directory(path, mode=0777, mtime=Time.now, atime=Time.now)
52-
directory = DirectoryEntry.new(path, mode, mtime, atime)
53-
yield directory if block_given?
54-
directory.stub!
55-
end
51+
def prepare_directory(path, mode = 0o0777, mtime = Time.now, atime = Time.now)
52+
directory = DirectoryEntry.new(path, mode, mtime, atime)
53+
yield directory if block_given?
54+
directory.stub!
55+
end
5656

57-
# The POSIX spec unfortunately allows all characters in file names except
58-
# ASCII 0x00(NUL) and 0x2F(/)
59-
#
60-
# Ideally, we should be testing filenames with newlines, but Mocha doesn't
61-
# like this at all, so we leave them out. However, the Shellwords module
62-
# handles newlines just fine, so we can be reasonably confident that they
63-
# will work in practice
64-
def awful_file_name
65-
(((0x00..0x7f).to_a - [0x00, 0x0a, 0x2b, 0x2f]).map { |n| n.chr }).join + '.txt'
66-
end
57+
# The POSIX spec unfortunately allows all characters in file names except
58+
# ASCII 0x00(NUL) and 0x2F(/)
59+
#
60+
# Ideally, we should be testing filenames with newlines, but Mocha doesn't
61+
# like this at all, so we leave them out. However, the Shellwords module
62+
# handles newlines just fine, so we can be reasonably confident that they
63+
# will work in practice
64+
def awful_file_name
65+
(((0x00..0x7f).to_a - [0x00, 0x0a, 0x2b, 0x2f]).map { |n| n.chr }).join + '.txt'
66+
end
6767

68-
def escaped_file_name
69-
"\\\001\\\002\\\003\\\004\\\005\\\006\\\a\\\b\\\t\\\v\\\f\\\r\\\016\\\017\\\020\\\021\\\022\\\023\\\024\\\025\\\026\\\027\\\030\\\031\\\032\\\e\\\034\\\035\\\036\\\037\\ \\!\\\"\\#\\$\\%\\&\\'\\(\\)\\*,-.0123456789:\\;\\<\\=\\>\\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[\\\\\\]\\^_\\`abcdefghijklmnopqrstuvwxyz\\{\\|\\}\\~\\\177.txt"
70-
end
68+
def escaped_file_name
69+
"\\\001\\\002\\\003\\\004\\\005\\\006\\\a\\\b\\\t\\\v\\\f\\\r\\\016\\\017\\\020\\\021\\\022\\\023\\\024\\\025" \
70+
"\\\026\\\027\\\030\\\031\\\032\\\e\\\034\\\035\\\036\\\037\\ \\!\\\"\\#\\$\\%\\&\\'\\(\\)\\*,-.0123456789:" \
71+
"\\;\\<\\=\\>\\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[\\\\\\]\\^_\\`abcdefghijklmnopqrstuvwxyz\\{\\|\\}\\~\\\177.txt"
72+
end
7173

72-
class FileEntry
73-
attr_reader :path, :contents, :mode, :mtime, :atime, :io
74+
class FileEntry
75+
attr_reader :path, :contents, :mode, :mtime, :atime, :io
7476

75-
def initialize(path, contents, mode=0666, mtime=Time.now, atime=Time.now)
76-
@path, @contents, @mode = path, contents, mode
77-
@mtime, @atime = mtime, atime
78-
end
77+
def initialize(path, contents, mode = 0o0666, mtime = Time.now, atime = Time.now)
78+
@path = path
79+
@contents = contents
80+
@mode = mode
81+
@mtime = mtime
82+
@atime = atime
83+
end
7984

80-
def name
81-
@name ||= File.basename(path)
82-
end
85+
def name
86+
@name ||= File.basename(path)
87+
end
8388

84-
def stub!
85-
stat = Mocha::Mock.new("file::stat")
86-
stat.stubs(:size => contents.length, :mode => mode, :mtime => mtime, :atime => atime, :directory? => false)
89+
def stub!
90+
# Stub for File::Stat
91+
stat = Object.new
92+
stat.stubs(size: contents.length, mode: mode, mtime: mtime, atime: atime, directory?: false)
8793

88-
File.stubs(:stat).with(path).returns(stat)
89-
File.stubs(:directory?).with(path).returns(false)
90-
File.stubs(:file?).with(path).returns(true)
91-
File.stubs(:open).with(path, "rb").returns(StringIO.new(contents))
94+
File.stubs(:stat).with(path).returns(stat)
95+
File.stubs(:directory?).with(path).returns(false)
96+
File.stubs(:file?).with(path).returns(true)
97+
File.stubs(:open).with(path, 'rb').returns(StringIO.new(contents))
9298

93-
@io = StringIO.new
94-
File.stubs(:new).with(path, "wb", mode).returns(io)
95-
end
99+
@io = StringIO.new
100+
File.stubs(:new).with(path, 'wb', mode).returns(io)
96101
end
102+
end
97103

98-
class DirectoryEntry
99-
attr_reader :path, :mode, :mtime, :atime
100-
attr_reader :entries
104+
class DirectoryEntry
105+
attr_reader :path, :mode, :mtime, :atime, :entries
101106

102-
def initialize(path, mode=0777, mtime=Time.now, atime=Time.now)
103-
@path, @mode = path, mode
104-
@mtime, @atime = mtime, atime
105-
@entries = []
106-
end
107+
def initialize(path, mode = 0o0777, mtime = Time.now, atime = Time.now)
108+
@path = path
109+
@mode = mode
110+
@mtime = mtime
111+
@atime = atime
112+
@entries = []
113+
end
107114

108-
def name
109-
@name ||= File.basename(path)
110-
end
115+
def name
116+
@name ||= File.basename(path)
117+
end
111118

112-
def file(name, *args)
113-
(entries << FileEntry.new(File.join(path, name), *args)).last
114-
end
119+
def file(name, *args)
120+
(entries << FileEntry.new(File.join(path, name), *args)).last
121+
end
115122

116-
def directory(name, *args)
117-
entry = DirectoryEntry.new(File.join(path, name), *args)
118-
yield entry if block_given?
119-
(entries << entry).last
120-
end
123+
def directory(name, *args)
124+
entry = DirectoryEntry.new(File.join(path, name), *args)
125+
yield entry if block_given?
126+
(entries << entry).last
127+
end
121128

122-
def stub!
123-
Dir.stubs(:mkdir).with { |*a| a.first == path }
129+
def stub!
130+
Dir.stubs(:mkdir).with { |*a| a.first == path }
124131

125-
stat = Mocha::Mock.new("file::stat")
126-
stat.stubs(:size => 1024, :mode => mode, :mtime => mtime, :atime => atime, :directory? => true)
132+
# Stub for File::Stat
133+
stat = Object.new
134+
stat.stubs(size: 1024, mode: mode, mtime: mtime, atime: atime, directory?: true)
127135

128-
File.stubs(:stat).with(path).returns(stat)
129-
File.stubs(:directory?).with(path).returns(true)
130-
File.stubs(:file?).with(path).returns(false)
131-
Dir.stubs(:entries).with(path).returns(%w(. ..) + entries.map { |e| e.name }.sort)
136+
File.stubs(:stat).with(path).returns(stat)
137+
File.stubs(:directory?).with(path).returns(true)
138+
File.stubs(:file?).with(path).returns(false)
139+
Dir.stubs(:entries).with(path).returns(%w[. ..] + entries.map { |e| e.name }.sort)
132140

133-
entries.each { |e| e.stub! }
134-
end
141+
entries.each { |e| e.stub! }
135142
end
143+
end
136144

137-
def expect_scp_session(arguments)
138-
story do |session|
139-
channel = session.opens_channel
140-
channel.sends_exec "scp #{arguments}"
141-
yield channel if block_given?
142-
channel.sends_eof
143-
channel.gets_exit_status
144-
channel.gets_eof
145-
channel.gets_close
146-
channel.sends_close
147-
end
145+
def expect_scp_session(arguments)
146+
story do |session|
147+
channel = session.opens_channel
148+
channel.sends_exec "scp #{arguments}"
149+
yield channel if block_given?
150+
channel.sends_eof
151+
channel.gets_exit_status
152+
channel.gets_eof
153+
channel.gets_close
154+
channel.sends_close
148155
end
156+
end
149157

150-
def scp(options={})
151-
@scp ||= Net::SCP.new(connection(options))
152-
end
158+
def scp(options = {})
159+
@scp ||= Net::SCP.new(connection(options))
160+
end
153161
end

0 commit comments

Comments
 (0)