Continue(s)

Twitter:@dn0t_ GitHub:@ogrew

perlでHashの一部分をテストで確認したいときにsuperhashof便利やん。

f:id:taiga006:20180509225412p:plain:w500
use Test::More;
use Test::Deep;

my $user = {
        name => '齋藤飛鳥',
        age => 19,
        height => 158,
        blood_type => 'O',
        center => '裸足でSummer',
    };

is $user->{age}, 19;
cmp_deeply $user, superhashof { name => '齋藤飛鳥', blood_type => 'O' };

done_testing;

[output]

₍ ᕕ( ‘ω’)ᕗ⁾ $ prove -lvf superhashof.pl
superhashof.pl ..
ok 1
ok 2
1..2
ok
All tests successful.
Files=1, Tests=2,  0 wallclock secs ( 0.03 usr  0.01 sys +  0.07 cusr  0.01 csys =  0.1
2 CPU)
Result: PASS

こける場合はこんな感じ。

use Test::More;
use Test::Deep;

my $user_2 = {
        name => '斉藤優里',
        age => 24,
        height => 157,
        blood_type => 'O',
        center => '13日の金曜日',
    };

cmp_deeply $user_2, superhashof { name => '齋藤優里', height => 157 };

done_testing;

[output]

₍ ᕕ( ‘ω’)ᕗ⁾ $ prove -lvf superhashof.pl
superhashof.pl ..
not ok 1
#   Failed test at superhashof.pl line 12.
# Compared $data->{"name"}
#    got : '斉藤優里'
# expect : '齋藤優里'
1..1
# Looks like you failed 1 test of 1.
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/1 subtests

Test Summary Report
-------------------
superhashof.pl (Wstat: 256 Tests: 1 Failed: 1)
  Failed test:  1
  Non-zero exit status: 1
Files=1, Tests=1,  0 wallclock secs ( 0.02 usr  0.00 sys +  0.05 cusr  0.01 csys =  0.08 CPU)
Result: FAIL

[参考]

Test::Deep - search.cpan.org

gitのfor-each-refで各ブランチで最後にcommitした人とその更新時刻を一覧表示させる。

普段はあまりそんな状況ないんですがふと今の作業ブランチだけでなくて、すべてのブランチを対象に最新のcommit情報をその更新時刻付きで表示したくなり「うーん、うまいやり方ないかなー」と思ってググったらgit for-each-refが使えるとのこと。知らなかった。

公式ドキュメント

Git - git-for-each-ref Documentation

【例】

git for-each-ref \
--sort=-committerdate \
--count=10 \
--format="
Date: %(color:red)%(authordate:iso)%(color:reset)
%(color:green)[%(authorname)]%(color:reset)
Ref: %(color:yellow)%(refname:short)%(color:reset)
Subject: %(subject)" \
refs/heads refs/remotes

上の例だと

  • 最新のコミット順
  • 10件
  • 日付とブランチ名とユーザ名とcommitメッセージ含め

て表示してくれる。 (色とかつけてるからごちゃごちゃしてるけど、整理すればシンプル。)

for-each-refは単体で使うよりこれを使ってブランチ一覧を取得してその上で何かする、みたいな状況で利用されることが多いらしい。

とはいえ、上の例を参考に上手く使えば
「さっき作業してたブランチなんだっけ?」
とか
「あのブランチって誰がいつ切ったの?」
みたいな調査が楽にできそう。

長いので.gitconfigにaliasを追加しておけば良さそう。

[alias]
    history = for-each-ref --sort=-committerdate --count=10 --format='Date: %(color:red)%(authordate:iso)%(color:reset)\t%(color:green)[%(authorname)]%(color:reset)\nRef: %(color:yellow)%(refname:short)%(color:reset)\nSubject: %(subject)\n' refs/heads refs/remotes

f:id:taiga006:20180504172346p:plain

RedashのRepo.で試しにやってみた図)


...と思ったらすでにこういうのがあるらしい。 まあ、誰かしら作ってるとは思ったけど。

github.com


参考

shuzo-kino.hateblo.jp

kakakakakku.hatenablog.com