Ejemplo n.º 1
0
        // todo: tests initializes configs in a different way. Maybe there should be an abstraction IConfigInitializer and two concrete classes ConfigInitializer + TestConfigInitializer
        internal void InitializeForAzureSession(AzureSession session)
        {
            IConfigManager configManager = GetConfigManager();

            session.RegisterComponent(nameof(IConfigManager), () => configManager);
            RegisterConfigs(configManager);
            configManager.BuildConfig();
        }
Ejemplo n.º 2
0
        public void ThrowWhenGetUnknownConfig()
        {
            IConfigManager entry = GetConfigManager();

            entry.BuildConfig();

            Assert.Throws <AzPSArgumentNullException>(() => { entry.GetConfigValue <int>(null); });
            Assert.Throws <AzPSArgumentException>(() => { entry.GetConfigValue <int>(""); });

            const string key = "KeyThatIsNotRegistered";

            Assert.Throws <AzPSArgumentException>(() => { entry.GetConfigValue <int>(key); });
        }
Ejemplo n.º 3
0
        public void CanGetDefaultValue()
        {
            IConfigManager          entry  = GetConfigManager();
            const string            key    = "CanGetConfigValue";
            SimpleTypedConfig <int> config = new SimpleTypedConfig <int>(key, "", -1);

            entry.RegisterConfig(config);
            entry.BuildConfig();
            Assert.Equal(-1, entry.GetConfigValue <int>(key));

            entry.UpdateConfig(new UpdateConfigOptions(key, 10, ConfigScope.Process));
            Assert.Equal(10, entry.GetConfigValue <int>(key));
        }
Ejemplo n.º 4
0
        public void CanClearSingleConfigInJson()
        {
            IConfigManager icm = GetConfigManager();
            string         key = "DisableSomething";

            icm.RegisterConfig(new SimpleTypedConfig <bool>(key, "{help message}", false));
            icm.BuildConfig();

            Assert.False(icm.GetConfigValue <bool>(key));

            icm.UpdateConfig(new UpdateConfigOptions(key, true, ConfigScope.CurrentUser));
            Assert.True(icm.GetConfigValue <bool>(key));

            icm.ClearConfig(new ClearConfigOptions(key, ConfigScope.CurrentUser));
            Assert.False(icm.GetConfigValue <bool>(key));
        }
Ejemplo n.º 5
0
        public void CanUpdateAndList()
        {
            IConfigManager configurationManager = GetConfigManager();
            const string   key = "EnableTelemetry";

            configurationManager.RegisterConfig(
                new SimpleTypedConfig <bool>(
                    key,
                    "Enable telemetry",
                    true));
            configurationManager.BuildConfig();
            var updatedConfig = configurationManager.UpdateConfig(new UpdateConfigOptions(key, false, ConfigScope.Process));

            Assert.Equal(key, updatedConfig.Definition.Key);
            Assert.False((bool)updatedConfig.Value);
            Assert.False(configurationManager.GetConfigValue <bool>(key));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Initializes and returns an <see cref="IConfigManager"/> with the specified configs registered with initial state.
        /// </summary>
        /// <param name="configFileWriter">An action to set up the config file before config manager initializes.</param>
        /// <param name="envVarWriter">An action to set up the environments before config manager initializes.</param>
        /// <param name="config">Definitions of configs to be registered to the config manager.</param>
        /// <returns>A config manager with initial state, ready to use.</returns>
        internal IConfigManager GetConfigManager(InitSettings settings, params ConfigDefinition[] config)
        {
            var dataStore  = settings?.DataStore ?? new MockDataStore();
            var configPath = settings?.Path ?? Path.GetRandomFileName();
            var environmentVariableProvider = settings?.EnvironmentVariableProvider ?? new MockEnvironmentVariableProvider();

            ConfigInitializer ci = new ConfigInitializer(new List <string>()
            {
                configPath
            })
            {
                DataStore = dataStore,
                EnvironmentVariableProvider = environmentVariableProvider
            };
            IConfigManager icm = ci.GetConfigManager();

            foreach (var configDefinition in config)
            {
                icm.RegisterConfig(configDefinition);
            }
            icm.BuildConfig();
            return(icm);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Initializes and returns an <see cref="IConfigManager"/> with the specified configs registered with initial state.
        /// </summary>
        /// <param name="configFileWriter">An action to set up the config file before config manager initializes.</param>
        /// <param name="envVarWriter">An action to set up the environments before config manager initializes.</param>
        /// <param name="config">Definitions of configs to be registered to the config manager.</param>
        /// <returns>A config manager with initial state, ready to use.</returns>
        protected IConfigManager GetConfigManagerWithInitState(Action <MockDataStore, string> configFileWriter, Action <MockEnvironmentVariableProvider> envVarWriter, params ConfigDefinition[] config)
        {
            if (configFileWriter == null)
            {
                configFileWriter = _noopFileWriter;
            }

            if (envVarWriter == null)
            {
                envVarWriter = _noopEnvVarWriter;
            }

            string configPath    = Path.GetRandomFileName();
            var    mockDataStore = new MockDataStore();

            configFileWriter(mockDataStore, configPath);
            var environmentVariables = new MockEnvironmentVariableProvider();

            envVarWriter(environmentVariables);
            ConfigInitializer ci = new ConfigInitializer(new List <string>()
            {
                configPath
            })
            {
                DataStore = mockDataStore,
                EnvironmentVariableProvider = environmentVariables
            };
            IConfigManager icm = ci.GetConfigManager();

            foreach (var configDefinition in config)
            {
                icm.RegisterConfig(configDefinition);
            }
            icm.BuildConfig();
            return(icm);
        }