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