From e48f1db3b737fb9f1200ba95f9bcca7432292436 Mon Sep 17 00:00:00 2001 From: blowfish Date: Thu, 7 Dec 2017 22:32:46 -0800 Subject: [PATCH] Error when checking name against node with none SOME_NODE[] now gives a an error if SOME_NODE is found without a name value --- ModuleManager/PatchApplier.cs | 15 +++++++------ ModuleManagerTests/PatchApplierTest.cs | 30 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/ModuleManager/PatchApplier.cs b/ModuleManager/PatchApplier.cs index 5e30ad02..15411dbc 100644 --- a/ModuleManager/PatchApplier.cs +++ b/ModuleManager/PatchApplier.cs @@ -96,14 +96,14 @@ private void ApplyPatches(string stage, IEnumerable patches) { foreach (UrlDir.UrlConfig url in file.configs) { - if (!IsMatch(url, type, patterns, condition)) continue; + if (!IsMatch(url, type, patterns, condition, context)) continue; if (loop) logger.Info("Looping on " + mod.SafeUrl() + " to " + url.SafeUrl()); do { progress.ApplyingUpdate(url, mod); url.config = MMPatchLoader.ModifyNode(new NodeStack(url.config), mod.config, context); - } while (loop && IsMatch(url, type, patterns, condition)); + } while (loop && IsMatch(url, type, patterns, condition, context)); if (loop) url.config.RemoveNodes("MM_PATCH_LOOP"); } @@ -115,7 +115,7 @@ private void ApplyPatches(string stage, IEnumerable patches) for (int i = 0; i < count; i++) { UrlDir.UrlConfig url = file.configs[i]; - if (!IsMatch(url, type, patterns, condition)) continue; + if (!IsMatch(url, type, patterns, condition, context)) continue; ConfigNode clone = MMPatchLoader.ModifyNode(new NodeStack(url.config), mod.config, context); if (url.config.HasValue("name") && url.config.GetValue("name") == clone.GetValue("name")) @@ -136,7 +136,7 @@ private void ApplyPatches(string stage, IEnumerable patches) { UrlDir.UrlConfig url = file.configs[i]; - if (IsMatch(url, type, patterns, condition)) + if (IsMatch(url, type, patterns, condition, context)) { progress.ApplyingDelete(url, mod); file.configs.RemoveAt(i); @@ -170,13 +170,16 @@ private void ApplyPatches(string stage, IEnumerable patches) } } - private static bool IsMatch(UrlDir.UrlConfig url, string type, string[] namePatterns, string constraints) + private static bool IsMatch(UrlDir.UrlConfig url, string type, string[] namePatterns, string constraints, PatchContext context) { if (url.type != type) return false; if (namePatterns != null) { - if (url.name == url.type) return false; + if (url.name == url.type) + { + context.progress.Error(context.patchUrl, $"Attempting to apply a patch with a name matcher/wildcard to a node with no name value: {context.patchUrl.SafeUrl()} -> {url.SafeUrl()}"); + } bool match = false; foreach (string pattern in namePatterns) diff --git a/ModuleManagerTests/PatchApplierTest.cs b/ModuleManagerTests/PatchApplierTest.cs index ae5d05b4..2260c682 100644 --- a/ModuleManagerTests/PatchApplierTest.cs +++ b/ModuleManagerTests/PatchApplierTest.cs @@ -813,7 +813,37 @@ public void TestApplyPatches__InvalidOperator() { "name", "000" }, { "aaa", "1" }, }, allConfigs[0].config); + } + + [Fact] + public void TestApplyPatches__NameMatcherToNodeWithNoName() + { + UrlDir.UrlConfig config1 = UrlBuilder.CreateConfig(new TestConfigNode("SOME_NODE") + { + { "aa", "00" }, + }, file); + UrlDir.UrlConfig patch1 = new UrlDir.UrlConfig(file, new ConfigNode("@SOME_NODE[blah*]")); + + patchList.firstPatches.Add(patch1); + + patchApplier.ApplyPatches(); + + progress.DidNotReceiveWithAnyArgs().Exception(null, null); + progress.DidNotReceiveWithAnyArgs().Exception(null, null, null); + + logger.DidNotReceiveWithAnyArgs().Error(null); + logger.DidNotReceiveWithAnyArgs().Exception(null, null); + + progress.Received().Error(patch1, "Attempting to apply a patch with a name matcher/wildcard to a node with no name value: abc/def/@SOME_NODE[blah*] -> abc/def/SOME_NODE"); + + UrlDir.UrlConfig[] allConfigs = databaseRoot.AllConfigs.ToArray(); + Assert.Equal(1, allConfigs.Length); + + AssertNodesEqual(new TestConfigNode("SOME_NODE") + { + { "aa", "00" }, + }, allConfigs[0].config); } [Fact]