Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility for using multiple file types (#101) #103

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func init() {

// InstallCmdExecutor execute basic configuration
func InstallCmdExecutor(args []string, fs afero.Fs) {
if yes, _ := afero.Exists(fs, getConfigYamlPath()); yes {
if hasValidConfigFile(fs) {
if !isConfigSync(fs) || force || aggressive {
log.Println(au.Cyan("SYNCING"), au.Bold("lefthook.yml"))
DeleteGitHooks(fs)
Expand Down Expand Up @@ -146,8 +146,28 @@ func getConfigYamlPath() string {
return filepath.Join(getRootPath(), configFileName) + configExtension
}

func getConfigLocalYamlPath() string {
return filepath.Join(getRootPath(), configLocalFileName) + configExtension
func getConfigYamlPattern() string {
return filepath.Join(getRootPath(), configFileName) + configExtensionPattern
}

func getConfigLocalYamlPattern() string {
return filepath.Join(getRootPath(), configLocalFileName) + configExtensionPattern
}

func hasValidConfigFile(fs afero.Fs) bool {
matches, err := afero.Glob(fs, getConfigYamlPattern())
if err != nil {
log.Println("Error occured for search config file: ", err.Error())
}
for _, match := range matches {
extension := filepath.Ext(match)
for _, possibleExtension := range configFileExtensions {
if extension == possibleExtension {
return true
}
}
}
return false
}

func contains(a []string, x string) bool {
Expand All @@ -165,7 +185,10 @@ func isConfigSync(fs afero.Fs) bool {

func configChecksum(fs afero.Fs) string {
var returnMD5String string
file, err := fs.Open(configFileName + configExtension)
matches, err := afero.Glob(fs, getConfigYamlPattern())
primaryMatch := matches[0]
check(err)
file, err := fs.Open(primaryMatch)
check(err)
defer file.Close()

Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
configLocalFileName string = "lefthook-local"
configExtendsOption string = "extends"
configExtension string = ".yml"
configExtensionPattern string = ".*"
defaultFilePermission os.FileMode = 0755
defaultDirPermission os.FileMode = 0666
)
Expand All @@ -31,6 +32,7 @@ var (
rootPath string
cfgFile string
originConfig *viper.Viper
configFileExtensions = []string{".yml", ".yaml"}

au aurora.Aurora
)
Expand Down
14 changes: 11 additions & 3 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,17 @@ func deleteConfig(fs afero.Fs) {
log.Println(getConfigYamlPath(), "removed")
}

err = fs.Remove(getConfigLocalYamlPath())
if err == nil {
log.Println(getConfigLocalYamlPath(), "removed")
results, err := afero.Glob(fs, getConfigLocalYamlPattern())
if err != nil {
log.Println("Error occured while remove config file!:", err.Error())
}
for _, fileName := range results {
err = fs.Remove(getConfigLocalYamlPattern())
if err == nil {
log.Println(fileName, "removed")
} else {
log.Println("Error occured while remove config file!:", err.Error())
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions spec/fixtures/lefthook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pre-push:
scripts:
ok_script: ""
fail_script: ""

pre-commit:
scripts:
ok_script: ""
fail_script: ""
24 changes: 1 addition & 23 deletions spec/install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,17 @@
end

describe 'install after cloning repo with existed lefthook structure' do
let(:pre_push_hook) do
File.join(FileStructure.tmp, '.git', 'hooks', 'pre-push')
end
let(:pre_commit_hook) do
File.join(FileStructure.tmp, '.git', 'hooks', 'pre-commit')
end
let(:expected_pre_push_hook) { FileStructure.pre_push_hook_path }
let(:expected_pre_commit_hook) { FileStructure.pre_commit_hook_path }

before do
FileStructure.make_config
FileStructure.make_scripts_preset
_, @status = Open3.capture2(command)
end

include_examples 'hook examples'
it 'exit with 0 status' do
expect(@status.success?).to be_truthy
end

it 'create pre-push git hook' do
expect(File.exist?(pre_push_hook)).to be_truthy
expect(
FileUtils.compare_file(expected_pre_push_hook, pre_push_hook)
).to be_truthy
end

it 'create pre-commit git hook' do
expect(File.exist?(pre_commit_hook)).to be_truthy
expect(
FileUtils.compare_file(expected_pre_commit_hook, pre_commit_hook)
).to be_truthy
end

it 'dont rename existed lefthook hook with .old extension' do
expect(File.exist?(pre_push_hook + '.old')).to be_falsy
end
Expand Down
9 changes: 5 additions & 4 deletions spec/support/file_structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ def clean
FileUtils.remove_dir(tmp)
end

def make_config
FileUtils.cp(config_yaml_path, tmp)
def make_config(extension = 'yml')
FileUtils.cp(config_yaml_path(extension), tmp)
end


def tmp
@tmp ||= File.join(root, 'tmp')
end

def config_yaml_path
File.join(fixtures, 'lefthook.yml')
def config_yaml_path(extension = 'yml')
File.join(fixtures, "lefthook.#{extension}")
end

def ok_script_path
Expand Down
28 changes: 28 additions & 0 deletions spec/support/hook_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

RSpec.shared_examples "hook examples" do
let(:pre_push_hook) do
File.join(FileStructure.tmp, '.git', 'hooks', 'pre-push')
end

let(:pre_commit_hook) do
File.join(FileStructure.tmp, '.git', 'hooks', 'pre-commit')
end

let(:expected_pre_push_hook) { FileStructure.pre_push_hook_path }
let(:expected_pre_commit_hook) { FileStructure.pre_commit_hook_path }

it 'create pre-push git hook' do
expect(File.exist?(pre_push_hook)).to be_truthy
expect(
FileUtils.compare_file(expected_pre_push_hook, pre_push_hook)
).to be_truthy
end

it 'create pre-commit git hook' do
expect(File.exist?(pre_commit_hook)).to be_truthy
expect(
FileUtils.compare_file(expected_pre_commit_hook, pre_commit_hook)
).to be_truthy
end
end