Skip to content

Commit

Permalink
Implement --config for specifying testconfig.json file (#4124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 authored Nov 25, 2024
1 parent 077bf0e commit e3900ac
Show file tree
Hide file tree
Showing 20 changed files with 281 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal sealed class PlatformCommandLineProvider : ICommandLineOptionsProvider
public const string MinimumExpectedTestsOptionKey = "minimum-expected-tests";
public const string TestHostControllerPIDOptionKey = "internal-testhostcontroller-pid";
public const string ExitOnProcessExitOptionKey = "exit-on-process-exit";
public const string ConfigFileOptionKey = "config-file";

public const string ServerOptionKey = "server";
public const string ClientPortOptionKey = "client-port";
Expand Down Expand Up @@ -59,6 +60,7 @@ internal sealed class PlatformCommandLineProvider : ICommandLineOptionsProvider
new(DiscoverTestsOptionKey, PlatformResources.PlatformCommandLineDiscoverTestsOptionDescription, ArgumentArity.Zero, false, isBuiltIn: true),
new(IgnoreExitCodeOptionKey, PlatformResources.PlatformCommandLineIgnoreExitCodeOptionDescription, ArgumentArity.ExactlyOne, false, isBuiltIn: true),
new(ExitOnProcessExitOptionKey, PlatformResources.PlatformCommandLineExitOnProcessExitOptionDescription, ArgumentArity.ExactlyOne, false, isBuiltIn: true),
new(ConfigFileOptionKey, PlatformResources.PlatformCommandLineConfigFileOptionDescription, ArgumentArity.ExactlyOne, false, isBuiltIn: true),

// Hidden options
new(HelpOptionQuestionMark, PlatformResources.PlatformCommandLineHelpOptionDescription, ArgumentArity.Zero, true, isBuiltIn: true),
Expand Down Expand Up @@ -120,6 +122,25 @@ public Task<ValidationResult> ValidateOptionArgumentsAsync(CommandLineOption com
}
}

if (commandOption.Name == ConfigFileOptionKey)
{
string arg = arguments[0];
if (!File.Exists(arg))
{
try
{
// Get the full path for better error messages.
// As this is only for the purpose of throwing an exception, ignore any exceptions during the GetFullPath call.
arg = Path.GetFullPath(arg);
}
catch
{
}

return ValidationResult.InvalidTask(string.Format(CultureInfo.InvariantCulture, PlatformResources.ConfigurationFileNotFound, arg));
}
}

// Now validate the minimum expected tests option
return IsMinimumExpectedTestsOptionValidAsync(commandOption, arguments);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Globalization;

using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Logging;
using Microsoft.Testing.Platform.Resources;
using Microsoft.Testing.Platform.Services;

namespace Microsoft.Testing.Platform.Configurations;

internal sealed partial class JsonConfigurationSource
{
internal sealed class JsonConfigurationProvider(ITestApplicationModuleInfo testApplicationModuleInfo, IFileSystem fileSystem, ILogger? logger) : IConfigurationProvider
internal sealed class JsonConfigurationProvider(
ITestApplicationModuleInfo testApplicationModuleInfo,
IFileSystem fileSystem,
CommandLineParseResult commandLineParseResult,
ILogger? logger) : IConfigurationProvider
{
private readonly ITestApplicationModuleInfo _testApplicationModuleInfo = testApplicationModuleInfo;
private readonly IFileSystem _fileSystem = fileSystem;
private readonly CommandLineParseResult _commandLineParseResult = commandLineParseResult;
private readonly ILogger? _logger = logger;
private Dictionary<string, string?>? _propertyToAllChildren;
private Dictionary<string, string?>? _singleValueData;
Expand All @@ -29,13 +38,35 @@ private async Task LogInformationAsync(string message)

public async Task LoadAsync()
{
string configFileName = $"{Path.Combine(
Path.GetDirectoryName(_testApplicationModuleInfo.GetCurrentTestApplicationFullPath())!,
Path.GetFileNameWithoutExtension(_testApplicationModuleInfo.GetCurrentTestApplicationFullPath()))}{PlatformConfigurationConstants.PlatformConfigSuffixFileName}";
if (!_fileSystem.Exists(configFileName))
string configFileName;
if (_commandLineParseResult.TryGetOptionArgumentList(PlatformCommandLineProvider.ConfigFileOptionKey, out string[]? configOptions))
{
await LogInformationAsync($"Config file '{configFileName}' not found.");
return;
configFileName = configOptions[0];
if (!_fileSystem.Exists(configFileName))
{
try
{
// Get the full path for better error messages.
// As this is only for the purpose of throwing an exception, ignore any exceptions during the GetFullPath call.
configFileName = Path.GetFullPath(configFileName);
}
catch
{
}

throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, PlatformResources.ConfigurationFileNotFound, configFileName), configFileName);
}
}
else
{
configFileName = $"{Path.Combine(
Path.GetDirectoryName(_testApplicationModuleInfo.GetCurrentTestApplicationFullPath())!,
Path.GetFileNameWithoutExtension(_testApplicationModuleInfo.GetCurrentTestApplicationFullPath()))}{PlatformConfigurationConstants.PlatformConfigSuffixFileName}";

if (!_fileSystem.Exists(configFileName))
{
return;
}
}

await LogInformationAsync($"Config file '{configFileName}' loaded.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ internal sealed partial class JsonConfigurationSource(ITestApplicationModuleInfo
public Task<bool> IsEnabledAsync() => Task.FromResult(true);

public Task<IConfigurationProvider> BuildAsync(CommandLineParseResult commandLineParseResult)
=> Task.FromResult((IConfigurationProvider)new JsonConfigurationProvider(_testApplicationModuleInfo, _fileSystem, _fileLoggerProvider?.CreateLogger(typeof(JsonConfigurationProvider).ToString())));
=> Task.FromResult((IConfigurationProvider)new JsonConfigurationProvider(_testApplicationModuleInfo, _fileSystem, commandLineParseResult, _fileLoggerProvider?.CreateLogger(typeof(JsonConfigurationProvider).ToString())));
}
Original file line number Diff line number Diff line change
Expand Up @@ -665,4 +665,10 @@ Takes one argument as string in the format &lt;value&gt;[h|m|s] where 'value' is
<data name="PlatformCommandLineMinimumExpectedTestsOptionDescription" xml:space="preserve">
<value>Specifies the minimum number of tests that are expected to run.</value>
</data>
</root>
<data name="PlatformCommandLineConfigFileOptionDescription" xml:space="preserve">
<value>Specifies a testconfig.json file.</value>
</data>
<data name="ConfigurationFileNotFound" xml:space="preserve">
<value>The configuration file '{0}' specified with '--config-file' could not be found.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
<target state="translated">Už je zaregistrovaná stejná instance CompositeExtensonFactory.</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationFileNotFound">
<source>The configuration file '{0}' specified with '--config-file' could not be found.</source>
<target state="new">The configuration file '{0}' specified with '--config-file' could not be found.</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationManagerCannotFindDefaultJsonConfigurationErrorMessage">
<source>Could not find the default json configuration</source>
<target state="translated">Nepovedlo se najít výchozí konfiguraci json.</target>
Expand Down Expand Up @@ -416,6 +421,11 @@
<target state="translated">Zadejte port klienta.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineConfigFileOptionDescription">
<source>Specifies a testconfig.json file.</source>
<target state="new">Specifies a testconfig.json file.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineDiagnosticFileLoggerSynchronousWriteOptionDescription">
<source>Force the built-in file logger to write the log synchronously.
Useful for scenario where you don't want to lose any log (i.e. in case of crash).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
<target state="translated">Die gleiche Instanz von "CompositeExtensonFactory" ist bereits registriert</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationFileNotFound">
<source>The configuration file '{0}' specified with '--config-file' could not be found.</source>
<target state="new">The configuration file '{0}' specified with '--config-file' could not be found.</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationManagerCannotFindDefaultJsonConfigurationErrorMessage">
<source>Could not find the default json configuration</source>
<target state="translated">Die standardmäßige JSON-Konfiguration wurde nicht gefunden.</target>
Expand Down Expand Up @@ -416,6 +421,11 @@
<target state="translated">Geben Sie den Port des Clients an.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineConfigFileOptionDescription">
<source>Specifies a testconfig.json file.</source>
<target state="new">Specifies a testconfig.json file.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineDiagnosticFileLoggerSynchronousWriteOptionDescription">
<source>Force the built-in file logger to write the log synchronously.
Useful for scenario where you don't want to lose any log (i.e. in case of crash).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
<target state="translated">La misma instancia de "CompositeExtensonFactory" ya está registrada</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationFileNotFound">
<source>The configuration file '{0}' specified with '--config-file' could not be found.</source>
<target state="new">The configuration file '{0}' specified with '--config-file' could not be found.</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationManagerCannotFindDefaultJsonConfigurationErrorMessage">
<source>Could not find the default json configuration</source>
<target state="translated">No se pudo encontrar la configuración json predeterminada</target>
Expand Down Expand Up @@ -416,6 +421,11 @@
<target state="translated">Especifique el puerto del cliente.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineConfigFileOptionDescription">
<source>Specifies a testconfig.json file.</source>
<target state="new">Specifies a testconfig.json file.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineDiagnosticFileLoggerSynchronousWriteOptionDescription">
<source>Force the built-in file logger to write the log synchronously.
Useful for scenario where you don't want to lose any log (i.e. in case of crash).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
<target state="translated">La même instance de « CompositeExtensonFactory » est déjà inscrite</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationFileNotFound">
<source>The configuration file '{0}' specified with '--config-file' could not be found.</source>
<target state="new">The configuration file '{0}' specified with '--config-file' could not be found.</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationManagerCannotFindDefaultJsonConfigurationErrorMessage">
<source>Could not find the default json configuration</source>
<target state="translated">Configuration JSON par défaut introuvable</target>
Expand Down Expand Up @@ -416,6 +421,11 @@
<target state="translated">Spécifier le port du client.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineConfigFileOptionDescription">
<source>Specifies a testconfig.json file.</source>
<target state="new">Specifies a testconfig.json file.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineDiagnosticFileLoggerSynchronousWriteOptionDescription">
<source>Force the built-in file logger to write the log synchronously.
Useful for scenario where you don't want to lose any log (i.e. in case of crash).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
<target state="translated">La stessa istanza di 'CompositeExtensonFactory' è già registrata</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationFileNotFound">
<source>The configuration file '{0}' specified with '--config-file' could not be found.</source>
<target state="new">The configuration file '{0}' specified with '--config-file' could not be found.</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationManagerCannotFindDefaultJsonConfigurationErrorMessage">
<source>Could not find the default json configuration</source>
<target state="translated">Non è stato possibile trovare la configurazione JSON predefinita</target>
Expand Down Expand Up @@ -416,6 +421,11 @@
<target state="translated">Specifica la porta del client.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineConfigFileOptionDescription">
<source>Specifies a testconfig.json file.</source>
<target state="new">Specifies a testconfig.json file.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineDiagnosticFileLoggerSynchronousWriteOptionDescription">
<source>Force the built-in file logger to write the log synchronously.
Useful for scenario where you don't want to lose any log (i.e. in case of crash).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
<target state="translated">'CompositeExtensonFactory' の同じインスタンスが既に登録されています</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationFileNotFound">
<source>The configuration file '{0}' specified with '--config-file' could not be found.</source>
<target state="new">The configuration file '{0}' specified with '--config-file' could not be found.</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationManagerCannotFindDefaultJsonConfigurationErrorMessage">
<source>Could not find the default json configuration</source>
<target state="translated">既定の JSON 構成が見つかりませんでした</target>
Expand Down Expand Up @@ -416,6 +421,11 @@
<target state="translated">クライアントのポートを指定します。</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineConfigFileOptionDescription">
<source>Specifies a testconfig.json file.</source>
<target state="new">Specifies a testconfig.json file.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineDiagnosticFileLoggerSynchronousWriteOptionDescription">
<source>Force the built-in file logger to write the log synchronously.
Useful for scenario where you don't want to lose any log (i.e. in case of crash).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
<target state="translated">동일한 'CompositeExtensonFactory' 인스턴스가 이미 등록됨</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationFileNotFound">
<source>The configuration file '{0}' specified with '--config-file' could not be found.</source>
<target state="new">The configuration file '{0}' specified with '--config-file' could not be found.</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationManagerCannotFindDefaultJsonConfigurationErrorMessage">
<source>Could not find the default json configuration</source>
<target state="translated">기본 json 구성을 찾을 수 없습니다.</target>
Expand Down Expand Up @@ -416,6 +421,11 @@
<target state="translated">클라이언트의 포트를 지정합니다.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineConfigFileOptionDescription">
<source>Specifies a testconfig.json file.</source>
<target state="new">Specifies a testconfig.json file.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineDiagnosticFileLoggerSynchronousWriteOptionDescription">
<source>Force the built-in file logger to write the log synchronously.
Useful for scenario where you don't want to lose any log (i.e. in case of crash).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
<target state="translated">To samo wystąpienie elementu „CompositeExtensonFactory” jest już zarejestrowane</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationFileNotFound">
<source>The configuration file '{0}' specified with '--config-file' could not be found.</source>
<target state="new">The configuration file '{0}' specified with '--config-file' could not be found.</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationManagerCannotFindDefaultJsonConfigurationErrorMessage">
<source>Could not find the default json configuration</source>
<target state="translated">Nie można odnaleźć domyślnej konfiguracji JSON</target>
Expand Down Expand Up @@ -416,6 +421,11 @@
<target state="translated">Określ port klienta.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineConfigFileOptionDescription">
<source>Specifies a testconfig.json file.</source>
<target state="new">Specifies a testconfig.json file.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineDiagnosticFileLoggerSynchronousWriteOptionDescription">
<source>Force the built-in file logger to write the log synchronously.
Useful for scenario where you don't want to lose any log (i.e. in case of crash).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
<target state="translated">A mesma instância de “CompositeExtensonFactory” já está registrada</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationFileNotFound">
<source>The configuration file '{0}' specified with '--config-file' could not be found.</source>
<target state="new">The configuration file '{0}' specified with '--config-file' could not be found.</target>
<note />
</trans-unit>
<trans-unit id="ConfigurationManagerCannotFindDefaultJsonConfigurationErrorMessage">
<source>Could not find the default json configuration</source>
<target state="translated">Não foi possível localizar a configuração padrão do json</target>
Expand Down Expand Up @@ -416,6 +421,11 @@
<target state="translated">Especifique a porta do cliente.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineConfigFileOptionDescription">
<source>Specifies a testconfig.json file.</source>
<target state="new">Specifies a testconfig.json file.</target>
<note />
</trans-unit>
<trans-unit id="PlatformCommandLineDiagnosticFileLoggerSynchronousWriteOptionDescription">
<source>Force the built-in file logger to write the log synchronously.
Useful for scenario where you don't want to lose any log (i.e. in case of crash).
Expand Down
Loading

0 comments on commit e3900ac

Please sign in to comment.