Project

hookspec

0.0
No commit activity in last 3 years
No release in over 3 years
write git hooks like spec
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0

Runtime

>= 2.13.0
 Project Readme

hookspec

Lastest version

v1.1.0(beta)

About

As the name, hookspec is an git hook test tool. Write hooks like test!

Install

gem install hookspec

or include in Gemfile

gem 'hookspec'

After hookspec gem is installed, you can use 'hookspec' command. Execute the command to initialize hookspec:

bundle exec hookspec install

Usage

New a spec file under git_hooks/[hook_type]/ named like FILENAME_spec.rb

Sample: Check push branch

# assert branch is not master branch
require 'hookspec'

describe "push" do
  let (:git_env) {HookSpec::GitEnv.new}
  it 'master branch can not be pushed' do
    expect(git_env).not_to be_branch('master')
  end
end

Sample: Check with rubocop

Include rubocop in your Gemfile

describe "rubocop check" do
  let (:git_env) {HookSpec::GitEnv.new}
  it 'rubocop must pass' do
    git_env.diff(model: 'cached', filter: 'AM').each do |f| 
      if f.end_with?('.rb')
        puts `rubocop #{f}`
        expect($?).to be_nil
      end
    end
  end
end

Supported hook_type

  • applypatch-msg
  • pre-commit
  • post-update
  • prepare-commit-msg
  • commit-msg
  • pre-push
  • pre-applypatch
  • update
  • post-push
  • pre-rebase

Samples

how to new get a git envirnment install

git_env = HookSpec::GitEnv.new

how to validate branch name

it 'master branch can not be pushed' do
  expect(git_env).not_to be_branch('master')
end

how to validate include certain file or not

it 'commit not delete certain files' do
  expect(git_env.diff(model: 'cached', filter: 'D').not_to include_file('README.md')
end
  • use git_env.diff(model: model_name, filter: filters)
  • filters: A|C|D|M|R|T|U|X|B
# get cached file list with filter 'AM'
git_env.diff(model: 'cached', filter: 'AM')

how to get git configure

it 'pull rebase must be setted' do
  expect(git_env.conf_of("pull.rebase")).to eql 'true'
end

how to execute git commands

git_env.exec("git status")

More...

More features are to be continued...

commit

fork and send pull-request