|
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
3 | 3 |
|
4 | 4 | using Microsoft.Testing.Platform.CommandLine; |
| 5 | +using Microsoft.Testing.Platform.Extensions.CommandLine; |
5 | 6 | using Microsoft.Testing.Platform.Helpers; |
6 | 7 | using Microsoft.Testing.Platform.Logging; |
7 | 8 | using Microsoft.Testing.Platform.Resources; |
@@ -410,6 +411,108 @@ internal IReadOnlyList<JsonCommandLineOptionEntry> EnumerateCommandLineOptions() |
410 | 411 | return result; |
411 | 412 | } |
412 | 413 |
|
| 414 | + /// <summary> |
| 415 | + /// Rewrites scalar <c>commandLineOptions:<name></c> entries to the indexed shape |
| 416 | + /// (<c>commandLineOptions:<name>:0</c>) for options registered with |
| 417 | + /// <see cref="ArgumentArity.Min"/> >= 1, using <paramref name="optionByName"/> as the |
| 418 | + /// option registry. |
| 419 | + /// </summary> |
| 420 | + /// <remarks> |
| 421 | + /// <para> |
| 422 | + /// The CLI-backed configuration provider stores zero-arity flags at the bare option key and |
| 423 | + /// arg-bearing options under indexed keys (one per argument). The JSON provider cannot |
| 424 | + /// distinguish these shapes at parse time because the user freely writes either |
| 425 | + /// <c>"foo": "value"</c> (scalar) or <c>"foo": ["value"]</c> (array). For arg-bearing |
| 426 | + /// options that always require at least one argument, a scalar value is always the first |
| 427 | + /// argument — never a presence marker. Storing it under the indexed key normalizes the |
| 428 | + /// shape so both <see cref="EnumerateCommandLineOptions"/> and |
| 429 | + /// <see cref="AggregatedConfiguration.TryGetCommandLineOptionFromProviders"/> see one |
| 430 | + /// consistent representation. |
| 431 | + /// </para> |
| 432 | + /// <para> |
| 433 | + /// In particular, this fixes the JSON scalar/bool ambiguity from #6349/#8830: a value like |
| 434 | + /// <c>"my-option": "true"</c> for an arg-bearing option used to be misinterpreted as a |
| 435 | + /// presence marker (zero arguments) instead of being passed as the first argument value. |
| 436 | + /// </para> |
| 437 | + /// <para> |
| 438 | + /// Optional-arg options (<c>Min == 0 && Max >= 1</c>) are left untouched because |
| 439 | + /// either interpretation (presence vs. scalar argument) is semantically valid; users who |
| 440 | + /// need to disambiguate should use the explicit array form. |
| 441 | + /// </para> |
| 442 | + /// </remarks> |
| 443 | + internal void NormalizeCommandLineOptionScalars(IReadOnlyDictionary<string, CommandLineOption> optionByName) |
| 444 | + { |
| 445 | + if (_singleValueData is null || _singleValueData.Count == 0) |
| 446 | + { |
| 447 | + return; |
| 448 | + } |
| 449 | + |
| 450 | + const string sectionName = PlatformConfigurationConstants.CommandLineOptionsSectionName; |
| 451 | + string sectionPrefix = sectionName + PlatformConfigurationConstants.KeyDelimiter; |
| 452 | + |
| 453 | + List<(string OldKey, string NewKey, string Value)>? rewrites = null; |
| 454 | + |
| 455 | + foreach (KeyValuePair<string, string?> kvp in _singleValueData) |
| 456 | + { |
| 457 | + if (!kvp.Key.StartsWith(sectionPrefix, StringComparison.OrdinalIgnoreCase)) |
| 458 | + { |
| 459 | + continue; |
| 460 | + } |
| 461 | + |
| 462 | + // Bare key only: "commandLineOptions:<name>" with no further colon. Indexed entries |
| 463 | + // already use the canonical shape and are not subject to the scalar/bool ambiguity. |
| 464 | + string remainder = kvp.Key.Substring(sectionPrefix.Length); |
| 465 | + if (remainder.Length == 0 |
| 466 | + || remainder.IndexOf(PlatformConfigurationConstants.KeyDelimiter, StringComparison.Ordinal) >= 0) |
| 467 | + { |
| 468 | + continue; |
| 469 | + } |
| 470 | + |
| 471 | + // A null value at the bare key represents an empty object/array; the schema |
| 472 | + // validator in EnumerateCommandLineOptions will reject it later. Skip here so we |
| 473 | + // never promote a placeholder to an indexed slot. |
| 474 | + if (kvp.Value is null) |
| 475 | + { |
| 476 | + continue; |
| 477 | + } |
| 478 | + |
| 479 | + if (!optionByName.TryGetValue(remainder, out CommandLineOption? option)) |
| 480 | + { |
| 481 | + // Unknown option name. Leave alone so the unknown-option validator pass can |
| 482 | + // surface a clear error referencing testconfig.json. |
| 483 | + continue; |
| 484 | + } |
| 485 | + |
| 486 | + if (option.Arity.Min < 1) |
| 487 | + { |
| 488 | + continue; |
| 489 | + } |
| 490 | + |
| 491 | + string newKey = kvp.Key + PlatformConfigurationConstants.KeyDelimiter + "0"; |
| 492 | + |
| 493 | + // Defensive: if the indexed slot already exists, the JSON contained both a scalar |
| 494 | + // and an array entry for the same option, which the schema validator will flag as |
| 495 | + // malformed. Don't clobber. |
| 496 | + if (_singleValueData.ContainsKey(newKey)) |
| 497 | + { |
| 498 | + continue; |
| 499 | + } |
| 500 | + |
| 501 | + (rewrites ??= []).Add((kvp.Key, newKey, kvp.Value)); |
| 502 | + } |
| 503 | + |
| 504 | + if (rewrites is null) |
| 505 | + { |
| 506 | + return; |
| 507 | + } |
| 508 | + |
| 509 | + foreach ((string oldKey, string newKey, string value) in rewrites) |
| 510 | + { |
| 511 | + _singleValueData.Remove(oldKey); |
| 512 | + _singleValueData[newKey] = value; |
| 513 | + } |
| 514 | + } |
| 515 | + |
413 | 516 | private static bool StartsWithChar(string text, char c) |
414 | 517 | { |
415 | 518 | for (int i = 0; i < text.Length; i++) |
|
0 commit comments