forked from toplenboren/simple-git-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-git-hooks.test.js
196 lines (138 loc) · 8.6 KB
/
simple-git-hooks.test.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const fs = require('fs')
const os = require('os')
const spc = require("./simple-git-hooks");
const path = require("path")
// Get project root directory
test('getProjectRootDirectory returns correct dir in typical case:', () => {
expect(spc.getProjectRootDirectoryFromNodeModules('var/my-project/node_modules/simple-git-hooks')).toBe('var/my-project')
})
test('getProjectRootDirectory returns correct dir when used with windows delimiters:', () => {
expect(spc.getProjectRootDirectoryFromNodeModules('user\\allProjects\\project\\node_modules\\simple-git-hooks')).toBe('user/allProjects/project')
})
test('getProjectRootDirectory falls back to undefined when we are not in node_modules:', () => {
expect(spc.getProjectRootDirectoryFromNodeModules('var/my-project/simple-git-hooks')).toBe(undefined)
})
// Get git root
const gitProjectRoot = path.normalize(path.join(__dirname, '.git'))
const currentPath = path.normalize(path.join(__dirname))
const currentFilePath = path.normalize(path.join(__filename))
test('get git root works from .git directory itself', () => {
expect(spc.getGitProjectRoot(gitProjectRoot)).toBe(gitProjectRoot)
})
test('get git root works from any directory', () => {
expect(spc.getGitProjectRoot(currentPath)).toBe(gitProjectRoot)
})
test('get git root works from any file', () => {
expect(spc.getGitProjectRoot(currentFilePath)).toBe(gitProjectRoot)
})
// Check if simple-pre-commit is in devDependencies or dependencies in package json
const correctPackageJsonProjectPath = path.normalize(path.join(process.cwd(), '_tests', 'project_with_simple_pre_commit_in_deps'))
const correctPackageJsonProjectPath_2 = path.normalize(path.join(process.cwd(), '_tests', 'project_with_simple_pre_commit_in_dev_deps'))
const incorrectPackageJsonProjectPath = path.normalize(path.join(process.cwd(), '_tests', 'project_without_simple_pre_commit'))
test('returns true if simple pre commit really in devDeps', () => {
expect(spc.checkSimpleGitHooksInDependencies(correctPackageJsonProjectPath)).toBe(true)
})
test('returns true if simple pre commit really in deps', () => {
expect(spc.checkSimpleGitHooksInDependencies(correctPackageJsonProjectPath_2)).toBe(true)
})
test('returns false if simple pre commit isn`t in deps', () => {
expect(spc.checkSimpleGitHooksInDependencies(incorrectPackageJsonProjectPath)).toBe(false)
})
// Set and remove git hooks
const testsFolder = path.normalize(path.join(process.cwd(), '_tests'))
// Correct configurations
const projectWithConfigurationInPackageJsonPath = path.normalize(path.join(testsFolder, 'project_with_configuration_in_package_json'))
const projectWithConfigurationInSeparateJsPath = path.normalize(path.join(testsFolder, 'project_with_configuration_in_separate_js'))
const projectWithConfigurationInAlternativeSeparateJsPath = path.normalize(path.join(testsFolder, 'project_with_configuration_in_alternative_separate_js'))
const projectWithConfigurationInSeparateJsonPath = path.normalize(path.join(testsFolder, 'project_with_configuration_in_separate_json'))
const projectWithConfigurationInAlternativeSeparateJsonPath = path.normalize(path.join(testsFolder, 'project_with_configuration_in_alternative_separate_json'))
// Incorrect configurations
const projectWithIncorrectConfigurationInPackageJson = path.normalize(path.join(testsFolder, 'project_with_incorrect_configuration_in_package_json'))
const projectWithoutConfiguration = path.normalize(path.join(testsFolder, 'project_without_configuration'))
/**
* Creates .git/hooks dir from root
* @param {string} root
*/
function createGitHooksFolder(root) {
if (fs.existsSync(root + '/.git')) {
return
}
fs.mkdirSync(root + '/.git')
fs.mkdirSync(root + '/.git/hooks')
}
/**
* Removes .git directory from root
* @param {string} root
*/
function removeGitHooksFolder(root) {
if (fs.existsSync(root + '/.git')) {
fs.rmdirSync(root + '/.git', { recursive: true })
}
}
/**
* Returns all installed git hooks
* @return { {string: string} }
*/
function getInstalledGitHooks(hooksDir) {
const result = {}
const hooks = fs.readdirSync(hooksDir)
for (let hook of hooks) {
result[hook] = fs.readFileSync(path.normalize(path.join(hooksDir, hook))).toString()
}
return result
}
test('creates git hooks if configuration is correct from .simple-git-hooks.js', () => {
createGitHooksFolder(projectWithConfigurationInAlternativeSeparateJsPath)
spc.setHooksFromConfig(projectWithConfigurationInAlternativeSeparateJsPath)
const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInAlternativeSeparateJsPath, '.git', 'hooks')))
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh${os.EOL}exit 1`, 'pre-push':`#!/bin/sh${os.EOL}exit 1`}))
removeGitHooksFolder(projectWithConfigurationInAlternativeSeparateJsPath)
})
test('creates git hooks if configuration is correct from simple-git-hooks.js', () => {
createGitHooksFolder(projectWithConfigurationInSeparateJsPath)
spc.setHooksFromConfig(projectWithConfigurationInSeparateJsPath)
const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInSeparateJsPath, '.git', 'hooks')))
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh${os.EOL}exit 1`, 'pre-push':`#!/bin/sh${os.EOL}exit 1`}))
removeGitHooksFolder(projectWithConfigurationInSeparateJsPath)
})
test('creates git hooks if configuration is correct from .simple-git-hooks.json', () => {
createGitHooksFolder(projectWithConfigurationInAlternativeSeparateJsonPath)
spc.setHooksFromConfig(projectWithConfigurationInAlternativeSeparateJsonPath)
const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInAlternativeSeparateJsonPath, '.git', 'hooks')))
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh${os.EOL}exit 1`, 'pre-push':`#!/bin/sh${os.EOL}exit 1`}))
removeGitHooksFolder(projectWithConfigurationInAlternativeSeparateJsonPath)
})
test('creates git hooks if configuration is correct from simple-git-hooks.json', () => {
createGitHooksFolder(projectWithConfigurationInSeparateJsonPath)
spc.setHooksFromConfig(projectWithConfigurationInSeparateJsonPath)
const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInSeparateJsonPath, '.git', 'hooks')))
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh${os.EOL}exit 1`, 'pre-push':`#!/bin/sh${os.EOL}exit 1`}))
removeGitHooksFolder(projectWithConfigurationInSeparateJsonPath)
})
test('creates git hooks if configuration is correct from package.json', () => {
createGitHooksFolder(projectWithConfigurationInPackageJsonPath)
spc.setHooksFromConfig(projectWithConfigurationInPackageJsonPath)
const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInPackageJsonPath, '.git', 'hooks')))
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh${os.EOL}exit 1`}))
removeGitHooksFolder(projectWithConfigurationInPackageJsonPath)
})
test('fails to create git hooks if configuration contains bad git hooks', () => {
createGitHooksFolder(projectWithIncorrectConfigurationInPackageJson)
expect(() => spc.setHooksFromConfig(projectWithIncorrectConfigurationInPackageJson)).toThrow('[ERROR] Config was not in correct format. Please check git hooks name')
removeGitHooksFolder(projectWithIncorrectConfigurationInPackageJson)
})
test('fails to create git hooks if not configured', () => {
createGitHooksFolder(projectWithoutConfiguration)
expect(() => spc.setHooksFromConfig(projectWithoutConfiguration)).toThrow('[ERROR] Config was not found! Please add .simple-git-hooks.json or simple-git-hooks.json or simple-git-hooks entry in package.json.')
removeGitHooksFolder(projectWithoutConfiguration)
})
test('removes git hooks', () => {
createGitHooksFolder(projectWithConfigurationInPackageJsonPath)
spc.setHooksFromConfig(projectWithConfigurationInPackageJsonPath)
let installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInPackageJsonPath, '.git', 'hooks')))
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh${os.EOL}exit 1`}))
spc.removeHooks(projectWithConfigurationInPackageJsonPath)
installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithConfigurationInPackageJsonPath, '.git', 'hooks')))
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({}))
removeGitHooksFolder(projectWithConfigurationInPackageJsonPath)
})