Top Level Namespace

Defined Under Namespace

Modules: Aruba

Instance Method Summary collapse

Instance Method Details

#a_path_matching_pattern(/pattern/) ⇒ Boolean

This matchers checks if matches pattern. pattern can be a string, regexp or an RSpec matcher.

Examples:

Use matcher with regexp


RSpec.describe do
  it { expect(files).to include a_path_matching_pattern /asdf/ }
end

Parameters:

  • pattern (String, Regexp, Matcher)

    Specifies the pattern

Returns:

  • (Boolean)

    The result

    false:

    • if pattern does not match

    true:

    • if pattern matches


21
# File 'lib/aruba/matchers/path/a_path_matching_pattern.rb', line 21

RSpec::Matchers.alias_matcher :a_path_matching_pattern, :match

#be_a_command_found_in_pathBoolean

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:

  • (Boolean)

    The result

    false:

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


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

RSpec::Matchers.define :be_a_command_found_in_path do
  match do |actual|
    !which(actual).nil?
  end

  failure_message do |actual|
    format(%(expected that command "%s" can be found in PATH "%s".),
           actual, aruba.environment["PATH"])
  end

  failure_message_when_negated do |actual|
    format(%(expected that command "%s" cannot be found in PATH "%s".),
           actual, aruba.environment["PATH"])
  end
end

#be_an_absolute_pathBoolean

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:

  • (Boolean)

    The result

    false:

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


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

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_directoryBoolean

This matchers checks if exists in filesystem

Examples:

Use matcher


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

Returns:

  • (Boolean)

    The result

    false:

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


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

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

    raise "String expected" 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_executableBoolean

This matchers checks if exists in filesystem

Examples:

Use matcher


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

Returns:

  • (Boolean)

    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
# File 'lib/aruba/matchers/file/be_an_existing_executable.rb', line 18

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_fileBoolean

This matchers checks if exists in filesystem

Examples:

Use matcher


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

Returns:

  • (Boolean)

    The result

    false:

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


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

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

    raise "String expected" 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_pathBoolean

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:

  • (Boolean)

    The result

    false:

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


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

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_executedBoolean

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:

  • (Boolean)

    The result

    false:

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


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

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

  failure_message do |_actual|
    "Expected `#{@actual}` to succeed" \
      " but got non-zero exit status and the following output:" \
      "\n\n#{@old_actual.output}\n"
  end
end

#have_exit_status(status) ⇒ Boolean

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:

  • (Boolean)

    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
39
40
41
42
43
44
# File 'lib/aruba/matchers/command/have_exit_status.rb', line 19

RSpec::Matchers.define :have_exit_status do |expected|
  match do |actual|
    actual.stop
    @actual_exit_status = actual.exit_status

    values_match? expected, @actual_exit_status
  end

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

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

#have_file_content(content) ⇒ Boolean

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:

  • (Boolean)

    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


39
40
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 39

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

  diffable if expected.is_a? String

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

#have_file_size(size) ⇒ Boolean

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:

  • (Boolean)

    The result

    false:

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


21
22
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 21

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_outputBoolean

This matchers checks if has created output

Examples:

Use matcher


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

Returns:

  • (Boolean)

    The result

    false:

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


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

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

    unless @old_actual.respond_to? :output
      raise "Expected #{@old_actual} to respond to #output"
    end

    @old_actual.stop

    @actual = sanitize_text(actual.output)

    values_match?(expected, @actual)
  end

  diffable

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

  failure_message do |_actual|
    "expected `#{@old_actual.commandline}` to have output #{description_of expected}\n" \
      "but was:\n#{Aruba::Matchers::Base::MessageIndenter.indent_multiline_message @actual}"
  end
end

#have_output_on_stderrBoolean

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:

  • (Boolean)

    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
33
34
# 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

    unless @old_actual.respond_to? :stderr
      raise "Expected #{@old_actual} to respond to #stderr"
    end

    @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_stdoutBoolean

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:

  • (Boolean)

    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
33
34
# 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

    unless @old_actual.respond_to? :stdout
      raise "Expected #{@old_actual} to respond to #stdout"
    end

    @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) ⇒ Boolean

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:

  • (Boolean)

    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|
    raise "Expected #{actual} to respond to #size" unless actual.respond_to? :size

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

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

#have_permissions(permissions) ⇒ Boolean

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:

  • (Boolean)

    The result

    false:

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


29
30
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
61
# File 'lib/aruba/matchers/path/have_permissions.rb', line 29

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 = case expected
                when Integer
                  expected.to_s(8)
                when 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_as(file_name) ⇒ Boolean

This matchers checks if has the same content like

Examples:

Use matcher


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

Parameters:

  • file_name (String)

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

Returns:

  • (Boolean)

    The result

    false:

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


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

RSpec::Matchers.define :have_same_file_content_as 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) ⇒ Boolean

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:

  • (Boolean)

    The result

    false:

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


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

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) ⇒ Boolean

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:

  • string (String)

    The value of the output string

Returns:

  • (Boolean)

    The result

    False:

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


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

RSpec::Matchers.define :include_output_string do |expected|
  match do |actual|
    actual.force_encoding("UTF-8")
    @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

#match_output_string(string) ⇒ Boolean

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:

  • string (String)

    The value of the output string

Returns:

  • (Boolean)

    The result

    False:

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


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

RSpec::Matchers.define :match_output_string do |expected|
  match do |actual|
    actual.force_encoding("UTF-8")
    @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

#output_string_eq(string) ⇒ Boolean

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:

  • string (String)

    The value of the output string

Returns:

  • (Boolean)

    False:

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


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

RSpec::Matchers.define :output_string_eq do |expected|
  match do |actual|
    actual.force_encoding("UTF-8")
    @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_longBoolean

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:

  • (Boolean)

    The result

    false:

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


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

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

    unless @old_actual.respond_to? :timed_out?
      raise "Expected #{@old_actual} to respond to #timed_out?"
    end

    @old_actual.stop

    @old_actual.timed_out? == false
  end
end