用ArubaCucumber写到stdin。

问题描述 投票:2回答:2

我在用Aruba向标准输入写东西时遇到了麻烦。 我已经尝试了三种方法。

方法1。

Scenario: Write to stdin take 1
  Given a file named "infile" with:
    """
    Hello World!
    """
  When I run `cat < infile`
  Then the output should contain exactly:
    """
    Hello World!
    """

我得到了以下错误:

  expected: "Hello World!"
       got: "Hello World!cat: <: No such file or directory\n" (using ==)
  Diff:
  @@ -1,2 +1,2 @@
  -Hello World!
  +Hello World!cat: <: No such file or directory
   (RSpec::Expectations::ExpectationNotMetError)
  features/cgi.feature:17:in `Then the output should contain exactly:'

Aruba将'<'按字面意思传过去,而shell会用管道做一些魔法。

方法2:我得到以下错误:Aruba将'<'按字面意思传过去,而shell会用管道做一些魔法。

Scenario: Write to stdin take 2
  When I run `cat` interactively
  And I type "Hello World!"
  Then the output should contain:
    """
    Hello World!
    """

我得到以下错误:

  process still alive after 3 seconds (ChildProcess::TimeoutError)
  features/cgi.feature:25:in `Then the output should contain:'

我不知道,但我假设cat没有收到一个EOF字符,所以cat仍然打开,等待进一步的输入,然后再写。有没有什么方法可以发出信号结束输入?

方法3。

Scenario: Write to stdin take 1
  Given a file named "infile" with:
    """
    Hello World!
    """
  When I run `sh -c "cat < infile"`
  Then the output should contain exactly:
    """
    Hello World!
    """

这个方法可行,但是通过shell进程传递输入 似乎不是理想的解决方案。

我本以为这是一个相当标准的要求,但还没有成功地让它工作。

有什么建议吗?

谢谢。

ruby cucumber bdd functional-testing aruba
2个回答
3
投票

EDIT: 我试着使用交互式模式来管道文件,但是在现实世界的使用中,我发现它明显比使用 sh -c "process < infile"我不太清楚为什么会这样,可能是在Ruby中写到stdin的时候有额外的开销吧。@interactive.stdin.write(input) 也可能是关闭管道需要一点时间。@interactive.stdin.close(). 我最后用的是 sh -c 来解决运行速度变慢的问题。如果需要跨平台支持,那么我期望运行速度慢一点可能是可以接受的。

原文。

我找到了几种方法来解决这个问题 I found a couple of ways to achieve this.

对于采取1。

Scenario: Write to stdin take 1
  Given a file named "infile" with:
    """
    Hello World!
    """
 -When I run `cat < infile`
 +When I run `cat` interactively
 +And I pipe in the file "infile"
  Then the output should contain exactly:
    """
    Hello World!
    """

在方案1中,我去掉了管道(<)的尝试,而是交互式地运行这个过程。 在后端我写了这一步。

When /^I pipe in the file "(.*?)"$/ do |file|
  in_current_dir do
    File.open(file, 'r').each_line do |line|
      _write_interactive(line)
    end
  end
  @interactive.stdin.close()
end

@interactive.stdin.close()应该移到arubaapi.rb中作为一个函数,但是这个想法是可行的。 对_write_interactive的调用也可以说是对type()的调用,但是type()总是会增加一行新的内容,这可能不是我们在管道文件中想要的。

对于采取2。

Scenario: Write to stdin take 2
  When I run `cat` interactively
  And I type "Hello World!"
 +Then I close the stdin stream
  And the output should contain:
    """
    Hello World!
    """

我添加了关闭stdin流,以及后台步骤。

Then /^I close the stdin stream$/ do
  @interactive.stdin.close()
end

再一次,这行应该在arubaapi.rb中加入一个方法,但代码是有效的。


0
投票

I pipe in the file 已经被Aruba实现了,所以你现在可以这样做了(与Allan5的回答有关)。

Scenario: Write to stdin
  Given a file named "infile" with:
    """
    Hello World!
    """
  When I run `cat` interactively
  And I pipe in the file "infile"
  Then the output should contain exactly:
    """
    Hello World!
    """
© www.soinside.com 2019 - 2024. All rights reserved.