From c95ff4ff7d25bc3583b5bf4b273f22ea0187c3b2 Mon Sep 17 00:00:00 2001 From: Gilad Shoham Date: Thu, 29 Feb 2024 13:42:37 +0200 Subject: [PATCH 1/2] internal(tests-sdk) - improve calculating bit bin name when running bit commands during tests --- src/e2e-helper/e2e-command-helper.ts | 34 +++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/e2e-helper/e2e-command-helper.ts b/src/e2e-helper/e2e-command-helper.ts index e42c7f080e9e..6c4e9ff7bc04 100644 --- a/src/e2e-helper/e2e-command-helper.ts +++ b/src/e2e-helper/e2e-command-helper.ts @@ -34,7 +34,39 @@ 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 + } + + 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[]) { From 069708474f59543c053a6e5e317ed8bd9e4663a2 Mon Sep 17 00:00:00 2001 From: Gilad Shoham Date: Thu, 29 Feb 2024 15:55:28 +0200 Subject: [PATCH 2/2] add docs about bin name --- src/e2e-helper/e2e-command-helper.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/e2e-helper/e2e-command-helper.ts b/src/e2e-helper/e2e-command-helper.ts index 6c4e9ff7bc04..69118b529940 100644 --- a/src/e2e-helper/e2e-command-helper.ts +++ b/src/e2e-helper/e2e-command-helper.ts @@ -37,6 +37,33 @@ export default class CommandHelper { 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;