Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Output for expect([]).to include have_output /asdf/ not easy to understand #823

Closed
@ghost

Description

Hi,

I'm now using the include-matcher within Aruba to check if any object matches have_output, have_exit_status et al. Unfortunately the output is not as expected and not very helpful for failures.

Can you give me some hints how to improve the output? The output of the all-matcher is much more helpful and suitable for my use case, where more complex objects than number and string are involved.

Example

require 'aruba/rspec'

RSpec.describe '#have_output' do
  context 'when using include', :type => :aruba do
    before :each do
    run('echo MyTest')
    end

    it { expect(all_commands).to include have_output 'MyTest' }
    it { expect(all_commands).to include have_output 'asdf' }
  end

  context 'when using all' do
    before :each do
    run('echo MyTest')
    end

    it { expect(all_commands).to all have_output 'MyTest' }
    it { expect(all_commands).to all have_output 'asdf' }
  end
end

The output of the "all"-failing example, contains a lot informatin, but also has this nice section about each object checked:

     object at index 0 failed to match:
        expected "MyTest" to have output: "asdf"
# ./tmp/blub3.rb:19:in `block (3 levels) in <top (required)>'

which exactly tells the user what's wrong. For the "include"-failing example, this is not true, here the user gets some weird output which is not that helpful.

Diff:
@@ -1,2 +1,86 @@
-[(have output: "asdf")]
+[#<Aruba::Processes::SpawnProcess:0x00000002041940
+  @cmd="echo MyTest",
+  @duplex=true,
+  @environment=

I can see, that just showing what's not included in a list, works for simple
objects - Numbers, Strings etc. - but fails for more complex ones.

Here's the matcher involved in the example given above. It's also part of this branch.

# @!method have_output
#   This matchers checks if <command> has created output
#
#   @return [TrueClass, FalseClass] The result
#
#     false:
#     * if command has not created output
#     true:
#     * if command created output
#
#   @example Use matcher
#
#     RSpec.describe do
#       it { expect(last_command_started).to have_output }
#     end
RSpec::Matchers.define :have_output do |expected|
  match do |actual|
    @old_actual = actual

    next false unless @old_actual.respond_to? :output

    @announcer ||= Aruba::Announcer.new(
      self,
      :stdout => @announce_stdout,
      :stderr => @announce_stderr,
      :dir    => @announce_dir,
      :cmd    => @announce_cmd,
      :env    => @announce_env
    )

    @old_actual.stop(@announcer) unless @old_actual.stopped?

    @actual = Aruba::Platform.unescape(actual.output.chomp, aruba.config.keep_ansi)

    values_match?(expected, @actual)
  end

  diffable

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

if RSpec::Expectations::Version::STRING >= '3.0'
  RSpec::Matchers.alias_matcher :a_command_having_output, :have_output
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions