-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitChecks.ts
162 lines (138 loc) · 4.88 KB
/
gitChecks.ts
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
import { promises as fs } from 'fs'
import { REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
import prepare from '@pnpm/prepare'
import PnpmError from '@pnpm/error'
import execa from 'execa'
import tempy from 'tempy'
// eslint-disable-next-line
import * as enquirer from 'enquirer'
// eslint-disable-next-line
import { publish } from '@pnpm/plugin-commands-publishing'
import { DEFAULT_OPTS } from './utils'
jest.mock('enquirer', () => ({ prompt: jest.fn() }))
// eslint-disable-next-line
const prompt = enquirer.prompt as any
const CREDENTIALS = [
`--registry=http://localhost:${REGISTRY_MOCK_PORT}/`,
`--//localhost:${REGISTRY_MOCK_PORT}/:username=username`,
`--//localhost:${REGISTRY_MOCK_PORT}/:_password=${Buffer.from('password').toString('base64')}`,
`--//localhost:${REGISTRY_MOCK_PORT}/:email=foo@bar.net`,
]
test('publish: fails git check if branch is not on master or main', async () => {
prepare({
name: 'test-publish-package.json',
version: '0.0.0',
})
await execa('git', ['init'])
await execa('git', ['checkout', '-b', 'test'])
await execa('git', ['config', 'user.email', 'x@y.z'])
await execa('git', ['config', 'user.name', 'xyz'])
await execa('git', ['add', '*'])
await execa('git', ['commit', '-m', 'init', '--no-gpg-sign'])
prompt.mockResolvedValue({
confirm: false,
})
await expect(
publish.handler({
...DEFAULT_OPTS,
argv: { original: ['publish', ...CREDENTIALS] },
dir: process.cwd(),
}, [])
).rejects.toThrow(
new PnpmError('GIT_NOT_CORRECT_BRANCH', "Branch is not on 'master|main'.")
)
})
test('publish: fails git check if branch is not on specified branch', async () => {
prepare({
name: 'test-publish-package.json',
version: '0.0.0',
})
await execa('git', ['init'])
await execa('git', ['checkout', '-b', 'master'])
await execa('git', ['config', 'user.email', 'x@y.z'])
await execa('git', ['config', 'user.name', 'xyz'])
await execa('git', ['add', '*'])
await execa('git', ['commit', '-m', 'init', '--no-gpg-sign'])
prompt.mockResolvedValue({
confirm: false,
})
await expect(
publish.handler({
...DEFAULT_OPTS,
argv: { original: ['publish', ...CREDENTIALS] },
dir: process.cwd(),
publishBranch: 'latest',
}, [])
).rejects.toThrow(
new PnpmError('GIT_NOT_CORRECT_BRANCH', "Branch is not on 'latest'.")
)
})
test('publish: fails git check if branch is not clean', async () => {
prepare({
name: 'test-publish-package.json',
version: '0.0.0',
})
await execa('git', ['init'])
await execa('git', ['config', 'user.email', 'x@y.z'])
await execa('git', ['config', 'user.name', 'xyz'])
await execa('git', ['add', '*'])
await execa('git', ['commit', '-m', 'init', '--no-gpg-sign'])
await fs.writeFile('LICENSE', 'workspace license', 'utf8')
await expect(
publish.handler({
...DEFAULT_OPTS,
argv: { original: ['publish', ...CREDENTIALS] },
dir: process.cwd(),
}, [])
).rejects.toThrow(
new PnpmError('GIT_NOT_UNCLEAN', 'Unclean working tree. Commit or stash changes first.')
)
})
test('publish: fails git check if branch is not up-to-date', async () => {
const remote = tempy.directory()
prepare({
name: 'test-publish-package.json',
version: '0.0.0',
})
await execa('git', ['init'])
await execa('git', ['config', 'user.email', 'x@y.z'])
await execa('git', ['config', 'user.name', 'xyz'])
await execa('git', ['init', '--bare'], { cwd: remote })
await execa('git', ['add', '*'])
await execa('git', ['commit', '-m', 'init', '--no-gpg-sign'])
await execa('git', ['commit', '--allow-empty', '--allow-empty-message', '-m', '', '--no-gpg-sign'])
await execa('git', ['remote', 'add', 'origin', remote])
await execa('git', ['push', '-u', 'origin', 'master'])
await execa('git', ['reset', '--hard', 'HEAD~1'])
await expect(
publish.handler({
...DEFAULT_OPTS,
argv: { original: ['publish', ...CREDENTIALS] },
dir: process.cwd(),
}, [])
).rejects.toThrow(
new PnpmError('GIT_NOT_LATEST', 'Remote history differs. Please pull changes.')
)
})
test('publish: fails git check if HEAD is detached', async () => {
prepare({
name: 'test-publish-package.json',
version: '0.0.0',
})
await execa('git', ['init'])
await execa('git', ['config', 'user.email', 'x@y.z'])
await execa('git', ['config', 'user.name', 'xyz'])
await execa('git', ['add', '*'])
await execa('git', ['commit', '-m', 'init', '--no-gpg-sign'])
await execa('git', ['commit', '--allow-empty', '--allow-empty-message', '-m', '', '--no-gpg-sign'])
await execa('git', ['checkout', 'HEAD~1'])
await expect(
publish.handler({
...DEFAULT_OPTS,
argv: { original: ['publish', ...CREDENTIALS] },
dir: process.cwd(),
}, [])
).rejects.toThrow(
new PnpmError('GIT_UNKNOWN_BRANCH', 'The Git HEAD may not attached to any branch, but your "publish-branch" is set to "master|main".')
)
})