From 0cbb7f59b046f84cb8d1fcf9879cb70bab2ffbeb Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Mon, 14 Sep 2020 01:57:18 -0700 Subject: [PATCH] naughty: Improve backward compatibility to protecting the constants. If the user copy/pasted `naughty.config.*` into their config rather than set values 1 by 1, we could no longer add new values since they would get removed. To prevent more users being affected by this, we now silently ignore the new table while still setting all the values. Fix #3145 --- lib/naughty/constants.lua | 36 ++++++++++++++++++++++++++--------- tests/test-naughty-legacy.lua | 11 +++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/lib/naughty/constants.lua b/lib/naughty/constants.lua index ce237b130..d1f5157d4 100644 --- a/lib/naughty/constants.lua +++ b/lib/naughty/constants.lua @@ -9,9 +9,10 @@ -- @copyright 2017 Emmanuel Lepage Vallee ---------------------------------------------------------------------------- local beautiful = require("beautiful") +local gtable = require("gears.table") local dpi = beautiful.xresources.apply_dpi -local ret = {} +local ret, no_clear = {}, {} ret.config = { padding = dpi(4), @@ -21,7 +22,7 @@ ret.config = { notify_callback = nil, } -ret.config.presets = { +no_clear.presets = { low = { timeout = 5 }, @@ -55,15 +56,15 @@ ret.config._urgency = { } ret.config.mapping = { - {{urgency = ret.config._urgency.low }, ret.config.presets.low}, --compat - {{urgency = ret.config._urgency.normal }, ret.config.presets.normal}, --compat - {{urgency = ret.config._urgency.critical}, ret.config.presets.critical}, --compat - {{urgency = "low" }, ret.config.presets.low}, - {{urgency = "normal" }, ret.config.presets.normal}, - {{urgency = "critical"}, ret.config.presets.critical}, + {{urgency = ret.config._urgency.low }, no_clear.presets.low}, --compat + {{urgency = ret.config._urgency.normal }, no_clear.presets.normal}, --compat + {{urgency = ret.config._urgency.critical}, no_clear.presets.critical}, --compat + {{urgency = "low" }, no_clear.presets.low}, + {{urgency = "normal" }, no_clear.presets.normal}, + {{urgency = "critical"}, no_clear.presets.critical}, } -ret.config.defaults = { +no_clear.defaults = { timeout = 5, text = "", screen = nil, @@ -92,4 +93,21 @@ ret.notification_closed_reason = { -- Legacy --TODO v5 remove this alias ret.notificationClosedReason = ret.notification_closed_reason +-- `no_clear` is used to prevent users from setting the entire table. +-- If they did and we added a new default value, then it would not be +-- backward compatible. For safety, we just crush the tables rather than +-- replace them. +setmetatable(ret.config, { + __index = function(_, key) + return no_clear[key] + end, + __newindex = function(_, key, value) + if no_clear[key] then + gtable.crush(no_clear[key], value) + else + rawset(ret.config, key, value) + end + end +}) + return ret diff --git a/tests/test-naughty-legacy.lua b/tests/test-naughty-legacy.lua index 86c992ee4..7d716e374 100644 --- a/tests/test-naughty-legacy.lua +++ b/tests/test-naughty-legacy.lua @@ -1256,4 +1256,15 @@ table.insert(steps, function() return true end) +-- Make sure it isn't possible to remove default variables (#3145). +table.insert(steps, function() + naughty.config.defaults = {fake_variable = 24} + naughty.config.text = 1337 + assert(naughty.config.defaults.fake_variable == 24) + assert(naughty.config.defaults.timeout == 5) + assert(naughty.config.text == 1337) + + return true +end) + require("_runner").run_steps(steps)