oss-gate / workshop

OSSの開発に未参加または参加したことはあるけどまだ自信がない人を後押しするワークショップ用のリポジトリー
124 stars 547 forks source link

OSS Gate Workshop: Online: 2020-09-12: mogumogu-tatsugoro: rspec-core: Work log #1421

Closed mogumogu-tatsugoro closed 3 years ago

mogumogu-tatsugoro commented 4 years ago

This is a work log of a "OSS Gate workshop". "OSS Gate workshop" is an activity to increase OSS developers. Here's been discussed in Japanese. Thanks.

作業ログ作成時の説明

以下のテンプレートを埋めてタイトルに設定します。埋め方例はスクロールすると見えてきます。

OSS Gate Workshop: ${LOCATION}: ${YEAR}-${MONTH}-${DAY}: ${ACCOUNT_NAME}: ${OSS_NAME}: Work log

タイトル例↓:

OSS Gate Workshop: Tokyo: 2017-01-16: kou: Rabbit: Work log

OSS Gateワークショップ関連情報

mogumogu-tatsugoro commented 4 years ago

rspec-core が OSS なのかを確認します

ogom commented 4 years ago

サポータです。よろしくお願いします。

mogumogu-tatsugoro commented 4 years ago

よろしくお願いいたします 🙏

mogumogu-tatsugoro commented 4 years ago

https://github.com/rspec/rspec-core/blob/main/LICENSE.md

The MIT License (MIT)

上記から MIT License (MIT) であることがわかったので MIT License (MIT) がオープンソースライセンスなのかを確認します!

mogumogu-tatsugoro commented 4 years ago

https://opensource.org/licenses/alphabetical

MIT License (MIT)

オープンソースライセンスであることがわかりました 👀

mogumogu-tatsugoro commented 4 years ago

https://github.com/rspec/rspec-core#install

上記を参照し、ローカルで動作させて参ります!

mogumogu-tatsugoro commented 4 years ago
% gem install rspec
Fetching rspec-3.9.0.gem
Fetching rspec-mocks-3.9.1.gem
Fetching rspec-support-3.9.3.gem
Fetching rspec-core-3.9.2.gem
Fetching diff-lcs-1.4.4.gem
Fetching rspec-expectations-3.9.2.gem
Successfully installed rspec-support-3.9.3
Successfully installed rspec-core-3.9.2
Successfully installed diff-lcs-1.4.4
Successfully installed rspec-expectations-3.9.2
Successfully installed rspec-mocks-3.9.1
Successfully installed rspec-3.9.0
Parsing documentation for rspec-support-3.9.3
Installing ri documentation for rspec-support-3.9.3
Parsing documentation for rspec-core-3.9.2
Installing ri documentation for rspec-core-3.9.2
Parsing documentation for diff-lcs-1.4.4
Installing ri documentation for diff-lcs-1.4.4
Parsing documentation for rspec-expectations-3.9.2
Installing ri documentation for rspec-expectations-3.9.2
Parsing documentation for rspec-mocks-3.9.1
Installing ri documentation for rspec-mocks-3.9.1
Parsing documentation for rspec-3.9.0
Installing ri documentation for rspec-3.9.0
Done installing documentation for rspec-support, rspec-core, diff-lcs, rspec-expectations, rspec-mocks, rspec after 13 seconds
6 gems installed
mogumogu-tatsugoro commented 4 years ago
gem install rspec-core
Successfully installed rspec-core-3.9.2
Parsing documentation for rspec-core-3.9.2
Done installing documentation for rspec-core after 2 seconds
1 gem installed
mogumogu-tatsugoro commented 4 years ago
% rspec --help
Usage: rspec [options] [files or directories]

    -I PATH                            Specify PATH to add to $LOAD_PATH (may be used more than once).
    -r, --require PATH                 Require a file.
    -O, --options PATH                 Specify the path to a custom options file.
        --order TYPE[:SEED]            Run examples by the specified order type.
                                         [defined] examples and groups are run in the order they are defined
                                         [rand]    randomize the order of groups and examples
                                         [random]  alias for rand
                                         [random:SEED] e.g. --order random:123
        --seed SEED                    Equivalent of --order rand:SEED.
        --bisect[=verbose]             Repeatedly runs the suite in order to isolate the failures to the
                                         smallest reproducible case.
        --[no-]fail-fast[=COUNT]       Abort the run after a certain number of failures (1 by default).
        --failure-exit-code CODE       Override the exit code used when there are failing specs.
    -X, --[no-]drb                     Run examples via DRb.
        --drb-port PORT                Port to connect to the DRb server.

  **** Output ****

    -f, --format FORMATTER             Choose a formatter.
                                         [p]rogress (default - dots)
                                         [d]ocumentation (group and example names)
                                         [h]tml
                                         [j]son
                                         [f]ailures ("file:line:reason", suitable for editors integration)
                                         custom formatter class name
    -o, --out FILE                     Write output to a file instead of $stdout. This option applies
                                         to the previously specified --format, or the default format
                                         if no format is specified.
        --deprecation-out FILE         Write deprecation warnings to a file instead of $stderr.
    -b, --backtrace                    Enable full backtrace.
        --force-color, --force-colour  Force the output to be in color, even if the output is not a TTY
        --no-color, --no-colour        Force the output to not be in color, even if the output is a TTY
    -p, --[no-]profile [COUNT]         Enable profiling of examples and list the slowest examples (default: 10).
        --dry-run                      Print the formatter output of your suite without
                                         running any examples or hooks
    -w, --warnings                     Enable ruby warnings

  **** Filtering/tags ****

    In addition to the following options for selecting specific files, groups, or
    examples, you can select individual examples by appending the line number(s) to
    the filename:

      rspec path/to/a_spec.rb:37:87

    You can also pass example ids enclosed in square brackets:

      rspec path/to/a_spec.rb[1:5,1:6] # run the 5th and 6th examples/groups defined in the 1st group

        --only-failures                Filter to just the examples that failed the last time they ran.
    -n, --next-failure                 Apply `--only-failures` and abort after one failure.
                                         (Equivalent to `--only-failures --fail-fast --order defined`)
    -P, --pattern PATTERN              Load files matching pattern (default: "spec/**/*_spec.rb").
        --exclude-pattern PATTERN      Load files except those matching pattern. Opposite effect of --pattern.
    -e, --example STRING               Run examples whose full nested names include STRING (may be
                                         used more than once)
    -E, --example-matches REGEX        Run examples whose full nested names match REGEX (may be
                                         used more than once)
    -t, --tag TAG[:VALUE]              Run examples with the specified tag, or exclude examples
                                       by adding ~ before the tag.
                                         - e.g. ~slow
                                         - TAG is always converted to a symbol
        --default-path PATH            Set the default path where RSpec looks for examples (can
                                         be a path to a file or a directory).

  **** Utility ****

        --init                         Initialize your project with RSpec.
    -v, --version                      Display the version.
    -h, --help                         You're looking at it.
mogumogu-tatsugoro commented 4 years ago

普段 Rails の中でしか利用をしたことがないので、単体での利用方法を確認中です 👀 ref. https://github.com/rspec/rspec-core

mogumogu-tatsugoro commented 4 years ago

https://github.com/rspec/rspec-core#the-rspec-command

The rspec Command When you install the rspec-core gem, it installs the rspec executable, which you'll use to run rspec. The rspec command comes with many useful options. Run rspec --help to see the complete list.

rspec-core を入れると rspec コマンドを利用できるようになる オプションは rspec --help で確認

mogumogu-tatsugoro commented 4 years ago

https://github.com/rspec/rspec-core#get-started

上記確認して参ります!

mogumogu-tatsugoro commented 4 years ago

Address the failure by defining a skeleton of the Calculator class:

# in lib/calculator.rb
class Calculator
def add(a, b)
end
end

上記作成しました!

mogumogu-tatsugoro commented 4 years ago

少し手順書と順番が前後してしまいましたが、以下を作成いたしました!

# in spec/calculator_spec.rb
RSpec.describe Calculator do
  describe '#add' do
    it 'returns the sum of its arguments' do
      expect(Calculator.new.add(1, 2)).to eq(3)
    end
  end
end
mogumogu-tatsugoro commented 4 years ago

readme の通り、テストが落ちることを確認

% rspec spec/calculator_spec.rb

An error occurred while loading ./spec/calculator_spec.rb.
Failure/Error:
  RSpec.describe Calculator do
    describe '#add' do
      it 'returns the sum of its arguments' do
        expect(Calculator.new.add(1, 2)).to eq(3)
      end
    end
  end

NameError:
  uninitialized constant Calculator
# ./spec/calculator_spec.rb:2:in `<top (required)>'
No examples found.

Finished in 0.00007 seconds (files took 0.3673 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
mogumogu-tatsugoro commented 4 years ago

以下を spec/calculator_spec.rb に追記いたしました!

# in spec/calculator_spec.rb
# - RSpec adds ./lib to the $LOAD_PATH
require "calculator"
mogumogu-tatsugoro commented 4 years ago

テストが期待値と違うことで落ちていることを確認いたしました!

% rspec spec/calculator_spec.rb
F

Failures:

  1) Calculator#add returns the sum of its arguments
     Failure/Error: expect(Calculator.new.add(1, 2)).to eq(3)

       expected: 3
            got: nil

       (compared using ==)
     # ./spec/calculator_spec.rb:6:in `block (3 levels) in <top (required)>'

Finished in 0.05996 seconds (files took 0.38288 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/calculator_spec.rb:5 # Calculator#add returns the sum of its arguments
mogumogu-tatsugoro commented 4 years ago

lib/calculator.rb の add メソッドの修正を行いました!

def add(a, b)
  a + b
end
mogumogu-tatsugoro commented 4 years ago

無事テストが通り、動作確認ができました 🎉

rspec spec/calculator_spec.rb
.

Finished in 0.0152 seconds (files took 0.37692 seconds to load)
1 example, 0 failures
mogumogu-tatsugoro commented 4 years ago

以下も確認できました!

% rspec spec/calculator_spec.rb --format doc
Calculator
  #add
    returns the sum of its arguments

Finished in 0.00454 seconds (files took 0.32838 seconds to load)
1 example, 0 failures
mogumogu-tatsugoro commented 4 years ago

テスト駆動の考え方から伝えてくれている素敵な readme だなと思いました ✨

mogumogu-tatsugoro commented 4 years ago

とにかく rspec を動かしたくてテスト駆動の readme 流れを無視してしまった 💦

mogumogu-tatsugoro commented 4 years ago

母国語でないので、コードからあたりをつけてとにかくデータを作ろうとしてしまったのかもしれない 💭

mogumogu-tatsugoro commented 4 years ago

https://github.com/oss-gate/workshop/issues/1421#issuecomment-691416860 のような(なぜ手順を飛ばしたかの動機的な)部分を言語化しておかないと、慣れて忘れてしまうのでメモ大切なので、普段の開発に活かして参ります!

mogumogu-tatsugoro commented 4 years ago

ogom さんはなんでも触れる Ruby や TypeScript や python などなんでも利用されている

mogumogu-tatsugoro commented 4 years ago

ライブラリを書いたりもなされている

mogumogu-tatsugoro commented 4 years ago

OSS 活動はやりたい時にできる!!!!!

mogumogu-tatsugoro commented 4 years ago

普段PRを送る動機 新しいライブラリでバグを踏んだり、機能が欲しくてPR送ったり

mogumogu-tatsugoro commented 4 years ago

今回の作業の中で readme に課題があるリポジトリとかもあると思うので、そういうところでも修正することができそう

mogumogu-tatsugoro commented 4 years ago

普段から課題を見つける意識を持つことが大切そう

mogumogu-tatsugoro commented 4 years ago

やりたいことに対して疑問があるので後ほど教えていただけると助かります 🙇

疑問点

やりたいこと特別なことでないので過去にも議論がありそうだと思うのですが、その辺りの調査方法は issue を見るとかになるのでしょうか 👀

課題だと感じていること

以下の to メソッドは not_to があるのですが、 and した際には not することができないのが課題だと思っています。

  expect { subject }.to change { x }.by(1).
                    and change { y }.by(2)

できるようにしたいイメージ

  expect { subject }.not_to change { x }.
                    and_not change { y }
mogumogu-tatsugoro commented 4 years ago

ハクトーバーフェスト 👀

mogumogu-tatsugoro commented 4 years ago

Issue をやるよりは、自分の要望が多そう? 似た Issue が上がっていればコメント

mogumogu-tatsugoro commented 4 years ago

コントリビューターとして実績を積むためにとかの動機もありそう

mogumogu-tatsugoro commented 4 years ago

とりあえず課題を見付けたら報告

mogumogu-tatsugoro commented 4 years ago

反応なければ原因特定してパッチ修正送ってしまってもいいかも

mogumogu-tatsugoro commented 4 years ago

リアクションもらえるかとかの雰囲気はリポジトリの大きさやクローズ具合とかの動きがあるかを見るといいかも

mogumogu-tatsugoro commented 4 years ago

ワークショップ内でフリートークの時間を組み込んでもいいかもしれない

@piroor さんのワークショップに対する課題意識も高くて見習って行きたい 👀

mogumogu-tatsugoro commented 4 years ago

メモは対象リポジトリをフォークして、そこでなさっているのか確認しよう 👀

mogumogu-tatsugoro commented 4 years ago

困っていることの具体例とこうして欲しいという希望を記載すると伝わりやすいとのことなので、記載して参ります!

mogumogu-tatsugoro commented 4 years ago

現状の対応の仕方です

class Example
  attr_accessor :a, :b, :c

  def init
    @a = 'not_changed_with_change_c'
    @b = 'not_changed_with_change_c'
  end

  def change_c
    @c = 'changed_with_change_c'
  end
end
require "example"

RSpec.describe Example do
  describe '#change_c' do
    subject { ex.change_c }
    let(:ex) { Example.new }

    it 'change only ex.c' do
      expect{ subject }.to change{ ex.c }.to('changed_with_change_c')
      expect{ subject }.not_to change{ ex.a }
      expect{ subject }.not_to change{ ex.b }
    end
  end
end

以下のような対応ができれば良いなと思っています!

require "example"

RSpec.describe Example do
  describe '#change_c' do
    subject { ex.change_c }
    let(:ex) { Example.new }

    it 'change only ex.c' do
      expect{ subject }.to change{ ex.c }.to('changed_with_change_c').
        and_not change{ ex.a }.
        and_not change{ ex.b }
    end
  end
end
mogumogu-tatsugoro commented 4 years ago

正常疎通させる例と、こう書きたいがこう書くと落ちてしまうというのを記載してまいります!

mogumogu-tatsugoro commented 4 years ago

https://github.com/oss-gate/workshop/issues/1421#issuecomment-691431852 の後の例の実行結果です!

% rspec
F

Failures:

  1) Example#change_c change only ex.c
     Failure/Error:
       expect{ subject }.to change{ ex.c }.to('changed_with_change_c').
         and_not change{ ex.a }.
         and_not change{ ex.b }

     NoMethodError:
       undefined method `and_not' for #<RSpec::Matchers::BuiltIn::Change:0x00007fe68b884180>
     # ./spec/example_spec.rb:10:in `block (3 levels) in <top (required)>'

Finished in 0.02387 seconds (files took 0.63064 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/example_spec.rb:8 # Example#change_c change only ex.c
ogom commented 4 years ago

rspec の and は & の記号に置きかえることできます。

ogom commented 4 years ago

and_not は &! になるかも

ogom commented 4 years ago

RSpec::Matchers.define_negated_matcher :not_change, :change を試してください。 https://relishapp.com/rspec/rspec-expectations/v/3-9/docs/define-negated-matcher

mogumogu-tatsugoro commented 4 years ago

ドキュメントをすみまで読むことで見付けられる

mogumogu-tatsugoro commented 4 years ago

Changelog で見つかることも

mogumogu-tatsugoro commented 4 years ago

everydayrails に not_change の記載はなさそう 👀 -> everydayrails にフィードバックしてもよさそう

mogumogu-tatsugoro commented 4 years ago

OSS はたくさんの人が幸せになる世界を目指している ✨