-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathset-tokens.test.ts
199 lines (177 loc) · 5.34 KB
/
set-tokens.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 {getPublishRepo, setPersonalToken, setGithubToken, setSSHKey} from '../src/set-tokens';
import {Inputs} from '../src/interfaces';
import fs from 'fs';
import * as exec from '@actions/exec';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import * as core from '@actions/core';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import * as io from '@actions/io';
jest.mock('@actions/exec', () => ({
exec: jest.fn(),
getExecOutput: jest.fn().mockReturnValue({
stderr: '# hostname',
stdout: 'hostinfo',
exitCode: 0
})
}));
jest.mock('@actions/io', () => ({
mkdirP: jest.fn()
}));
jest.mock('@actions/core');
jest.mock('fs');
jest.mock('child_process');
beforeEach(() => {
jest.resetModules();
});
// afterEach(() => {
// });
describe('setSSHKey()', () => {
const createInputs = (): Inputs => ({
DeployKey: 'DEPLOY_KEY',
GithubToken: '',
PersonalToken: '',
PublishBranch: 'gh-pages',
PublishDir: '',
DestinationDir: '',
ExternalRepository: '',
AllowEmptyCommit: false,
KeepFiles: false,
ForceOrphan: false,
UserName: '',
UserEmail: '',
CommitMessage: '',
FullCommitMessage: '',
TagName: '',
TagMessage: '',
DisableNoJekyll: false,
CNAME: '',
ExcludeAssets: ''
});
beforeEach(() => {
jest.resetModules();
});
test('return correct repo address', async () => {
const inps: Inputs = createInputs();
const test = await setSSHKey(inps, 'owner/repo');
});
test('set known_hosts with the ssh-keyscan output if it succeeded', async () => {
const inps: Inputs = createInputs();
await setSSHKey(inps, 'owner/repo');
const mockGetExecOutput = await exec.getExecOutput('');
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('known_hosts'),
mockGetExecOutput.stderr + mockGetExecOutput.stdout
);
});
test('SSH key fallbacks to default value if ssh-keyscan fails', async () => {
const inps: Inputs = createInputs();
(exec.getExecOutput as jest.Mock).mockImplementation(() => {
throw new Error('error');
});
await setSSHKey(inps, 'owner/repo');
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('known_hosts'),
expect.stringContaining('# github.com:22 SSH-2.0-babeld-1f0633a6')
);
});
});
describe('getPublishRepo()', () => {
test('return repository name', () => {
const test = getPublishRepo('', 'owner', 'repo');
expect(test).toMatch('owner/repo');
});
test('return external repository name', () => {
const test = getPublishRepo('extOwner/extRepo', 'owner', 'repo');
expect(test).toMatch('extOwner/extRepo');
});
});
describe('setGithubToken()', () => {
test('return remote url with GITHUB_TOKEN gh-pages', () => {
const test = setGithubToken(
'GITHUB_TOKEN',
'owner/repo',
'gh-pages',
'',
'refs/heads/master',
'push'
);
expect(test).toMatch(expected);
});
test('return remote url with GITHUB_TOKEN master', () => {
const test = setGithubToken(
'GITHUB_TOKEN',
'owner/repo',
'master',
'',
'refs/heads/source',
'push'
);
expect(test).toMatch(expected);
});
test('return remote url with GITHUB_TOKEN gh-pages (RegExp)', () => {
const test = setGithubToken(
'GITHUB_TOKEN',
'owner/repo',
'gh-pages',
'',
'refs/heads/gh-pages-base',
'push'
);
expect(test).toMatch(expected);
});
test('throw error gh-pages-base to gh-pages-base (RegExp)', () => {
expect(() => {
setGithubToken(
'GITHUB_TOKEN',
'owner/repo',
'gh-pages-base',
'',
'refs/heads/gh-pages-base',
'push'
);
}).toThrowError('You deploy from gh-pages-base to gh-pages-base');
});
test('throw error master to master', () => {
expect(() => {
setGithubToken('GITHUB_TOKEN', 'owner/repo', 'master', '', 'refs/heads/master', 'push');
}).toThrowError('You deploy from master to master');
});
test('throw error external repository with GITHUB_TOKEN', () => {
expect(() => {
setGithubToken(
'GITHUB_TOKEN',
'owner/repo',
'gh-pages',
'extOwner/extRepo',
'refs/heads/master',
'push'
);
}).toThrowError(`\
The generated GITHUB_TOKEN (github_token) does not support to push to an external repository.
Use deploy_key or personal_token.
`);
});
test('return remote url with GITHUB_TOKEN pull_request', () => {
const test = setGithubToken(
'GITHUB_TOKEN',
'owner/repo',
'gh-pages',
'',
'refs/pull/29/merge',
'pull_request'
);
expect(test).toMatch(expected);
});
});
describe('setPersonalToken()', () => {
test('return remote url with personal access token', () => {
const test = setPersonalToken('pat', 'owner/repo');
expect(test).toMatch(expected);
});
});