Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lane-switch): block switching a lane when on main and there are staging components #6981

Merged
merged 3 commits into from
Jan 31, 2023
Merged
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
4 changes: 1 addition & 3 deletions e2e/harmony/lanes/lane-from-lane.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ describe('bit lane command', function () {
});
it('bit create should throw an error suggesting to export or reset first', () => {
const output = helper.general.runWithTryCatch('bit lane create lane-b');
expect(output).to.have.string(
'unable to create a new lane, please export or reset the following components first'
);
expect(output).to.have.string('please export or reset the following components first');
});
});
});
18 changes: 18 additions & 0 deletions e2e/harmony/lanes/switch-lanes.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,22 @@ describe('bit lane command', function () {
expect(list).to.have.lengthOf(1);
});
});
describe('switch from main to a lane when main has staged components', () => {
before(() => {
helper.scopeHelper.setNewLocalAndRemoteScopes();
helper.fixtures.populateComponents(1);
helper.command.tagAllWithoutBuild();
helper.command.export();
helper.command.createLane();
helper.command.snapAllComponentsWithoutBuild('--unmodified');
helper.command.export();
helper.command.switchLocalLane('main');
helper.command.tagAllWithoutBuild('--unmodified');
});
it('should block the switch and suggest to export or reset', () => {
expect(() => helper.command.switchLocalLane('dev')).to.throw(
'please export or reset the following components first'
);
});
});
});
4 changes: 2 additions & 2 deletions scopes/lanes/lanes/create-lane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ export function throwForInvalidLaneName(laneName: string) {
}
}

async function throwForStagedComponents(consumer: Consumer) {
export async function throwForStagedComponents(consumer: Consumer) {
const componentList = new ComponentsList(consumer);
const stagedComponents = await componentList.listExportPendingComponentsIds();
if (stagedComponents.length) {
throw new BitError(
`unable to create a new lane, please export or reset the following components first: ${stagedComponents.join(
`unable to switch/create a new lane, please export or reset the following components first: ${stagedComponents.join(
', '
)}`
);
Expand Down
4 changes: 4 additions & 0 deletions scopes/lanes/lanes/switch-lanes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Workspace } from '@teambit/workspace';
import { Logger } from '@teambit/logger';
import { BitError } from '@teambit/bit-error';
import { LanesMain } from './lanes.main.runtime';
import { throwForStagedComponents } from './create-lane';

export type SwitchProps = {
laneName: string;
Expand All @@ -51,6 +52,9 @@ export class LaneSwitcher {

async switch(): Promise<ApplyVersionResults> {
this.logger.setStatusLine(`switching lanes`);
if (this.workspace.isOnMain()) {
await throwForStagedComponents(this.consumer);
}
await this.populateSwitchProps();
const allComponentsStatus: ComponentStatus[] = await this.getAllComponentsStatus();
const componentWithConflict = allComponentsStatus.find(
Expand Down
4 changes: 4 additions & 0 deletions scopes/workspace/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,10 @@ export class Workspace implements ComponentFactory {
return this.consumer.getCurrentLaneId();
}

isOnMain(): boolean {
return this.consumer.isOnMain();
}

/**
* if checked out to a lane and the lane exists in the remote,
* return the remote lane id (name+scope). otherwise, return null.
Expand Down