Skip to content

Commit

Permalink
Increased coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dalyIsaac committed Nov 12, 2024
1 parent c830e69 commit f421510
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 39 deletions.
59 changes: 36 additions & 23 deletions src/Whim.Yaml.Tests/YamlLoader/YamlLoader_LoadStylesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public void Load_FallbackStyles_AddsUserDictionaries(string config, bool isYaml,
{
// Given
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);
ctx.FileManager.WhimDir.Returns("C:/Users/username/.whim");
ctx.FileManager.WhimDir.Returns("C:\\Users\\username\\.whim");
ctx.FileManager.FileExists(Arg.Is<string>(p => p.StartsWith("path"))).Returns(false);
ctx.FileManager.FileExists(Arg.Is<string>(p => p.StartsWith("C:/Users/username/.whim"))).Returns(true);
ctx.FileManager.FileExists(Arg.Is<string>(p => p.StartsWith("C:\\Users\\username\\.whim"))).Returns(true);

// When
bool result = YamlLoader.Load(ctx, showErrorWindow: false);
Expand Down Expand Up @@ -87,65 +87,78 @@ public void Load_FallbackStyles_AddsUserDictionaries(string config, bool isYaml,
""",
false
},
{
"""
styles: {}
""",
true
},
{
"""
{
"styles": {}
}
""",
false
},
};

[Theory]
[MemberAutoSubstituteData<YamlLoaderCustomization>(nameof(NoStylesConfig))]
public void Load_NoStyles_DoesNotAddUserDictionaries(string config, bool isYaml, IContext ctx)
{
// Given
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);

// When
bool result = YamlLoader.Load(ctx, showErrorWindow: false);

// Then
Assert.True(result);
ctx.ResourceManager.DidNotReceive().AddUserDictionary(Arg.Any<string>());
}

public static TheoryData<string, bool> InvalidStylesConfig =>
public static TheoryData<string, bool> InvalidPathsStylesConfig =>
new()
{
{
"""
styles:
user_dictionaries: "path/to/dict.xaml"
user_dictionaries:
- "the path to nowhere"
""",
true
},
{
"""
{
"styles": {
"user_dictionaries": "path/to/dict.xaml"
"user_dictionaries": [
"the path to nowhere"
]
}
}
""",
false
},
};

public static TheoryData<string, bool> InvalidStylesConfig =>
new()
{
{
"""
styles: {}
styles:
user_dictionaries: "path/to/dict.xaml"
""",
true
},
{
"""
{
"styles": {}
"styles": {
"user_dictionaries": "path/to/dict.xaml"
}
}
""",
false
},
};

[Theory]
[MemberAutoSubstituteData<YamlLoaderCustomization>(nameof(NoStylesConfig))]
[MemberAutoSubstituteData<YamlLoaderCustomization>(nameof(InvalidPathsStylesConfig))]
[MemberAutoSubstituteData<YamlLoaderCustomization>(nameof(InvalidStylesConfig))]
public void Load_InvalidStyles_DoesNotAddUserDictionaries(string config, bool isYaml, IContext ctx)
public void Load_DoesNotAddUserDictionaries(string config, bool isYaml, IContext ctx)
{
// Given
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);
ctx.FileManager.FileExists(Arg.Is<string>(p => !p.Contains("yaml") && !p.Contains("json"))).Returns(false);

// When
bool result = YamlLoader.Load(ctx, showErrorWindow: false);
Expand Down
43 changes: 27 additions & 16 deletions src/Whim.Yaml/YamlLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static bool Load(IContext ctx, bool showErrorWindow = true)
UpdateKeybinds(ctx, schema);
UpdateFilters(ctx, schema);
UpdateRouters(ctx, schema);
UpdateStyles(ctx, schema);
UpdateStyles(ctx, schema, showErrorWindow);

YamlPluginLoader.LoadPlugins(ctx, schema);
YamlLayoutEngineLoader.UpdateLayoutEngines(ctx, schema);
Expand Down Expand Up @@ -280,7 +280,7 @@ private static void UpdateRouters(IContext ctx, Schema schema)
}
}

private static void UpdateStyles(IContext ctx, Schema schema)
private static void UpdateStyles(IContext ctx, Schema schema, bool showErrorWindow)
{
if (!schema.Styles.IsValid())
{
Expand All @@ -296,24 +296,35 @@ private static void UpdateStyles(IContext ctx, Schema schema)

foreach (var userDictionary in userDictionaries)
{
string filePath = (string)userDictionary;

if (!ctx.FileManager.FileExists(filePath))
if (GetUserDictionaryPath(ctx, (string)userDictionary, showErrorWindow) is not string filePath)
{
string relativePath = Path.Combine(ctx.FileManager.WhimDir, filePath);

if (!ctx.FileManager.FileExists(relativePath))
{
string error = $"User dictionary not found: {filePath}";
Logger.Error(error);
ShowError(ctx, error);
continue;
}

filePath = relativePath;
continue;
}

ctx.ResourceManager.AddUserDictionary(filePath);
}
}

private static string? GetUserDictionaryPath(IContext ctx, string filePath, bool showErrorWindow)
{
if (ctx.FileManager.FileExists(filePath))
{
return filePath;
}

string relativePath = Path.Combine(ctx.FileManager.WhimDir, filePath);
if (!ctx.FileManager.FileExists(relativePath))
{
string error = $"User dictionary not found: {filePath}";
Logger.Error(error);

if (showErrorWindow)
{
ShowError(ctx, error);
}

Check warning on line 324 in src/Whim.Yaml/YamlLoader.cs

View check run for this annotation

Codecov / codecov/patch

src/Whim.Yaml/YamlLoader.cs#L322-L324

Added lines #L322 - L324 were not covered by tests
return null;
}

return relativePath;
}
}

0 comments on commit f421510

Please sign in to comment.