|
2 | 2 | require 'mocha/test_unit'
|
3 | 3 |
|
4 | 4 | begin
|
5 |
| - gem 'net-ssh', ">= 2.0.0" |
| 5 | + gem 'net-ssh', '>= 2.0.0' |
6 | 6 | require 'net/ssh'
|
7 | 7 | rescue LoadError
|
8 | 8 | $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../net-ssh/lib"
|
9 | 9 |
|
10 | 10 | begin
|
11 | 11 | require 'net/ssh'
|
12 | 12 | 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' |
14 | 14 | rescue LoadError => e
|
15 | 15 | abort "could not load net/ssh v2 (#{e.inspect})"
|
16 | 16 | end
|
@@ -42,112 +42,120 @@ def default_test
|
42 | 42 |
|
43 | 43 | protected
|
44 | 44 |
|
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 |
50 | 50 |
|
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 |
56 | 56 |
|
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 |
67 | 67 |
|
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 |
71 | 73 |
|
72 |
| - class FileEntry |
73 |
| - attr_reader :path, :contents, :mode, :mtime, :atime, :io |
| 74 | + class FileEntry |
| 75 | + attr_reader :path, :contents, :mode, :mtime, :atime, :io |
74 | 76 |
|
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 |
79 | 84 |
|
80 |
| - def name |
81 |
| - @name ||= File.basename(path) |
82 |
| - end |
| 85 | + def name |
| 86 | + @name ||= File.basename(path) |
| 87 | + end |
83 | 88 |
|
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) |
87 | 93 |
|
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)) |
92 | 98 |
|
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) |
96 | 101 | end
|
| 102 | + end |
97 | 103 |
|
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 |
101 | 106 |
|
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 |
107 | 114 |
|
108 |
| - def name |
109 |
| - @name ||= File.basename(path) |
110 |
| - end |
| 115 | + def name |
| 116 | + @name ||= File.basename(path) |
| 117 | + end |
111 | 118 |
|
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 |
115 | 122 |
|
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 |
121 | 128 |
|
122 |
| - def stub! |
123 |
| - Dir.stubs(:mkdir).with { |*a| a.first == path } |
| 129 | + def stub! |
| 130 | + Dir.stubs(:mkdir).with { |*a| a.first == path } |
124 | 131 |
|
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) |
127 | 135 |
|
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) |
132 | 140 |
|
133 |
| - entries.each { |e| e.stub! } |
134 |
| - end |
| 141 | + entries.each { |e| e.stub! } |
135 | 142 | end
|
| 143 | + end |
136 | 144 |
|
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 |
148 | 155 | end
|
| 156 | + end |
149 | 157 |
|
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 |
153 | 161 | end
|
0 commit comments