如何在 Ruby 中使用 RSpec 在 Windows 命令提示符下获取颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1733348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:26:28  来源:igfitidea点击:

How do I get colour with Windows command prompt using RSpec in Ruby?

ruby-on-railswindowsrubyrspeccommand-prompt

提问by Evolve

In other o/s RSpec returns nicely coloured results (red, green etc).

在其他 o/s 中,RSpec 返回颜色很好的结果(红色、绿色等)。

However in the windows (Vista) command prompt my text output is just plain old boring white.

然而,在 windows (Vista) 命令提示符下,我的文本输出只是普通的、无聊的白色。

How can I bring colour to my RSpec test results?

如何为 RSpec 测试结果增添色彩?

Thanks

谢谢

Evolve

发展

采纳答案by ob1

UPDATE: Win32Console no longer works with rspec. ANSICON recommended. https://github.com/rspec/rspec-rails/issues/487#issuecomment-3556806

更新:Win32Console 不再适用于 rspec。推荐使用 ANSICON。 https://github.com/rspec/rspec-rails/issues/487#issuecomment-3556806

回答by Stéphane Gerber

I had to install ansicon, and now everything is fine. (Even in my Aptana terminal).

我不得不安装 ansicon,现在一切都很好。(即使在我的 Aptana 终端中)。

Install ansicon description: http://qastuffs.blogspot.com/2011/02/how-to-install-ansicon-for-cucumber-to.html

安装ansicon说明:http://qastuffs.blogspot.com/2011/02/how-to-install-ansicon-for-cucumber-to.html

回答by Selva

Did you specify '--color' in your rake rspec tasks' spec_opts? Something like this..

您是否在 rake rspec 任务的 spec_opts 中指定了“--color”?像这样的东西..

  Spec::Rake::SpecTask.new(:your_task_name) do |t|
    t.spec_opts = ["--color"]
    t.spec_files = [] # List of spec files
  end

回答by J?rg W Mittag

You need to install the win32consolegem. BTW: which version of RSpec are you using? Every single version of RSpec I have ever used, actually prints out

您需要安装win32consolegem。顺便说一句:您使用的是哪个版本的 RSpec?我用过的每一个 RSpec 版本,实际上都会打印出来

You must 'gem install win32console' to use colour on Windows

when you try to colorize output.

当您尝试为输出着色时。

回答by Douglas G. Allen

I'm running in Windows XP which I believe is an NT system. The ANSICON claims that this is not supported. Yes it does not work for cmd. But if you put the files in system32 folder, just run the ansicon.exe from anywhere. Win32Console is deprecated for rspec but you may still use it for other programs you write if you want color output from the command line. see the readmes in the gem. I put ansicon in my start>run>open text box. It works just fine.

我在 Windows XP 中运行,我认为它是一个 NT 系统。ANSICON 声称这不受支持。是的,它不适用于 cmd。但是,如果您将文件放在 system32 文件夹中,则只需从任何地方运行 ansicon.exe。Win32Console 不推荐用于 rspec 但如果您想从命令行输出颜色,您仍然可以将它用于您编写的其他程序。请参阅 gem 中的自述文件。我把 ansicon 放在我的开始>运行>打开文本框中。它工作得很好。

回答by Steve Bonds

When using the MINGW64 bash shell provided by Git on Windows rspec displays things without any color. While this shell environment is perfectly capable of displaying ANSI color, rspec fails to detect it as a TTY. A super simple workaround is to use:

在 Windows 上使用 Git 提供的 MINGW64 bash shell 时,rspec 显示没有任何颜色的东西。虽然此 shell 环境完全能够显示 ANSI 颜色,但 rspec 无法将其检测为 TTY。一个超级简单的解决方法是使用:

$ rspec --force-color

For example, take this boring old monochrome: :Boring old monochrome

以这个无聊的旧单色为例:Boring old monochrome

And make it happy with one simple command line argument: Wow, it's beautiful full color!

并使用一个简单的命令行参数使其满意: Wow, it's beautiful full color!

回答by IslandCoder

I had this issue. I've been unable to get ansicon to work, as it crashes when running from cmd.

我有这个问题。我一直无法让 ansicon 工作,因为它在从 cmd 运行时崩溃。

To fix I added the Win32consolegem to my gem fileand then ran bundle update.

为了修复,我将Win32consolegem添加到我的gem 文件中,然后运行 ​​bundle update。

**As an update: Put the gem in the test group of your gemfile:

**作为更新:将 gem 放入 gemfile 的测试组中:

    group :test do
      gem 'rails-controller-testing', '1.0.2'
      gem 'minitest-reporters',       '1.1.14'
      gem 'guard',                    '2.13.0'
      gem 'guard-minitest',           '2.4.4'
      gem 'win32console'
    end

This will prevent errors when pushing to heroku

这将防止推送到 heroku 时出错

Cheers!

干杯!

回答by Shikasan

The best method is to create a file name .rspec in the root folder of your app and include this single line of code in it:

最好的方法是在您的应用程序的根文件夹中创建一个文件名 .rspec 并在其中包含以下一行代码:

--colour

--colour

Done and dusted

尘埃落定