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

internal(tests-sdk) - improve calculating bit bin name when running bit commands during tests #8596

Merged
merged 3 commits into from
Feb 29, 2024
Merged
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
61 changes: 60 additions & 1 deletion src/e2e-helper/e2e-command-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,66 @@ export default class CommandHelper {
constructor(scopes: ScopesData, debugMode: boolean) {
this.scopes = scopes;
this.debugMode = debugMode;
this.bitBin = process.env.npm_config_bit_bin || 'bit'; // e.g. npm run e2e-test --bit_bin=bit-dev
this.bitBin = this.getBitBin(); // e.g. npm run e2e-test --bit_bin=bit-dev
}

/**
* Handle different cases of running the command helper:
* In general the command helper run in 2 main cases:
* 1. When running the e2e-test from the e2e-test directory. (aka npm run e2e-test)
* 2. When running aspects tests that uses the mock-workspace for example (bit test teambit.semantics/schema)
*
* The binary of bit might be loaded from few different places:
* 1. The repo itself (usually bin name created by running `npm run dev-link bd`)
* 2. A custom bvm link (generated by `bvm link` command like `bvm link bit1670 1.6.70`)
* 3. Default bvm link (usually just "bit")
*
* On the e2e script we have a flag to control the used bin name (`--bit-bin`) but for `bit test` or `bit build`
* we don't have such.
* This function calculate the bin name based on what the user actually called on his command.
*
* Examples:
*
* - npm run e2e-test --bit-bin=bd3 --debug --keep-envs // should use bd3 from the `--bit-bin`
* - npm run e2e-test --debug --keep-envs // should use bit (default bvm link)
* - bit test teambit.semantics/schema // should use bit - the name of the bin name that the user run
* - bd test teambit.semantics/schema // should use bd - the name of the bin name that the user run
* (usually point to the repo bin)
* - bvm link bit1670 1.6.70
* then
* bit1760 test teambit.semantics/schema // should use bit1670 the name of the bin name that the user run
* (point to the custom bvm link)
*/
getBitBin() {
if (process.env.npm_config_bit_bin) return process.env.npm_config_bit_bin;
const [processBin, processPath] = process.argv;
if (processBin.endsWith('node')) {
const binDir = path.dirname(processPath);
const binName = path.basename(processPath);
const binNameFromBvm = this.getBinFromBvmLinks(binDir);
if (binNameFromBvm) return binNameFromBvm;
if (this.isInPath(binDir) || binDir.includes('.bvm')) return binName;
if (binName === 'mocha') return 'bit';
return `${processBin} ${processPath}`;
}
return 'bit';
}

getBinFromBvmLinks(binDir: string): string | undefined {
const linksDir = path.join('.bvm', 'links');
if (binDir.includes(linksDir)) {
const splitted = binDir.split(path.sep);
const linksIndex = splitted.indexOf('links');
if (linksIndex === -1) return undefined;
return splitted[linksIndex + 1];
}
return undefined;
}

isInPath(binDir: string): boolean {
const osPaths = (process.env.PATH || process.env.Path || process.env.path || '').split(path.delimiter);
if (osPaths.indexOf(binDir) !== -1) return true;
return false;
}

setFeatures(featuresToggle: string | string[]) {
Expand Down