From 95722a9649c7ad49c7e82ad37524dab0af0b9b26 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Sun, 9 Mar 2025 12:36:43 -0700 Subject: [PATCH 1/6] Implicit restore dotnet list package --- src/Cli/dotnet/Properties/launchSettings.json | 5 ++- .../list/ListPackageReferencesCommand.cs | 38 ++++++++++++++++++- .../list/LocalizableStrings.resx | 7 ++++ .../list/PackageListCommandParser.cs | 7 ++++ .../list/xlf/LocalizableStrings.cs.xlf | 10 +++++ .../list/xlf/LocalizableStrings.de.xlf | 10 +++++ .../list/xlf/LocalizableStrings.es.xlf | 10 +++++ .../list/xlf/LocalizableStrings.fr.xlf | 10 +++++ .../list/xlf/LocalizableStrings.it.xlf | 10 +++++ .../list/xlf/LocalizableStrings.ja.xlf | 10 +++++ .../list/xlf/LocalizableStrings.ko.xlf | 10 +++++ .../list/xlf/LocalizableStrings.pl.xlf | 10 +++++ .../list/xlf/LocalizableStrings.pt-BR.xlf | 10 +++++ .../list/xlf/LocalizableStrings.ru.xlf | 10 +++++ .../list/xlf/LocalizableStrings.tr.xlf | 10 +++++ .../list/xlf/LocalizableStrings.zh-Hans.xlf | 10 +++++ .../list/xlf/LocalizableStrings.zh-Hant.xlf | 10 +++++ 17 files changed, 184 insertions(+), 3 deletions(-) diff --git a/src/Cli/dotnet/Properties/launchSettings.json b/src/Cli/dotnet/Properties/launchSettings.json index facffd64a35f..f5b5aa977cce 100644 --- a/src/Cli/dotnet/Properties/launchSettings.json +++ b/src/Cli/dotnet/Properties/launchSettings.json @@ -1,7 +1,8 @@ { "profiles": { "dotnet": { - "commandName": "Project" + "commandName": "Project", + "commandLineArgs": "package list --project N:/trash/p7/p7.sln --format json" } } -} +} \ No newline at end of file diff --git a/src/Cli/dotnet/commands/dotnet-package/list/ListPackageReferencesCommand.cs b/src/Cli/dotnet/commands/dotnet-package/list/ListPackageReferencesCommand.cs index 4b9552f431c5..439dd3877bba 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/ListPackageReferencesCommand.cs +++ b/src/Cli/dotnet/commands/dotnet-package/list/ListPackageReferencesCommand.cs @@ -2,9 +2,11 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.CommandLine; +using System.Globalization; using Microsoft.DotNet.Cli; using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Tools.Common; +using Microsoft.DotNet.Tools.MSBuild; using Microsoft.DotNet.Tools.NuGet; namespace Microsoft.DotNet.Tools.Package.List @@ -30,7 +32,41 @@ private static string GetAbsolutePath(string currentDirectory, string relativePa public override int Execute() { - return NuGetCommand.Run(TransformArgs()); + string projectFile = _parseResult.GetValue(PackageCommandParser.ProjectOption); + bool noRestore = _parseResult.HasOption(PackageListCommandParser.NoRestore); + int restoreExitCode = 0; + + if (!noRestore) + { + restoreExitCode = RunRestore(projectFile); + } + + return restoreExitCode == 0 + ? NuGetCommand.Run(TransformArgs()) + : restoreExitCode; + } + + private int RunRestore(string projectOrSolution) + { + MSBuildForwardingApp restoringCommand = new MSBuildForwardingApp(argsToForward: ["-target:restore", projectOrSolution, "-noConsoleLogger"]); + + int exitCode = 0; + + try + { + exitCode = restoringCommand.Execute(); + } + catch (Exception) + { + exitCode = 1; + } + + if (exitCode != 0) + { + Console.WriteLine(String.Format(CultureInfo.CurrentCulture, LocalizableStrings.Error_restore)); + } + + return exitCode; } internal static void EnforceOptionRules(ParseResult parseResult) diff --git a/src/Cli/dotnet/commands/dotnet-package/list/LocalizableStrings.resx b/src/Cli/dotnet/commands/dotnet-package/list/LocalizableStrings.resx index 5772b3e00c00..cdc316de3276 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/LocalizableStrings.resx +++ b/src/Cli/dotnet/commands/dotnet-package/list/LocalizableStrings.resx @@ -174,4 +174,11 @@ Specifies the version of machine-readable output. Requires the '--format json' option. + + Do not restore before running the command. + + + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + \ No newline at end of file diff --git a/src/Cli/dotnet/commands/dotnet-package/list/PackageListCommandParser.cs b/src/Cli/dotnet/commands/dotnet-package/list/PackageListCommandParser.cs index 453ddfe81eab..aeb8e6b0cfa8 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/PackageListCommandParser.cs +++ b/src/Cli/dotnet/commands/dotnet-package/list/PackageListCommandParser.cs @@ -78,6 +78,12 @@ internal static class PackageListCommandParser Arity = ArgumentArity.Zero }.ForwardAs("--interactive"); + public static readonly CliOption NoRestore = new CliOption("--no-restore") + { + Description = LocalizableStrings.CmdNoRestoreDescription, + Arity = ArgumentArity.Zero + }; + public static readonly CliOption VerbosityOption = new ForwardedOption("--verbosity", "-v") { Description = CommonLocalizableStrings.VerbosityOptionDescription, @@ -119,6 +125,7 @@ private static CliCommand ConstructCommand() command.Options.Add(InteractiveOption); command.Options.Add(FormatOption); command.Options.Add(OutputVersionOption); + command.Options.Add(NoRestore); command.Options.Add(PackageCommandParser.ProjectOption); command.SetAction((parseResult) => new ListPackageReferencesCommand(parseResult).Execute()); diff --git a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.cs.xlf b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.cs.xlf index 5d005c4a8ede..673f2462168b 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.cs.xlf +++ b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.cs.xlf @@ -27,6 +27,11 @@ Zvolí architekturu, pro kterou se mají zobrazit balíčky. Pokud chcete určit více architektur, zadejte tento přepínač vícekrát. + + Do not restore before running the command. + Do not restore before running the command. + + Lists packages that have newer versions. Cannot be combined with '--deprecated' or '--vulnerable' options. Vypíše balíčky, které mají novější verze. Nedá se kombinovat s možností --deprecated ani --vulnerable. @@ -47,6 +52,11 @@ Vypíše balíčky, které mají známá ohrožení zabezpečení. Nedá se kombinovat s možností --deprecated ani --outdated. + + Restore failed. Please try running `dotnet restore`. + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + A project or solution file could not be found in {0}. Specify a project or solution file to use. {0} neobsahuje žádný soubor projektu nebo řešení. Zadejte soubor projektu nebo řešení, který chcete použít. diff --git a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.de.xlf b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.de.xlf index f02036da25fe..7fe845fae961 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.de.xlf +++ b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.de.xlf @@ -27,6 +27,11 @@ Wählt ein Framework für die Anzeige der zugehörigen Pakete aus. Für mehrere Frameworks verwenden Sie die Option mehrmals. + + Do not restore before running the command. + Do not restore before running the command. + + Lists packages that have newer versions. Cannot be combined with '--deprecated' or '--vulnerable' options. Listet Pakete mit neueren Versionen auf. Kann nicht mit den Optionen "--deprecated" oder "--vulnerable" kombiniert werden. @@ -47,6 +52,11 @@ Listet Pakete auf, die bekannte Sicherheitsrisiken aufweisen. Kann nicht mit den Optionen "--deprecated" oder "--outdated" kombiniert werden. + + Restore failed. Please try running `dotnet restore`. + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + A project or solution file could not be found in {0}. Specify a project or solution file to use. In "{0}" wurde keine Projekt- oder Projektmappendatei gefunden. Geben Sie eine Projekt- oder Projektmappendatei an, die Sie verwenden möchten. diff --git a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.es.xlf b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.es.xlf index 67dc13fa8eb5..eb11773fbd1b 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.es.xlf +++ b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.es.xlf @@ -27,6 +27,11 @@ Elige un marco para mostrar sus paquetes. Utilice la opción varias veces para marcos múltiples. + + Do not restore before running the command. + Do not restore before running the command. + + Lists packages that have newer versions. Cannot be combined with '--deprecated' or '--vulnerable' options. Muestra los paquetes que tienen versiones más recientes. No se puede combinar con las opciones '--deprecated" o "--vulnerable". @@ -47,6 +52,11 @@ Muestra los paquetes que tienen vulnerabilidades conocidas. No se puede combinar con las opciones "--outdated" o "--deprecated". + + Restore failed. Please try running `dotnet restore`. + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + A project or solution file could not be found in {0}. Specify a project or solution file to use. No se encontró un archivo de proyecto o solución en {0}. Especifique un archivo de proyecto o solución para utilizar. diff --git a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.fr.xlf b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.fr.xlf index 3a35675340e4..73cce86ce468 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.fr.xlf +++ b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.fr.xlf @@ -27,6 +27,11 @@ Choisit un framework pour afficher ses packages. Utilisez l'option plusieurs fois selon le nombre de frameworks. + + Do not restore before running the command. + Do not restore before running the command. + + Lists packages that have newer versions. Cannot be combined with '--deprecated' or '--vulnerable' options. Liste les packages qui ont versions plus récentes. Impossible à combiner avec les options '--deprecated' ou '--vulnerable'. @@ -47,6 +52,11 @@ Liste les packages qui ont vulnérabilités. Impossible à combiner avec les options '--deprecated' ou '--outdated'. + + Restore failed. Please try running `dotnet restore`. + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + A project or solution file could not be found in {0}. Specify a project or solution file to use. Fichier projet ou solution introuvable dans {0}. Spécifiez un fichier projet ou solution à utiliser. diff --git a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.it.xlf b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.it.xlf index 8a07a0bc233c..5d2331c89eef 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.it.xlf +++ b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.it.xlf @@ -27,6 +27,11 @@ Consente di scegliere un framework per mostrarne i pacchetti. Usare l'opzione più volte in caso di framework multipli. + + Do not restore before running the command. + Do not restore before running the command. + + Lists packages that have newer versions. Cannot be combined with '--deprecated' or '--vulnerable' options. Elenca i pacchetti con versioni più recenti. Non può essere combinato con l'opzione '--deprecated' o '--vulnerable'. @@ -47,6 +52,11 @@ Elenca i pacchetti che presentano vulnerabilità note. Non può essere combinato con l'opzione '--deprecated' o '--outdated'. + + Restore failed. Please try running `dotnet restore`. + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + A project or solution file could not be found in {0}. Specify a project or solution file to use. Non è stato possibile trovare un file di progetto o di soluzione in {0}. Specificare un file di progetto o di soluzione da usare. diff --git a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.ja.xlf b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.ja.xlf index 5e3a3c15656b..24b805e8d24b 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.ja.xlf +++ b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.ja.xlf @@ -27,6 +27,11 @@ パッケージを表示するフレームワークを選択します。複数のフレームワークに対してはこのオプションを複数回使用します。 + + Do not restore before running the command. + Do not restore before running the command. + + Lists packages that have newer versions. Cannot be combined with '--deprecated' or '--vulnerable' options. 新しいバージョンのパッケージを一覧表示します。'--deprecated' または '--vulnerable' オプションと組み合わせることはできません。 @@ -47,6 +52,11 @@ 既知の脆弱性を持つパッケージを一覧表示します。'--deprecated' または '--outdated' オプションと組み合わせることはできません。 + + Restore failed. Please try running `dotnet restore`. + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + A project or solution file could not be found in {0}. Specify a project or solution file to use. {0} でプロジェクトまたはソリューション ファイルが見つかりませんでした。使用するプロジェクトまたはソリューション ファイルを指定します。 diff --git a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.ko.xlf b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.ko.xlf index 58435f6470c2..64cd476a0228 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.ko.xlf +++ b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.ko.xlf @@ -27,6 +27,11 @@ 프레임워크를 선택하여 패키지를 표시합니다. 여러 프레임워크의 경우 옵션을 여러 번 사용합니다. + + Do not restore before running the command. + Do not restore before running the command. + + Lists packages that have newer versions. Cannot be combined with '--deprecated' or '--vulnerable' options. 최신 버전이 포함된 패키지를 나열합니다. '--deprecated' 또는 '--vulnerable' 옵션과 함께 사용할 수 없습니다. @@ -47,6 +52,11 @@ 알려진 취약성이 있는 패키지를 나열합니다. '--deprecated' 또는 '--outdated' 옵션과 함께 사용할 수 없습니다. + + Restore failed. Please try running `dotnet restore`. + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + A project or solution file could not be found in {0}. Specify a project or solution file to use. {0}에서 프로젝트 또는 솔루션 파일을 찾을 수 없습니다. 사용할 프로젝트 또는 솔루션 파일을 지정하세요. diff --git a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.pl.xlf b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.pl.xlf index 731c526325f5..3349b3b04ebe 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.pl.xlf +++ b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.pl.xlf @@ -27,6 +27,11 @@ Wybiera strukturę, aby wyświetlić jej pakiety. W przypadku wielu struktur użyj tej opcji wielokrotnie. + + Do not restore before running the command. + Do not restore before running the command. + + Lists packages that have newer versions. Cannot be combined with '--deprecated' or '--vulnerable' options. Wyświetla pakiety, które mają nowszą wersję. Nie można łączyć z opcją „--deprecated” ani „--vulnerable”. @@ -47,6 +52,11 @@ Wyświetla pakiety ze znanymi lukami w zabezpieczeniach. Nie można łączyć z opcją „--deprecated” ani „--outdated”. + + Restore failed. Please try running `dotnet restore`. + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + A project or solution file could not be found in {0}. Specify a project or solution file to use. W lokalizacji {0} nie można odnaleźć pliku projektu lub rozwiązania. Podaj plik projektu lub rozwiązania do użycia. diff --git a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.pt-BR.xlf b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.pt-BR.xlf index c4f970b2eafa..a02f545be4fe 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.pt-BR.xlf +++ b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.pt-BR.xlf @@ -27,6 +27,11 @@ Escolhe uma estrutura para mostrar seus pacotes. Use a opção várias vezes para várias estruturas. + + Do not restore before running the command. + Do not restore before running the command. + + Lists packages that have newer versions. Cannot be combined with '--deprecated' or '--vulnerable' options. Lista os pacotes que têm versões mais recentes. Não pode ser combinado com a opção '--deprecated' nem com a opção '--vulnerable'. @@ -47,6 +52,11 @@ Lista os pacotes que têm vulnerabilidades conhecidas. Não pode ser combinado com a opção '--deprecated' nem com a opção '--outdated'. + + Restore failed. Please try running `dotnet restore`. + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + A project or solution file could not be found in {0}. Specify a project or solution file to use. Um arquivo de projeto ou de solução não pôde ser encontrado em {0}. Especifique um arquivo de projeto ou de solução a ser usado. diff --git a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.ru.xlf b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.ru.xlf index 5dc3d9be90dc..1370d4c5e1f3 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.ru.xlf +++ b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.ru.xlf @@ -27,6 +27,11 @@ Выбирает платформу, список пакетов для которой необходимо отобразить. Для нескольких платформ укажите этот параметр несколько раз. + + Do not restore before running the command. + Do not restore before running the command. + + Lists packages that have newer versions. Cannot be combined with '--deprecated' or '--vulnerable' options. Возвращает список пакетов, у которых есть более новые версии. Не может использоваться с параметрами "--deprecated" или "--vulnerable". @@ -47,6 +52,11 @@ Возвращает список пакетов, у которых есть известные уязвимости. Не может использоваться с параметрами "--deprecated" или "--outdated". + + Restore failed. Please try running `dotnet restore`. + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + A project or solution file could not be found in {0}. Specify a project or solution file to use. Не удалось найти файл проекта или решения в {0}. Укажите используемый файл проекта или решения. diff --git a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.tr.xlf b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.tr.xlf index 5e1f10a5f046..ccf102bb6137 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.tr.xlf +++ b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.tr.xlf @@ -27,6 +27,11 @@ Paketlerini görüntülemek için bir çerçeve seçin. Birden fazla çerçeve için seçeneği birden fazla kez kullanın. + + Do not restore before running the command. + Do not restore before running the command. + + Lists packages that have newer versions. Cannot be combined with '--deprecated' or '--vulnerable' options. Daha yeni sürümlere sahip paketleri listeler. '--deprecated' veya '--vulnerable' seçenekleriyle birleştirilemez. @@ -47,6 +52,11 @@ Bilinen güvenlik açıklarına sahip paketleri listeler. '--deprecated' veya '--outdated' seçenekleriyle birleştirilemez. + + Restore failed. Please try running `dotnet restore`. + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + A project or solution file could not be found in {0}. Specify a project or solution file to use. {0} içinde bir proje veya çözüm dosyası bulunamadı. Kullanmak için bir proje veya çözüm dosyası belirtin. diff --git a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.zh-Hans.xlf b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.zh-Hans.xlf index 646965b33d17..df6a1d662428 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.zh-Hans.xlf +++ b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.zh-Hans.xlf @@ -27,6 +27,11 @@ 选择一个框架来显示其包。对多个框架多次使用该选项。 + + Do not restore before running the command. + Do not restore before running the command. + + Lists packages that have newer versions. Cannot be combined with '--deprecated' or '--vulnerable' options. 列出具有较新版本的包。不能与 "--deprecated" 或 "--vulnerable" 选项结合使用。 @@ -47,6 +52,11 @@ 列出具有已知漏洞的包。不能与 "--deprecated" 或 "--outdated" 选项结合使用。 + + Restore failed. Please try running `dotnet restore`. + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + A project or solution file could not be found in {0}. Specify a project or solution file to use. 在 {0} 中找不到项目或解决方案文件。指定要使用的项目或解决方案文件。 diff --git a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.zh-Hant.xlf b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.zh-Hant.xlf index fa56180b4fce..3f44ea2bab01 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.zh-Hant.xlf +++ b/src/Cli/dotnet/commands/dotnet-package/list/xlf/LocalizableStrings.zh-Hant.xlf @@ -27,6 +27,11 @@ 選擇架構以顯示其套件。若有多個架構,則重複使用該選項。 + + Do not restore before running the command. + Do not restore before running the command. + + Lists packages that have newer versions. Cannot be combined with '--deprecated' or '--vulnerable' options. 列出有更新版本的套件。無法與 '--deprecated' 或 '--vulnerable' 選項一併使用。 @@ -47,6 +52,11 @@ 列出有已知弱點的套件。無法與 '--deprecated' 或 '--outdated' 選項一併使用。 + + Restore failed. Please try running `dotnet restore`. + Restore failed. Please try running `dotnet restore`. + do not translate `dotnet restore` + A project or solution file could not be found in {0}. Specify a project or solution file to use. 在 {0} 中找不到專案或解決方案檔案。請指定要使用的專案或解決方案檔案。 From 62c14c74096ea4f20d47f5347660910544b44cf1 Mon Sep 17 00:00:00 2001 From: Nigusu Solomon Yenework <59111203+Nigusu-Allehu@users.noreply.github.com> Date: Sun, 9 Mar 2025 12:44:47 -0700 Subject: [PATCH 2/6] undo --- src/Cli/dotnet/Properties/launchSettings.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Cli/dotnet/Properties/launchSettings.json b/src/Cli/dotnet/Properties/launchSettings.json index f5b5aa977cce..facffd64a35f 100644 --- a/src/Cli/dotnet/Properties/launchSettings.json +++ b/src/Cli/dotnet/Properties/launchSettings.json @@ -1,8 +1,7 @@ { "profiles": { "dotnet": { - "commandName": "Project", - "commandLineArgs": "package list --project N:/trash/p7/p7.sln --format json" + "commandName": "Project" } } -} \ No newline at end of file +} From bf0216a3164cbc52ab81e9a19b6ec343d1d74417 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Sun, 9 Mar 2025 14:48:01 -0700 Subject: [PATCH 3/6] test --- .../list/ListPackageReferencesCommand.cs | 12 +++++------ .../Commands/ListPackageCommand.cs | 3 ++- .../GivenDotnetListPackage.cs | 20 ++++++++++++------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Cli/dotnet/commands/dotnet-package/list/ListPackageReferencesCommand.cs b/src/Cli/dotnet/commands/dotnet-package/list/ListPackageReferencesCommand.cs index 439dd3877bba..3d54b74c89dd 100644 --- a/src/Cli/dotnet/commands/dotnet-package/list/ListPackageReferencesCommand.cs +++ b/src/Cli/dotnet/commands/dotnet-package/list/ListPackageReferencesCommand.cs @@ -22,7 +22,7 @@ public ListPackageReferencesCommand( _fileOrDirectory = GetAbsolutePath(Directory.GetCurrentDirectory(), parseResult.HasOption(PackageCommandParser.ProjectOption) ? parseResult.GetValue(PackageCommandParser.ProjectOption) : - parseResult.GetValue(ListCommandParser.SlnOrProjectArgument)); + parseResult.GetValue(ListCommandParser.SlnOrProjectArgument) ?? ""); } private static string GetAbsolutePath(string currentDirectory, string relativePath) @@ -32,17 +32,17 @@ private static string GetAbsolutePath(string currentDirectory, string relativePa public override int Execute() { - string projectFile = _parseResult.GetValue(PackageCommandParser.ProjectOption); + string projectFile = GetProjectOrSolution(); bool noRestore = _parseResult.HasOption(PackageListCommandParser.NoRestore); int restoreExitCode = 0; - if (!noRestore) + if (!noRestore ) { restoreExitCode = RunRestore(projectFile); } return restoreExitCode == 0 - ? NuGetCommand.Run(TransformArgs()) + ? NuGetCommand.Run(TransformArgs(projectFile)) : restoreExitCode; } @@ -81,7 +81,7 @@ internal static void EnforceOptionRules(ParseResult parseResult) } } - private string[] TransformArgs() + private string[] TransformArgs(string projectOrSolution) { var args = new List { @@ -89,7 +89,7 @@ private string[] TransformArgs() "list", }; - args.Add(GetProjectOrSolution()); + args.Add(projectOrSolution); args.AddRange(_parseResult.OptionValuesToBeForwarded(PackageListCommandParser.GetCommand())); diff --git a/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs b/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs index cf0a6717daf2..252d40c9a89b 100644 --- a/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs @@ -16,12 +16,13 @@ public ListPackageCommand(ITestOutputHelper log, params string[] args) : base(lo public override CommandResult Execute(IEnumerable args) { List newArgs = new(); + newArgs.Add("package"); newArgs.Add("list"); if (!string.IsNullOrEmpty(_projectName)) { + newArgs.Add("--project"); newArgs.Add(_projectName); } - newArgs.Add("package"); newArgs.AddRange(args); return base.Execute(newArgs); diff --git a/test/dotnet-list-package.Tests/GivenDotnetListPackage.cs b/test/dotnet-list-package.Tests/GivenDotnetListPackage.cs index b0324b724873..471fe0dc1391 100644 --- a/test/dotnet-list-package.Tests/GivenDotnetListPackage.cs +++ b/test/dotnet-list-package.Tests/GivenDotnetListPackage.cs @@ -31,7 +31,7 @@ public void ItShowsCoreOutputOnMinimalVerbosity() new ListPackageCommand(Log) .WithWorkingDirectory(projectDirectory) - .Execute("--verbosity", "quiet") + .Execute("--verbosity", "quiet", "--no-restore") .Should() .Pass() .And.NotHaveStdErr() @@ -63,7 +63,7 @@ public void RequestedAndResolvedVersionsMatch() new ListPackageCommand(Log) .WithWorkingDirectory(projectDirectory) - .Execute() + .Execute("--no-restore") .Should() .Pass() .And.NotHaveStdErr() @@ -88,7 +88,7 @@ public void ItListsAutoReferencedPackages() new ListPackageCommand(Log) .WithWorkingDirectory(projectDirectory) - .Execute() + .Execute("--no-restore") .Should() .Pass() .And.NotHaveStdErr() @@ -121,7 +121,7 @@ public void ItRunOnSolution() new ListPackageCommand(Log) .WithProject("App.sln") .WithWorkingDirectory(projectDirectory) - .Execute() + .Execute("--no-restore") .Should() .Pass() .And.NotHaveStdErr() @@ -139,7 +139,7 @@ public void AssetsPathExistsButNotRestored() new ListPackageCommand(Log) .WithWorkingDirectory(projectDirectory) - .Execute() + .Execute("--no-restore") .Should() .Fail() .And.HaveStdErr(); @@ -194,7 +194,7 @@ public static void Main(string[] args) new ListPackageCommand(Log) .WithWorkingDirectory(projectDirectory) - .Execute() + .Execute("--no-restore") .Should() .Pass() .And.NotHaveStdErr() @@ -202,7 +202,7 @@ public static void Main(string[] args) new ListPackageCommand(Log) .WithWorkingDirectory(projectDirectory) - .Execute(args: "--include-transitive") + .Execute(args: ["--include-transitive", "--no-restore"]) .Should() .Pass() .And.NotHaveStdErr() @@ -303,7 +303,13 @@ public void ItListsFSharpProject() } [Theory] + [InlineData(false, "--no-restore")] [InlineData(false, "--vulnerable")] + [InlineData(false, "--no-restore", "--include-transitive")] + [InlineData(false, "--no-restore", "--include-prerelease")] + [InlineData(false, "--no-restore", "--deprecated")] + [InlineData(false, "--no-restore", "--outdated")] + [InlineData(false, "--no-restore", "--vulnerable")] [InlineData(false, "--vulnerable", "--include-transitive")] [InlineData(false, "--vulnerable", "--include-prerelease")] [InlineData(false, "--deprecated", "--highest-minor")] From ddd951c2bdf07d96b03f3a77ec055c507f4d97a1 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Mon, 10 Mar 2025 08:41:41 -0700 Subject: [PATCH 4/6] completion tests --- .../bash/DotnetCliSnapshotTests.VerifyCompletions.verified.sh | 4 ++-- .../DotnetCliSnapshotTests.VerifyCompletions.verified.ps1 | 1 + .../zsh/DotnetCliSnapshotTests.VerifyCompletions.verified.zsh | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/test/dotnet-completions.Tests/snapshots/bash/DotnetCliSnapshotTests.VerifyCompletions.verified.sh b/test/dotnet-completions.Tests/snapshots/bash/DotnetCliSnapshotTests.VerifyCompletions.verified.sh index 4a6f19e8fb56..c7f1b9d59255 100644 --- a/test/dotnet-completions.Tests/snapshots/bash/DotnetCliSnapshotTests.VerifyCompletions.verified.sh +++ b/test/dotnet-completions.Tests/snapshots/bash/DotnetCliSnapshotTests.VerifyCompletions.verified.sh @@ -1080,7 +1080,7 @@ _testhost_package_list() { prev="${COMP_WORDS[COMP_CWORD-1]}" COMPREPLY=() - opts="--verbosity --outdated --deprecated --vulnerable --framework --include-transitive --include-prerelease --highest-patch --highest-minor --config --source --interactive --format --output-version --project --help" + opts="--verbosity --outdated --deprecated --vulnerable --framework --include-transitive --include-prerelease --highest-patch --highest-minor --config --source --interactive --format --output-version --no-restore --project --help" if [[ $COMP_CWORD == "$1" ]]; then COMPREPLY=( $(compgen -W "$opts" -- "$cur") ) @@ -2180,4 +2180,4 @@ _testhost_completions_script() { -complete -F _testhost testhost \ No newline at end of file +complete -F _testhost testhost diff --git a/test/dotnet-completions.Tests/snapshots/pwsh/DotnetCliSnapshotTests.VerifyCompletions.verified.ps1 b/test/dotnet-completions.Tests/snapshots/pwsh/DotnetCliSnapshotTests.VerifyCompletions.verified.ps1 index 0d99759ac018..866401a06486 100644 --- a/test/dotnet-completions.Tests/snapshots/pwsh/DotnetCliSnapshotTests.VerifyCompletions.verified.ps1 +++ b/test/dotnet-completions.Tests/snapshots/pwsh/DotnetCliSnapshotTests.VerifyCompletions.verified.ps1 @@ -635,6 +635,7 @@ Register-ArgumentCompleter -Native -CommandName 'testhost' -ScriptBlock { [CompletionResult]::new('--interactive', '--interactive', [CompletionResultType]::ParameterName, "Allows the command to stop and wait for user input or action (for example to complete authentication).") [CompletionResult]::new('--format', '--format', [CompletionResultType]::ParameterName, "Specifies the output format type for the list packages command.") [CompletionResult]::new('--output-version', '--output-version', [CompletionResultType]::ParameterName, "Specifies the version of machine-readable output. Requires the `'--format json`' option.") + [CompletionResult]::new('--no-restore', '--no-restore', [CompletionResultType]::ParameterName, "Do not restore before running the command.") [CompletionResult]::new('--project', '--project', [CompletionResultType]::ParameterName, "--project") [CompletionResult]::new('--help', '--help', [CompletionResultType]::ParameterName, "Show command line help.") [CompletionResult]::new('--help', '-h', [CompletionResultType]::ParameterName, "Show command line help.") diff --git a/test/dotnet-completions.Tests/snapshots/zsh/DotnetCliSnapshotTests.VerifyCompletions.verified.zsh b/test/dotnet-completions.Tests/snapshots/zsh/DotnetCliSnapshotTests.VerifyCompletions.verified.zsh index 28368bd81438..5aeab3d1563c 100644 --- a/test/dotnet-completions.Tests/snapshots/zsh/DotnetCliSnapshotTests.VerifyCompletions.verified.zsh +++ b/test/dotnet-completions.Tests/snapshots/zsh/DotnetCliSnapshotTests.VerifyCompletions.verified.zsh @@ -629,6 +629,7 @@ _testhost() { '--interactive[Allows the command to stop and wait for user input or action (for example to complete authentication).]' \ '--format=[Specifies the output format type for the list packages command.]: :((console\:"console" json\:"json" ))' \ '--output-version=[Specifies the version of machine-readable output. Requires the '\''--format json'\'' option.]: : ' \ + '--no-restore[Do not restore before running the command.]' \ '--project=[]: : ' \ '--help[Show command line help.]' \ '-h[Show command line help.]' \ From 7601bdf1e0c593472881fec3b0b5361e8f49334f Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Mon, 10 Mar 2025 13:04:00 -0700 Subject: [PATCH 5/6] seperate tests for dotnet package list --- .../Commands/ListPackageCommand.cs | 3 +- .../Commands/PackageListCommand.cs | 35 ++ .../GivenDotnetListPackage.cs | 20 +- .../GivenDotnetPackageList.cs | 441 ++++++++++++++++++ test/dotnet.Tests/dotnet.Tests.csproj | 1 + 5 files changed, 485 insertions(+), 15 deletions(-) create mode 100644 test/Microsoft.NET.TestFramework/Commands/PackageListCommand.cs create mode 100644 test/dotnet-package-list.Tests/GivenDotnetPackageList.cs diff --git a/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs b/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs index 252d40c9a89b..cf0a6717daf2 100644 --- a/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs +++ b/test/Microsoft.NET.TestFramework/Commands/ListPackageCommand.cs @@ -16,13 +16,12 @@ public ListPackageCommand(ITestOutputHelper log, params string[] args) : base(lo public override CommandResult Execute(IEnumerable args) { List newArgs = new(); - newArgs.Add("package"); newArgs.Add("list"); if (!string.IsNullOrEmpty(_projectName)) { - newArgs.Add("--project"); newArgs.Add(_projectName); } + newArgs.Add("package"); newArgs.AddRange(args); return base.Execute(newArgs); diff --git a/test/Microsoft.NET.TestFramework/Commands/PackageListCommand.cs b/test/Microsoft.NET.TestFramework/Commands/PackageListCommand.cs new file mode 100644 index 000000000000..b99407ed4072 --- /dev/null +++ b/test/Microsoft.NET.TestFramework/Commands/PackageListCommand.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Microsoft.DotNet.Cli.Utils; + +namespace Microsoft.NET.TestFramework.Commands +{ + public class PackageListCommand : DotnetCommand + { + private string? _projectName = null; + + public PackageListCommand(ITestOutputHelper log, params string[] args) : base(log, args) + { + } + + public override CommandResult Execute(IEnumerable args) + { + List newArgs = ["package", "list"]; + if (!string.IsNullOrEmpty(_projectName)) + { + newArgs.Add("--project"); + newArgs.Add(_projectName); + } + newArgs.AddRange(args); + + return base.Execute(newArgs); + } + + public PackageListCommand WithProject(string projectName) + { + _projectName = projectName; + return this; + } + } +} diff --git a/test/dotnet-list-package.Tests/GivenDotnetListPackage.cs b/test/dotnet-list-package.Tests/GivenDotnetListPackage.cs index 471fe0dc1391..b0324b724873 100644 --- a/test/dotnet-list-package.Tests/GivenDotnetListPackage.cs +++ b/test/dotnet-list-package.Tests/GivenDotnetListPackage.cs @@ -31,7 +31,7 @@ public void ItShowsCoreOutputOnMinimalVerbosity() new ListPackageCommand(Log) .WithWorkingDirectory(projectDirectory) - .Execute("--verbosity", "quiet", "--no-restore") + .Execute("--verbosity", "quiet") .Should() .Pass() .And.NotHaveStdErr() @@ -63,7 +63,7 @@ public void RequestedAndResolvedVersionsMatch() new ListPackageCommand(Log) .WithWorkingDirectory(projectDirectory) - .Execute("--no-restore") + .Execute() .Should() .Pass() .And.NotHaveStdErr() @@ -88,7 +88,7 @@ public void ItListsAutoReferencedPackages() new ListPackageCommand(Log) .WithWorkingDirectory(projectDirectory) - .Execute("--no-restore") + .Execute() .Should() .Pass() .And.NotHaveStdErr() @@ -121,7 +121,7 @@ public void ItRunOnSolution() new ListPackageCommand(Log) .WithProject("App.sln") .WithWorkingDirectory(projectDirectory) - .Execute("--no-restore") + .Execute() .Should() .Pass() .And.NotHaveStdErr() @@ -139,7 +139,7 @@ public void AssetsPathExistsButNotRestored() new ListPackageCommand(Log) .WithWorkingDirectory(projectDirectory) - .Execute("--no-restore") + .Execute() .Should() .Fail() .And.HaveStdErr(); @@ -194,7 +194,7 @@ public static void Main(string[] args) new ListPackageCommand(Log) .WithWorkingDirectory(projectDirectory) - .Execute("--no-restore") + .Execute() .Should() .Pass() .And.NotHaveStdErr() @@ -202,7 +202,7 @@ public static void Main(string[] args) new ListPackageCommand(Log) .WithWorkingDirectory(projectDirectory) - .Execute(args: ["--include-transitive", "--no-restore"]) + .Execute(args: "--include-transitive") .Should() .Pass() .And.NotHaveStdErr() @@ -303,13 +303,7 @@ public void ItListsFSharpProject() } [Theory] - [InlineData(false, "--no-restore")] [InlineData(false, "--vulnerable")] - [InlineData(false, "--no-restore", "--include-transitive")] - [InlineData(false, "--no-restore", "--include-prerelease")] - [InlineData(false, "--no-restore", "--deprecated")] - [InlineData(false, "--no-restore", "--outdated")] - [InlineData(false, "--no-restore", "--vulnerable")] [InlineData(false, "--vulnerable", "--include-transitive")] [InlineData(false, "--vulnerable", "--include-prerelease")] [InlineData(false, "--deprecated", "--highest-minor")] diff --git a/test/dotnet-package-list.Tests/GivenDotnetPackageList.cs b/test/dotnet-package-list.Tests/GivenDotnetPackageList.cs new file mode 100644 index 000000000000..ab9bec10d80e --- /dev/null +++ b/test/dotnet-package-list.Tests/GivenDotnetPackageList.cs @@ -0,0 +1,441 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#nullable disable + +using Microsoft.DotNet.Cli.Utils; +using Microsoft.DotNet.Tools.Package.List; + +namespace Microsoft.DotNet.Cli.Package.List.Tests +{ + public class GivenDotnetPackageList : SdkTest + { + public GivenDotnetPackageList(ITestOutputHelper output) : base(output) + { + } + + [Fact] + public void ItShowsCoreOutputOnMinimalVerbosity() + { + var testAssetName = "NewtonSoftDependentProject"; + var testAsset = _testAssetsManager + .CopyTestAsset(testAssetName) + .WithSource(); + var projectDirectory = testAsset.Path; + + new RestoreCommand(testAsset) + .Execute() + .Should() + .Pass() + .And.NotHaveStdErr(); + + new PackageListCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute("--verbosity", "quiet", "--no-restore") + .Should() + .Pass() + .And.NotHaveStdErr() + .And.HaveStdOutContaining("NewtonSoft.Json"); + } + + [Fact] + public void RequestedAndResolvedVersionsMatch() + { + var testAssetName = "TestAppSimple"; + var testAsset = _testAssetsManager + .CopyTestAsset(testAssetName) + .WithSource(); + + var projectDirectory = testAsset.Path; + + var packageName = "Newtonsoft.Json"; + var packageVersion = ToolsetInfo.GetNewtonsoftJsonPackageVersion(); + var cmd = new DotnetCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute("add", "package", packageName, "--version", packageVersion); + cmd.Should().Pass(); + + new RestoreCommand(testAsset) + .Execute() + .Should() + .Pass() + .And.NotHaveStdErr(); + + new PackageListCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute("--no-restore") + .Should() + .Pass() + .And.NotHaveStdErr() + .And.HaveStdOutContainingIgnoreSpaces(packageName + packageVersion + packageVersion); + } + + [Fact] + public void ItListsAutoReferencedPackages() + { + var testAssetName = "TestAppSimple"; + var testAsset = _testAssetsManager + .CopyTestAsset(testAssetName) + .WithSource() + .WithProjectChanges(ChangeTargetFrameworkTo2_1); + var projectDirectory = testAsset.Path; + + new RestoreCommand(testAsset) + .Execute() + .Should() + .Pass() + .And.NotHaveStdErr(); + + new PackageListCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute("--no-restore") + .Should() + .Pass() + .And.NotHaveStdErr() + .And.HaveStdOutContainingIgnoreSpaces("Microsoft.NETCore.App(A)") + .And.HaveStdOutContainingIgnoreSpaces("(A):Auto-referencedpackage"); + + static void ChangeTargetFrameworkTo2_1(XDocument project) + { + project.Descendants() + .Single(e => e.Name.LocalName == "TargetFramework") + .Value = "netcoreapp2.1"; + } + } + + [Fact] + public void ItRunOnSolution() + { + var sln = "TestAppWithSlnAndSolutionFolders"; + var testAsset = _testAssetsManager + .CopyTestAsset(sln) + .WithSource(); + var projectDirectory = testAsset.Path; + + new RestoreCommand(testAsset, "App.sln") + .Execute() + .Should() + .Pass() + .And.NotHaveStdErr(); + + new PackageListCommand(Log) + .WithProject("App.sln") + .WithWorkingDirectory(projectDirectory) + .Execute("--no-restore") + .Should() + .Pass() + .And.NotHaveStdErr() + .And.HaveStdOutContainingIgnoreSpaces("NewtonSoft.Json"); + } + + [Fact] + public void AssetsPathExistsButNotRestored() + { + var testAsset = "NewtonSoftDependentProject"; + var projectDirectory = _testAssetsManager + .CopyTestAsset(testAsset) + .WithSource() + .Path; + + new PackageListCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute("--no-restore") + .Should() + .Fail() + .And.HaveStdErr(); + } + + [Fact] + public void ItListsTransitivePackage() + { + var testProject = new TestProject + { + Name = "NewtonSoftDependentProject", + TargetFrameworks = ToolsetInfo.CurrentTargetFramework, + IsExe = true, + SourceFiles = + { +["Program.cs"] = @" +using System; +using System.Collections; +using Newtonsoft.Json.Linq; + +class Program +{ + public static void Main(string[] args) + { + ArrayList argList = new ArrayList(args); + JObject jObject = new JObject(); + + foreach (string arg in argList) + { + jObject[arg] = arg; + } + Console.WriteLine(jObject.ToString()); + } +} +", + } + }; + + testProject.PackageReferences.Add(new TestPackageReference("NewtonSoft.Json", "9.0.1")); + + // Disable package pruning so that there are still transitive dependencies to test the command + testProject.AdditionalProperties["RestoreEnablePackagePruning"] = "false"; + + var testAsset = _testAssetsManager.CreateTestProject(testProject); + var projectDirectory = Path.Combine(testAsset.Path, testProject.Name); + + new RestoreCommand(testAsset) + .Execute() + .Should() + .Pass() + .And.NotHaveStdErr(); + + new PackageListCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute("--no-restore") + .Should() + .Pass() + .And.NotHaveStdErr() + .And.NotHaveStdOutContaining("System.IO.FileSystem"); + + new PackageListCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute(args: ["--include-transitive", "--no-restore"]) + .Should() + .Pass() + .And.NotHaveStdErr() + .And.HaveStdOutContaining("System.IO.FileSystem"); + } + + [Theory] + [InlineData("", "[net451]", null)] + [InlineData("", $"[{ToolsetInfo.CurrentTargetFramework}]", null)] + [InlineData($"--framework {ToolsetInfo.CurrentTargetFramework} --framework net451", "[net451]", null)] + [InlineData($"--framework {ToolsetInfo.CurrentTargetFramework} --framework net451", $"[{ToolsetInfo.CurrentTargetFramework}]", null)] + [InlineData($"--framework {ToolsetInfo.CurrentTargetFramework}", $"[{ToolsetInfo.CurrentTargetFramework}]", "[net451]")] + [InlineData("--framework net451", "[net451]", "[netcoreapp3.0]")] + [InlineData($"-f {ToolsetInfo.CurrentTargetFramework} -f net451", "[net451]", null)] + [InlineData($"-f {ToolsetInfo.CurrentTargetFramework} -f net451", $"[{ToolsetInfo.CurrentTargetFramework}]", null)] + [InlineData($"-f {ToolsetInfo.CurrentTargetFramework}", $"[{ToolsetInfo.CurrentTargetFramework}]", "[net451]")] + [InlineData("-f net451", "[net451]", "[netcoreapp3.0]")] + public void ItListsValidFrameworks(string args, string shouldInclude, string shouldntInclude) + { + var testAssetName = "MSBuildAppWithMultipleFrameworks"; + var testAsset = _testAssetsManager + .CopyTestAsset(testAssetName, identifier: args.GetHashCode().ToString() + shouldInclude) + .WithSource(); + var projectDirectory = testAsset.Path; + + new RestoreCommand(testAsset) + .Execute() + .Should() + .Pass() + .And.NotHaveStdErr(); + + if (shouldntInclude == null) + { + new PackageListCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute(args.Split(' ', options: StringSplitOptions.RemoveEmptyEntries)) + .Should() + .Pass() + .And.NotHaveStdErr() + .And.HaveStdOutContainingIgnoreSpaces(shouldInclude.Replace(" ", "")); + } + else + { + new PackageListCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute(args.Split(' ', options: StringSplitOptions.RemoveEmptyEntries)) + .Should() + .Pass() + .And.NotHaveStdErr() + .And.HaveStdOutContainingIgnoreSpaces(shouldInclude.Replace(" ", "")) + .And.NotHaveStdOutContaining(shouldntInclude.Replace(" ", "")); + } + + } + + [Fact] + public void ItDoesNotAcceptInvalidFramework() + { + var testAssetName = "MSBuildAppWithMultipleFrameworks"; + var testAsset = _testAssetsManager + .CopyTestAsset(testAssetName) + .WithSource(); + var projectDirectory = testAsset.Path; + + new RestoreCommand(testAsset) + .Execute() + .Should() + .Pass(); + + new PackageListCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute("--framework", "invalid") + .Should() + .Fail(); + } + + [FullMSBuildOnlyFact] + public void ItListsFSharpProject() + { + var testAssetName = "FSharpTestAppSimple"; + var testAsset = _testAssetsManager + .CopyTestAsset(testAssetName) + .WithSource(); + var projectDirectory = testAsset.Path; + + new RestoreCommand(testAsset) + .Execute() + .Should() + .Pass() + .And.NotHaveStdErr(); + + new PackageListCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute() + .Should() + .Pass() + .And.NotHaveStdErr(); + } + + [Theory] + [InlineData(false, "--no-restore")] + [InlineData(false, "--vulnerable")] + [InlineData(false, "--no-restore", "--include-transitive")] + [InlineData(false, "--no-restore", "--include-prerelease")] + [InlineData(false, "--no-restore", "--deprecated")] + [InlineData(false, "--no-restore", "--outdated")] + [InlineData(false, "--no-restore", "--vulnerable")] + [InlineData(false, "--vulnerable", "--include-transitive")] + [InlineData(false, "--vulnerable", "--include-prerelease")] + [InlineData(false, "--deprecated", "--highest-minor")] + [InlineData(false, "--deprecated", "--highest-patch")] + [InlineData(false, "--outdated", "--include-prerelease")] + [InlineData(false, "--outdated", "--highest-minor")] + [InlineData(false, "--outdated", "--highest-patch")] + [InlineData(false, "--config")] + [InlineData(false, "--configfile")] + [InlineData(false, "--source")] + [InlineData(false, "-s")] + [InlineData(false, "--config", "--deprecated")] + [InlineData(false, "--configfile", "--deprecated")] + [InlineData(false, "--source", "--vulnerable")] + [InlineData(false, "-s", "--vulnerable")] + [InlineData(true, "--vulnerable", "--deprecated")] + [InlineData(true, "--vulnerable", "--outdated")] + [InlineData(true, "--deprecated", "--outdated")] + public void ItEnforcesOptionRules(bool throws, params string[] options) + { + var parseResult = Parser.Instance.Parse($"dotnet package list {string.Join(' ', options)}"); + Action checkRules = () => ListPackageReferencesCommand.EnforceOptionRules(parseResult); + + if (throws) + { + Assert.Throws(checkRules); + } + else + { + checkRules(); // Test for no throw + } + } + + [UnixOnlyFact] + public void ItRunsInCurrentDirectoryWithPoundInPath() + { + // Regression test for https://github.com/dotnet/sdk/issues/19654 + var testAssetName = "TestAppSimple"; + var testAsset = _testAssetsManager + .CopyTestAsset(testAssetName, "C#") + .WithSource(); + var projectDirectory = testAsset.Path; + + new RestoreCommand(testAsset) + .Execute() + .Should() + .Pass(); + + new PackageListCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute() + .Should() + .Pass(); + } + + [Fact] + public void ItRecognizesRelativePathsForAProject() + { + var testAssetName = "TestAppSimple"; + var testAsset = _testAssetsManager + .CopyTestAsset(testAssetName) + .WithSource(); + + var projectDirectory = testAsset.Path; + + new RestoreCommand(testAsset) + .Execute() + .Should() + .Pass(); + + new PackageListCommand(Log) + .WithProject("TestAppSimple.csproj") + .WithWorkingDirectory(projectDirectory) + .Execute() + .Should() + .Pass(); + } + + [Fact] + public void ItRecognizesRelativePathsForASolution() + { + var sln = "TestAppWithSlnAndSolutionFolders"; + var testAsset = _testAssetsManager + .CopyTestAsset(sln) + .WithSource(); + + var projectDirectory = testAsset.Path; + + new RestoreCommand(testAsset, "App.sln") + .Execute() + .Should() + .Pass(); + + new PackageListCommand(Log) + .WithProject("App.sln") + .WithWorkingDirectory(projectDirectory) + .Execute() + .Should() + .Pass(); + } + + [Fact] + public void ItRecognizesRelativePathsForASolutionFromSubFolder() + { + var sln = "TestAppWithSlnAndSolutionFolders"; + var testAsset = _testAssetsManager + .CopyTestAsset(sln) + .WithSource(); + + var projectDirectory = testAsset.Path; + + string subFolderName = "subFolder"; + var subFolderPath = Path.Combine(projectDirectory, subFolderName); + Directory.CreateDirectory(subFolderPath); + + new RestoreCommand(testAsset, "App.sln") + .Execute() + .Should() + .Pass(); + + new PackageListCommand(Log) + .WithProject("../App.sln") + .WithWorkingDirectory(subFolderPath) + .Execute() + .Should() + .Pass(); + } + } +} diff --git a/test/dotnet.Tests/dotnet.Tests.csproj b/test/dotnet.Tests/dotnet.Tests.csproj index e22cdf7dfa23..40fa3b9bdcfa 100644 --- a/test/dotnet.Tests/dotnet.Tests.csproj +++ b/test/dotnet.Tests/dotnet.Tests.csproj @@ -38,6 +38,7 @@ + From bb5c8011dfda365f6ef4a64322c6cf237952d36e Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Mon, 10 Mar 2025 13:38:01 -0700 Subject: [PATCH 6/6] test restore --- .../ListPackageReferencesCommandParser.cs | 1 + .../GivenDotnetListPackage.cs | 20 ++++++++++++++++++- .../GivenDotnetPackageList.cs | 19 ++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/Cli/dotnet/commands/dotnet-list/dotnet-list-package/ListPackageReferencesCommandParser.cs b/src/Cli/dotnet/commands/dotnet-list/dotnet-list-package/ListPackageReferencesCommandParser.cs index c98364cb9644..fed66c1a48ba 100644 --- a/src/Cli/dotnet/commands/dotnet-list/dotnet-list-package/ListPackageReferencesCommandParser.cs +++ b/src/Cli/dotnet/commands/dotnet-list/dotnet-list-package/ListPackageReferencesCommandParser.cs @@ -34,6 +34,7 @@ private static CliCommand ConstructCommand() command.Options.Add(PackageListCommandParser.InteractiveOption); command.Options.Add(PackageListCommandParser.FormatOption); command.Options.Add(PackageListCommandParser.OutputVersionOption); + command.Options.Add(PackageListCommandParser.NoRestore); command.SetAction((parseResult) => new ListPackageReferencesCommand(parseResult).Execute()); diff --git a/test/dotnet-list-package.Tests/GivenDotnetListPackage.cs b/test/dotnet-list-package.Tests/GivenDotnetListPackage.cs index b0324b724873..d026e14c4100 100644 --- a/test/dotnet-list-package.Tests/GivenDotnetListPackage.cs +++ b/test/dotnet-list-package.Tests/GivenDotnetListPackage.cs @@ -139,12 +139,30 @@ public void AssetsPathExistsButNotRestored() new ListPackageCommand(Log) .WithWorkingDirectory(projectDirectory) - .Execute() + .Execute("--no-restore") .Should() .Fail() .And.HaveStdErr(); } + [Fact] + public void RestoresAndLists() + { + var testAsset = "NewtonSoftDependentProject"; + var projectDirectory = _testAssetsManager + .CopyTestAsset(testAsset) + .WithSource() + .Path; + + new PackageListCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute() + .Should() + .Pass() + .And.HaveStdOut() + .And.HaveStdOutContaining("NewtonSoft.Json"); + } + [Fact] public void ItListsTransitivePackage() { diff --git a/test/dotnet-package-list.Tests/GivenDotnetPackageList.cs b/test/dotnet-package-list.Tests/GivenDotnetPackageList.cs index ab9bec10d80e..5b4960896f43 100644 --- a/test/dotnet-package-list.Tests/GivenDotnetPackageList.cs +++ b/test/dotnet-package-list.Tests/GivenDotnetPackageList.cs @@ -145,6 +145,25 @@ public void AssetsPathExistsButNotRestored() .And.HaveStdErr(); } + + [Fact] + public void RestoresAndLists() + { + var testAsset = "NewtonSoftDependentProject"; + var projectDirectory = _testAssetsManager + .CopyTestAsset(testAsset) + .WithSource() + .Path; + + new PackageListCommand(Log) + .WithWorkingDirectory(projectDirectory) + .Execute() + .Should() + .Pass() + .And.HaveStdOut() + .And.HaveStdOutContaining("NewtonSoft.Json"); + } + [Fact] public void ItListsTransitivePackage() {