module RSpecCommand::ClassMethods
@!classmethods
Public Instance Methods
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.
@see command
@param cmd [String, Array] Command to run. If passed as an array, no shell
expansion will be performed.
@param options [Hash<Symbol, Object>] Options to pass to
Mixlib::ShellOut.new.
@param block [Proc] Optional block to return a command to run. @option options [Boolean] allow_error If true, don't raise an error on
failed commands.
@example
describe 'myapp' do command 'myapp show' its(:stdout) { is_expected.to match(/a thing/) } end
# File lib/rspec_command.rb, line 276 def command(cmd=nil, options={}, &block) metadata[:command] = true subject do |example| # If a block is given, use it to get the command. cmd = instance_eval(&block) if block command(cmd, options) end end
Set an environment variable for this example.
@param variables [Hash] Key/value pairs to set. @example
describe 'myapp' do command 'myapp show' environment DEBUG: true its(:stderr) { is_expected.to include('[debug]') } end
# 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
Create a file in the temporary directory for this example.
@param path [String] Path within the temporary directory to write to. @param content [String] File data to write. @param block [Proc] Optional block to return file data to write. @example
describe 'myapp' do command 'myapp read data.txt' file 'data.txt', <<-EOH a thing EOH its(:exitstatus) { is_expected.to eq 0 } end
# 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.expand_path(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
Copy fixture data from the spec folder to the temporary directory for this example.
@param path [String] Path of the fixture to copy. @param dest [String] Optional destination path. By default the destination
is the same as path.
@example
describe 'myapp' do command 'myapp run test/' fixture_file 'test' its(:exitstatus) { is_expected.to eq 0 } end
# 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.expand_path(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
# File lib/rspec_command.rb, line 359 def included(klass) super klass.extend ClassMethods end