0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

--force-color、--format documentation

Posted at

ファイルからコマンドライン設定オプションを読むこと

Reading command line configuration options from files

RSpecは複数の異なるファイルからコマンドライン設定オブションを読み、特定の特異度のレベルに全て従います。より高い特異度からのオプションはより低い特異度ファイルから矛盾する設定オプションをオーバーライドするだろう

RSpec reads command line configuration options from several different files, all conforming to a specific level of specificity. Options from a higher specificity will override conflicting options from lower specificity files.

位置は、

The locations are:

# グローバルオプション:以下のリストからの最初ののファイル(言い換えるとユーザーの個人的なグローバルオプション)
* **Global options:** First file from the following list (i.e. the user's
  personal global options)
  
  * `$XDG_CONFIG_HOME/rspec/options` ([XDG Base Directory
    Specification](https://specifications.freedesktop.org/basedir-spec/latest/)
    config)
  * `~/.rspec`
# プロジェクトオプション:`./.rspec`(言い換えると、プロジェクトのルートディレクトリ内にある、通常はチェックインする
* **Project options:**  `./.rspec` (i.e. in the project's root directory, usually
  checked into the project)
# ローカル:`./.rspec-local`(プロジェクトのルートディレクトリ内にある場合は、gitignoredすることができる)
* **Local:** `./.rspec-local` (i.e. in the project's root directory, can be
  gitignored)

コマンドラインで指定されたオプションはSPEC_OPTS環境変数と同じようにより高い特異度を持つ。つまり、コマンドラインオプションはproject-specificオプションを上書きし、オプションのグローバル値をオーバーライドする

Options specified at the command-line has even higher specificity, as does the SPEC_OPTS environment variable. That means that a command-line option would overwrite a project-specific option, which overrides the global value of that option.

初期オプションファイルは--options command-line 引数を使って全てを無視し、オプションフォームをロードするカスタムファイルを選びます。

The default options files can all be ignored using the --options command-line argument, which selects a custom file to load options from.

Color set in .rspec

Given a file named “.rspec” with:

--force-color
RSpec.describe "color_enabled?" do
  context "when set with RSpec.configure" do
    it "is true" do
      expect(RSpec.configuration).to be_color_enabled
    end
  end
end
rspec ************.rb --force-color
Finished in 0.03134 seconds (files took 1.07 seconds to load)
1 example, 0 failures

3.6から自動でカラー出力される(色をつけて表示する わかりやすくなる)

Core: 出力先が TTY の場合、自動でカラー出力が有効に
これまでの RSpec では、カラー出力をしたい場合、 出力先がターミナルであろうが、ファイルであろうが、CI 環境であろうが、 常に --color オプションを指定する必要がありました。 RSpec 3.6 では、出力先が TTY の場合はカラー出力が自動的に有効化されるようになりました。 これまで通り、出力先に関わらず常にカラーを有効にしたい場合は --color オプションを使えますし、 --no-color オプションを使えば TTY でカラー出力を明示的に無効化することもできます。

ノーカラーにするとエラーになる 

 rspec .***************.rb --no-color
F

Failures:

  1) color_enabled? when set with RSpec.configure is true
     Failure/Error: expect(RSpec.configuration).to be_color_enabled
       expected `#<RSpec::Core::Configuration:0x0000000108803e88 @start_time=2025-01-09 13:36:47.811742 +0900, @expect...025-01-09 13:36:50.042378 +0900, :description=>"color_enabled?"}}>>, @default_formatter="progress">>.color_enabled?` to be truthy, got false
     # .***************.rb:18:in `block (3 levels) in <top (required)>'

Finished in 0.04539 seconds (files took 2.22 seconds to load)
1 example, 1 failure

Failed examples:

rspec .***************.rb:17 # color_enabled? when set with RSpec.configure is true
RSpec.describe "color_enabled?" do
  context "when set with RSpec.configure" do
    it "is true" do

    end
  end
end
 rspec ***********.rb --no-color
.

Finished in 0.0306 seconds (files took 2.03 seconds to load)
1 example, 0 failures <---- このオプションをつけると文字に色がつかない 

Custom options file

--format documentation
RSpec.describe "formatter set in custom options file" do
  it "sets formatter" do
    expect(RSpec.configuration.formatters.first).
      to be_a(RSpec::Core::Formatters::DocumentationFormatter)
  end
end

オプションをつけると

formatter set in custom options file
  sets formatter

Finished in 0.02056 seconds (files took 1.03 seconds to load)
1 example, 0 failures

オプションをつけないと

rspec spec/models/user_spec.rb             
F

Failures:

  1) formatter set in custom options file sets formatter
     Failure/Error:
       expect(RSpec.configuration.formatters.first).
         to be_a(RSpec::Core::Formatters::DocumentationFormatter)
     
       expected #<RSpec::Core::Formatters::ProgressFormatter:0x000000010e993248 @output=#<IO:<STDOUT>>, @example_group=RSpec::ExampleGroups::FormatterSetInCustomOptionsFile, @old_sync=false, @example_count=1> to be a kind of RSpec::Core::Formatters::DocumentationFormatter
     # .**************.rb:17:in `block (2 levels) in <top (required)>'

Finished in 0.02489 seconds (files took 1.01 seconds to load)
1 example, 1 failure

Failed examples:

rspec .**************.rb:16 # formatter set in custom options file sets formatter

--format documentationとは

desicribeit、それらのエイリアスへ渡されたドキュメント文字列を見るために、ドキュメントフォーマッタを使う。

Use the documentation formatter to see the documentation strings passed to describe, it, and their aliases:

 $ rspec spec --format documentation
標準出力を変更できる

あなたはformatオプションにすぐに続いて--outオプションを使って出力目的(初期では$stdout)を明示することができる。

You can also specify an output target ($stdout by default) with an --out option immediately following the --format option:

 $ rspec spec --format documentation --out rspec.txt

itやdiscribeの文をつけて読みやすくなる
rspec spec/models/user_spec.rb --format doc

Finished in 0.03251 seconds (files took 2.07 seconds to load)
1 example, 0 failures

 rspec spec/models/user_spec.rb --format doc
...
formatter set in custom options file
  sets formatter

Finished in 0.02196 seconds (files took 1.07 seconds to load)
1 example, 0 failures

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?