Top Level Namespace

Defined Under Namespace

Modules: Aruba, RSpec Classes: String

Instance Method Summary collapse

Instance Method Details

#be_a_command_found_in_pathTrueClass, FalseClass

This matchers checks if can be found in path

Examples:

Use matcher


RSpec.describe do
  it { expect(cmd).to be_a_command_found_in_path }
end

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if command was not found in PATH true:
    • if command can be found in PATH


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/aruba/matchers/file/be_a_command_found_in_path.rb', line 18

RSpec::Matchers.define :be_a_command_found_in_path do
  match do |actual|
    @actual = Shellwords.split(actual.commandline).first if actual.respond_to? :commandline

    !which(@actual).nil?
  end

  failure_message do |actual|
    format(%(expected that command "%s" can be found in PATH "#{ENV['PATH']}".), actual)
  end

  failure_message_when_negated do |actual|
    format(%(expected that command "%s" cannot be found in PATH "#{ENV['PATH']}".), actual)
  end
end

#be_an_absolute_pathTrueClass, FalseClass

This matchers checks if exists in filesystem

Examples:

Use matcher


RSpec.describe do
  it { expect(file).to be_an_absolute_path }
  it { expect(directory).to be_an_absolute_path }
  it { expect(directories).to include an_absolute_path }
end

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if path is not absolute true:
    • if path is absolute


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/aruba/matchers/path/be_an_absolute_path.rb', line 20

RSpec::Matchers.define :be_an_absolute_path do |_|
  match do |actual|
    absolute?(actual)
  end

  failure_message do |actual|
    format("expected that path \"%s\" is absolute, but it's not", actual)
  end

  failure_message_when_negated do |actual|
    format("expected that path \"%s\" is not absolute, but it is", actual)
  end
end

#be_an_existing_directoryTrueClass, FalseClass

This matchers checks if exists in filesystem

Examples:

Use matcher


RSpec.describe do
  it { expect(directory1).to be_an_existing_directory }
end

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if directory does not exist true:
    • if directory exists


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/aruba/matchers/directory/be_an_existing_directory.rb', line 18

RSpec::Matchers.define :be_an_existing_directory do |_|
  match do |actual|
    stop_all_commands

    next false unless actual.is_a? String

    directory?(actual)
  end

  failure_message do |actual|
    format("expected that directory \"%s\" exists", actual)
  end

  failure_message_when_negated do |actual|
    format("expected that directory \"%s\" does not exist", actual)
  end
end

#be_an_existing_executableTrueClass, FalseClass

This matchers checks if exists in filesystem

Examples:

Use matcher


RSpec.describe do
  it { expect(file1).to be_an_existing_executable }
end

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if file does not exist true:
    • if file exists


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/aruba/matchers/file/be_an_existing_executable.rb', line 19

RSpec::Matchers.define :be_an_existing_executable do |_|
  match do |actual|
    @actual = Shellwords.split(actual.commandline).first if actual.respond_to? :commandline

    executable?(@actual)
  end

  failure_message do |actual|
    format("expected that executable \"%s\" exists", actual)
  end

  failure_message_when_negated do |actual|
    format("expected that executable \"%s\" does not exist", actual)
  end
end

#be_an_existing_fileTrueClass, FalseClass

This matchers checks if exists in filesystem

Examples:

Use matcher


RSpec.describe do
  it { expect(file1).to be_an_existing_file }
end

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if file does not exist true:
    • if file exists


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/aruba/matchers/file/be_an_existing_file.rb', line 18

RSpec::Matchers.define :be_an_existing_file do |_|
  match do |actual|
    stop_all_commands

    next false unless actual.is_a? String

    file?(actual)
  end

  failure_message do |actual|
    format("expected that file \"%s\" exists", actual)
  end

  failure_message_when_negated do |actual|
    format("expected that file \"%s\" does not exist", actual)
  end
end

#be_an_existing_pathTrueClass, FalseClass

This matchers checks if exists in filesystem

Examples:

Use matcher


RSpec.describe do
  it { expect(file).to be_an_existing_path }
  it { expect(directory).to be_an_existing_path }
  it { expect(all_directories).to all be_an_existing_path }
  it { expect(all_directories).to include an_existing_path }
end

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if path does not exist true:
    • if path exists


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/aruba/matchers/path/be_an_existing_path.rb', line 21

RSpec::Matchers.define :be_an_existing_path do |_|
  match do |actual|
    exist?(actual)
  end

  failure_message do |actual|
    format("expected that path \"%s\" exists", actual)
  end

  failure_message_when_negated do |actual|
    format("expected that path \"%s\" does not exist", actual)
  end
end

#be_successfuly_executedTrueClass, FalseClass

This matchers checks if execution of was successful

Examples:

Use matcher


RSpec.describe do
  it { expect(last_command_started).to be_successfully_executed }
  it { expect(last_command_started).not_to be_successfully_executed }
  it { expect(last_command_started).to have_failed_running }
end

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if command was not successful true:
    • if command was successful


23
24
25
26
27
28
29
30
# File 'lib/aruba/matchers/command/be_successfully_executed.rb', line 23

RSpec::Matchers.define :be_successfully_executed do
  match do |actual|
    @old_actual = actual
    @actual     = @old_actual.commandline

    expect(@old_actual).to have_exit_status(0)
  end
end

#have_exit_status(status) ⇒ TrueClass, FalseClass

This matchers checks if has exit status

Examples:

Use matcher


RSpec.describe do
  it { expect(last_command_started).to have_exit_status(0) }
end

Parameters:

  • status (Integer)

    The value of the exit status

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if command does not have exit status true:
    • if command has exit status


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/aruba/matchers/command/have_exit_status.rb', line 19

RSpec::Matchers.define :have_exit_status do |expected|
  match do |actual|
    @old_actual = actual

    @old_actual.stop
    @actual = actual.exit_status

    next false unless @old_actual.respond_to? :exit_status

    values_match? expected, @actual
  end

  failure_message do |actual|
    format(%(expected that command "%s" has exit status of "%s", but has "%s".), @old_actual.commandline, expected.to_s, actual.to_s)
  end

  failure_message_when_negated do |actual|
    format(%(expected that command "%s" does not have exit status of "%s", but has "%s".), @old_actual.commandline, expected.to_s, actual.to_s)
  end
end

#have_file_content(content) ⇒ TrueClass, FalseClass

This matchers checks if has content. content can be a string, regexp or an RSpec matcher.

Examples:

Use matcher with string


RSpec.describe do
  it { expect(file1).to have_file_content('a') }
end

Use matcher with regexp


RSpec.describe do
  it { expect(file1).to have_file_content(/a/) }
end

Use matcher with an RSpec matcher


RSpec.describe do
  it { expect(file1).to have_file_content(a_string_starting_with 'a') }
  it { expect(files1).to include a_file_having_content(a_string_starting_with 'a') }
end

Parameters:

  • content (String, Regexp, Matcher)

    Specifies the content of the file

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if file does not exist
    • if file content is not equal string
    • if file content does not include regexp
    • if file content does not match the content specification

    true:

    • if file content includes regexp
    • if file content is equal string
    • if file content matches the content specification


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aruba/matchers/file/have_file_content.rb', line 41

RSpec::Matchers.define :have_file_content do |expected|
  match do |actual|
    stop_all_commands

    next false unless file? actual

    @actual   = read(actual).join("\n").chomp
    @expected = if expected.is_a? String
                  expected.chomp
                else
                  expected
                end

    values_match?(@expected, @actual)
  end

  description { "have file content: #{description_of expected}" }
end

#have_file_size(size) ⇒ TrueClass, FalseClass

This matchers checks if path has file size

Examples:

Use matcher


RSpec.describe do
  it { expect('file.txt').to have_file_size(0) }
  it { expect(%w(file.txt file2.txt)).to all have_file_size(0) }
  it { expect(%w(file.txt file2.txt)).to include a_file_of_size(0) }
end

Parameters:

  • size (Fixnum)

    The size to check

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if path does not have size true:
    • if path has size


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/aruba/matchers/file/have_file_size.rb', line 23

RSpec::Matchers.define :have_file_size do |expected|
  match do |actual|
    stop_all_commands

    next false unless file?(actual)

    @old_actual = actual
    @actual = file_size(actual)
    @expected = expected.to_i

    values_match?(@expected, @actual)
  end

  failure_message do |actual|
    format("expected that file \"%s\" has size \"%s\", but has \"%s\"", @old_actual, @actual, @expected)
  end

  failure_message_when_negated do |actual|
    format("expected that file \"%s\" does not have size \"%s\", but has \"%s\"", @old_actual, @actual, @expected)
  end
end

#have_outputTrueClass, FalseClass

This matchers checks if has created output

Examples:

Use matcher


RSpec.describe do
  it { expect(last_command_started).to have_output }
end

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if command has not created output true:
    • if command created output


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/aruba/matchers/command/have_output.rb', line 16

RSpec::Matchers.define :have_output do |expected|
  match do |actual|
    @old_actual = actual

    next false unless @old_actual.respond_to? :output

    @old_actual.stop

    @actual = sanitize_text(actual.output)

    values_match?(expected, @actual)
  end

  diffable

  description { "have output: #{description_of expected}" }
end

#have_output_on_stderrTrueClass, FalseClass

This matchers checks if has created output on stderr

Examples:

Use matcher


RSpec.describe do
  it { expect(last_command_started).to have_output_on_stderr }
end

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if command has not created output on stderr true:
    • if command created output on stderr


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/aruba/matchers/command/have_output_on_stderr.rb', line 16

RSpec::Matchers.define :have_output_on_stderr do |expected|
  match do |actual|
    @old_actual = actual

    next false unless @old_actual.respond_to? :stderr

    @old_actual.stop

    @actual = sanitize_text(actual.stderr)

    values_match?(expected, @actual)
  end

  diffable

  description { "have output on stderr: #{description_of expected}" }
end

#have_output_on_stdoutTrueClass, FalseClass

This matchers checks if has created output on stdout

Examples:

Use matcher


RSpec.describe do
  it { expect(last_command_started).to have_output_on_stdout }
end

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if command has not created output on stdout true:
    • if command created output on stdout


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/aruba/matchers/command/have_output_on_stdout.rb', line 16

RSpec::Matchers.define :have_output_on_stdout do |expected|
  match do |actual|
    @old_actual = actual

    next false unless @old_actual.respond_to? :stdout

    @old_actual.stop

    @actual = sanitize_text(actual.stdout)

    values_match?(expected, @actual)
  end

  diffable

  description { "have output on stdout: #{description_of expected}" }
end

#have_output_size(output) ⇒ TrueClass, FalseClass

This matchers checks if output has size.

Examples:

Use matcher


RSpec.describe do
  it { expect(file1).to have_output_size(256) }
end

Parameters:

  • output (String)

    The content which should be checked

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if output does not have size true:
    • if output has size


19
20
21
22
23
24
25
26
27
28
# File 'lib/aruba/matchers/command/have_output_size.rb', line 19

RSpec::Matchers.define :have_output_size do |expected|
  match do |actual|
    next false unless actual.respond_to? :size

    @actual = actual.size
    values_match? expected, @actual
  end

  description { "output has size #{description_of expected}" }
end

#have_permissions(permissions) ⇒ TrueClass, FalseClass

This matchers checks if or has permissions

Examples:

Use matcher with octal number


RSpec.describe do
  it { expect(file).to have_permissions(0700) }
  it { expect(directory).to have_permissions(0700) }
end

Use matcher with string


RSpec.describe do
  it { expect(file).to have_permissions('0700') }
  it { expect(files).to include a_path_with_permissions('0700') }
  it { expect(directory).to have_permissions('0700') }
  it { expect(directories).to include a_path_with_permissions('0700') }
end

Parameters:

  • permissions (Fixnum, String)

    The permissions as octal number, e.g. 0700, or String, e.g. '0700'

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if file has permissions true:
    • if file does not have permissions


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/aruba/matchers/path/have_permissions.rb', line 31

RSpec::Matchers.define :have_permissions do |expected|
  def permissions(file)
    @actual = Aruba.platform.filesystem_status.new(file).mode
  end

  match do |actual|
    stop_all_commands

    @old_actual = actual
    @actual = permissions(expand_path(@old_actual))

    @expected = if expected.is_a? Integer
                  expected.to_s(8)
                elsif expected.is_a? String
                  expected.gsub(/^0*/, '')
                else
                  expected
                end

    values_match? @expected, @actual
  end

  failure_message do |actual|
    format("expected that path \"%s\" has permissions \"%s\", but has \"%s\".", @old_actual, @expected, @actual)
  end

  failure_message_when_negated do |actual|
    format("expected that path \"%s\" does not have permissions \"%s\", but has \"%s\".", @old_actual, @expected, @actual)
  end
end

#have_same_file_content_like(file_name) ⇒ TrueClass, FalseClass

This matchers checks if has the same content like

Examples:

Use matcher


RSpec.describe do
  it { expect(file1).to have_same_file_content_like(file2) }
  it { expect(files).to include a_file_with_same_content_like(file2) }
end

Parameters:

  • file_name (String)

    The name of the file which should be compared with the file in the expect()-call.

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if file1 is not equal file2 true:
    • if file1 is equal file2


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/aruba/matchers/file/have_same_file_content.rb', line 25

RSpec::Matchers.define :have_same_file_content_like do |expected|
  match do |actual|
    stop_all_commands

    next false unless file?(actual) && file?(expected)

    @actual = expand_path(actual)
    @expected = expand_path(expected)

    FileUtils.compare_file(@actual,@expected)
  end

  failure_message do |actual|
    format("expected that file \"%s\" is the same as file \"%s\".", actual, expected)
  end

  failure_message_when_negated do |actual|
    format("expected that file \"%s\" differs from file \"%s\".", actual, expected)
  end
end

#have_sub_directory(sub_directory) ⇒ TrueClass, FalseClass

This matchers checks if has given sub-directory

Examples:

Use matcher with single directory


RSpec.describe do
  it { expect('dir1.d').to have_sub_directory('subdir.1.d') }
end

Use matcher with multiple directories


RSpec.describe do
  it { expect('dir1.d').to have_sub_directory(['subdir.1.d', 'subdir.2.d']) }
  it { expect(directories).to include a_directory_with_sub_directory(['subdir.1.d', 'subdir.2.d']) }
end

Parameters:

  • sub_directory (Array)

    A list of sub-directory relative to current directory

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if directory does not have sub-directory true:
    • if directory has sub-directory


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/aruba/matchers/directory/have_sub_directory.rb', line 28

RSpec::Matchers.define :have_sub_directory do |expected|
  match do |actual|
    next false unless directory?(actual)

    @old_actual = actual
    @actual = list(actual)

    @expected = Array(expected).map { |p| File.join(@old_actual, p) }

    (@expected - @actual).empty?
  end

  diffable

  failure_message do |actual|
    format("expected that directory \"%s\" has the following sub-directories: %s.", actual.join(', '), expected)
  end

  failure_message_when_negated do |actual|
    format("expected that directory \"%s\" does not have the following sub-directories: %s.", actual.join(', '), expected)
  end
end

#include_output_string(string) ⇒ TrueClass, FalseClass

This matchers checks if the output string of a command includes string.

Examples:

Use matcher


RSpec.describe do
  it { expect(last_command_started).to have_output an_output_string_including string) }
  it { expect(last_command_started).to have_output include_output_string string) }
end

Parameters:

  • status (Integer)

    The value of the exit status

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if the output string does not include string true:
    • if the output string includes string


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aruba/matchers/string/include_output_string.rb', line 20

RSpec::Matchers.define :include_output_string do |expected|
  match do |actual|
    @expected = Regexp.new(Regexp.escape(sanitize_text(expected.to_s)), Regexp::MULTILINE)
    @actual   = sanitize_text(actual)

    values_match? @expected, @actual
  end

  diffable

  description { "string includes: #{description_of expected}" }
end

#include_regexp(regexp) ⇒ TrueClass, FalseClass

This matchers checks if items of an matches regexp

Examples:

Use matcher


RSpec.describe do
  it { expect(%w(asdf qwert)).to include_regexp(/asdf/) }
end

Parameters:

  • regexp (Regexp)

    The regular expression to use

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if regexp does not match true:
    • if regexp matches


19
20
21
22
23
24
25
# File 'lib/aruba/matchers/rspec_matcher_include_regexp.rb', line 19

RSpec::Matchers.define :include_regexp do |expected|
  match do |actual|
    Aruba.platform.deprecated('The use of "include_regexp"-matchers is deprecated. It will be removed soon.')

    !actual.grep(expected).empty?
  end
end

#match_output_string(string) ⇒ TrueClass, FalseClass

This matchers checks if the output string of a command matches regular expression.

Examples:

Use matcher


RSpec.describe do
  it { expect(last_command_started).to have_output an_output_string_matching regex) }
  it { expect(last_command_started).to have_output match_output_string regex) }
end

Parameters:

  • status (Integer)

    The value of the exit status

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if the output string does not match regex true:
    • if the output string matches regex


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aruba/matchers/string/match_output_string.rb', line 20

RSpec::Matchers.define :match_output_string do |expected|
  match do |actual|
    @expected = Regexp.new(unescape_text(expected), Regexp::MULTILINE)
    @actual   = sanitize_text(actual)

    values_match? @expected, @actual
  end

  diffable

  description { "output string matches: #{description_of expected}" }
end

#match_path_pattern(pattern) ⇒ TrueClass, FalseClass

This matchers checks if /directories match

Examples:

Use matcher with regex


RSpec.describe do
  it { expect(Dir.glob(**/*)).to match_file_pattern(/.txt$/) }
end

Use matcher with string


RSpec.describe do
  it { expect(Dir.glob(**/*)).to match_file_pattern('.txt$') }
end

Parameters:

  • pattern (String, Regexp)

    The pattern to use.

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if there are no files/directories which match the pattern true:
    • if there are files/directories which match the pattern


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/aruba/matchers/path/match_path_pattern.rb', line 25

RSpec::Matchers.define :match_path_pattern do |_|
  match do |actual|
    Aruba.platform.deprecated('The use of `expect().to match_path_pattern` is deprecated. Please use `expect().to include pattern /regex/` instead.')

    next !actual.select { |a| a == expected }.empty? if expected.is_a? String

    !actual.grep(expected).empty?
  end

  failure_message do |actual|
    format("expected that path \"%s\" matches pattern \"%s\".", actual.join(", "), expected)
  end

  failure_message_when_negated do |actual|
    format("expected that path \"%s\" does not match pattern \"%s\".", actual.join(", "), expected)
  end
end

#output_string_eq(string) ⇒ TrueClass, FalseClass

This matchers checks if the output string of a command includes string.

Examples:

Use matcher


RSpec.describe do
  it { expect(last_command_started).to have_output output_string_eq string) }
  it { expect(last_command_started).to have_output an_output_string_begin_eq string) }
end

Parameters:

  • status (Integer)

    The value of the exit status

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if the output string does not include string true:
    • if the output string includes string


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aruba/matchers/string/output_string_eq.rb', line 20

RSpec::Matchers.define :output_string_eq do |expected|
  match do |actual|
    @expected = sanitize_text(expected.to_s)
    @actual   = sanitize_text(actual.to_s)

    values_match? @expected, @actual
  end

  diffable

  description { "output string is eq: #{description_of expected}" }
end

#run_too_longTrueClass, FalseClass

This matchers checks if run too long. Say the timeout is 10 seconds and it takes to finish in 15. This matchers will succeed.

Examples:

Use matcher


RSpec.describe do
  it { expect(last_command_started).to run_too_long }
  it { expect(last_command_started).not_to run_too_long }
  it { expect(last_command_started).to finish_its_run_in_time }
end

Returns:

  • (TrueClass, FalseClass)

    The result

    false:

    • if command not to run too long true:
    • if command run too long


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/aruba/matchers/command/have_finished_in_time.rb', line 22

RSpec::Matchers.define :have_finished_in_time do
  match do |actual|
    @old_actual = actual
    @actual     = @old_actual.commandline

    next false unless @old_actual.respond_to? :timed_out?

    @old_actual.stop

    @old_actual.timed_out? == false
  end
end