-
Notifications
You must be signed in to change notification settings - Fork 288
fix init command with environment variable #1541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ef9b74b
fix init command with environment variable
abhishekkumams 3751da5
fixing tests
abhishekkumams 79d4dc1
fix formatting
abhishekkumams 39d9e43
Merge branch 'main' into dev/abhishekkuma/fix_dab_init
abhishekkumams baa40f7
resolving merge conflicts
abhishekkumams 7879e18
removing unnecessary comments
abhishekkumams d9956f2
fix formatting
abhishekkumams 3125e3a
improving tests
abhishekkumams d2341ea
fix formatting
abhishekkumams e5a3260
fix formatting
abhishekkumams 6012053
Merge branch 'main' into dev/abhishekkuma/fix_dab_init
abhishekkumams 1747af3
using right file system
abhishekkumams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Cli.Tests; | ||
|
|
||
| /// <summary> | ||
| /// Config Generation Tests for CLI. | ||
| /// </summary> | ||
| [TestClass] | ||
| public class ConfigGeneratorTests | ||
| { | ||
| private IFileSystem? _fileSystem; | ||
| private RuntimeConfigLoader? _runtimeConfigLoader; | ||
|
|
||
| [TestInitialize] | ||
| public void TestInitialize() | ||
| { | ||
| _fileSystem = FileSystemUtils.ProvisionMockFileSystem(); | ||
|
|
||
| _runtimeConfigLoader = new RuntimeConfigLoader(_fileSystem); | ||
|
|
||
| ILoggerFactory loggerFactory = TestLoggerSupport.ProvisionLoggerFactory(); | ||
|
|
||
| SetLoggerForCliConfigGenerator(loggerFactory.CreateLogger<ConfigGenerator>()); | ||
| SetCliUtilsLogger(loggerFactory.CreateLogger<Utils>()); | ||
| } | ||
|
|
||
| [TestCleanup] | ||
| public void TestCleanup() | ||
| { | ||
| _fileSystem = null; | ||
| _runtimeConfigLoader = null; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that user provided config file is successfully generated when that file is not already present. | ||
| /// </summary> | ||
| [DataTestMethod] | ||
| [DataRow(true, false, DisplayName = "Failed to generate config file when user provided config file is present.")] | ||
| [DataRow(false, true, DisplayName = "Successfully generated config file when user provided config file is not present.")] | ||
| public void TryGenerateConfig_WithUserProvidedConfig( | ||
| bool isConfigFilePresent, | ||
| bool isConfigGenerationSuccessful) | ||
| { | ||
| HandleConfigFileCreationAndDeletion(TEST_RUNTIME_CONFIG_FILE, isConfigFilePresent); | ||
| Assert.AreEqual(isConfigFilePresent, _fileSystem!.File.Exists(TEST_RUNTIME_CONFIG_FILE)); | ||
|
|
||
| InitOptions options = CreateBasicInitOptionsForMsSqlWithConfig(config: TEST_RUNTIME_CONFIG_FILE); | ||
|
|
||
| // Mocking logger to assert on logs | ||
| Mock<ILogger<ConfigGenerator>> loggerMock = new(); | ||
| ConfigGenerator.SetLoggerForCliConfigGenerator(loggerMock.Object); | ||
|
|
||
| Assert.AreEqual(isConfigGenerationSuccessful, ConfigGenerator.TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!)); | ||
|
|
||
| if (!isConfigFilePresent) | ||
| { | ||
| Assert.AreEqual(isConfigGenerationSuccessful, _fileSystem!.File.Exists(TEST_RUNTIME_CONFIG_FILE)); | ||
| } | ||
| else | ||
| { | ||
| // Assert on the log message to verify the failure | ||
| loggerMock.Verify( | ||
| x => x.Log( | ||
| LogLevel.Error, | ||
| It.IsAny<EventId>(), | ||
| It.Is<It.IsAnyType>((o, t) => o.ToString()!.Contains($"{TEST_RUNTIME_CONFIG_FILE} already exists.")), | ||
| It.IsAny<Exception?>(), | ||
| (Func<It.IsAnyType, Exception?, string>)It.IsAny<object>() | ||
| ), | ||
| Times.Once | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that environment config file is successfully generated when that file is not already present. | ||
| /// When environment variable is not set, it should generate the default config file. | ||
| /// </summary> | ||
| [DataTestMethod] | ||
| [DataRow(true, false, "Test", "dab-config.Test.json", DisplayName = "Failed to generate the config file when environment config file is present.")] | ||
| [DataRow(false, true, "Test", "dab-config.Test.json", DisplayName = "Successfully generated the config file when environment config file is not present.")] | ||
| [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.")] | ||
| [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.")] | ||
| public void TryGenerateConfig_UsingEnvironmentVariable( | ||
| bool isConfigFilePresent, | ||
| bool isConfigGenerationSuccessful, | ||
| string? environmentValue, | ||
| string configFileName) | ||
| { | ||
| Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue); | ||
| HandleConfigFileCreationAndDeletion(configFileName, isConfigFilePresent); | ||
| Assert.AreEqual(isConfigFilePresent, _fileSystem!.File.Exists(configFileName)); | ||
|
|
||
| InitOptions options = CreateBasicInitOptionsForMsSqlWithConfig(); | ||
|
|
||
| // Mocking logger to assert on logs | ||
| Mock<ILogger<ConfigGenerator>> loggerMock = new(); | ||
| ConfigGenerator.SetLoggerForCliConfigGenerator(loggerMock.Object); | ||
|
|
||
| Assert.AreEqual(isConfigGenerationSuccessful, ConfigGenerator.TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!)); | ||
| if (!isConfigFilePresent) | ||
| { | ||
| Assert.AreEqual(isConfigGenerationSuccessful, _fileSystem!.File.Exists(configFileName)); | ||
| } | ||
| else | ||
| { | ||
| // Assert on the log message to verify the failure | ||
| loggerMock.Verify( | ||
| x => x.Log( | ||
| LogLevel.Error, | ||
| It.IsAny<EventId>(), | ||
| It.Is<It.IsAnyType>((o, t) => o.ToString()!.Contains($"{configFileName} already exists.")), | ||
| It.IsAny<Exception?>(), | ||
| (Func<It.IsAnyType, Exception?, string>)It.IsAny<object>() | ||
| ), | ||
| Times.Once | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This method handles the creation and deletion of a configuration file. | ||
| /// </summary> | ||
| private void HandleConfigFileCreationAndDeletion(string configFilePath, bool configFilePresent) | ||
| { | ||
| if (!configFilePresent) | ||
| { | ||
| _fileSystem!.File.Delete(configFilePath); | ||
| } | ||
| else if (configFilePresent) | ||
| { | ||
| _fileSystem!.File.Create(configFilePath).Dispose(); | ||
abhishekkumams marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.