• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / Key2Joy / 6557152774

18 Oct 2023 06:36AM UTC coverage: 52.519% (+39.8%) from 12.718%
6557152774

push

github

web-flow
Adding more tests, fixing bugs in the process (#48)

* Add config manager tests

* legacy config and mapping profile tests (should fix #42)

* remove comment, problem was caused by transfer across appdomain (or to/fro scripting environment)

* Test core functionality #48 + includes minor refactoring to be able to test + added docs

* Add interop tests + implement and test async test utility (refactors away from singletons)

* fix not all tests running in workflow

* config and interop tests

* Refactor and allow mocking global input hook class

* add capture action test

* Make Execute override optional for script only methods

* add dependency injection + refactor and try test gamepad service

* Refactor config singleton to using dependency injection

* add tests for scripting

* add tests for plugin set + fix plugin showing as loaded even if checksum match failed

* fix tests failing because it relied on config exist (I guess the test order was accidentally correct earlier, this means we should really fix cleanup so we catch this sooner)

* refactor docs code + fix wrong enum summary

* refactor docs builder and start testing it a bit

* fix cmd project structure

* ignore designer files in tests

* cleanup and refactor UI code + show latest version in help

* truncate listview action column

* allow user config to minimize app when pressing X (defaults to shut down app) resolves #45

696 of 1757 branches covered (0.0%)

Branch coverage included in aggregate %.

4597 of 4597 new or added lines in 138 files covered. (100.0%)

3619 of 6459 relevant lines covered (56.03%)

17089.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.21
/Core/Key2Joy.Core/Config/ConfigManager.cs
1
using System.IO;
2
using System.Text.Json;
3
using Key2Joy.Contracts;
4
using Key2Joy.Util;
5

6
namespace Key2Joy.Config;
7

8
/// <summary>
9
/// Manages user configurations being loaded from and saved to disk.
10
/// </summary>
11
public class ConfigManager : IConfigManager
12
{
13
    protected const string CONFIG_PATH = "config.json";
14

15
    public bool IsInitialized { get; private set; }
66✔
16

17
    private ConfigState configState;
18

19
    public ConfigManager()
16✔
20
        => this.LoadOrCreate();
16✔
21

22
    /// <returns>The path to where the config file is located.</returns>
23
    protected virtual string GetAppDataDirectory() => Output.GetAppDataDirectory();
×
24

25
    public ConfigState GetConfigState()
26
        => this.configState;
12✔
27

28
    public void Save()
29
    {
30
        var options = GetSerializerOptions();
26✔
31
        var configPath = Path.Combine(
26✔
32
            this.GetAppDataDirectory(),
26✔
33
            CONFIG_PATH);
26✔
34

35
        File.WriteAllText(configPath, JsonSerializer.Serialize(this.configState, options));
26✔
36
    }
26✔
37

38
    /// <summary>
39
    /// Loads the configuration or creates a default one on disk.
40
    /// </summary>
41
    public void LoadOrCreate()
42
    {
43
        var configPath = Path.Combine(
16✔
44
            this.GetAppDataDirectory(),
16✔
45
            CONFIG_PATH);
16✔
46

47
        this.configState = new ConfigState(this);
16✔
48

49
        if (!File.Exists(configPath))
16✔
50
        {
51
            this.IsInitialized = true;
5✔
52
            this.Save();
5✔
53
            return;
5✔
54
        }
55

56
        var options = GetSerializerOptions();
11✔
57
        // Merge the loaded config state with the default config state
58
        JsonUtilities.PopulateObject(
11✔
59
            File.ReadAllText(configPath),
11✔
60
            this.configState,
11✔
61
            options
11✔
62
        );
11✔
63

64
        var assembly = System.Reflection.Assembly.GetEntryAssembly();
11✔
65

66
        // If the assembly is null then we are running in a unit test
67
        if (assembly == null)
11!
68
        {
69
            this.CompleteInitialization();
11✔
70
            return;
11✔
71
        }
72

73
        var executablePath = assembly.Location;
×
74
        if (executablePath.EndsWith("Key2Joy.exe")
×
75
            && this.configState.LastInstallPath != executablePath)
×
76
        {
77
            this.configState.LastInstallPath = executablePath;
×
78
        }
79

80
        this.CompleteInitialization();
×
81
        return;
×
82
    }
83

84
    private void CompleteInitialization()
85
    {
86
        this.IsInitialized = true;
11✔
87

88
        // We save so old properties are removed and new ones are added to the config file immediately
89
        this.Save();
11✔
90
    }
11✔
91

92
    protected static JsonSerializerOptions GetSerializerOptions()
93
    {
94
        JsonSerializerOptions options = new()
37✔
95
        {
37✔
96
            WriteIndented = true,
37✔
97
        };
37✔
98

99
        return options;
37✔
100
    }
101

102
    /// <summary>
103
    /// Turns the plugin path into a relative one from the app directory
104
    /// </summary>
105
    /// <param name="pluginAssemblyPath"></param>
106
    /// <returns></returns>
107
    protected string NormalizePluginPath(string pluginAssemblyPath)
108
    {
109
        var appDirectory = Path.GetDirectoryName(this.configState.LastInstallPath);
20✔
110
        var pluginPath = Path.GetFullPath(pluginAssemblyPath);
20✔
111
        var relativePath = pluginPath.Replace(appDirectory, string.Empty).TrimStart('\\');
20✔
112

113
        return relativePath;
20✔
114
    }
115

116
    /// <summary>
117
    /// Sets a plugin as enabled, the permissions checksum is stored so no changes to the permissions
118
    /// are accepted when loading the plugin later.
119
    ///
120
    /// Set permissionsChecksumOrNull to null to disable the plugin.
121
    /// </summary>
122
    /// <param name="pluginAssemblyPath"></param>
123
    /// <param name="permissionsChecksumOrNull"></param>
124
    public void SetPluginEnabled(string pluginAssemblyPath, string permissionsChecksumOrNull)
125
    {
126
        pluginAssemblyPath = this.NormalizePluginPath(pluginAssemblyPath);
6✔
127

128
        if (permissionsChecksumOrNull != null)
6✔
129
        {
130
            if (!this.configState.EnabledPlugins.ContainsKey(pluginAssemblyPath))
5✔
131
            {
132
                this.configState.EnabledPlugins.Add(pluginAssemblyPath, permissionsChecksumOrNull);
3✔
133
            }
134
            else
135
            {
136
                this.configState.EnabledPlugins[pluginAssemblyPath] = permissionsChecksumOrNull;
2✔
137
            }
138
        }
139
        else
140
        {
141
            if (this.configState.EnabledPlugins.ContainsKey(pluginAssemblyPath))
1✔
142
            {
143
                this.configState.EnabledPlugins.Remove(pluginAssemblyPath);
1✔
144
            }
145
        }
146

147
        this.Save();
6✔
148
    }
6✔
149

150
    public bool IsPluginEnabled(string pluginAssemblyPath)
151
        => this.configState.EnabledPlugins.ContainsKey(this.NormalizePluginPath(pluginAssemblyPath));
6✔
152

153
    public string GetExpectedPluginChecksum(string pluginAssemblyPath)
154
        => this.configState.EnabledPlugins.ContainsKey(this.NormalizePluginPath(pluginAssemblyPath))
5✔
155
            ? this.configState.EnabledPlugins[this.NormalizePluginPath(pluginAssemblyPath)]
5✔
156
            : null;
5✔
157
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc