forked from JamesIves/github-pages-deploy-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworktree.test.ts
199 lines (195 loc) · 6.01 KB
/
worktree.test.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
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
197
198
199
import {rmRF} from '@actions/io'
import {TestFlag} from '../src/constants'
import {generateWorktree} from '../src/worktree'
import {execute} from '../src/execute'
import fs from 'fs'
import os from 'os'
import path from 'path'
jest.mock('@actions/core', () => ({
setFailed: jest.fn(),
getInput: jest.fn(),
isDebug: jest.fn(),
info: jest.fn()
}))
/*
Test generateWorktree against a known git repository.
The upstream repository `origin` is set up once for the test suite,
and for each test run, a new clone is created.
See workstree.error.test.ts for testing mocked errors from git.*/
describe('generateWorktree', () => {
let tempdir: string | null = null
let clonedir: string | null = null
beforeAll(async () => {
// Set up origin repository
const silent = true
tempdir = fs.mkdtempSync(path.join(os.tmpdir(), 'gh-deploy-'))
const origin = path.join(tempdir, 'origin')
await execute('git init origin', tempdir, silent)
await execute('git config user.email "[email protected]"', origin, silent)
await execute('git config user.name "Jane Doe"', origin, silent)
await execute('git checkout -b main', origin, silent)
fs.writeFileSync(path.join(origin, 'f1'), 'hello world\n')
await execute('git add .', origin, silent)
await execute('git commit -mc0', origin, silent)
fs.writeFileSync(path.join(origin, 'f1'), 'hello world\nand planets\n')
await execute('git add .', origin, silent)
await execute('git commit -mc1', origin, silent)
await execute('git checkout --orphan gh-pages', origin, silent)
await execute('git reset --hard', origin, silent)
await fs.promises.writeFile(path.join(origin, 'gh1'), 'pages content\n')
await execute('git add .', origin, silent)
await execute('git commit -mgh0', origin, silent)
await fs.promises.writeFile(
path.join(origin, 'gh1'),
'pages content\ngoes on\n'
)
await execute('git add .', origin, silent)
await execute('git commit -mgh1', origin, silent)
})
beforeEach(async () => {
// Clone origin to our workspace for each test
const silent = true
clonedir = path.join(tempdir as string, 'clone')
await execute('git init clone', tempdir as string, silent)
await execute('git config user.email "[email protected]"', clonedir, silent)
await execute('git config user.name "Jane Doe"', clonedir, silent)
await execute(
`git remote add origin ${path.join(tempdir as string, 'origin')}`,
clonedir,
silent
)
await execute('git fetch --depth=1 origin main', clonedir, silent)
await execute('git checkout main', clonedir, silent)
})
afterEach(async () => {
// Tear down workspace
await rmRF(clonedir as string)
})
afterAll(async () => {
// Tear down origin repository
if (tempdir) {
await rmRF(tempdir)
// console.log(tempdir)
}
})
describe('with existing branch and new commits', () => {
it('should check out the latest commit', async () => {
const workspace = clonedir as string
await generateWorktree(
{
hostname: 'github.com',
workspace,
singleCommit: false,
branch: 'gh-pages',
folder: '',
silent: true,
isTest: TestFlag.NONE
},
'worktree',
true
)
const dirEntries = await fs.promises.readdir(
path.join(workspace, 'worktree')
)
expect(dirEntries.sort((a, b) => a.localeCompare(b))).toEqual([
'.git',
'gh1'
])
const commitMessages = await execute(
'git log --format=%s',
path.join(workspace, 'worktree'),
true
)
expect(commitMessages).toBe('gh1')
})
})
describe('with missing branch and new commits', () => {
it('should create initial commit', async () => {
const workspace = clonedir as string
await generateWorktree(
{
hostname: 'github.com',
workspace,
singleCommit: false,
branch: 'no-pages',
folder: '',
silent: true,
isTest: TestFlag.NONE
},
'worktree',
false
)
const dirEntries = await fs.promises.readdir(
path.join(workspace, 'worktree')
)
expect(dirEntries).toEqual(['.git'])
const commitMessages = await execute(
'git log --format=%s',
path.join(workspace, 'worktree'),
true
)
expect(commitMessages).toBe('Initial no-pages commit')
})
})
describe('with existing branch and singleCommit', () => {
it('should check out the latest commit', async () => {
const workspace = clonedir as string
await generateWorktree(
{
hostname: 'github.com',
workspace,
singleCommit: true,
branch: 'gh-pages',
folder: '',
silent: true,
isTest: TestFlag.NONE
},
'worktree',
true
)
const dirEntries = await fs.promises.readdir(
path.join(workspace, 'worktree')
)
expect(dirEntries.sort((a, b) => a.localeCompare(b))).toEqual([
'.git',
'gh1'
])
expect(async () => {
await execute(
'git log --format=%s',
path.join(workspace, 'worktree'),
true
)
}).rejects.toThrow()
})
})
describe('with missing branch and singleCommit', () => {
it('should create initial commit', async () => {
const workspace = clonedir as string
await generateWorktree(
{
hostname: 'github.com',
workspace,
singleCommit: true,
branch: 'no-pages',
folder: '',
silent: true,
isTest: TestFlag.NONE
},
'worktree',
false
)
const dirEntries = await fs.promises.readdir(
path.join(workspace, 'worktree')
)
expect(dirEntries).toEqual(['.git'])
expect(async () => {
await execute(
'git log --format=%s',
path.join(workspace, 'worktree'),
true
)
}).rejects.toThrow()
})
})
})