Module: RSpecCommand
- Extended by:
- RSpec::SharedContext
- Defined in:
- lib/rspec_command.rb,
lib/rspec_command/rake.rb,
lib/rspec_command/match_fixture.rb
Overview
An RSpec helper module for testing command-line tools.
Defined Under Namespace
Modules: Rake
Instance Attribute Summary collapse
-
#fixture_root ⇒ String
readonly
Base path for the fixtures directory.
-
#temp_path ⇒ String
readonly
Path to the temporary directory created for the current example.
Class Method Summary collapse
-
.command(cmd = nil, options = {}, &block) ⇒ Object
Run a command as the subject of this example.
-
.environment(variables) ⇒ Object
Set an environment variable for this example.
-
.file(path, content = nil, &block) ⇒ Object
Create a file in the temporary directory for this example.
-
.fixture_file(path, dest = nil) ⇒ Object
Copy fixture data from the spec folder to the temporary directory for this example.
Instance Method Summary collapse
-
#capture_output(&block) ⇒ String
Run a local block with $stdout and $stderr redirected to a strings.
-
#match_fixture(fixture_path, local_path = nil) ⇒ Object
Matcher to compare files or folders from the temporary directory to a fixture.
Instance Attribute Details
#fixture_root ⇒ String (readonly)
Base path for the fixtures directory. Default value is 'fixtures'.
63 |
# File 'lib/rspec_command.rb', line 63 let(:fixture_root) { 'fixtures' } |
#temp_path ⇒ String (readonly)
Path to the temporary directory created for the current example.
54 55 56 |
# File 'lib/rspec_command.rb', line 54 let(:temp_path) do |example| example.[:rspec_command_temp_path] end |
Class Method Details
.command(cmd = nil, options = {}, &block) ⇒ Object
Run a command as the subject of this example. The command can be passed in as a string, array, or block. The subject will be a Mixlib::ShellOut object, all attributes from there will work with rspec-its.
276 277 278 279 280 281 282 283 |
# File 'lib/rspec_command.rb', line 276 def command(cmd=nil, ={}, &block) [:command] = true subject do |example| # If a block is given, use it to get the command. cmd = instance_eval(&block) if block command(cmd, ) end end |
.environment(variables) ⇒ Object
Set an environment variable for this example.
347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/rspec_command.rb', line 347 def environment(variables) before do variables.each do |key, value| if value.nil? _environment.delete(key.to_s) else _environment[key.to_s] = value.to_s end end end end |
.file(path, content = nil, &block) ⇒ Object
Create a file in the temporary directory for this example.
298 299 300 301 302 303 304 305 306 |
# File 'lib/rspec_command.rb', line 298 def file(path, content=nil, &block) raise "file path should be relative the the temporary directory." if path == File.(path) before do content = instance_eval(&block) if block dest_path = File.join(temp_path, path) FileUtils.mkdir_p(File.dirname(dest_path)) IO.write(dest_path, content) end end |
.fixture_file(path, dest = nil) ⇒ Object
Copy fixture data from the spec folder to the temporary directory for this example.
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/rspec_command.rb', line 320 def fixture_file(path, dest=nil) raise "file path should be relative the the temporary directory." if path == File.(path) before do |example| fixture_path = find_fixture(example.file_path, path) dest_path = dest ? File.join(temp_path, dest) : temp_path FileUtils.mkdir_p(dest_path) file_list = MatchFixture::FileList.new(fixture_path) file_list.files.each do |file| abs = file_list.absolute(file) if File.directory?(abs) FileUtils.mkdir_p(File.join(dest_path, file)) else FileUtils.copy(abs , File.join(dest_path, file), preserve: true) end end end end |
Instance Method Details
#capture_output(&block) ⇒ String
Run a local block with $stdout and $stderr redirected to a strings. Useful
for running CLI code in unit tests. The returned string has #stdout
,
#stderr
and #exitstatus
attributes to emulate the output from command.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rspec_command.rb', line 141 def capture_output(&block) old_stdout = $stdout.dup old_stderr = $stderr.dup # Potential future improvement is to use IO.pipe instead of temp files, but # that would require threads or something to read contiuously since the # buffer is only 64k on the kernel side. Tempfile.open('capture_stdout') do |tmp_stdout| Tempfile.open('capture_stderr') do |tmp_stderr| $stdout.reopen(tmp_stdout) $stdout.sync = true $stderr.reopen(tmp_stderr) $stderr.sync = true output = nil begin # Inner block to make sure the ensure happens first. begin block.call ensure # Rewind. tmp_stdout.seek(0, 0) tmp_stderr.seek(0, 0) # Read in the output. output = OutputString.new(tmp_stdout.read, tmp_stderr.read) end rescue Exception => e if output # Try to add the output so far as an attribute on the exception via # a closure. e.define_singleton_method(:output_so_far) do output end end raise else output end end end ensure $stdout.reopen(old_stdout) $stderr.reopen(old_stderr) end |
#match_fixture(fixture_path, local_path = nil) ⇒ Object
Matcher to compare files or folders from the temporary directory to a fixture.
123 124 125 |
# File 'lib/rspec_command.rb', line 123 def match_fixture(fixture_path, local_path=nil) MatchFixture.new(find_fixture(self.class.file_path), temp_path, fixture_path, local_path) end |