Skip to content

Commit d520c4f

Browse files
committed
chore(beta): guard branch-sync output handling
1 parent dca07e9 commit d520c4f

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

scripts/branch-sync.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ const { getPromotePlan, getSyncBetaPlan } = require('./branch-sync-utils');
55
// Wrapper around git commands used by the release workflows.
66
// Keeping this logic in a script makes the YAML shorter and easier to review.
77
function runGit(args, options = {}) {
8-
return execFileSync('git', args, {
8+
const output = execFileSync('git', args, {
99
encoding: 'utf8',
1010
...options,
11-
}).trim();
11+
});
12+
13+
return typeof output === 'string' ? output.trim() : '';
1214
}
1315

1416
// GitHub Actions exposes a file path for step outputs through GITHUB_OUTPUT.
@@ -156,4 +158,11 @@ function main() {
156158
throw new Error(`Unsupported mode "${mode}". Expected "sync-beta" or "promote-master".`);
157159
}
158160

159-
main();
161+
if (require.main === module) {
162+
main();
163+
}
164+
165+
module.exports = {
166+
runGit,
167+
main,
168+
};

test/UnitTests/BranchSync.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
jest.mock('child_process', () => ({
2+
execFileSync: jest.fn(),
3+
}));
4+
5+
const { execFileSync } = require('child_process');
6+
const { runGit } = require('../../scripts/branch-sync');
7+
8+
describe('branch-sync', () => {
9+
beforeEach(() => {
10+
jest.clearAllMocks();
11+
});
12+
13+
it('should trim captured git output', () => {
14+
execFileSync.mockReturnValue(' ok \n');
15+
16+
expect(runGit(['status'])).toBe('ok');
17+
expect(execFileSync).toHaveBeenCalledWith('git', ['status'], expect.objectContaining({
18+
encoding: 'utf8',
19+
}));
20+
});
21+
22+
it('should return an empty string when stdio is inherited', () => {
23+
execFileSync.mockReturnValue(null);
24+
25+
expect(runGit(['checkout', '-B', 'beta', 'origin/beta'], { stdio: 'inherit' })).toBe('');
26+
});
27+
});

0 commit comments

Comments
 (0)