|
3 | 3 | import * as net from 'net'; |
4 | 4 | import * as path from 'path'; |
5 | 5 | import { CancellationToken, Position, TestController, TestItem, Uri, Range } from 'vscode'; |
6 | | -import { traceError, traceInfo, traceLog, traceVerbose } from '../../../logging'; |
| 6 | +import { traceError, traceLog, traceVerbose } from '../../../logging'; |
7 | 7 |
|
8 | 8 | import { EnableTestAdapterRewrite } from '../../../common/experiments/groups'; |
9 | 9 | import { IExperimentService } from '../../../common/types'; |
@@ -351,39 +351,103 @@ export function splitTestNameWithRegex(testName: string): [string, string] { |
351 | 351 | } |
352 | 352 |
|
353 | 353 | /** |
354 | | - * Takes a list of arguments and adds an key-value pair to the list if the key doesn't already exist. Searches each element |
355 | | - * in the array for the key to see if it is contained within the element. |
356 | | - * @param args list of arguments to search |
357 | | - * @param argToAdd argument to add if it doesn't already exist |
358 | | - * @returns the list of arguments with the key-value pair added if it didn't already exist |
| 354 | + * Converts an array of strings (with or without '=') into a map. |
| 355 | + * If a string contains '=', it is split into a key-value pair, with the portion |
| 356 | + * before the '=' as the key and the portion after the '=' as the value. |
| 357 | + * If no '=' is found in the string, the entire string becomes a key with a value of null. |
| 358 | + * |
| 359 | + * @param args - Readonly array of strings to be converted to a map. |
| 360 | + * @returns A map representation of the input strings. |
359 | 361 | */ |
360 | | -export function addValueIfKeyNotExist(args: string[], key: string, value: string | null): string[] { |
| 362 | +export const argsToMap = (args: ReadonlyArray<string>): { [key: string]: Array<string> | null | undefined } => { |
| 363 | + const map: { [key: string]: Array<string> | null } = {}; |
361 | 364 | for (const arg of args) { |
362 | | - if (arg.includes(key)) { |
363 | | - traceInfo(`arg: ${key} already exists in args, not adding.`); |
364 | | - return args; |
| 365 | + const delimiter = arg.indexOf('='); |
| 366 | + if (delimiter === -1) { |
| 367 | + // If no delimiter is found, the entire string becomes a key with a value of null. |
| 368 | + map[arg] = null; |
| 369 | + } else { |
| 370 | + const key = arg.slice(0, delimiter); |
| 371 | + const value = arg.slice(delimiter + 1); |
| 372 | + if (map[key]) { |
| 373 | + // add to the array |
| 374 | + const arr = map[key] as string[]; |
| 375 | + arr.push(value); |
| 376 | + map[key] = arr; |
| 377 | + } else { |
| 378 | + // create a new array |
| 379 | + map[key] = [value]; |
| 380 | + } |
365 | 381 | } |
366 | 382 | } |
367 | | - if (value) { |
368 | | - args.push(`${key}=${value}`); |
369 | | - } else { |
370 | | - args.push(`${key}`); |
| 383 | + |
| 384 | + return map; |
| 385 | +}; |
| 386 | + |
| 387 | +/** |
| 388 | + * Converts a map into an array of strings. |
| 389 | + * Each key-value pair in the map is transformed into a string. |
| 390 | + * If the value is null, only the key is represented in the string. |
| 391 | + * If the value is defined (and not null), the string is in the format "key=value". |
| 392 | + * If a value is undefined, the key-value pair is skipped. |
| 393 | + * |
| 394 | + * @param map - The map to be converted to an array of strings. |
| 395 | + * @returns An array of strings representation of the input map. |
| 396 | + */ |
| 397 | +export const mapToArgs = (map: { [key: string]: Array<string> | null | undefined }): string[] => { |
| 398 | + const out: string[] = []; |
| 399 | + for (const key of Object.keys(map)) { |
| 400 | + const value = map[key]; |
| 401 | + if (value === undefined) { |
| 402 | + // eslint-disable-next-line no-continue |
| 403 | + continue; |
| 404 | + } |
| 405 | + if (value === null) { |
| 406 | + out.push(key); |
| 407 | + } else { |
| 408 | + const values = Array.isArray(value) ? (value as string[]) : [value]; |
| 409 | + for (const v of values) { |
| 410 | + out.push(`${key}=${v}`); |
| 411 | + } |
| 412 | + } |
371 | 413 | } |
372 | | - return args; |
373 | | -} |
| 414 | + |
| 415 | + return out; |
| 416 | +}; |
374 | 417 |
|
375 | 418 | /** |
376 | | - * Checks if a key exists in a list of arguments. Searches each element in the array |
377 | | - * for the key to see if it is contained within the element. |
378 | | - * @param args list of arguments to search |
379 | | - * @param key string to search for |
380 | | - * @returns true if the key exists in the list of arguments, false otherwise |
| 419 | + * Adds an argument to the map only if it doesn't already exist. |
| 420 | + * |
| 421 | + * @param map - The map of arguments. |
| 422 | + * @param argKey - The argument key to be checked and added. |
| 423 | + * @param argValue - The value to set for the argument if it's not already in the map. |
| 424 | + * @returns The updated map. |
381 | 425 | */ |
382 | | -export function argKeyExists(args: string[], key: string): boolean { |
383 | | - for (const arg of args) { |
384 | | - if (arg.includes(key)) { |
385 | | - return true; |
| 426 | +export function addArgIfNotExist( |
| 427 | + map: { [key: string]: Array<string> | null | undefined }, |
| 428 | + argKey: string, |
| 429 | + argValue: string | null, |
| 430 | +): { [key: string]: Array<string> | null | undefined } { |
| 431 | + // Only add the argument if it doesn't exist in the map. |
| 432 | + if (map[argKey] === undefined) { |
| 433 | + // if null then set to null, otherwise set to an array with the value |
| 434 | + if (argValue === null) { |
| 435 | + map[argKey] = null; |
| 436 | + } else { |
| 437 | + map[argKey] = [argValue]; |
386 | 438 | } |
387 | 439 | } |
388 | | - return false; |
| 440 | + |
| 441 | + return map; |
| 442 | +} |
| 443 | + |
| 444 | +/** |
| 445 | + * Checks if an argument key exists in the map. |
| 446 | + * |
| 447 | + * @param map - The map of arguments. |
| 448 | + * @param argKey - The argument key to be checked. |
| 449 | + * @returns True if the argument key exists in the map, false otherwise. |
| 450 | + */ |
| 451 | +export function argKeyExists(map: { [key: string]: Array<string> | null | undefined }, argKey: string): boolean { |
| 452 | + return map[argKey] !== undefined; |
389 | 453 | } |
0 commit comments