Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion core/packages/tools/bin/minifyProtoJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ if (process.argv[2] === '--help') {
console.log(`Usage: node ${process.argv[1]} [directory]`);
process.exit(1);
}
main(process.argv[2]);
main(process.argv[2]).catch(err => {
console.error(err);
process.exitCode = 1;
});
6 changes: 4 additions & 2 deletions core/packages/tools/src/minify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ if (require.main === module) {
process.exit(1);
}

// eslint-disable-next-line @typescript-eslint/no-floating-promises
main(process.argv[2]);
main(process.argv[2]).catch(err => {
console.error(err);
process.exitCode = 1;
});
}
16 changes: 16 additions & 0 deletions core/packages/tools/test/minify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

import * as assert from 'assert';
import {spawnSync} from 'child_process';
import {describe, it, beforeEach} from 'mocha';
import * as fs from 'fs';
import {promises as fsp} from 'fs';
Expand Down Expand Up @@ -60,4 +61,19 @@ describe('minify tool', () => {
assert(statBefore.size > statAfter.size);
assert.deepEqual(resultBefore, resultAfter);
});

it('exits non-zero when CLI execution fails', () => {
const cliPath = path.join(
__dirname,
'..',
'..',
'bin',
'minifyProtoJson.js',
);
Comment on lines +66 to +72

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The relative path resolution using ../../bin/minifyProtoJson.js assumes the test is always executed from the compiled build/test directory. If the tests are run directly from the source directory (e.g., using ts-node or tsx during development), __dirname will be core/packages/tools/test, and going up two levels will resolve to core/packages/bin/minifyProtoJson.js which does not exist.

To make this robust across both compiled and source environments, consider dynamically resolving the path based on whether __dirname contains build (or dist), or use process.cwd() if the tests are guaranteed to run from the package root.

    const cliPath = path.resolve(
      __dirname,
      __dirname.includes('build')
        ? '../../bin/minifyProtoJson.js'
        : '../bin/minifyProtoJson.js'
    );

const missingDir = path.join(testDir, 'missing-dir');
const result = spawnSync(process.execPath, [cliPath, missingDir], {
encoding: 'utf8',
});
assert.strictEqual(result.status, 1);
});
});
Loading