UNPKG

6.59 kBJavaScriptView Raw
1"use strict";
2/**
3 * © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
4 * (see file: README.md)
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7Object.defineProperty(exports, "__esModule", { value: true });
8const child_process_1 = require("child_process");
9const lodash_1 = require("lodash");
10const nock = require("nock");
11const zlib = require("zlib");
12function runCmd(cmd, env) {
13 try {
14 const customEnv = env ? { env: Object.assign({}, process.env) } : {};
15 var result = child_process_1.execSync(cmd, Object.assign({ cwd: process.cwd() }, customEnv));
16 }
17 catch (error) {
18 throw new Error(error.output.toString());
19 }
20 return result.toString();
21}
22exports.runCmd = runCmd;
23const nockBack = nock.back;
24function prepareTestFixtures(cmdName, argv) {
25 let id = 0;
26 // These should only include the flags that you need for e2e tests
27 const cmds = [
28 {
29 name: 'Help',
30 flags: [],
31 },
32 {
33 name: 'Issue',
34 flags: ['--comment', '--lock', '--new', '--open', '--close', '--search', '--assign'],
35 },
36 {
37 name: 'PullRequest',
38 flags: [
39 '--detailed',
40 '--info',
41 '--fetch',
42 '--fwd',
43 '--comment',
44 '--open',
45 '--close',
46 '--draft',
47 '--submit',
48 ],
49 },
50 {
51 name: 'Gists',
52 flags: ['--new', '--fork', '--delete'],
53 },
54 {
55 name: 'Milestone',
56 flags: ['--list'],
57 },
58 {
59 name: 'Notifications',
60 },
61 {
62 name: 'Repo',
63 flags: ['--label', '--list', '--new', '--fork', '--delete', '--search'],
64 },
65 {
66 name: 'User',
67 flags: ['--login', '--logout', '--whoami'],
68 },
69 {
70 name: 'Version',
71 flags: ['--version'],
72 },
73 ].filter(cmd => cmd.name === cmdName);
74 const newCmdName = formatCmdName(cmds[0], argv);
75 if (!newCmdName) {
76 return () => { };
77 }
78 nock.disableNetConnect();
79 nockBack.fixtures = `${process.cwd()}/__tests__/nockFixtures`;
80 nockBack.setMode('record');
81 const nockPromise = nockBack(`${newCmdName}.json`, {
82 before,
83 afterRecord,
84 });
85 return () => nockPromise
86 .then(({ nockDone }) => nockDone())
87 .catch(err => {
88 throw new Error(`Nock ==> ${err}`);
89 });
90 /* --- Normalization Functions --- */
91 function normalize(value, key) {
92 if (!value)
93 return value;
94 if (lodash_1.isPlainObject(value)) {
95 return lodash_1.mapValues(value, normalize);
96 }
97 if (lodash_1.isArray(value) && lodash_1.isPlainObject(value[0])) {
98 return lodash_1.map(value, normalize);
99 }
100 if (key.includes('token')) {
101 return '234lkj23l4kj234lkj234lkj234lkj23l4kj234l';
102 }
103 if (key.includes('_at')) {
104 return '2017-10-10T16:00:00Z';
105 }
106 if (key.includes('_count')) {
107 return 42;
108 }
109 if (key.includes('id')) {
110 return 1000 + id++;
111 }
112 if (key.includes('node_id')) {
113 return 'MDA6RW50aXR5MQ==';
114 }
115 if (key.includes('url')) {
116 return value.replace(/[1-9][0-9]{2,10}/, '000000001');
117 }
118 return value;
119 }
120 function decodeBuffer(fixture) {
121 const response = lodash_1.isArray(fixture.response) ? fixture.response.join('') : fixture.response;
122 if (!lodash_1.isObject(response)) {
123 try {
124 // Decode the hex buffer that nock made
125 const decoded = Buffer.from(response, 'hex');
126 var unzipped = zlib.gunzipSync(decoded).toString('utf-8');
127 }
128 catch (err) {
129 throw new Error(`Error decoding nock hex:\n${err}`);
130 }
131 }
132 return JSON.parse(unzipped);
133 }
134 // This only executes when first recording the request, but not on subsequent requests
135 function afterRecord(fixtures) {
136 const normalizedFixtures = fixtures.map(fixture => {
137 const isGzipped = fixture.rawHeaders.includes('gzip');
138 let res = fixture.response;
139 if (fixture.body.note) {
140 fixture.body.note = 'Hello from the inside!';
141 }
142 fixture.path = stripAccessToken(fixture.path);
143 fixture.rawHeaders = fixture.rawHeaders.map(header => stripAccessToken(header));
144 if (isGzipped) {
145 res = decodeBuffer(fixture);
146 }
147 if (lodash_1.isArray(res)) {
148 res = res.slice(0, 3).map(res => {
149 return lodash_1.mapValues(res, normalize);
150 });
151 }
152 else {
153 res = lodash_1.mapValues(res, normalize);
154 }
155 if (isGzipped) {
156 try {
157 // Re-gzip to keep the octokittens happy
158 const stringified = JSON.stringify(res);
159 var zipped = zlib.gzipSync(stringified);
160 }
161 catch (err) {
162 throw new Error(`Error re-gzipping nock ==> ${err}`);
163 }
164 }
165 fixture.response = zipped || res;
166 return fixture;
167 });
168 return normalizedFixtures;
169 }
170 function stripAccessToken(header) {
171 return header.includes('access_token')
172 ? header.replace(/access_token(.*?)(&|$)/gi, '')
173 : header;
174 }
175 function before(scope) {
176 scope.filteringPath = () => stripAccessToken(scope.path);
177 scope.filteringRequestBody = (body, aRecordedBody) => {
178 if (body.includes('note')) {
179 body = JSON.parse(body);
180 body.note = aRecordedBody.note;
181 return JSON.stringify(body);
182 }
183 return body;
184 };
185 }
186}
187exports.prepareTestFixtures = prepareTestFixtures;
188function formatCmdName(cmd, argv) {
189 if (argv.length === 1) {
190 return cmd.name;
191 }
192 return cmd.flags.reduce((flagName, current) => {
193 if (flagName) {
194 return flagName;
195 }
196 if (argv.includes(current)) {
197 return concatUpper(cmd.name, current.slice(2));
198 }
199 }, null);
200}
201function concatUpper(one, two) {
202 return `${one}${lodash_1.upperFirst(two)}`;
203}
204//# sourceMappingURL=test-utils.js.map
\No newline at end of file