Skip to content

Commit b3946c4

Browse files
committed
Add BrokenSymlinks pre-commit hook
The recent bug in the pre-commit `HookContext`'s handling of broken symlinks prompted the need for a hook that detects these problems explicitly (otherwise the user might get a cryptic error in one of the other hooks). Being explicitly told that a symlink is broken is useful, so add it as a pre-commit hook. Change-Id: I1a1330bdf812550c9bbb0631c785b0016a727b38 Reviewed-on: http://gerrit.causes.com/42396 Tested-by: jenkins <jenkins@causes.com> Reviewed-by: Shane da Silva <shane.dasilva@brigade.com>
1 parent cdd0c66 commit b3946c4

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* All built-in hooks will now fail if the required executable is not present
1313
* Fix bug where pre-commit hook would crash if user attempted to commit a
1414
broken symlink
15+
* Add `BrokenSymlinks` pre-commit hook which checks for broken symlinks
1516

1617
## 0.17.0
1718

config/default.yml

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ PreCommit:
6464
include:
6565
- '**/*.rb'
6666

67+
BrokenSymlinks:
68+
description: 'Checking for broken symlinks'
69+
quiet: true
70+
6771
BundleCheck:
6872
description: 'Checking Gemfile dependencies'
6973
required_executable: 'bundle'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Overcommit::Hook::PreCommit
2+
# Checks for broken symlinks.
3+
class BrokenSymlinks < Base
4+
def run
5+
broken_symlinks = applicable_files.
6+
select { |file| Overcommit::Utils.broken_symlink?(file) }
7+
8+
if broken_symlinks.any?
9+
return :fail, "Broken symlinks detected:\n#{broken_symlinks.join("\n")}"
10+
end
11+
12+
:pass
13+
end
14+
end
15+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PreCommit::BrokenSymlinks do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
let(:staged_file) { 'staged-file.txt' }
7+
subject { described_class.new(config, context) }
8+
9+
before do
10+
subject.stub(:applicable_files).and_return([staged_file])
11+
end
12+
13+
before do
14+
Overcommit::Utils.stub(:broken_symlink?).with(staged_file).and_return(broken)
15+
end
16+
17+
context 'when the symlink is broken' do
18+
let(:broken) { true }
19+
20+
it { should fail_hook }
21+
end
22+
23+
context 'when the symlink is not broken' do
24+
let(:broken) { false }
25+
26+
it { should pass }
27+
end
28+
end

0 commit comments

Comments
 (0)