/
gitWorkflow.spec.js
109 lines (92 loc) Β· 2.84 KB
/
gitWorkflow.spec.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
import fs from 'fs-extra'
import normalize from 'normalize-path'
import os from 'os'
import path from 'path'
import nanoid from 'nanoid'
import execGitBase from '../lib/execGit'
import GitWorkflow from '../lib/gitWorkflow'
jest.unmock('execa')
jest.setTimeout(20000)
const isAppveyor = !!process.env.APPVEYOR
const osTmpDir = isAppveyor ? 'C:\\projects' : fs.realpathSync(os.tmpdir())
/**
* Create temporary directory and return its path
* @returns {Promise<String>}
*/
const createTempDir = async () => {
const dirname = path.resolve(osTmpDir, 'lint-staged-test', nanoid())
await fs.ensureDir(dirname)
return dirname
}
/**
* Remove temporary directory
* @param {String} dirname
* @returns {Promise<Void>}
*/
const removeTempDir = async dirname => {
await fs.remove(dirname)
}
let tmpDir, cwd
/** Append to file, creating if it doesn't exist */
const appendFile = async (filename, content, dir = cwd) =>
fs.appendFile(path.resolve(dir, filename), content)
/** Wrap execGit to always pass `gitOps` */
const execGit = async args => execGitBase(args, { cwd })
/** Initialize git repo for test */
const initGitRepo = async () => {
await execGit('init')
await execGit(['config', 'user.name', '"test"'])
await execGit(['config', 'user.email', '"test@test.com"'])
await appendFile('README.md', '# Test\n')
await execGit(['add', 'README.md'])
await execGit(['commit', '-m initial commit'])
}
describe('gitWorkflow', () => {
beforeEach(async () => {
tmpDir = await createTempDir()
cwd = normalize(tmpDir)
await initGitRepo()
})
afterEach(async () => {
if (!isAppveyor) {
await removeTempDir(tmpDir)
}
})
describe('hasPatch', () => {
it('should return false when patch file not found', async () => {
const gitWorkflow = new GitWorkflow({
gitDir: cwd,
gitConfigDir: path.resolve(cwd, './.git')
})
expect(await gitWorkflow.hasPatch('foo')).toEqual(false)
})
})
describe('dropBackup', () => {
it('should handle errors', async () => {
const gitWorkflow = new GitWorkflow({
gitDir: cwd,
gitConfigDir: path.resolve(cwd, './.git')
})
const ctx = {}
await expect(gitWorkflow.dropBackup(ctx)).rejects.toThrowErrorMatchingInlineSnapshot(
`"lint-staged automatic backup is missing!"`
)
expect(ctx).toEqual({
gitError: true,
gitGetBackupStashError: true
})
})
})
describe('cleanUntrackedFiles', () => {
it('should remove untracked files', async () => {
const tempFile = path.resolve(cwd, 'tempFile')
await fs.writeFile(tempFile, 'Hello')
const gitWorkflow = new GitWorkflow({
gitDir: cwd,
gitConfigDir: path.resolve(cwd, './.git')
})
await gitWorkflow.cleanUntrackedFiles()
await expect(fs.access(tempFile)).rejects.toThrow('ENOENT')
})
})
})