Skip to content

Commit

Permalink
refactor: Add formatting to an error message from EnvFileNames class (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDave1999 authored Jun 14, 2024
1 parent aa7d982 commit 77077df
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
15 changes: 0 additions & 15 deletions src/Common/FormattingMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Text;
using static DotEnv.Core.DotEnvHelper;
using static DotEnv.Core.EnvFileNames;

namespace DotEnv.Core;

Expand All @@ -11,20 +10,6 @@ namespace DotEnv.Core;
/// </summary>
internal class FormattingMessage
{
/// <summary>
/// Formats an error message in case the local file is not present in any directory.
/// </summary>
/// <param name="message">The message that describes the error.</param>
/// <param name="environmentName">The name of the current environment.</param>
/// <returns>A formatted error message.</returns>
public static string FormatLocalFileNotPresentMessage(string message = null, string environmentName = null)
{
message ??= "error: Any of these .env files must be present in the root directory of your project:";
return environmentName is not null ?
$"{message} .env.{environmentName}.local or {EnvLocalName}" :
$"{message} {EnvDevelopmentLocalName} or {EnvDevLocalName} or {EnvLocalName}";
}

/// <summary>
/// Formats an error message in case the parser encounters errors.
/// </summary>
Expand Down
24 changes: 19 additions & 5 deletions src/Loader/EnvFileNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,23 @@ namespace DotEnv.Core;
internal class EnvFileNames
{
public const string EnvDevelopmentLocalName = ".env.development.local";
public const string EnvDevLocalName = ".env.dev.local";
public const string EnvLocalName = ".env.local";
public const string EnvDevelopmentName = ".env.development";
public const string EnvDevName = ".env.dev";
public const string EnvName = ".env";
public const string EnvDevLocalName = ".env.dev.local";
public const string EnvLocalName = ".env.local";
public const string EnvDevelopmentName = ".env.development";
public const string EnvDevName = ".env.dev";
public const string EnvName = ".env";

/// <summary>
/// Formats an error message in case the local file is not present in any directory.
/// </summary>
/// <param name="message">The message that describes the error.</param>
/// <param name="environmentName">The name of the current environment.</param>
/// <returns>A formatted error message.</returns>
public static string FormatLocalFileNotPresentMessage(string message = null, string environmentName = null)
{
message ??= "error: Any of these .env files must be present in the root directory of your project:";
return environmentName is not null ?
$"{message} .env.{environmentName}.local or {EnvLocalName}" :
$"{message} {EnvDevelopmentLocalName} or {EnvDevLocalName} or {EnvLocalName}";
}
}
6 changes: 4 additions & 2 deletions tests/Loader/EnvLoaderTests.LoadEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ public void LoadEnv_WhenLocalEnvFilesNotExistAndEnvironmentIsNotDefined_ShouldGe
// Arrange
Env.CurrentEnvironment = null;
var loader = new EnvLoader();
var expectedErrorMessages = EnvFileNames.FormatLocalFileNotPresentMessage();

// Act
loader
Expand All @@ -240,7 +241,7 @@ public void LoadEnv_WhenLocalEnvFilesNotExistAndEnvironmentIsNotDefined_ShouldGe
result.HasError().Should().BeTrue();
result.Should().HaveCount(1);
Env.CurrentEnvironment.Should().Be("development");
result.ErrorMessages.Should().Contain(FormatLocalFileNotPresentMessage());
result.ErrorMessages.Should().Contain(expectedErrorMessages);
}

[TestMethod]
Expand All @@ -249,6 +250,7 @@ public void LoadEnv_WhenLocalEnvFilesNotExistAndEnvironmentIsDefined_ShouldGener
// Arrange
var loader = new EnvLoader();
Env.CurrentEnvironment = "test";
var expectedErrorMessages = EnvFileNames.FormatLocalFileNotPresentMessage(environmentName: Env.CurrentEnvironment);

// Act
loader
Expand All @@ -260,6 +262,6 @@ public void LoadEnv_WhenLocalEnvFilesNotExistAndEnvironmentIsDefined_ShouldGener
result.Should().HaveCount(1);
result.ErrorMessages
.Should()
.Contain(FormatLocalFileNotPresentMessage(environmentName: Env.CurrentEnvironment));
.Contain(expectedErrorMessages);
}
}

0 comments on commit 77077df

Please sign in to comment.