< Summary

Information
Class: Anichron.Infrastructure.Configuration.AppIniInitializer
Assembly: Anichron.Infrastructure
File(s): /home/runner/work/anichron/anichron/src/Anichron.Infrastructure/Configuration/AppIniInitializer.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 49
Uncovered lines: 0
Coverable lines: 49
Total lines: 80
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
PathUnder(...)100%11100%
EnsureUpToDate(...)100%44100%
WriteNew(...)100%44100%
AppendMissing(...)100%44100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Infrastructure/Configuration/AppIniInitializer.cs

#LineLine coverage
 1using Microsoft.Extensions.Configuration;
 2using System.Globalization;
 3using System.IO.Abstractions;
 4using System.Text;
 5
 6namespace Anichron.Infrastructure.Configuration;
 7
 8public sealed record IniEntry(string Section, string Key, Func<string> DefaultValue);
 9
 2110public sealed class AppIniInitializer(IFileSystem fileSystem)
 11{
 12    public static string PathUnder(string contentRootPath)
 213        => Path.Combine(contentRootPath, "configuration", "app.ini");
 14
 15    public void EnsureUpToDate(string iniPath, IReadOnlyList<IniEntry> expectedEntries)
 2116    {
 2117        fileSystem.Directory.CreateDirectory(fileSystem.Path.GetDirectoryName(iniPath)!);
 18
 2119        if (!fileSystem.File.Exists(iniPath))
 1220        {
 1221            WriteNew(iniPath, expectedEntries);
 1122            return;
 23        }
 24
 925        using var stream = new MemoryStream(Encoding.UTF8.GetBytes(fileSystem.File.ReadAllText(iniPath)));
 926        var config = new ConfigurationBuilder().AddIniStream(stream).Build();
 927        var missing = expectedEntries
 928            .Where(e => config[$"{e.Section}:{e.Key}"] is null)
 929            .ToList();
 30
 931        if (missing.Count == 0)
 532            return;
 33
 434        AppendMissing(iniPath, missing);
 2435    }
 36
 37    private void WriteNew(string iniPath, IReadOnlyList<IniEntry> entries)
 1238    {
 1239        var sb = new StringBuilder();
 7640        foreach (var section in entries.GroupBy(e => e.Section))
 2041        {
 2042            sb.AppendLine(CultureInfo.InvariantCulture, $"[{section.Key}]");
 11843            foreach (var entry in section)
 2944                sb.AppendLine(CultureInfo.InvariantCulture, $"{entry.Key} = {entry.DefaultValue()}");
 2045            sb.AppendLine();
 2046        }
 47
 48        try
 1249        {
 50            // FileMode.CreateNew is O_CREAT|O_EXCL — atomic exclusive creation; only one wins if two instances race.
 1251            using var stream = fileSystem.FileStream.New(iniPath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
 1052            using var writer = new StreamWriter(stream);
 1053            writer.Write(sb.ToString());
 1054        }
 155        catch (IOException)
 156        {
 157            return; // Another instance created the file concurrently — proceed with that file.
 58        }
 59
 1060        Console.Error.WriteLine(
 1061            $"[WARN] Configuration file created at {iniPath}. Review the values and keep this file secure.");
 1162    }
 63
 64    private void AppendMissing(string iniPath, IReadOnlyList<IniEntry> missing)
 465    {
 466        var sb = new StringBuilder();
 2667        foreach (var section in missing.GroupBy(e => e.Section))
 768        {
 769            sb.AppendLine();
 770            sb.AppendLine(CultureInfo.InvariantCulture, $"[{section.Key}]");
 3571            foreach (var entry in section)
 772                sb.AppendLine(CultureInfo.InvariantCulture, $"{entry.Key} = {entry.DefaultValue()}");
 773        }
 74
 75        // Not atomic: two processes racing here can both append the same entries.
 76        // The INI provider tolerates duplicate keys (last value wins), so behavior is correct.
 477        fileSystem.File.AppendAllText(iniPath, sb.ToString());
 478        Console.Error.WriteLine($"[INFO] Added missing configuration entries to {iniPath}.");
 479    }
 80}