-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.spec.js
136 lines (117 loc) · 3.82 KB
/
options.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
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
const { createOptions } = require('.');
describe('Options', () => {
beforeEach(cleanEnvironmentVariables);
it('extends passed options with default directory to deploy, master branch, defaultBranch master, and no dotfiles', () => {
const options = {
token: 'a-token',
owner: 'an-owner',
repo: 'a-repo',
};
expect(createOptions(options)).toEqual({
...options,
directory: 'public',
branch: 'master',
defaultBranch: 'master',
dotfiles: false,
verbose: false,
});
});
it('prefers passed options to default options', () => {
const options = {
token: 'a-token',
owner: 'an-owner',
repo: 'a-repo',
branch: 'a-branch',
defaultBranch: 'a-branch',
directory: 'a-directory',
dotfiles: true,
verbose: true,
};
expect(createOptions(options)).toEqual(options);
});
it('extends passed options with github token variable if exists', () => {
process.env.GITHUB_TOKEN = 'a-token';
const options = {
owner: 'an-owner',
repo: 'a-repo',
branch: 'a-branch',
defaultBranch: 'a-branch',
directory: 'a-directory',
dotfiles: true,
verbose: true,
};
expect(createOptions(options)).toEqual({ ...options, token: 'a-token' });
});
it('extends passed options with circle variables if circle', () => {
process.env.CIRCLECI = true;
process.env.CIRCLE_PROJECT_USERNAME = 'an-owner';
process.env.CIRCLE_PROJECT_REPONAME = 'a-repo';
process.env.CIRCLE_BRANCH = 'a-branch';
process.env.CIRCLE_BUILD_URL = '/a-build-url';
const options = { token: 'a-token', directory: 'a-directory', defaultBranch: 'a-branch' };
expect(createOptions(options)).toEqual({
token: 'a-token',
owner: 'an-owner',
repo: 'a-repo',
branch: 'a-branch',
defaultBranch: 'a-branch',
buildUrl: '/a-build-url',
directory: 'a-directory',
dotfiles: false,
verbose: false,
});
delete process.env.CIRCLECI;
delete process.env.CIRCLE_PROJECT_USERNAME;
delete process.env.CIRCLE_PROJECT_REPONAME;
delete process.env.CIRCLE_BRANCH;
delete process.env.CIRCLE_BUILD_URL;
});
it('extends passed options with github actions variables if github actions', () => {
process.env.GITHUB_WORKFLOW = 'Workflow name';
process.env.GITHUB_RUN_ID = '123456';
process.env.GITHUB_REPOSITORY = 'an-owner/a-repo/with-slash';
process.env.GITHUB_REF = 'refs/heads/a-branch/with-slash';
const options = { token: 'a-token', directory: 'a-directory' };
expect(createOptions(options)).toEqual({
token: 'a-token',
owner: 'an-owner',
repo: 'a-repo/with-slash',
branch: 'a-branch/with-slash',
buildUrl: 'https://github.com/an-owner/a-repo/with-slash/actions/runs/123456',
directory: 'a-directory',
dotfiles: false,
verbose: false,
defaultBranch: 'master',
});
delete process.env.GITHUB_WORKFLOW;
delete process.env.GITHUB_RUN_ID;
delete process.env.GITHUB_REPOSITORY;
delete process.env.GITHUB_REF;
});
it('throws if any required option is missing', () => {
expect.assertions(3);
const requiredOptions = ['token', 'owner', 'repo'];
const fullOptions = () => ({
token: 'a-token',
owner: 'an-owner',
repo: 'a-repo',
});
requiredOptions.forEach(name => {
const options = fullOptions();
delete options[name];
try {
createOptions(options);
} catch (err) {
expect(err.message).toContain(name);
}
});
});
function cleanEnvironmentVariables() {
delete process.env.GITHUB_TOKEN;
delete process.env.CIRCLECI;
delete process.env.CIRCLE_PROJECT_USERNAME;
delete process.env.CIRCLE_PROJECT_REPONAME;
delete process.env.CIRCLE_BRANCH;
delete process.env.CIRCLE_BUILD_URL;
}
});