UNPKG

10.5 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __generator = (this && this.__generator) || function (thisArg, body) {
11 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13 function verb(n) { return function (v) { return step([n, v]); }; }
14 function step(op) {
15 if (f) throw new TypeError("Generator is already executing.");
16 while (_) try {
17 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18 if (y = 0, t) op = [op[0] & 2, t.value];
19 switch (op[0]) {
20 case 0: case 1: t = op; break;
21 case 4: _.label++; return { value: op[1], done: false };
22 case 5: _.label++; y = op[1]; op = [0]; continue;
23 case 7: op = _.ops.pop(); _.trys.pop(); continue;
24 default:
25 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29 if (t[2]) _.ops.pop();
30 _.trys.pop(); continue;
31 }
32 op = body.call(thisArg, _);
33 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35 }
36};
37var __read = (this && this.__read) || function (o, n) {
38 var m = typeof Symbol === "function" && o[Symbol.iterator];
39 if (!m) return o;
40 var i = m.call(o), r, ar = [], e;
41 try {
42 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
43 }
44 catch (error) { e = { error: error }; }
45 finally {
46 try {
47 if (r && !r.done && (m = i["return"])) m.call(i);
48 }
49 finally { if (e) throw e.error; }
50 }
51 return ar;
52};
53var __spread = (this && this.__spread) || function () {
54 for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
55 return ar;
56};
57var __importDefault = (this && this.__importDefault) || function (mod) {
58 return (mod && mod.__esModule) ? mod : { "default": mod };
59};
60var _this = this;
61Object.defineProperty(exports, "__esModule", { value: true });
62var config_1 = __importDefault(require("../config"));
63var logger_1 = require("../utils/logger");
64var fetchSpy = jest.fn();
65// @ts-ignore
66jest.mock('node-fetch', function () { return function () {
67 var args = [];
68 for (var _i = 0; _i < arguments.length; _i++) {
69 args[_i] = arguments[_i];
70 }
71 return fetchSpy.apply(void 0, __spread(args));
72}; });
73beforeEach(function () {
74 fetchSpy.mockClear();
75});
76var log = logger_1.dummyLog();
77var importMock = jest.fn();
78jest.mock('import-cwd', function () { return function (path) { return importMock(path); }; });
79describe('loadExtendConfig', function () {
80 test('should reject when no config found', function () { return __awaiter(_this, void 0, void 0, function () {
81 var config;
82 return __generator(this, function (_a) {
83 switch (_a.label) {
84 case 0:
85 config = new config_1.default(log);
86 return [4 /*yield*/, expect(config.loadExtendConfig('nothing')).rejects.toBeInstanceOf(Error)];
87 case 1:
88 _a.sent();
89 return [2 /*return*/];
90 }
91 });
92 }); });
93 test('should load file path', function () { return __awaiter(_this, void 0, void 0, function () {
94 var config, _a;
95 return __generator(this, function (_b) {
96 switch (_b.label) {
97 case 0:
98 config = new config_1.default(log);
99 importMock.mockImplementation(function (path) {
100 return path === '../fake/path.json' ? { someOption: 'url' } : undefined;
101 });
102 _a = expect;
103 return [4 /*yield*/, config.loadExtendConfig('../fake/path.json')];
104 case 1:
105 _a.apply(void 0, [_b.sent()]).toEqual({
106 someOption: 'url'
107 });
108 return [2 /*return*/];
109 }
110 });
111 }); });
112 test('should load package.json file from path', function () { return __awaiter(_this, void 0, void 0, function () {
113 var config, _a;
114 return __generator(this, function (_b) {
115 switch (_b.label) {
116 case 0:
117 config = new config_1.default(log);
118 importMock.mockImplementation(function (path) {
119 return path === './package.json' ? { auto: { someOption: 'url' } } : undefined;
120 });
121 _a = expect;
122 return [4 /*yield*/, config.loadExtendConfig('./package.json')];
123 case 1:
124 _a.apply(void 0, [_b.sent()]).toEqual({
125 someOption: 'url'
126 });
127 return [2 /*return*/];
128 }
129 });
130 }); });
131 test('should fail if file path points to js file', function () { return __awaiter(_this, void 0, void 0, function () {
132 var config;
133 return __generator(this, function (_a) {
134 switch (_a.label) {
135 case 0:
136 config = new config_1.default(log);
137 importMock.mockImplementation(function (path) {
138 return path === '../fake/path.js' ? { someOption: 'url' } : undefined;
139 });
140 return [4 /*yield*/, expect(config.loadExtendConfig('../fake/path.js')).rejects.toBeInstanceOf(Error)];
141 case 1:
142 _a.sent();
143 return [2 /*return*/];
144 }
145 });
146 }); });
147 test('should call fetch on URL with config', function () { return __awaiter(_this, void 0, void 0, function () {
148 var config, mockFetchJson;
149 return __generator(this, function (_a) {
150 switch (_a.label) {
151 case 0:
152 config = new config_1.default(log);
153 mockFetchJson = jest.fn();
154 mockFetchJson.mockReturnValue({});
155 fetchSpy.mockResolvedValueOnce({
156 json: mockFetchJson
157 });
158 // tslint:disable-next-line:no-http-string
159 return [4 /*yield*/, config.loadExtendConfig('http://www.test.com/config.json')];
160 case 1:
161 // tslint:disable-next-line:no-http-string
162 _a.sent();
163 expect(fetchSpy).toHaveBeenCalled();
164 expect(mockFetchJson).toHaveBeenCalled();
165 return [2 /*return*/];
166 }
167 });
168 }); });
169 test('should reject if extends URL fails to fetch', function () { return __awaiter(_this, void 0, void 0, function () {
170 var config;
171 return __generator(this, function (_a) {
172 switch (_a.label) {
173 case 0:
174 config = new config_1.default(log);
175 fetchSpy.mockRejectedValueOnce(new Error());
176 return [4 /*yield*/, expect(
177 // tslint:disable-next-line:no-http-string
178 config.loadExtendConfig('http://www.test.com/config.json')).rejects.toBeInstanceOf(Error)];
179 case 1:
180 _a.sent();
181 return [2 /*return*/];
182 }
183 });
184 }); });
185 test('should load @NAME/auto-config', function () { return __awaiter(_this, void 0, void 0, function () {
186 var config, _a;
187 return __generator(this, function (_b) {
188 switch (_b.label) {
189 case 0:
190 config = new config_1.default(log);
191 importMock.mockImplementation(function (path) {
192 return path === '@artsy/auto-config/package.json'
193 ? { auto: { onlyPublishWithReleaseLabel: true } }
194 : undefined;
195 });
196 _a = expect;
197 return [4 /*yield*/, config.loadExtendConfig('@artsy')];
198 case 1:
199 _a.apply(void 0, [_b.sent()]).toEqual({
200 onlyPublishWithReleaseLabel: true
201 });
202 return [2 /*return*/];
203 }
204 });
205 }); });
206 test('should load auto-config-NAME', function () { return __awaiter(_this, void 0, void 0, function () {
207 var config, _a;
208 return __generator(this, function (_b) {
209 switch (_b.label) {
210 case 0:
211 config = new config_1.default(log);
212 importMock.mockImplementation(function (path) {
213 return path === 'auto-config-fuego/package.json'
214 ? { auto: { noVersionPrefix: true } }
215 : undefined;
216 });
217 _a = expect;
218 return [4 /*yield*/, config.loadExtendConfig('fuego')];
219 case 1:
220 _a.apply(void 0, [_b.sent()]).toEqual({
221 noVersionPrefix: true
222 });
223 return [2 /*return*/];
224 }
225 });
226 }); });
227});
228//# sourceMappingURL=config.test.js.map
\No newline at end of file