Skip to content

Commit 4d8dce3

Browse files
committed
feat(js): Add live: rejectOnError execution mode to execute method
1 parent 49afbb4 commit 4d8dce3

File tree

5 files changed

+112
-42
lines changed

5 files changed

+112
-42
lines changed

js/__tests__/helper.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@ describe('SentryCli helper', () => {
2222
expect(helper.getPath()).toMatch(pattern);
2323
});
2424

25+
describe('execute', () => {
26+
test('execute with live=false returns stdout', async () => {
27+
const output = await helper.execute(['--version'], false);
28+
expect(output.trim()).toBe('sentry-cli DEV');
29+
});
30+
31+
test('execute with live=true resolves without output', async () => {
32+
// TODO (v3): This should resolve with a string, not undefined/void
33+
const result = await helper.execute(['--version'], true);
34+
expect(result).toBeUndefined();
35+
});
36+
37+
test('execute with live=rejectOnError resolves on success', async () => {
38+
const result = await helper.execute(['--version'], 'rejectOnError');
39+
expect(result).toBe('success (live mode)');
40+
});
41+
42+
test('execute with live=rejectOnError rejects on failure', async () => {
43+
await expect(helper.execute(['fail'], 'rejectOnError')).rejects.toThrow(
44+
'Command fail failed with exit code 1'
45+
);
46+
});
47+
});
48+
2549
describe('`prepare` command', () => {
2650
test('call prepare command add default ignore', () => {
2751
const command = ['releases', 'files', 'release', 'upload-sourcemaps', '/dev/null'];

js/helper.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,11 @@ function getPath() {
280280
* expect(output.trim()).toBe('sentry-cli x.y.z');
281281
*
282282
* @param {string[]} args Command line arguments passed to `sentry-cli`.
283-
* @param {boolean} live We inherit stdio to display `sentry-cli` output directly.
283+
* @param {boolean | 'rejectOnError'} live can be set to:
284+
* - `true` to inherit stdio to display `sentry-cli` output directly.
285+
* - `false` to not inherit stdio and return the output as a string.
286+
* - `'rejectOnError'` to inherit stdio and reject the promise if the command
287+
* exits with a non-zero exit code.
284288
* @param {boolean} silent Disable stdout for silents build (CI/Webpack Stats, ...)
285289
* @param {string} [configFile] Relative or absolute path to the configuration file.
286290
* @param {Object} [config] More configuration to pass to the CLI
@@ -321,15 +325,26 @@ async function execute(args, live, silent, configFile, config = {}) {
321325
]);
322326
args = [...headers, ...args];
323327
}
328+
324329
return new Promise((resolve, reject) => {
325-
if (live === true) {
330+
if (live === true || live === 'rejectOnError') {
326331
const output = silent ? 'ignore' : 'inherit';
327332
const pid = childProcess.spawn(getPath(), args, {
328333
env,
329334
// stdin, stdout, stderr
330335
stdio: ['ignore', output, output],
331336
});
332-
pid.on('exit', () => {
337+
pid.on('exit', (exitCode) => {
338+
if (live === 'rejectOnError') {
339+
if (exitCode === 0) {
340+
resolve('success (live mode)');
341+
}
342+
reject(new Error(`Command ${args.join(' ')} failed with exit code ${exitCode}`));
343+
}
344+
// According to the type definition, resolving with void is not allowed.
345+
// However, for backwards compatibility, we resolve void here to
346+
// avoid a behaviour-breaking change.
347+
// TODO (v3): Clean this up and always resolve a string (or change the type definition)
333348
resolve();
334349
});
335350
} else {

js/releases/__tests__/index.test.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ describe('SentryCli releases', () => {
77
test('call sentry-cli releases propose-version', () => {
88
expect.assertions(1);
99
const cli = new SentryCli();
10-
return cli.releases.proposeVersion().then(version => expect(version).toBeTruthy());
10+
return cli.releases.proposeVersion().then((version) => expect(version).toBeTruthy());
1111
});
1212

1313
describe('with mock', () => {
1414
let cli;
1515
let mockExecute;
1616
beforeAll(() => {
17-
mockExecute = jest.fn(async () => { });
17+
mockExecute = jest.fn(async () => {});
1818
jest.doMock('../../helper', () => ({
1919
...jest.requireActual('../../helper'),
2020
execute: mockExecute,
@@ -100,7 +100,7 @@ describe('SentryCli releases', () => {
100100
await cli.releases.uploadSourceMaps('my-version', { include: paths });
101101

102102
expect(mockExecute).toHaveBeenCalledTimes(2);
103-
paths.forEach(path =>
103+
paths.forEach((path) =>
104104
expect(mockExecute).toHaveBeenCalledWith(
105105
[
106106
'releases',
@@ -159,6 +159,25 @@ describe('SentryCli releases', () => {
159159
{ silent: false }
160160
);
161161
});
162+
163+
test.each([true, false, 'rejectOnError'])('handles live mode %s', async (live) => {
164+
await cli.releases.uploadSourceMaps('my-version', { include: ['path'], live });
165+
expect(mockExecute).toHaveBeenCalledWith(
166+
[
167+
'releases',
168+
'files',
169+
'my-version',
170+
'upload-sourcemaps',
171+
'path',
172+
'--ignore',
173+
'node_modules',
174+
],
175+
live,
176+
false,
177+
undefined,
178+
{ silent: false }
179+
);
180+
});
162181
});
163182
});
164183
});

js/releases/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ class Releases {
199199

200200
return uploadPaths.map((path) =>
201201
// `execute()` is async and thus we're returning a promise here
202-
this.execute(helper.prepareCommand([...args, path], SOURCEMAPS_SCHEMA, newOptions), true)
202+
this.execute(
203+
helper.prepareCommand([...args, path], SOURCEMAPS_SCHEMA, newOptions),
204+
options.live != null ? options.live : true
205+
)
203206
);
204207
});
205208

@@ -255,7 +258,11 @@ class Releases {
255258
/**
256259
* See {helper.execute} docs.
257260
* @param {string[]} args Command line arguments passed to `sentry-cli`.
258-
* @param {boolean} live We inherit stdio to display `sentry-cli` output directly.
261+
* @param {boolean | 'rejectOnError'} live can be set to:
262+
* - `true` to inherit stdio to display `sentry-cli` output directly.
263+
* - `false` to not inherit stdio and return the output as a string.
264+
* - `'rejectOnError'` to inherit stdio and reject the promise if the command
265+
* exits with a non-zero exit code.
259266
* @returns {Promise.<string>} A promise that resolves to the standard output.
260267
*/
261268
async execute(args, live) {

yarn.lock

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -665,40 +665,45 @@
665665
estree-walker "^2.0.1"
666666
picomatch "^2.2.2"
667667

668-
"@sentry/cli-darwin@2.42.1":
669-
version "2.42.1"
670-
resolved "https://registry.yarnpkg.com/@sentry/cli-darwin/-/cli-darwin-2.42.1.tgz#ad4323091e2bc530907b3018fea3d4e2b6d0516f"
671-
integrity sha512-WZFsrzSWtsRK24SiTa+Xod+4Hjlw7xaggmM4lbuo0lISO1EQj+K29jyGX+Ku0qflO1qp1z32bSP/RlWx/1rBjg==
672-
673-
"@sentry/cli-linux-arm64@2.42.1":
674-
version "2.42.1"
675-
resolved "https://registry.yarnpkg.com/@sentry/cli-linux-arm64/-/cli-linux-arm64-2.42.1.tgz#5f30014bb316da5e68c16a0b7bbccba48c1626f4"
676-
integrity sha512-8A43bLvDIzquCXblHNadaRm109ANw1Q9VRXg5qLYv7DrPkUm2oQP+oRnuNUgOJ3W/8QQSvANpG9pPko+mJs4xw==
677-
678-
"@sentry/cli-linux-arm@2.42.1":
679-
version "2.42.1"
680-
resolved "https://registry.yarnpkg.com/@sentry/cli-linux-arm/-/cli-linux-arm-2.42.1.tgz#4dfd3bcc5d40da8a45a045ccc178ed1ee1fe16f2"
681-
integrity sha512-3xR2B9v8e7NjB6U9+oMu2puR3xOv/Axd7qNuUrZxQnNZYtgtnAqIDgSmFTWHOOoged1+AZXe+xDWLN0Y11Q03Q==
682-
683-
"@sentry/cli-linux-i686@2.42.1":
684-
version "2.42.1"
685-
resolved "https://registry.yarnpkg.com/@sentry/cli-linux-i686/-/cli-linux-i686-2.42.1.tgz#b7646f19c922834c775f699b8acd320e11449735"
686-
integrity sha512-YBz6prKqh1i0gzTg3Rus8ALQWmAk5Acap2U2dGuVYgTt7Bbu6SJbxNC9d8j3RUGu7ylupofUEMqKd391mTHf7g==
687-
688-
"@sentry/cli-linux-x64@2.42.1":
689-
version "2.42.1"
690-
resolved "https://registry.yarnpkg.com/@sentry/cli-linux-x64/-/cli-linux-x64-2.42.1.tgz#6ecb98811b351993cfb38afb7ae2c0ed6a23e0f2"
691-
integrity sha512-Rvc6Jy3kLZrcyO7Ysy1gj0iQi0nGVUN79VqC3OO9JDV44aDtKBDYuBkeFKE3gd1SL8EvPetKH85en2u2wdWxYg==
692-
693-
"@sentry/cli-win32-i686@2.42.1":
694-
version "2.42.1"
695-
resolved "https://registry.yarnpkg.com/@sentry/cli-win32-i686/-/cli-win32-i686-2.42.1.tgz#fd2b6d990ef514844fd8416556cbf035cc67926b"
696-
integrity sha512-FC8FE6dk+G83PCO09Ux/9NJNouF5yXKhpzLV5BZkqQye39hV9GDrFTu+VWTnwI1P77fnaJkPEEKRkjwNiPGjLA==
697-
698-
"@sentry/cli-win32-x64@2.42.1":
699-
version "2.42.1"
700-
resolved "https://registry.yarnpkg.com/@sentry/cli-win32-x64/-/cli-win32-x64-2.42.1.tgz#acc8ff57802186f1e8686d82122f2a6a13ec5076"
701-
integrity sha512-1595wD7JQSu5J9pA4m/B3WrjjIXltSV9VzuErehvanBvfusQ/YgBcvsNzgIf8aJsgSAYGbpR3Zqu81pjohdjgA==
668+
"@sentry/cli-darwin@2.47.1":
669+
version "2.47.1"
670+
resolved "https://registry.yarnpkg.com/@sentry/cli-darwin/-/cli-darwin-2.47.1.tgz#f3062e5a6e605afa855cbd182b68563ea730a68e"
671+
integrity sha512-Vq+8Hs1AR5MFYCI8vkz+rdRJmcNgUf8b8dW8aSLYCHy7wS/X61OB00LupLaaaoN5c/xemb0rZCg4M0ftUqB5Kw==
672+
673+
"@sentry/cli-linux-arm64@2.47.1":
674+
version "2.47.1"
675+
resolved "https://registry.yarnpkg.com/@sentry/cli-linux-arm64/-/cli-linux-arm64-2.47.1.tgz#748d0773123c21abcc651857b39f4c88fa0f3689"
676+
integrity sha512-Kuda8/BFMVyqYayQjP0NQnxnAz5Xpfo2crG1/RRXF9lYQ9O/5YRb3dvlMPX6WasplCzajaSuLrYt/LXcs4McwA==
677+
678+
"@sentry/cli-linux-arm@2.47.1":
679+
version "2.47.1"
680+
resolved "https://registry.yarnpkg.com/@sentry/cli-linux-arm/-/cli-linux-arm-2.47.1.tgz#ad1c6a0f6fa6af186b4544fab895e8a256ee198e"
681+
integrity sha512-Wkcvr0LYP1XMSoaczQnUtOSZPfyBzdGk7wQyloYWyMv9oZWJYkt1wYI0/FaNM+MIX15RqEAx0nI5CjotLMlj8w==
682+
683+
"@sentry/cli-linux-i686@2.47.1":
684+
version "2.47.1"
685+
resolved "https://registry.yarnpkg.com/@sentry/cli-linux-i686/-/cli-linux-i686-2.47.1.tgz#a9ae365be7836357724eea6e9fa8d5eabe410ef4"
686+
integrity sha512-WB3FbRjeJmKHhGc5CftaFFJfFc7c+Mu/XKwbI8Es/9f65bVWdB6BA2tH7aHyoAQngA++1ZVXUJwUpxYPNxQEag==
687+
688+
"@sentry/cli-linux-x64@2.47.1":
689+
version "2.47.1"
690+
resolved "https://registry.yarnpkg.com/@sentry/cli-linux-x64/-/cli-linux-x64-2.47.1.tgz#b249bdfe8134698cd88ee7853392abe4436fa873"
691+
integrity sha512-C+3GJLDpZQMO45toUKiF4bPZpxQiU5/10LtZg2vhpUyyzFGNseVQO/Bsnu9hG/LVjYGLkTgEaorl1liRQsfKVg==
692+
693+
"@sentry/cli-win32-arm64@2.47.1":
694+
version "2.47.1"
695+
resolved "https://registry.yarnpkg.com/@sentry/cli-win32-arm64/-/cli-win32-arm64-2.47.1.tgz#f6d6faa720e529f6fab59603cc551d828fde9311"
696+
integrity sha512-K3yb1yLvA6Lh0UaXjsU6lP/2uOMkZ47cVq0dFxL/hEr4fBHRkXuvg3oOJNDkJ2xXt2W2s7AIa83T2EisZ0a/NQ==
697+
698+
"@sentry/cli-win32-i686@2.47.1":
699+
version "2.47.1"
700+
resolved "https://registry.yarnpkg.com/@sentry/cli-win32-i686/-/cli-win32-i686-2.47.1.tgz#bf8a2aba712e24d91157b58360c76f98cdbd2746"
701+
integrity sha512-wk+6IIT+VT28c9uPe9PDzxdh+OiTEDb/0PIdFv1khSfAmEuVSNWzuDWsra7MnA7OPfgzzNDPkP4HRW1CKb3Xiw==
702+
703+
"@sentry/cli-win32-x64@2.47.1":
704+
version "2.47.1"
705+
resolved "https://registry.yarnpkg.com/@sentry/cli-win32-x64/-/cli-win32-x64-2.47.1.tgz#5b8dbf1c444cdb11140e18f5851b8fe72a965533"
706+
integrity sha512-blseDhuUJDsb+3Ku9dvR4b0JO4nunRokF/9jzW+qHqTha7UHE2kQYXkCfsoDg65juvJFeKeQASYV7VphEJgIGQ==
702707

703708
"@sinonjs/commons@^1.7.0":
704709
version "1.8.6"

0 commit comments

Comments
 (0)