我在我的rails3应用程序中使用watchr,因为我不能让自动测试正常工作。Watchr一直很棒,但唯一的问题是我无法获得彩色输出。我正在使用它作为我的watchr file
如果我通过"rspec spec/“手动运行我的测试,那么我会得到彩色输出。但是,让watchr运行测试(通过修改我的一个文件),输出不再是彩色的。
我注意到的一件事是,这部分似乎以某种方式阻止了彩色输出
def run(cmd)
puts(cmd)
`#{cmd}`
end通常,watchr使用以下代码运行测试(在我的watchr文件中)
result = run "rake spec"但是,如果我执行以下操作,我会得到彩色输出。
result = system "rake spec"唯一的问题是,上面这行代码不再捕获"result“的输出,这反过来会导致人类的咆哮通知不再起作用。
我怎么才能两全其美呢?Growl通知和我的彩色测试输出?
发布于 2010-12-11 14:21:53
没有以颜色显示的输出与将测试输出保存到变量有关。我让watchr设置的方法是将"rake spec“输出保存到一个变量中,然后使用”put“将该变量输出到终端。
def run(cmd)
puts(cmd)
`#{cmd}`
end
# what's called to run my tests
result = run "rake spec"
# then output it
puts result现在我只需要弄清楚如何以颜色输出信息!
发布于 2011-02-25 12:15:01
我也遇到了这个问题。解决方案是除了--color之外,还为rspec命令行调用提供--tty选项,例如:
result = run("bundle exec rspec --color --tty #{spec}")根据this thread在rspec google组上的说法:
的--tty选项是用来告诉RSpec输出是流向终端的,而不是文件,在这种情况下,即使调用了--tty,我们也不想打印颜色代码。
如果您随后将结果“放回”终端,您将看到原始的着色。如果您想要解析输出中是否有咆哮,那么您需要使用Bill Turner提供的strip_ansi方法来去除ansi颜色代码。
希望这能有所帮助!
发布于 2010-12-11 12:57:02
这就是我如何在我的.watchr文件中包含这些内容的:
def run(cmd)
`#{cmd}`
end
def run_spec_file(spec)
system('clear')
puts "Running #{spec}"
result = run("bundle exec rspec #{spec}")
puts result
growl result.split("\n").last rescue nil
end最令人恼火的是:
def growl(message)
growlnotify = `which growlnotify`.chomp
unless growlnotify.empty?
title = "Test Results"
image = message.match(/\s0\s(errors|failures)/) ? "~/.watchr_images/ruby_green.png" : "~/.watchr_images/ruby_red.png"
options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{strip_ansi(message)}' '#{title}'"
run("#{growlnotify} #{options} &")
end
end它希望在您的开发环境中的~/..watchr_ images /目录中有一些图像,并且安装了growlnotify binary。
编辑:哦,你可能还想检查一下你的spec.opts文件中有什么--它可能没有指定--color。
growl方法中的strip_ansi被用来剥离颜色代码,这样它就会显示在growl通知中。
def strip_ansi(m)
m.gsub(/\e\[[^m]*m/, '')
endhttps://stackoverflow.com/questions/4414983
复制相似问题