Beispiel #1
0
 public void ShouldFailIfBadIncludePathIsProvided()
 {
     Check.ThatCode(() => TomlConfig.FromFile("files/sample-with-bad-include.toml").Read <SampleConfig>())
     .Throws <TomlConfigurationException>()
     .AndWhichMessage()
     .StartsWith("Missing include");
 }
Beispiel #2
0
 public void ArrayValuesCanNotBeAssignedToNonArrayProperties()
 {
     Check.ThatCode(() => TomlConfig.FromStream(Resources.Load("array-type-mismatch.toml")).Read <ArrayTestConfig>())
     .Throws <TomlConfigurationException>()
     .AndWhichMessage()
     .Contains("System.Int32");
 }
Beispiel #3
0
 private static void ExternalConfigurationBatch(TomlConfig.ITomlConfigBuilder builder)
 {
     builder
         .ConfigureType<IFoo>(ct => ct
             .CreateInstance(() => new Foo())
         );
 }
Beispiel #4
0
        public void ShouldReadConfigWith3Levels()
        {
            var subject = TomlConfig
                          .FromStream(Resources.Load("sample-3-levels.toml"))
                          .Read <SampleConfigWithTowLevelDimension>();

            var root = subject;

            Check.That(root.Host).IsEqualTo("www.default.com");
            Check.That(root.Path).IsEqualTo("/");
            Check.That(root.FileType).IsEqualTo("html");

            var subDomain = subject.Hosts.Single(x => x.Host == "www.site1.com");

            Check.That(subDomain.Path).IsEqualTo("/");
            Check.That(subDomain.FileType).IsEqualTo("java");

            var subDomainPhpPath = subDomain.Paths.Single(x => x.Path == "/site1/php");

            Check.That(subDomainPhpPath.Host).IsEqualTo(subDomain.Host);
            Check.That(subDomainPhpPath.Path).IsEqualTo("/site1/php");
            Check.That(subDomainPhpPath.FileType).IsEqualTo("php");

            var subDomainJavaPath = subDomain.Paths.Single(x => x.Path == "/site1/java");

            Check.That(subDomainJavaPath.Host).IsEqualTo(subDomain.Host);
            Check.That(subDomainJavaPath.Path).IsEqualTo("/site1/java");
            Check.That(subDomainJavaPath.FileType).IsEqualTo("java");


            var subDomain2Path2 = subject.Hosts.Single(x => x.Host == "www.site2.com")
                                  .Paths.Single(x => x.Path == "/site2/dotnet");

            Check.That(subDomain2Path2.FileType).IsEqualTo("dotnet");
        }
Beispiel #5
0
        /// <summary>
        /// Loads settings from specified file.
        /// </summary>
        /// <param name="path">Path to settings file.</param>
        /// <exception cref="System.InvalidOperationException">Settings already loaded.</exception>
        public static AppSettings Load(string path)
        {
            if (Instance != null)
            {
                throw new InvalidOperationException("Settings already loaded.");
            }

            TomlConfig config = TomlConfig.Create((builder) =>
            {
                builder.AllowImplicitConversions(TomlConfig.ConversionLevel.DotNetImplicit);
            });

            AppSettings settings;

            try
            {
                settings = Toml.ReadFile <AppSettings>(path, config);
            }
            catch (Exception)
            {
                settings = new AppSettings();
            }
            settings.Loaded();

            return(settings);
        }
Beispiel #6
0
        public void ShouldReadConfigWithOneDimension()
        {
            var subject = TomlConfig
                          .FromStream(Resources.Load("cascading-sample.toml"))
                          .Read <SampleConfig>();

            var root = subject;

            Check.That(root.Host).IsEqualTo("www.default-hosting.com");
            Check.That(root.WebServerRoot).IsEqualTo("/var/sites/default/www");
            Check.That(root.Stack).IsEqualTo("java");
            Check.That(root.User[0].Name).IsEqualTo("root");
            Check.That(root.User[0].Rights).IsEquivalentTo("read", "write", "create", "remove");

            var site1 = subject.Site[0];

            Check.That(site1.Host).IsEqualTo("www.myproject.com");
            Check.That(site1.WebServerRoot).IsEqualTo("/var/sites/myproject/www");
            Check.That(site1.Stack).IsEqualTo("php");
            Check.That(site1.User[0].Name).IsEqualTo("root");
            Check.That(site1.User[0].Rights).IsEquivalentTo("read", "write", "create", "remove");
            Check.That(site1.User[1].Name).IsEqualTo("john");
            Check.That(site1.User[1].Rights).IsEquivalentTo("read", "write");

            var site2 = subject.Site[1];

            Check.That(site2.Host).IsEqualTo("www.second-project.com");
            Check.That(site2.WebServerRoot).IsEqualTo("/var/sites/second-project/www");
            Check.That(site2.Stack).IsEqualTo("haskell");
            Check.That(site2.User[0].Name).IsEqualTo("root");
            Check.That(site2.User[0].Rights).IsEquivalentTo("read", "write", "create", "remove");
            Check.That(site2.User[1].Name).IsEqualTo("jess");
            Check.That(site2.User[1].Rights).IsEquivalentTo("read", "write", "remove");
        }
Beispiel #7
0
        public void ShouldInheritUsingDirective()
        {
            var config = TomlConfig.FromFile("files/sample-with-include.toml")
                         .Read <SampleConfig>();

            Check.That(config.Database)
            .IsNotNull();
        }
Beispiel #8
0
        public void ShouldParseDictionaries()
        {
            var config = TomlConfig.FromFile("files/my-application/dictionary.toml")
                         .Read <Dictionary <string, UserConfig> >();

            Check.That(config.Keys)
            .IsEquivalentTo(new [] { "User1", "User2", "User3" });
        }
Beispiel #9
0
        public void ShouldReadEnums()
        {
            var file     = "./files/my-application/enums.toml";
            var instance = TomlConfig
                           .FromFile(file)
                           .Read <StoreConfig>();

            CompareToExpectedResult(instance, file);
        }
Beispiel #10
0
        public void ShouldInheritNonSpecifiedValuesFromParent()
        {
            var subject = TomlConfig
                          .FromStream(Resources.Load("cascading-sample.toml"))
                          .Read <SampleConfig>();

            Check.That(subject.Site.Select(x => x.CopyRight).Distinct())
            .IsEquivalentTo("ACME LTD.");
        }
Beispiel #11
0
        public void ReadConfigFromFile()
        {
            var file = "./files/my-application/common.toml";

            var config = TomlConfig
                         .FromFile(file)
                         .Read <MyApplicationConfiguration>();

            CompareToExpectedResult(config, file);
        }
Beispiel #12
0
        public void ShouldNotFailIfPasswordIsNotSpecified()
        {
            var instance = TomlConfig
                           .FromString($"MyPassword = \"\"")
                           .WithMasterKey(Security.GenerateKeyAsString())
                           .Read <ConfigWithSecret>();

            Check.That(instance.MyPassword)
            .IsEmpty();
        }
Beispiel #13
0
        public void ShouldFailIfPasswordIsInvalid()
        {
            var key = Security.GenerateKeyAsString();

            var data = $"MyPassword = \"BAD VALUE\"";

            Check.ThatCode(() => TomlConfig.FromString(data).WithMasterKey(key).Read <ConfigWithSecret>())
            .Throws <TomlConfigurationException>()
            .AndWhichMessage().Contains("BAD VALUE");
        }
Beispiel #14
0
        public void RemoveDuplicationByIncludingACommonFile()
        {
            var file = "./files/my-application/staging.toml";

            var config = TomlConfig
                         .FromFile(file)
                         .Read <MyApplicationConfiguration>();

            CompareToExpectedResult(config, file);
        }
Beispiel #15
0
        public void ShouldCascadeValuesForHierarchicalValues()
        {
            var file = "./files/my-application/ftp-config.toml";

            var config = TomlConfig
                         .FromFile(file)
                         .Read <FtpConfig>();

            CompareToExpectedResult(config, file);
        }
Beispiel #16
0
        public void ShouldDecryptSecretWhenLoading()
        {
            var keeper = new SecretKeeper("KEY");

            var instance = TomlConfig
                           .FromString($"Password = \"{keeper.Encrypt("42")}\"")
                           .WithMasterKey("KEY")
                           .Read <ConfigWithSecret>();

            Check.That(instance.Password).IsEqualTo("42");
        }
Beispiel #17
0
        public void ReadConfigFromFileWithSecrets()
        {
            var file = "./files/my-application/production.toml";

            var config = TomlConfig
                         .FromFile(file)
                         .WithMasterKey("masterkey")
                         .Read <MyApplicationConfiguration>();

            CompareToExpectedResult(config, file);
        }
Beispiel #18
0
        public void ShouldDecryptSecrets()
        {
            var key          = Security.GenerateKeyAsString();
            var secretKeeper = new SecretKeeper(key);
            var secret       = "MyVerySecretPassword";

            var instance = TomlConfig
                           .FromString($"MyPassword = \"{secretKeeper.Encrypt(secret)}\"")
                           .WithMasterKey(key)
                           .Read <ConfigWithSecret>();

            Check.That(instance.MyPassword)
            .IsEqualTo(secret);
        }
Beispiel #19
0
        public void ShouldReadWithDefaultValues()
        {
            var @default = new SampleConfig
            {
                PiValue = 3.14159254f
            };

            var objectInstance = TomlConfig
                                 .FromStream(Resources.Load("read.toml"))
                                 .ReadWithDefault(@default);

            Check.That(objectInstance.PiValue)
            .IsEqualTo(@default.PiValue);
        }
Beispiel #20
0
        public void ShouldOverrideProperties()
        {
            var over    = "overridden";
            var subject = TomlConfig.FromStream(Resources.Load("multi-level.toml"))
                          .WithOverrides(new Dictionary <string, string>
            {
                { "Value", "42" },
                { "Path", over }
            })
                          .Read <MultiLevelConfig>();

            foreach (var entry in subject.GetAllConfigEntries(x => x.SubPaths))
            {
                Check.That(entry.Value).IsEqualTo(42);
                Check.That(entry.Path).IsEqualTo(over);
            }
        }
Beispiel #21
0
        public void ReadConfigAndOverrideFromEnvironmentValue()
        {
            Environment.SetEnvironmentVariable("ApplicationName", "overriden");

            var file = "./files/my-application/common.toml";

            var config = TomlConfig
                         .FromFile(file)
                         .WithOverrideFromEnvironmentVariables() /* Override from environment values */
                         .Read <MyApplicationConfiguration>();

            Check.That(config.ApplicationName)
            .IsEqualTo("overriden");

            var config2 = TomlConfig
                          .FromFile(file)
                          /* Override by value*/
                          .WithOverride(nameof(MyApplicationConfiguration.ApplicationName), "Another Value")
                          .Read <MyApplicationConfiguration>();

            Check.That(config2.ApplicationName)
            .IsEqualTo("Another Value");
        }
Beispiel #22
0
        public void ShouldReadStreamToObject()
        {
            var instance = TomlConfig
                           .FromStream(Resources.Load("read.toml"))
                           .Read <SampleConfig>();

            Check.That(instance.Value).IsEqualTo("Simple Value");
            Check.That(instance.IntValue).IsEqualTo(42);
            Check.That(instance.FloatValue).IsEqualTo(3.14);
            Check.That(instance.BooleanValue).IsEqualTo(true);
            Check.That(instance.LocalDateTime).IsEqualTo(new DateTime(2002, 5, 27, 7, 32, 0));
            Check.That(instance.DateTimeOffset).IsEqualTo(new DateTime(2002, 5, 27, 16, 32, 0, DateTimeKind.Local));
            Check.That(instance.LocalTime.TimeOfDay).IsEqualTo(new TimeSpan(0, 7, 32, 0));
            Check.That(instance.Array).IsEqualTo(new[] { 1, 2, 3 });

            Check.That(instance.Database.User).IsEqualTo("root");
            Check.That(instance.Database.Port).IsEqualTo(5432);

            Check.That(instance.Account[0].UserName).IsEqualTo("root");
            Check.That(instance.Account[0].IsAdmin).IsEqualTo(true);

            Check.That(instance.Account[1].UserName).IsEqualTo("guest");
            Check.That(instance.Account[1].IsAdmin).IsEqualTo(false);
        }
Beispiel #23
0
 public TomlWriter(FormattingStreamWriter writer, TomlConfig config)
 {
     this.writer = writer;
     this.config = config;
 }
Beispiel #24
0
 public TomlTableWriter(FormattingStreamWriter writer, TomlConfig config)
     : base(writer, config)
 {
     Assert(writer != null);
     Assert(config != null);
 }
Beispiel #25
0
 public TomlInlineTableWriter(FormattingStreamWriter writer, TomlConfig config)
     : base(writer, config)
 {
 }
Beispiel #26
0
 public void ShouldFailWithExpectedErrorIfFieldIsMissingOnTheType()
 {
     Check.ThatCode(() => TomlConfig.FromStream(Resources.Load("missing-field.toml")).Read <SampleConfig>())
     .Throws <TomlConfigurationException>().AndWhichMessage()
     .Contains("MagicValue");
 }