@hubspot/npm-scripts 0.3.4 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,19 @@
1
1
  import { describe, it, expect, vi, beforeEach } from 'vitest';
2
2
  import { buildReleaseScript } from '../release.js';
3
- import { exec as _exec } from 'node:child_process';
3
+ import { exec as _exec, execFile as _execFile } from 'node:child_process';
4
4
  import yargs from 'yargs';
5
5
  import { getCurrentGitBranch, gitCleanup } from '../utils/git.js';
6
- import { getDistTags, publish, updateNextTag, updatePreviousTag, } from '../utils/npm.js';
7
- import { confirm, input } from '../utils/prompting.js';
6
+ import { getDistTags, getPublishedVersions, publish, updateNextTag, updatePreviousTag, } from '../utils/npm.js';
7
+ import { confirm, select } from '../utils/prompting.js';
8
8
  import { logger } from '../utils/logging.js';
9
9
  import fs from 'fs-extra';
10
+ import open from 'open';
10
11
  vi.mock('util', () => ({
11
12
  promisify: vi.fn(fn => fn),
12
13
  }));
13
14
  vi.mock('node:child_process', () => ({
14
15
  exec: vi.fn(),
16
+ execFile: vi.fn(),
15
17
  execSync: vi.fn(),
16
18
  }));
17
19
  vi.mock('../utils/git.js', () => ({
@@ -20,20 +22,23 @@ vi.mock('../utils/git.js', () => ({
20
22
  }));
21
23
  vi.mock('../utils/npm.js', () => ({
22
24
  getDistTags: vi.fn(),
25
+ getPublishedVersions: vi.fn(),
23
26
  publish: vi.fn(),
24
27
  updateNextTag: vi.fn(),
25
28
  updatePreviousTag: vi.fn(),
26
29
  }));
27
30
  vi.mock('../utils/prompting.js', () => ({
28
31
  confirm: vi.fn(),
29
- input: vi.fn(),
32
+ select: vi.fn(),
30
33
  }));
31
34
  vi.mock('../utils/logging.js', () => ({
32
35
  logger: {
33
36
  log: vi.fn(),
34
37
  error: vi.fn(),
35
38
  success: vi.fn(),
39
+ warn: vi.fn(),
36
40
  },
41
+ uiLink: (text, url) => `${text}: ${url}`,
37
42
  }));
38
43
  vi.mock('open', () => ({
39
44
  default: vi.fn().mockResolvedValue(undefined),
@@ -41,6 +46,9 @@ vi.mock('open', () => ({
41
46
  vi.mock('fs-extra', () => ({
42
47
  default: {
43
48
  readJsonSync: vi.fn(),
49
+ mkdtemp: vi.fn(),
50
+ readFile: vi.fn(),
51
+ remove: vi.fn(),
44
52
  },
45
53
  }));
46
54
  vi.mock('semver', async () => vi.importActual('semver'));
@@ -48,18 +56,22 @@ vi.mock('yargs', () => ({
48
56
  default: vi.fn(),
49
57
  }));
50
58
  const mockedExec = vi.mocked(_exec);
59
+ const mockedExecFile = vi.mocked(_execFile);
51
60
  const DEFAULT_PACKAGE_JSON = {
52
61
  name: '@hubspot/test-package',
53
62
  version: '1.0.0-beta.1',
54
63
  publishConfig: { registry: 'https://registry.npmjs.org/' },
55
64
  };
56
- function setupAndGetHandler(packageJson = DEFAULT_PACKAGE_JSON, handlerOptions = {}) {
57
- let capturedHandler;
65
+ function expectOnlyRepoAccessCheck() {
66
+ expect(mockedExecFile.mock.calls.filter(([, args]) => args?.[0] !== 'repo')).toEqual([]);
67
+ }
68
+ function setupAndGetHandler(packageJson = DEFAULT_PACKAGE_JSON, handlerOptions = {}, commandName = 'release') {
69
+ const capturedHandlers = {};
58
70
  const yargsInstance = {
59
71
  scriptName: vi.fn().mockReturnThis(),
60
72
  usage: vi.fn().mockReturnThis(),
61
- command: vi.fn((_name, _desc, _builder, handler) => {
62
- capturedHandler = handler;
73
+ command: vi.fn((name, _desc, _builder, handler) => {
74
+ capturedHandlers[name] = handler;
63
75
  return yargsInstance;
64
76
  }),
65
77
  version: vi.fn().mockReturnThis(),
@@ -76,8 +88,9 @@ function setupAndGetHandler(packageJson = DEFAULT_PACKAGE_JSON, handlerOptions =
76
88
  ...handlerOptions,
77
89
  },
78
90
  });
91
+ const capturedHandler = capturedHandlers[commandName];
79
92
  if (!capturedHandler) {
80
- throw new Error('yargs command handler was not captured');
93
+ throw new Error(`yargs handler for '${commandName}' was not captured`);
81
94
  }
82
95
  return capturedHandler;
83
96
  }
@@ -106,8 +119,8 @@ describe('release handler', () => {
106
119
  experimental: '1.0.0-experimental.1',
107
120
  previous: '0.9.0',
108
121
  });
122
+ vi.mocked(getPublishedVersions).mockResolvedValue([]);
109
123
  vi.mocked(confirm).mockResolvedValue(true);
110
- vi.mocked(input).mockResolvedValue('123456');
111
124
  vi.mocked(publish).mockResolvedValue(undefined);
112
125
  vi.mocked(updateNextTag).mockResolvedValue(undefined);
113
126
  vi.mocked(updatePreviousTag).mockResolvedValue(undefined);
@@ -478,6 +491,473 @@ describe('release handler', () => {
478
491
  expect(mockedExec).toHaveBeenCalledWith('yarn version --no-git-tag-version --new-version 1.0.0-beta.1');
479
492
  });
480
493
  });
494
+ describe('--yes', () => {
495
+ it('does not prompt for confirmation', async () => {
496
+ const handler = setupAndGetHandler();
497
+ await handler({
498
+ versionIncrement: 'patch',
499
+ tag: 'next',
500
+ dryRun: false,
501
+ skipBranchValidation: false,
502
+ skipVersionCheck: false,
503
+ yes: true,
504
+ });
505
+ expect(confirm).not.toHaveBeenCalled();
506
+ expect(publish).toHaveBeenCalled();
507
+ });
508
+ it('exits with an actionable error instead of prompting for npm login', async () => {
509
+ mockedExec.mockImplementation((cmd) => {
510
+ if (cmd.startsWith('npm whoami')) {
511
+ return Promise.reject(new Error('not logged in'));
512
+ }
513
+ return Promise.resolve({ stdout: '', stderr: '' });
514
+ });
515
+ const handler = setupAndGetHandler();
516
+ await withExitMocked(() => handler({
517
+ versionIncrement: 'patch',
518
+ tag: 'next',
519
+ dryRun: false,
520
+ skipBranchValidation: false,
521
+ skipVersionCheck: false,
522
+ yes: true,
523
+ }));
524
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('npm login --registry'));
525
+ expect(publish).not.toHaveBeenCalled();
526
+ });
527
+ it('exits when the working tree is dirty', async () => {
528
+ mockedExec.mockImplementation((cmd) => Promise.resolve({
529
+ stdout: cmd === 'git status --porcelain' ? '?? notes.txt\n' : '',
530
+ stderr: '',
531
+ }));
532
+ const handler = setupAndGetHandler();
533
+ await withExitMocked(() => handler({
534
+ versionIncrement: 'patch',
535
+ tag: 'next',
536
+ dryRun: false,
537
+ skipBranchValidation: false,
538
+ skipVersionCheck: false,
539
+ yes: true,
540
+ }));
541
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Working tree has uncommitted changes'));
542
+ expect(publish).not.toHaveBeenCalled();
543
+ });
544
+ it('also checks the working tree without --yes', async () => {
545
+ const handler = setupAndGetHandler();
546
+ await handler({
547
+ versionIncrement: 'patch',
548
+ tag: 'next',
549
+ dryRun: false,
550
+ skipBranchValidation: false,
551
+ skipVersionCheck: false,
552
+ });
553
+ expect(mockedExec).toHaveBeenCalledWith('git status --porcelain');
554
+ });
555
+ });
556
+ describe('publishFromCI', () => {
557
+ beforeEach(() => {
558
+ mockedExecFile.mockResolvedValue({
559
+ stdout: 'https://github.com/test/repo/pull/1\n',
560
+ stderr: '',
561
+ });
562
+ });
563
+ function mockDirtyTree() {
564
+ mockedExec.mockImplementation((cmd) => Promise.resolve({
565
+ stdout: cmd === 'git status --porcelain' ? ' M src/index.ts\n' : '',
566
+ stderr: '',
567
+ }));
568
+ }
569
+ it.each(['next', 'latest'])('opens a release PR instead of publishing for the %s tag', async (tag) => {
570
+ const postLatestRelease = vi.fn();
571
+ const handler = setupAndGetHandler(DEFAULT_PACKAGE_JSON, {
572
+ publishFromCI: true,
573
+ postLatestRelease,
574
+ });
575
+ await handler({
576
+ versionIncrement: 'patch',
577
+ tag,
578
+ dryRun: false,
579
+ skipBranchValidation: false,
580
+ skipVersionCheck: true,
581
+ });
582
+ const calls = mockedExec.mock.calls.map(args => args[0]);
583
+ expect(calls.some(cmd => cmd.startsWith('git checkout -b'))).toBe(true);
584
+ expect(mockedExecFile).toHaveBeenCalledWith('gh', expect.arrayContaining(['pr', 'create']));
585
+ expect(calls).not.toContain('yarn build');
586
+ expect(calls.some(cmd => cmd.startsWith('npm whoami'))).toBe(false);
587
+ expect(calls.some(cmd => cmd.startsWith('git push --atomic'))).toBe(false);
588
+ expect(publish).not.toHaveBeenCalled();
589
+ expect(updateNextTag).not.toHaveBeenCalled();
590
+ expect(postLatestRelease).not.toHaveBeenCalled();
591
+ expect(vi.mocked(open)).not.toHaveBeenCalled();
592
+ });
593
+ it('still runs version validation before opening a PR', async () => {
594
+ vi.mocked(getDistTags).mockResolvedValue({
595
+ latest: '1.0.0',
596
+ next: '1.0.0-beta.2',
597
+ experimental: '1.0.0-experimental.1',
598
+ previous: '0.9.0',
599
+ });
600
+ const handler = setupAndGetHandler(DEFAULT_PACKAGE_JSON, {
601
+ publishFromCI: true,
602
+ });
603
+ await withExitMocked(() => handler({
604
+ versionIncrement: 'patch',
605
+ tag: 'next',
606
+ dryRun: false,
607
+ skipBranchValidation: false,
608
+ skipVersionCheck: false,
609
+ }));
610
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('out of sync'));
611
+ expectOnlyRepoAccessCheck();
612
+ });
613
+ it('does not create a branch, push, or open a PR on dry run', async () => {
614
+ const handler = setupAndGetHandler(DEFAULT_PACKAGE_JSON, {
615
+ publishFromCI: true,
616
+ });
617
+ await handler({
618
+ versionIncrement: 'patch',
619
+ tag: 'next',
620
+ dryRun: true,
621
+ skipBranchValidation: false,
622
+ skipVersionCheck: false,
623
+ });
624
+ expect(mockedExec).not.toHaveBeenCalledWith(expect.stringMatching(/^(git checkout -b|git push)/));
625
+ expectOnlyRepoAccessCheck();
626
+ expect(gitCleanup).toHaveBeenCalledWith('1.0.1-beta.0', false);
627
+ });
628
+ it('previews the temp branch on a dry run from the main branch', async () => {
629
+ const handler = setupAndGetHandler(DEFAULT_PACKAGE_JSON, {
630
+ publishFromCI: true,
631
+ });
632
+ await handler({
633
+ versionIncrement: 'patch',
634
+ tag: 'next',
635
+ dryRun: true,
636
+ skipBranchValidation: false,
637
+ skipVersionCheck: false,
638
+ });
639
+ expect(logger.log).toHaveBeenCalledWith(' git push -u origin v1.0.1-beta.0-publish');
640
+ });
641
+ it('exits before prompting or changing anything when the working tree is dirty', async () => {
642
+ mockDirtyTree();
643
+ const handler = setupAndGetHandler(DEFAULT_PACKAGE_JSON, {
644
+ publishFromCI: true,
645
+ });
646
+ await withExitMocked(() => handler({
647
+ versionIncrement: 'patch',
648
+ tag: 'next',
649
+ dryRun: true,
650
+ skipBranchValidation: false,
651
+ skipVersionCheck: false,
652
+ }));
653
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Working tree has uncommitted changes'));
654
+ expect(confirm).not.toHaveBeenCalled();
655
+ expect(mockedExec).not.toHaveBeenCalledWith(expect.stringMatching(/^(yarn version|git checkout)/));
656
+ expect(gitCleanup).not.toHaveBeenCalled();
657
+ });
658
+ it('exits before creating a branch when gh is not logged in', async () => {
659
+ mockedExec.mockImplementation((cmd) => cmd.startsWith('gh auth status')
660
+ ? Promise.reject(new Error('not logged in'))
661
+ : Promise.resolve({ stdout: '', stderr: '' }));
662
+ const handler = setupAndGetHandler(DEFAULT_PACKAGE_JSON, {
663
+ publishFromCI: true,
664
+ });
665
+ await withExitMocked(() => handler({
666
+ versionIncrement: 'patch',
667
+ tag: 'next',
668
+ dryRun: false,
669
+ skipBranchValidation: false,
670
+ skipVersionCheck: false,
671
+ }));
672
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('gh auth login --hostname github.com'));
673
+ expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('git checkout -b'));
674
+ });
675
+ it('exits before creating a branch when gh is not installed', async () => {
676
+ mockedExec.mockImplementation((cmd) => {
677
+ if (cmd === 'gh --version') {
678
+ return Promise.reject(new Error('command not found: gh'));
679
+ }
680
+ return Promise.resolve({ stdout: '', stderr: '' });
681
+ });
682
+ const handler = setupAndGetHandler(DEFAULT_PACKAGE_JSON, {
683
+ publishFromCI: true,
684
+ });
685
+ await withExitMocked(() => handler({
686
+ versionIncrement: 'patch',
687
+ tag: 'next',
688
+ dryRun: false,
689
+ skipBranchValidation: false,
690
+ skipVersionCheck: false,
691
+ }));
692
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('GitHub CLI (gh) is required'));
693
+ expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('git checkout -b'));
694
+ });
695
+ });
696
+ describe('experimental version', () => {
697
+ it('computes the experimental version from latest and published versions', async () => {
698
+ vi.mocked(getCurrentGitBranch).mockResolvedValue('feature-branch');
699
+ vi.mocked(getPublishedVersions).mockResolvedValue([
700
+ '1.0.0',
701
+ '1.0.0-experimental.4',
702
+ '0.9.0-experimental.9',
703
+ ]);
704
+ const handler = setupAndGetHandler();
705
+ await handler({
706
+ versionIncrement: 'prerelease',
707
+ tag: 'experimental',
708
+ dryRun: false,
709
+ skipBranchValidation: false,
710
+ skipVersionCheck: false,
711
+ });
712
+ expect(getPublishedVersions).toHaveBeenCalledWith('@hubspot/test-package');
713
+ expect(logger.log).toHaveBeenCalledWith('\nCurrent latest version: 1.0.0');
714
+ expect(logger.log).toHaveBeenCalledWith('Last experimental release: 1.0.0-experimental.1');
715
+ expect(mockedExec).toHaveBeenCalledWith('yarn version --no-git-tag-version --new-version 1.0.0-experimental.5');
716
+ });
717
+ });
718
+ describe('latest, next, and experimental commands', () => {
719
+ it('registers latest, next, and experimental alongside release', () => {
720
+ for (const name of [
721
+ 'release',
722
+ 'latest',
723
+ 'next',
724
+ 'previous',
725
+ 'experimental',
726
+ ]) {
727
+ expect(() => setupAndGetHandler(DEFAULT_PACKAGE_JSON, {}, name)).not.toThrow();
728
+ }
729
+ });
730
+ it('releases on the tag named by the command', async () => {
731
+ const handler = setupAndGetHandler(DEFAULT_PACKAGE_JSON, {}, 'next');
732
+ await handler({
733
+ versionIncrement: 'minor',
734
+ dryRun: false,
735
+ skipBranchValidation: false,
736
+ skipVersionCheck: false,
737
+ });
738
+ expect(publish).toHaveBeenCalledWith('next', false, false, 'https://registry.npmjs.org/');
739
+ expect(mockedExec).toHaveBeenCalledWith('yarn version --new-version 1.1.0-beta.0');
740
+ });
741
+ it('prompts for the increment when -v is omitted', async () => {
742
+ vi.mocked(select).mockResolvedValue('patch');
743
+ const handler = setupAndGetHandler(DEFAULT_PACKAGE_JSON, {}, 'latest');
744
+ await handler({
745
+ dryRun: false,
746
+ skipBranchValidation: false,
747
+ skipVersionCheck: true,
748
+ });
749
+ expect(select).toHaveBeenCalled();
750
+ expect(publish).toHaveBeenCalledWith('latest', false, false, 'https://registry.npmjs.org/');
751
+ });
752
+ it('fails with an actionable message when -v is omitted with --yes', async () => {
753
+ const handler = setupAndGetHandler(DEFAULT_PACKAGE_JSON, {}, 'latest');
754
+ await withExitMocked(() => handler({
755
+ dryRun: false,
756
+ skipBranchValidation: false,
757
+ skipVersionCheck: false,
758
+ yes: true,
759
+ }));
760
+ expect(select).not.toHaveBeenCalled();
761
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Pass -v <patch|minor|major>'));
762
+ });
763
+ it('does not ask for an increment on the experimental command', async () => {
764
+ vi.mocked(getCurrentGitBranch).mockResolvedValue('feature-branch');
765
+ const handler = setupAndGetHandler(DEFAULT_PACKAGE_JSON, {}, 'experimental');
766
+ await handler({
767
+ dryRun: false,
768
+ skipBranchValidation: false,
769
+ skipVersionCheck: false,
770
+ });
771
+ expect(select).not.toHaveBeenCalled();
772
+ expect(publish).toHaveBeenCalledWith('experimental', false, false, 'https://registry.npmjs.org/');
773
+ });
774
+ });
775
+ describe('previous', () => {
776
+ const PREVIOUS_OPTIONS = {
777
+ publishFromCI: true,
778
+ previousBranch: 'previous-branch',
779
+ };
780
+ const ARGS = {
781
+ versionIncrement: 'patch',
782
+ dryRun: false,
783
+ skipBranchValidation: false,
784
+ skipVersionCheck: false,
785
+ };
786
+ beforeEach(() => {
787
+ vi.mocked(getCurrentGitBranch).mockResolvedValue('previous-branch');
788
+ vi.mocked(getDistTags).mockResolvedValue({
789
+ latest: '1.0.0',
790
+ next: '1.0.0-beta.1',
791
+ experimental: '1.0.0-experimental.1',
792
+ previous: '0.9.0',
793
+ });
794
+ mockedExecFile.mockResolvedValue({
795
+ stdout: 'https://github.com/test/repo/pull/2\n',
796
+ stderr: '',
797
+ });
798
+ });
799
+ function getPreviousHandler(handlerOptions = PREVIOUS_OPTIONS) {
800
+ return setupAndGetHandler({ ...DEFAULT_PACKAGE_JSON, version: '0.9.0' }, handlerOptions, 'previous');
801
+ }
802
+ it('opens a release PR against previousBranch with publishFromCI', async () => {
803
+ await getPreviousHandler()(ARGS);
804
+ expect(mockedExec).toHaveBeenCalledWith('git checkout -b v0.9.1-publish');
805
+ expect(mockedExec).toHaveBeenCalledWith('yarn version --no-git-tag-version --new-version 0.9.1');
806
+ expect(mockedExecFile).toHaveBeenCalledWith('gh', expect.arrayContaining([
807
+ '--base',
808
+ 'previous-branch',
809
+ '--head',
810
+ 'v0.9.1-publish',
811
+ ]));
812
+ expect(publish).not.toHaveBeenCalled();
813
+ expect(vi.mocked(open)).not.toHaveBeenCalled();
814
+ });
815
+ it('previews the PR against previousBranch on a dry run', async () => {
816
+ await getPreviousHandler()({ ...ARGS, dryRun: true });
817
+ expectOnlyRepoAccessCheck();
818
+ expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('gh pr create --base previous-branch --head v0.9.1-publish'));
819
+ });
820
+ it('still publishes locally without publishFromCI', async () => {
821
+ await getPreviousHandler({ previousBranch: 'previous-branch' })(ARGS);
822
+ expect(publish).toHaveBeenCalledWith('previous', false, false, 'https://registry.npmjs.org/');
823
+ expectOnlyRepoAccessCheck();
824
+ });
825
+ it('rejects releasing from a branch other than previousBranch', async () => {
826
+ vi.mocked(getCurrentGitBranch).mockResolvedValue('master');
827
+ await withExitMocked(() => getPreviousHandler()(ARGS));
828
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('can only be published from the previous-branch branch'));
829
+ expectOnlyRepoAccessCheck();
830
+ });
831
+ it('rejects a local version out of sync with the previous dist-tag', async () => {
832
+ await withExitMocked(() => setupAndGetHandler({ ...DEFAULT_PACKAGE_JSON, version: '0.8.0' }, PREVIOUS_OPTIONS, 'previous')(ARGS));
833
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('out of sync with published version 0.9.0'));
834
+ });
835
+ it('fails without previousBranch configured', async () => {
836
+ await withExitMocked(() => getPreviousHandler({ publishFromCI: true })(ARGS));
837
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('previousBranch is not configured'));
838
+ });
839
+ it('fails with patch|minor|major when -v is omitted with --yes', async () => {
840
+ await withExitMocked(() => getPreviousHandler()({
841
+ dryRun: false,
842
+ skipBranchValidation: false,
843
+ skipVersionCheck: false,
844
+ yes: true,
845
+ }));
846
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Pass -v <patch|minor|major>'));
847
+ });
848
+ });
849
+ describe('experimental via CI (publishFromCI)', () => {
850
+ const HEAD_SHA = 'a'.repeat(40);
851
+ const RUN = {
852
+ databaseId: 99,
853
+ displayTitle: `Experimental release feature-branch@${HEAD_SHA}`,
854
+ url: 'https://github.com/test/repo/actions/runs/99',
855
+ };
856
+ const ARGS = {
857
+ dryRun: false,
858
+ skipBranchValidation: false,
859
+ skipVersionCheck: false,
860
+ };
861
+ let runStatus;
862
+ beforeEach(() => {
863
+ runStatus = { status: 'completed', conclusion: 'success', jobs: [] };
864
+ vi.mocked(getCurrentGitBranch).mockResolvedValue('feature-branch');
865
+ mockedExec.mockImplementation((cmd) => Promise.resolve({
866
+ stdout: cmd.startsWith('git rev-parse HEAD') ||
867
+ cmd.startsWith('git rev-parse origin/')
868
+ ? `${HEAD_SHA}\n`
869
+ : '',
870
+ stderr: '',
871
+ }));
872
+ let isDispatched = false;
873
+ mockedExecFile.mockImplementation((_file, args) => {
874
+ if (args[0] === 'workflow') {
875
+ isDispatched = true;
876
+ }
877
+ return Promise.resolve({
878
+ stdout: args[0] === 'run' && args[1] === 'list'
879
+ ? JSON.stringify(isDispatched ? [RUN] : [])
880
+ : args[0] === 'run' && args[1] === 'view'
881
+ ? JSON.stringify(runStatus)
882
+ : '',
883
+ stderr: '',
884
+ });
885
+ });
886
+ vi.mocked(fs.mkdtemp).mockResolvedValue('/tmp/experimental-version-x');
887
+ vi.mocked(fs.readFile).mockResolvedValue('1.0.0-experimental.0\n');
888
+ });
889
+ function getExperimentalHandler() {
890
+ return setupAndGetHandler(DEFAULT_PACKAGE_JSON, { publishFromCI: true }, 'experimental');
891
+ }
892
+ it('dispatches the workflow from the main branch with the branch and sha', async () => {
893
+ await getExperimentalHandler()(ARGS);
894
+ expect(mockedExecFile).toHaveBeenCalledWith('gh', [
895
+ 'workflow',
896
+ 'run',
897
+ 'experimental-release.yml',
898
+ '--ref',
899
+ 'master',
900
+ '-f',
901
+ 'branch=feature-branch',
902
+ '-f',
903
+ `sha=${HEAD_SHA}`,
904
+ ]);
905
+ expect(mockedExecFile).toHaveBeenCalledWith('gh', [
906
+ 'run',
907
+ 'view',
908
+ '99',
909
+ '--json',
910
+ 'status,conclusion,jobs',
911
+ ]);
912
+ expect(logger.success).toHaveBeenCalledWith('Published @hubspot/test-package@1.0.0-experimental.0 on the experimental tag');
913
+ });
914
+ it('uses the experimentalWorkflow option', async () => {
915
+ await setupAndGetHandler(DEFAULT_PACKAGE_JSON, { publishFromCI: true, experimentalWorkflow: 'custom.yml' }, 'experimental')(ARGS);
916
+ expect(mockedExecFile).toHaveBeenCalledWith('gh', expect.arrayContaining(['workflow', 'run', 'custom.yml']));
917
+ });
918
+ it('does not build, modify package.json, check npm auth, or publish locally', async () => {
919
+ await getExperimentalHandler()(ARGS);
920
+ const calls = mockedExec.mock.calls.map(args => args[0]);
921
+ expect(calls).not.toContain('yarn build');
922
+ expect(calls.some(cmd => cmd.startsWith('yarn version'))).toBe(false);
923
+ expect(calls.some(cmd => cmd.startsWith('npm whoami'))).toBe(false);
924
+ expect(publish).not.toHaveBeenCalled();
925
+ });
926
+ it('does not dispatch on a dry run', async () => {
927
+ await getExperimentalHandler()({ ...ARGS, dryRun: true });
928
+ expect(mockedExecFile).not.toHaveBeenCalledWith('gh', expect.arrayContaining(['workflow', 'run']));
929
+ expect(logger.log).toHaveBeenCalledWith(` gh workflow run experimental-release.yml --ref master -f branch=feature-branch -f sha=${HEAD_SHA}`);
930
+ });
931
+ it('fails before dispatching when the branch is not pushed', async () => {
932
+ mockedExec.mockImplementation((cmd) => Promise.resolve({
933
+ stdout: cmd.startsWith('git rev-parse origin/')
934
+ ? `${'b'.repeat(40)}\n`
935
+ : cmd.startsWith('git rev-parse HEAD')
936
+ ? `${HEAD_SHA}\n`
937
+ : '',
938
+ stderr: '',
939
+ }));
940
+ await withExitMocked(() => getExperimentalHandler()(ARGS));
941
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Push your branch first: git push -u origin feature-branch'));
942
+ expectOnlyRepoAccessCheck();
943
+ });
944
+ it('still rejects experimental releases from the main branch', async () => {
945
+ vi.mocked(getCurrentGitBranch).mockResolvedValue('master');
946
+ await withExitMocked(() => getExperimentalHandler()(ARGS));
947
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('experimental tag cannot be published from the master branch'));
948
+ expectOnlyRepoAccessCheck();
949
+ });
950
+ it('exits non-zero with the run URL when the workflow fails', async () => {
951
+ runStatus = { status: 'completed', conclusion: 'failure', jobs: [] };
952
+ const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
953
+ throw new Error('process.exit called');
954
+ });
955
+ await expect(getExperimentalHandler()(ARGS)).rejects.toThrow('process.exit called');
956
+ expect(exitSpy).toHaveBeenCalledWith(1);
957
+ expect(logger.error).toHaveBeenCalledWith(expect.stringContaining(RUN.url));
958
+ exitSpy.mockRestore();
959
+ });
960
+ });
481
961
  describe('custom build function', () => {
482
962
  it('calls the provided build function instead of running yarn build', async () => {
483
963
  const buildFn = vi.fn().mockResolvedValue(undefined);
@@ -1,4 +1,6 @@
1
1
  export declare const DEFAULT_MAIN_BRANCH = "master";
2
+ export declare const DEFAULT_EXPERIMENTAL_WORKFLOW = "experimental-release.yml";
3
+ export declare const EXPERIMENTAL_VERSION_ARTIFACT = "experimental-version";
2
4
  export declare const TAG: {
3
5
  readonly LATEST: "latest";
4
6
  readonly NEXT: "next";
@@ -12,6 +14,7 @@ export declare const INCREMENT: {
12
14
  readonly PRERELEASE: "prerelease";
13
15
  };
14
16
  export declare const VERSION_INCREMENT_OPTIONS: readonly ["patch", "minor", "major", "prerelease"];
17
+ export declare const LATEST_INCREMENT_OPTIONS: readonly ["patch", "minor", "major"];
15
18
  export declare const TAG_OPTIONS: readonly ["latest", "next", "experimental", "previous"];
16
19
  export declare const PRERELEASE_IDENTIFIER: {
17
20
  readonly NEXT: "beta";
@@ -1,4 +1,6 @@
1
1
  export const DEFAULT_MAIN_BRANCH = 'master';
2
+ export const DEFAULT_EXPERIMENTAL_WORKFLOW = 'experimental-release.yml';
3
+ export const EXPERIMENTAL_VERSION_ARTIFACT = 'experimental-version';
2
4
  export const TAG = {
3
5
  LATEST: 'latest',
4
6
  NEXT: 'next',
@@ -17,6 +19,11 @@ export const VERSION_INCREMENT_OPTIONS = [
17
19
  INCREMENT.MAJOR,
18
20
  INCREMENT.PRERELEASE,
19
21
  ];
22
+ export const LATEST_INCREMENT_OPTIONS = [
23
+ INCREMENT.PATCH,
24
+ INCREMENT.MINOR,
25
+ INCREMENT.MAJOR,
26
+ ];
20
27
  export const TAG_OPTIONS = [
21
28
  TAG.LATEST,
22
29
  TAG.NEXT,