UNPKG

4.08 kBJavaScriptView Raw
1'use strict'
2
3const fs = require('fs')
4const path = require('path')
5const mock = require('mock-fs')
6const installFrom = require('../src/install')
7const uninstallFrom = require('../src/uninstall')
8
9const gitDir = '/.git'
10
11function readHook(hookPath) {
12 return fs.readFileSync(path.join(gitDir, hookPath), 'utf-8')
13}
14
15function exists(hookPath) {
16 return fs.existsSync(path.join(gitDir, hookPath))
17}
18
19describe('husky', function() {
20 afterEach(function() {
21 mock.restore()
22 })
23
24 it('should support basic layout', function() {
25 mock({
26 '/.git/hooks': {},
27 '/node_modules/husky': {}
28 })
29
30 installFrom('/node_modules/husky')
31 const hook = readHook('hooks/pre-commit')
32
33 expect(hook).toMatch('#husky')
34 expect(hook).toMatch('cd .')
35 expect(hook).toMatch('npm run -s precommit')
36 expect(hook).toMatch('--no-verify')
37
38 const prepareCommitMsg = readHook('hooks/prepare-commit-msg')
39 expect(prepareCommitMsg).toMatch('cannot be bypassed')
40
41 uninstallFrom('/node_modules/husky')
42 expect(exists('hooks/pre-push')).toBeFalsy()
43 })
44
45 it('should support project installed in sub directory', function() {
46 mock({
47 '/.git/hooks': {},
48 '/A/B/node_modules/husky': {}
49 })
50
51 installFrom('/A/B/node_modules/husky')
52 const hook = readHook('hooks/pre-commit')
53
54 expect(hook).toMatch('cd A/B')
55
56 uninstallFrom('/A/B/node_modules/husky')
57 expect(exists('hooks/pre-push')).toBeFalsy()
58 })
59
60 it('should support git submodule', function() {
61 mock({
62 '/.git/modules/A/B': {},
63 '/A/B/.git': 'git: ../../.git/modules/A/B',
64 '/A/B/node_modules/husky': {}
65 })
66
67 installFrom('/A/B/node_modules/husky')
68 const hook = readHook('modules/A/B/hooks/pre-commit')
69
70 expect(hook).toMatch('cd .')
71
72 uninstallFrom('/A/B/node_modules/husky')
73 expect(exists('hooks/pre-push')).toBeFalsy()
74 })
75
76 it('should support git submodule and sub directory', function() {
77 mock({
78 '/.git/modules/A/B': {},
79 '/A/B/.git': 'git: ../../.git/modules/A/B',
80 '/A/B/C/node_modules/husky': {}
81 })
82
83 installFrom('/A/B/C/node_modules/husky')
84 const hook = readHook('modules/A/B/hooks/pre-commit')
85
86 expect(hook).toMatch('cd C')
87
88 uninstallFrom('/A/B/app/node_modules/husky')
89 expect(exists('hooks/pre-push')).toBeFalsy()
90 })
91
92 it('should support git worktrees', function() {
93 mock({
94 '/.git/worktrees/B': {},
95 '/A/B/.git': 'git: /.git/worktrees/B',
96 '/A/B/node_modules/husky': {}
97 })
98
99 installFrom('/A/B/node_modules/husky')
100 const hook = readHook('worktrees/B/hooks/pre-commit')
101
102 expect(hook).toMatch('cd .')
103
104 uninstallFrom('/A/B/node_modules/husky')
105 expect(exists('hooks/pre-commit')).toBeFalsy()
106 })
107
108 it('should not modify user hooks', function() {
109 mock({
110 '/.git/hooks': {},
111 '/.git/hooks/pre-push': 'foo',
112 '/node_modules/husky': {}
113 })
114
115 // Verify that it's not overwritten
116 installFrom('/node_modules/husky')
117 const hook = readHook('hooks/pre-push')
118 expect(hook).toBe('foo')
119
120 uninstallFrom('/node_modules/husky')
121 expect(exists('hooks/pre-push')).toBeTruthy()
122 })
123
124 it('should not install from /node_modules/A/node_modules', function() {
125 mock({
126 '/.git/hooks': {},
127 '/node_modules/A/node_modules/husky': {}
128 })
129
130 installFrom('/node_modules/A/node_modules/husky')
131 expect(exists('hooks/pre-push')).toBeFalsy()
132 })
133
134 it("should not crash if there's no .git directory", function() {
135 mock({
136 '/node_modules/husky': {}
137 })
138
139 expect(function() {
140 installFrom('/node_modules/husky')
141 }).not.toThrow()
142
143 expect(function() {
144 uninstallFrom('/node_modules/husky')
145 }).not.toThrow()
146 })
147
148 it('should migrate ghooks scripts', function() {
149 mock({
150 '/.git/hooks/pre-commit':
151 '// Generated by ghooks. Do not edit this file.',
152 '/node_modules/husky': {}
153 })
154
155 installFrom('/node_modules/husky')
156 const hook = readHook('hooks/pre-commit')
157 expect(hook).toMatch('husky')
158 expect(hook).not.toMatch('ghooks')
159 })
160})