From 7bafb00f05d579dd4c140f34742e11f8433db559 Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Tue, 20 Sep 2016 10:16:52 -0700 Subject: [PATCH] Improve error message when project file does not exist. Resolve #171 --- .../Program.cs | 5 +++++ .../Properties/Resources.Designer.cs | 16 ++++++++++++++++ .../Resources.resx | 3 +++ .../SecretManagerTests.cs | 12 ++++++++++++ 4 files changed, 36 insertions(+) diff --git a/src/Microsoft.Extensions.SecretManager.Tools/Program.cs b/src/Microsoft.Extensions.SecretManager.Tools/Program.cs index 0da29bd567..b8d3e9f0b9 100644 --- a/src/Microsoft.Extensions.SecretManager.Tools/Program.cs +++ b/src/Microsoft.Extensions.SecretManager.Tools/Program.cs @@ -151,6 +151,11 @@ namespace Microsoft.Extensions.SecretManager.Tools var fileInfo = new PhysicalFileInfo(new FileInfo(projectPath)); + if (!fileInfo.Exists) + { + throw new GracefulException(Resources.FormatError_ProjectPath_NotFound(projectPath)); + } + Logger.LogDebug(Resources.Message_Project_File_Path, fileInfo.PhysicalPath); return ReadUserSecretsId(fileInfo); } diff --git a/src/Microsoft.Extensions.SecretManager.Tools/Properties/Resources.Designer.cs b/src/Microsoft.Extensions.SecretManager.Tools/Properties/Resources.Designer.cs index c79e6e1fa2..d9ae9fe2c6 100644 --- a/src/Microsoft.Extensions.SecretManager.Tools/Properties/Resources.Designer.cs +++ b/src/Microsoft.Extensions.SecretManager.Tools/Properties/Resources.Designer.cs @@ -58,6 +58,22 @@ namespace Microsoft.Extensions.SecretManager.Tools return GetString("Error_No_Secrets_Found"); } + /// + /// The project file '{path}' does not exist. + /// + internal static string Error_ProjectPath_NotFound + { + get { return GetString("Error_ProjectPath_NotFound"); } + } + + /// + /// The project file '{path}' does not exist. + /// + internal static string FormatError_ProjectPath_NotFound(object path) + { + return string.Format(CultureInfo.CurrentCulture, GetString("Error_ProjectPath_NotFound", "path"), path); + } + /// /// Project file path {project}. /// diff --git a/src/Microsoft.Extensions.SecretManager.Tools/Resources.resx b/src/Microsoft.Extensions.SecretManager.Tools/Resources.resx index 13c953c727..76631dea0d 100644 --- a/src/Microsoft.Extensions.SecretManager.Tools/Resources.resx +++ b/src/Microsoft.Extensions.SecretManager.Tools/Resources.resx @@ -126,6 +126,9 @@ No secrets configured for this application. + + The project file '{path}' does not exist. + Project file path {project}. diff --git a/test/Microsoft.Extensions.SecretManager.Tools.Tests/SecretManagerTests.cs b/test/Microsoft.Extensions.SecretManager.Tools.Tests/SecretManagerTests.cs index 4618b5a92b..038bb0d464 100644 --- a/test/Microsoft.Extensions.SecretManager.Tools.Tests/SecretManagerTests.cs +++ b/test/Microsoft.Extensions.SecretManager.Tools.Tests/SecretManagerTests.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.Configuration.UserSecrets.Tests; using Microsoft.Extensions.Logging; using Xunit; using Xunit.Abstractions; +using Microsoft.Extensions.SecretManager.Tools.Internal; namespace Microsoft.Extensions.SecretManager.Tools.Tests { @@ -22,6 +23,17 @@ namespace Microsoft.Extensions.SecretManager.Tools.Tests _logger = new TestLogger(output); } + [Fact] + public void Error_Project_DoesNotExist() + { + var projectPath = Path.Combine(Path.GetTempPath(), "dne", Guid.NewGuid().ToString(), "project.json"); + var secretManager = new Program(Console.Out, Directory.GetCurrentDirectory()) { Logger = _logger }; + + var ex = Assert.Throws(() => secretManager.RunInternal("list", "--project", projectPath)); + + Assert.Equal(ex.Message, Resources.FormatError_ProjectPath_NotFound(projectPath)); + } + [Theory] [InlineData(true)] [InlineData(false)]