我在用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:
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进程传递输入似乎不是理想的解决方案。
我本以为这是一个相当标准的要求,但还没有成功地让它工作。
有什么建议吗?
谢谢。
发布于 2012-09-06 12:06:10
编辑:我尝试使用交互式模式来输入文件,但是在实际使用中我发现它比使用sh -c "process < infile"慢得多,我不太确定为什么会这样,在Ruby @interactive.stdin.write(input)中写入标准输入可能会有额外的开销,或者关闭管道需要一点@interactive.stdin.close()时间。我最终使用了sh -c来绕过速度慢的问题。如果需要跨平台支持,我希望较慢的运行时可能是可以接受的。
原文:
我找到了几种方法来实现这一点。
对于take 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()应该作为一个函数移动到aruba/api.rb,但是这个想法是可行的。可以说,对_write_interactive的调用也应该是对type()的调用,但是type()总是添加新的一行,这可能不是我们在通过管道输入文件时想要的。
对于take 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同样,这一行应该在aruba/api.rb中变成一个方法,但是代码可以工作。
发布于 2020-06-06 01:10:46
I pipe in the file已经被阿鲁巴实现了,所以你现在可以这样做了(与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!
"""https://stackoverflow.com/questions/12170071
复制相似问题