Upgrade to Pro — share decks privately, control downloads, hide ads and more …

バグ報告テンプレートの活用 / Utilization of bug report templates

mh-mobile
October 24, 2020
3.2k

バグ報告テンプレートの活用 / Utilization of bug report templates

バグ報告テンプレートの活用
~ FjordBootcampの歩み方 ~

@mh ⚡️初めてのLT会 Vol.5 (2020.10.24)

mh-mobile

October 24, 2020
Tweet

Transcript

  1. ⾃⼰紹介 @mh 普段はモバイルエンジニア Swift やObjctive-C でアプリやSDK の開発を⾏う モバイル向けの⾃動化ツールのFastlane でRuby を触

    る機会が多い FjordBootcamp は2019 年10 ⽉23 ⽇の開始⽇から今⽇で丸 ⼀年 28 期⽣ マイペースで継続中 継続のコツは草を絶やさないこと 現在は⾃作Web サービスのプラクティスを進⾏中です 2
  2. バグ報告テンプレートについて Rails のissue 提出時に使うバグ再現⽤のスクリプトファイル。 1 ファイルでテストを実⾏することができる。 $ ruby active_record_gem.rb Fetching

    gem metadata from https://rubygems.org/........ Resolving dependencies... ... D, [2020-10-16T16:51:52.344883 #3431] DEBUG -- : Comment Load (0.2ms) SELECT "comments".* FROM "comments" ORDER BY "comments"."id" ASC LIMIT ? [["LIMIT", 1]] D, [2020-10-16T16:51:52.346871 #3431] DEBUG -- : Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] . Finished in 0.048739s, 20.5175 runs/s, 61.5524 assertions/s. 1 runs, 3 assertions, 0 failures, 0 errors, 0 skips 4
  3. バグ報告テンプレートにどのようなものがあるか 種類 対象 action_controller_{gem|master}.rb ActionController active_mailbox_{gem|master}.rb ActiveMailbox active_job_{gem|master}.rb ActiveJob active_record_{gem|master}.rb

    ActiveRecord active_record_migrations_{gem|master}.rb ActiveRecord のマイグレーション action_storage_{gem|master}.rb ActionStorage benchmark.rb ベンチマーク計測 generic_{gem|master}.rb 汎⽤的なバグ https://github.com/rails/rails/tree/master/guides/bug_report_templates xxx_{gem|master}.rb のgem とmaster の違いは? gem は任意バージョンを指定 master は最新バージョンを指定 5
  4. ActiveRecord テンプレートの構成 bunlder/inline gem の読み込み 各設定処理 DB のスキーマ定義 ActiveRecord クラスの定義

    テストクラスの定義 https://github.com/rails/rails/blob/v6.0.2/guides/bug_report_tem plates/active_record_gem.rb 6
  5. bundler/inline # frozen_string_literal: true require "bundler/inline" インラインでGemfile を宣⾔する gemfile メソッドを使えるようになる。

    インラインで依存関係を宣⾔するので、バグ報告する時などスクリプトファイルを共有する際に便利。 gist で共有するのも便利 curl でgist のスクリプトをダウンロードして、スクリプトを実⾏するなど(ただし、安全なスクリプトに限る) $ curl -sSL {gist のraw URL} | ruby unless File.exist?('Gemfile') File.write('Gemfile', <<-GEMFILE) source 'https://rubygems.org' gem 'rails' gem 'mysql2' GEMFILE system 'bundle' end require 'bundler' Bundler.setup(:default) bunlder/inline を使う以前は、実⾏時にGemfile を作成してGem を読み込むテクニックが使われていた。 7
  6. gem の読み込み gemfile(true) do source "https://rubygems.org" git_source(:github) { |repo| "https://github.com/#{repo}.git"

    } # Activate the gem you are reporting the issue against. gem "activerecord", "6.0.0" gem "sqlite3" end gemfile メソッドを使って、インラインで定義されたgem をrequire する。 gem が未インストールの場合は、実⾏時にシステムにインストールする。 8
  7. 各設定処理 require "active_record" require "minitest/autorun" require "logger" # This connection

    will do for database-independent bug reports. ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") ActiveRecord::Base.logger = Logger.new(STDOUT) ActiveRecord::Base.logger で標準出⼒にログを出⼒する。 ActiveRecord::Base.establish_connection メソッドで各DB の接続処理を⾏う。 MySQL PostgreSQL SQLite3 Rails のバグ報告テンプレートで使われている 9
  8. SQLite3 で接続する場合 adapter 属性に sqlite3 を指定する sqlite3 gem の指定が必要 インメモリで保存する場合

    database 属性に :memory: を指定する ActiveRecord::Base.establish_connection( adapter: "sqlite3", database: ":memory:" ) ファイルで保存する場合 database 属性に任意のDB のパスを指定する ActiveRecord::Base.establish_connection( adapter: "sqlite3", database: "./db.sqlite3" ) 10
  9. PostgreSQL で接続する場合 ActiveRecord::Base.establish_connection( adapter: "postgres", host: '{DB ホスト}', database: "{DB

    名}", password: '{ パスワード}', username: '{ ユーザー名}' ) adapter 属性に postgres を指定する その他の属性にDB の接続情報を指定する gemfile(true) do ... gem "activerecord", "6.0.0" gem "pg" end pg gem の指定が必要 11
  10. MySQL で接続する場合 ActiveRecord::Base.establish_connection( adapter: "mysql2", host: '{DB ホスト}', database: "{DB

    名}", password: '{ パスワード}', username: '{ ユーザー名}' ) adapter 属性に mysql2 を指定する その他の属性にDB の接続情報を指定する gemfile(true) do ... gem "activerecord", "6.0.0" gem "mysql2" end mysql2 gem の指定が必要 12
  11. posts id created_at updated_at comments id post_id created_at updated_at DB

    のスキーマ定義 ActiveRecord::Schema.define do create_table :posts, force: true do |t| end create_table :comments, force: true do |t| t.integer :post_id end end ActiveRecord::Schema.define メソッドで、実⾏時にDB の スキーマを定義できる create_table に force: true のオプションを指定すること で、テーブル作成の前に以前のテーブルを削除する 13
  12. Post Comment ActiveRecord::Base ActiveRecord クラスの定義 class Post < ActiveRecord::Base has_many

    :comments end class Comment < ActiveRecord::Base belongs_to :post end 14
  13. テストクラスの定義 class BugTest < Minitest::Test def test_association_stuff post = Post.create!

    post.comments << Comment.create! assert_equal 1, post.comments.count assert_equal 1, Comment.count assert_equal post.id, Comment.first.post.id end end MiniTest のテストメソッドを使って、関連付け動作を検証できる MiniTest のかわりにRSpec を使うこともできる テストデータの⽣成にFactoryBot を使うこともできる。 15
  14. Gemfile Gem インライン定義 スクリプト Rails アプリ DB のスキーマ定義 DB の接続処理

    ActiveRecord クラス テストクラス db/schema.rb db/database.yml app/models 配下のモデル test 配下の テストケース ActiveRecord テンプレートの活⽤ バグ報告以外に特定の機能の動作検証 フォローの関連付けの検証 https://gist.github.com/mh- mobile/2fcf17570bef032872ceb4f48be756a5 ポリモーフィック関連の検証 https://gist.github.com/mh- mobile/01ea7a4e14011b9a59605ffa3de36cab 1 ファイルなので、簡単に動作を再現できる ⼩さな機能に切り分けて検証を⾏いやすい gem にbygbug やpry なども導⼊できるので、デバッグも簡 単 標準でBinding.irb も使える 16
  15. Docker コンテナの利⽤ クリーンな環境で検証できる。 Gem はRuby のコンテナ上のシステムにインストール する。 DB はPostgreSQL のコンテナを利⽤する

    VSCode Remote Containers でDocker コンテナに接続し、 VSCode を使ってスクリプトを実⾏することができる。 ActiveRecord のバグ報告テンプレートをVSCode Remote Containers で使えるように移植したコード https://github.com/mh- mobile/vscode_remote_containers_for_active_record GitHub のCodeSpaces を使うと、ブラウザ上からDocker コンテナを起動し、スクリプトを実⾏することもできる。 17
  16. 接続 接続 Ruby コンテナ PostgreSQL コンテナ コンテナ領域 接続 SQL クライアント

    ローカル領域 マウント ワーク スペース ワーク スペース VS Code VS Code Server .devcontainer 起動 起動 起動 docker- compose.yml devcontainer.json Selenium コンテナ Bootcamp アプリの ソースを配置 (余談)Bootcamp アプリをVSCode Remote Containers で動かす Bootcamp のリポジトリをfork して、VSCode Remote Container 環境を構築 Docker コンテナの活⽤ Rails ⽤のRuby コンテナ DB ⽤のPostgreSQL コンテナ テストブラウザ⽤のSelenium コンテナ Bootcamp アプリをVSCode Remote Containers で使えるよう に移植したコード https://github.com/mh- mobile/bootcamp/tree/master/.devcontainer 19
  17. 参考 バグ報告テンプレート https://github.com/rails/rails/tree/master/guides/bug_report_templates フォローの関連付けの検証⽤のGist https://gist.github.com/mh-mobile/2fcf17570bef032872ceb4f48be756a5 ポリモーフィック関連の検証⽤のGist https://gist.github.com/mh-mobile/01ea7a4e14011b9a59605ffa3de36cab バグ報告テンプレートを知った際のQ&A ポリモーフィック関連のpolymorphic オプションについて

    | FJORD BOOT CAMP (フィヨルドブートキャンプ) https://bootcamp.fjord.jp/questions/510 VSCode Remote Containers 関連 Developing inside a Container using Visual Studio Code Remote Development https://code.visualstudio.com/docs/remote/containers VSCode Dev Container Guidebook https://booth.pm/ja/items/2425642 ActiveRecord のバグ報告テンプレートをVSCode Remote Containers で使えるように移植したコード https://github.com/mh-mobile/vscode_remote_containers_for_active_record Bootcamp アプリをVSCode Remote Containers で使えるように移植したコード https://github.com/mh-mobile/bootcamp/tree/master/.devcontainer 20