Skip to content

Commit 6aad7db

Browse files
fix init command with environment variable (#1541)
## Why make this change? - Closes #1527 - dab init doesn't create file with the environment Value, and always create default config if not specified explicitly. - For ex: if DAB_ENVIRONMENT is set to Development, it should create dab-config.Development.json, which is not the case. ## What is this change? - Updating the logic to create config file giving precedence to environment value, also making sure error is thrown if the file is not present already. ## How was this tested? - [X] Unit Tests ## Sample Request(s) `dab init --database-type mssql` above command will create: 1. DAB_ENVIRONMENT = null dab-config.json 2. DAB_ENVIRONMENT = Development dab-config.Development.json
1 parent d840baf commit 6aad7db

13 files changed

+191
-24
lines changed

src/Cli.Tests/AddEntityTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using Azure.DataApiBuilder.Config.ObjectModel;
5-
using Cli.Commands;
6-
74
namespace Cli.Tests
85
{
96
/// <summary>
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace Cli.Tests;
5+
6+
/// <summary>
7+
/// Config Generation Tests for CLI.
8+
/// </summary>
9+
[TestClass]
10+
public class ConfigGeneratorTests
11+
{
12+
private IFileSystem? _fileSystem;
13+
private RuntimeConfigLoader? _runtimeConfigLoader;
14+
15+
[TestInitialize]
16+
public void TestInitialize()
17+
{
18+
_fileSystem = FileSystemUtils.ProvisionMockFileSystem();
19+
20+
_runtimeConfigLoader = new RuntimeConfigLoader(_fileSystem);
21+
22+
ILoggerFactory loggerFactory = TestLoggerSupport.ProvisionLoggerFactory();
23+
24+
SetLoggerForCliConfigGenerator(loggerFactory.CreateLogger<ConfigGenerator>());
25+
SetCliUtilsLogger(loggerFactory.CreateLogger<Utils>());
26+
}
27+
28+
[TestCleanup]
29+
public void TestCleanup()
30+
{
31+
_fileSystem = null;
32+
_runtimeConfigLoader = null;
33+
}
34+
35+
/// <summary>
36+
/// Tests that user provided config file is successfully generated when that file is not already present.
37+
/// </summary>
38+
[DataTestMethod]
39+
[DataRow(true, false, DisplayName = "Failed to generate config file when user provided config file is present.")]
40+
[DataRow(false, true, DisplayName = "Successfully generated config file when user provided config file is not present.")]
41+
public void TryGenerateConfig_WithUserProvidedConfig(
42+
bool isConfigFilePresent,
43+
bool isConfigGenerationSuccessful)
44+
{
45+
HandleConfigFileCreationAndDeletion(TEST_RUNTIME_CONFIG_FILE, isConfigFilePresent);
46+
Assert.AreEqual(isConfigFilePresent, _fileSystem!.File.Exists(TEST_RUNTIME_CONFIG_FILE));
47+
48+
InitOptions options = CreateBasicInitOptionsForMsSqlWithConfig(config: TEST_RUNTIME_CONFIG_FILE);
49+
50+
// Mocking logger to assert on logs
51+
Mock<ILogger<ConfigGenerator>> loggerMock = new();
52+
ConfigGenerator.SetLoggerForCliConfigGenerator(loggerMock.Object);
53+
54+
Assert.AreEqual(isConfigGenerationSuccessful, ConfigGenerator.TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!));
55+
56+
if (!isConfigFilePresent)
57+
{
58+
Assert.AreEqual(isConfigGenerationSuccessful, _fileSystem!.File.Exists(TEST_RUNTIME_CONFIG_FILE));
59+
}
60+
else
61+
{
62+
// Assert on the log message to verify the failure
63+
loggerMock.Verify(
64+
x => x.Log(
65+
LogLevel.Error,
66+
It.IsAny<EventId>(),
67+
It.Is<It.IsAnyType>((o, t) => o.ToString()!.Contains($"{TEST_RUNTIME_CONFIG_FILE} already exists.")),
68+
It.IsAny<Exception?>(),
69+
(Func<It.IsAnyType, Exception?, string>)It.IsAny<object>()
70+
),
71+
Times.Once
72+
);
73+
}
74+
}
75+
76+
/// <summary>
77+
/// Tests that environment config file is successfully generated when that file is not already present.
78+
/// When environment variable is not set, it should generate the default config file.
79+
/// </summary>
80+
[DataTestMethod]
81+
[DataRow(true, false, "Test", "dab-config.Test.json", DisplayName = "Failed to generate the config file when environment config file is present.")]
82+
[DataRow(false, true, "Test", "dab-config.Test.json", DisplayName = "Successfully generated the config file when environment config file is not present.")]
83+
[DataRow(false, true, "", "dab-config.json", DisplayName = "Successfully generated the config file when environment config file is not present and environment variable is set as empty.")]
84+
[DataRow(false, true, null, "dab-config.json", DisplayName = "Successfully generated the config file when environment config file is not present and environment variable is not set.")]
85+
public void TryGenerateConfig_UsingEnvironmentVariable(
86+
bool isConfigFilePresent,
87+
bool isConfigGenerationSuccessful,
88+
string? environmentValue,
89+
string configFileName)
90+
{
91+
Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
92+
HandleConfigFileCreationAndDeletion(configFileName, isConfigFilePresent);
93+
Assert.AreEqual(isConfigFilePresent, _fileSystem!.File.Exists(configFileName));
94+
95+
InitOptions options = CreateBasicInitOptionsForMsSqlWithConfig();
96+
97+
// Mocking logger to assert on logs
98+
Mock<ILogger<ConfigGenerator>> loggerMock = new();
99+
ConfigGenerator.SetLoggerForCliConfigGenerator(loggerMock.Object);
100+
101+
Assert.AreEqual(isConfigGenerationSuccessful, ConfigGenerator.TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!));
102+
if (!isConfigFilePresent)
103+
{
104+
Assert.AreEqual(isConfigGenerationSuccessful, _fileSystem!.File.Exists(configFileName));
105+
}
106+
else
107+
{
108+
// Assert on the log message to verify the failure
109+
loggerMock.Verify(
110+
x => x.Log(
111+
LogLevel.Error,
112+
It.IsAny<EventId>(),
113+
It.Is<It.IsAnyType>((o, t) => o.ToString()!.Contains($"{configFileName} already exists.")),
114+
It.IsAny<Exception?>(),
115+
(Func<It.IsAnyType, Exception?, string>)It.IsAny<object>()
116+
),
117+
Times.Once
118+
);
119+
}
120+
}
121+
122+
/// <summary>
123+
/// This method handles the creation and deletion of a configuration file.
124+
/// </summary>
125+
private void HandleConfigFileCreationAndDeletion(string configFilePath, bool configFilePresent)
126+
{
127+
if (!configFilePresent)
128+
{
129+
_fileSystem!.File.Delete(configFilePath);
130+
}
131+
else if (configFilePresent)
132+
{
133+
_fileSystem!.File.Create(configFilePath).Dispose();
134+
}
135+
}
136+
}

src/Cli.Tests/EndToEndTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System.IO.Abstractions;
5-
using System.IO.Abstractions.TestingHelpers;
6-
using Azure.DataApiBuilder.Config.ObjectModel;
74
using Azure.DataApiBuilder.Service;
85

96
namespace Cli.Tests;

src/Cli.Tests/EnvironmentTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System.Text.Json;
54
using Azure.DataApiBuilder.Config.Converters;
65

76
namespace Cli.Tests;

src/Cli.Tests/FileSystemUtils.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System.IO.Abstractions.TestingHelpers;
54
using System.Reflection;
65

76
namespace Cli.Tests;

src/Cli.Tests/InitTests.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System.IO.Abstractions;
5-
using System.IO.Abstractions.TestingHelpers;
6-
using Azure.DataApiBuilder.Config.ObjectModel;
7-
using Cli.Commands;
8-
94
namespace Cli.Tests
105
{
116
/// <summary>

src/Cli.Tests/ModuleInitializer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using System.Runtime.CompilerServices;
5-
using Azure.DataApiBuilder.Config.ObjectModel;
65

76
namespace Cli.Tests;
87

src/Cli.Tests/TestHelper.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,5 +1095,25 @@ public static Process ExecuteDabCommand(string command, string flags)
10951095
}
10961096
}
10971097
}";
1098+
1099+
/// <summary>
1100+
/// Creates basic initialization options for MS SQL config.
1101+
/// </summary>
1102+
/// <param name="config">Optional config file name.</param>
1103+
/// <returns>InitOptions</returns>
1104+
public static InitOptions CreateBasicInitOptionsForMsSqlWithConfig(string? config = null)
1105+
{
1106+
return new(
1107+
databaseType: DatabaseType.MSSQL,
1108+
connectionString: "testconnectionstring",
1109+
cosmosNoSqlDatabase: null,
1110+
cosmosNoSqlContainer: null,
1111+
graphQLSchemaPath: null,
1112+
setSessionContext: true,
1113+
hostMode: HostMode.Development,
1114+
corsOrigin: new List<string>(),
1115+
authenticationProvider: EasyAuthType.StaticWebApps.ToString(),
1116+
config: config);
1117+
}
10981118
}
10991119
}

src/Cli.Tests/UpdateEntityTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Licensed under the MIT License.
33

44
using Azure.DataApiBuilder.Config.Converters;
5-
using Azure.DataApiBuilder.Config.ObjectModel;
6-
using Cli.Commands;
75

86
namespace Cli.Tests
97
{

src/Cli.Tests/Usings.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
// Licensed under the MIT License.
33

44
global using System.Diagnostics;
5+
global using System.IO.Abstractions;
6+
global using System.IO.Abstractions.TestingHelpers;
7+
global using System.Text.Json;
58
global using Azure.DataApiBuilder.Config;
9+
global using Azure.DataApiBuilder.Config.ObjectModel;
610
global using Azure.DataApiBuilder.Service.Exceptions;
11+
global using Cli.Commands;
712
global using Microsoft.Extensions.Logging;
813
global using Microsoft.VisualStudio.TestTools.UnitTesting;
914
global using Moq;
1015
global using Newtonsoft.Json.Linq;
16+
global using static Azure.DataApiBuilder.Config.RuntimeConfigLoader;
1117
global using static Cli.ConfigGenerator;
1218
global using static Cli.Tests.TestHelper;
1319
global using static Cli.Utils;

0 commit comments

Comments
 (0)