UNPKG

6.27 kBJavaScriptView Raw
1'use strict';
2
3var _process$env$BEEMO_TE;
4
5function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
6
7function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
8
9function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
10
11Object.defineProperty(exports, '__esModule', {
12 value: true
13});
14
15const common = require('@boost/common');
16
17const test = require('@boost/debug/test');
18
19require('./index.js');
20
21const Driver = require('./Driver.js');
22
23const Script = require('./Script.js');
24
25const Tool = require('./Tool.js');
26
27const ConfigContext = require('./contexts/ConfigContext.js');
28
29const DriverContext = require('./contexts/DriverContext.js');
30
31const ScriptContext = require('./contexts/ScriptContext.js');
32
33const ScaffoldContext = require('./contexts/ScaffoldContext.js');
34
35const TEST_ROOT = new common.Path((_process$env$BEEMO_TE = process.env.BEEMO_TEST_ROOT) !== null && _process$env$BEEMO_TE !== void 0 ? _process$env$BEEMO_TE : process.cwd());
36
37class TestDriver extends Driver.Driver {
38 constructor(...args) {
39 super(...args);
40 this.name = 'test-driver';
41 }
42
43}
44
45class TestScript extends Script.Script {
46 constructor(...args) {
47 super(...args);
48 this.name = 'test-script';
49 }
50
51 execute() {
52 return Promise.resolve();
53 }
54
55}
56
57function mockConsole(name) {
58 return jest.spyOn(console, name).mockImplementation(() => {});
59}
60
61function mockToolConfig() {
62 return {
63 configure: {
64 cleanup: false,
65 parallel: true
66 },
67 debug: false,
68 drivers: [],
69 execute: {
70 concurrency: 1,
71 graph: true,
72 output: ''
73 },
74 module: '@local',
75 scripts: [],
76 settings: {}
77 };
78}
79
80function mockTool(argv = []) {
81 const tool = new Tool.Tool({
82 argv,
83 cwd: TEST_ROOT
84 }); // @ts-expect-error Allow readonly
85
86 tool.debug = test.mockDebugger();
87 tool.config = mockToolConfig();
88 return tool;
89}
90
91function mockDriver(name, tool = null, metadata = {}) {
92 const driver = new TestDriver(); // @ts-expect-error For testing purposes
93
94 driver.name = name;
95 driver.tool = tool !== null && tool !== void 0 ? tool : mockTool();
96 driver.setMetadata(_objectSpread({
97 bin: name.toLowerCase(),
98 configName: `${name}.json`,
99 title: name
100 }, metadata));
101 return driver;
102}
103
104function mockScript(name, tool = null) {
105 const script = new TestScript(); // @ts-expect-error For testing purposes
106
107 script.name = name;
108 script.tool = tool !== null && tool !== void 0 ? tool : mockTool();
109 return script;
110}
111
112function stubArgs(options, params = []) {
113 return {
114 command: [],
115 errors: [],
116 options,
117 params,
118 rest: [],
119 unknown: {}
120 };
121}
122
123function stubConfigArgs() {
124 return stubArgs({});
125}
126
127function stubDriverArgs(fields) {
128 return stubArgs(_objectSpread({
129 concurrency: 1,
130 graph: false,
131 workspaces: ''
132 }, fields));
133}
134
135function stubScaffoldArgs(fields) {
136 return stubArgs(_objectSpread({
137 dry: false
138 }, fields));
139}
140
141function stubScriptArgs(fields) {
142 return stubArgs(_objectSpread({
143 concurrency: 1,
144 graph: false,
145 workspaces: ''
146 }, fields));
147}
148
149function applyContext(context) {
150 context.args = stubArgs({
151 a: true,
152 foo: 'bar'
153 }, ['baz']);
154 context.argv = ['-a', '--foo', 'bar', 'baz'];
155 context.cwd = TEST_ROOT;
156 context.configModuleRoot = TEST_ROOT;
157 context.workspaceRoot = TEST_ROOT;
158 context.workspaces = [];
159 return context;
160}
161
162function stubConfigContext() {
163 return applyContext(new ConfigContext.ConfigContext(stubArgs({})));
164}
165
166function stubDriverContext(driver) {
167 return applyContext(new DriverContext.DriverContext(stubDriverArgs(), driver !== null && driver !== void 0 ? driver : new TestDriver()));
168}
169
170function stubScriptContext(script) {
171 const context = applyContext(new ScriptContext.ScriptContext(stubScriptArgs(), 'script'));
172
173 if (script) {
174 context.setScript(script);
175 }
176
177 return context;
178}
179
180function stubScaffoldContext(generator = 'generator', action = 'action', name = '') {
181 return applyContext(new ScaffoldContext.ScaffoldContext(stubScaffoldArgs(), generator, action, name));
182}
183
184function prependRoot(part) {
185 return TEST_ROOT.append(part);
186}
187
188function getRoot() {
189 return TEST_ROOT;
190}
191
192function stubExecResult(fields) {
193 return _objectSpread({
194 all: '',
195 command: '',
196 escapedCommand: '',
197 exitCode: 0,
198 failed: false,
199 isCanceled: false,
200 killed: false,
201 signal: undefined,
202 stderr: '',
203 stdout: '',
204 timedOut: false
205 }, fields);
206}
207
208Object.defineProperty(exports, 'mockDebugger', {
209 enumerable: true,
210 get: function () {
211 return test.mockDebugger;
212 }
213});
214exports.TestDriver = TestDriver;
215exports.TestScript = TestScript;
216exports.applyContext = applyContext;
217exports.getRoot = getRoot;
218exports.mockConsole = mockConsole;
219exports.mockDriver = mockDriver;
220exports.mockScript = mockScript;
221exports.mockTool = mockTool;
222exports.mockToolConfig = mockToolConfig;
223exports.prependRoot = prependRoot;
224exports.stubArgs = stubArgs;
225exports.stubConfigArgs = stubConfigArgs;
226exports.stubConfigContext = stubConfigContext;
227exports.stubDriverArgs = stubDriverArgs;
228exports.stubDriverContext = stubDriverContext;
229exports.stubExecResult = stubExecResult;
230exports.stubScaffoldArgs = stubScaffoldArgs;
231exports.stubScaffoldContext = stubScaffoldContext;
232exports.stubScriptArgs = stubScriptArgs;
233exports.stubScriptContext = stubScriptContext;
234//# sourceMappingURL=test.js.map