diff --git a/src/e2e-helper/e2e-command-helper.ts b/src/e2e-helper/e2e-command-helper.ts index e42c7f080e9e..69118b529940 100644 --- a/src/e2e-helper/e2e-command-helper.ts +++ b/src/e2e-helper/e2e-command-helper.ts @@ -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[]) {