diff --git a/lua/octo/utils.lua b/lua/octo/utils.lua index b03ad42e7..7f9eadfb3 100644 --- a/lua/octo/utils.lua +++ b/lua/octo/utils.lua @@ -748,8 +748,7 @@ end --- Escapes a characters on a string to be used as a JSON string function M.escape_char(string) - return string.gsub(string, '["\\]', { - ['"'] = '\\"', + return string.gsub(string, "[\\]", { ["\\"] = "\\\\", }) end diff --git a/lua/tests/plenary/utils_spec.lua b/lua/tests/plenary/utils_spec.lua index 18b4bdcae..be964c657 100644 --- a/lua/tests/plenary/utils_spec.lua +++ b/lua/tests/plenary/utils_spec.lua @@ -47,3 +47,23 @@ describe("Utils module:", function() end) end) end) + +describe("Utils escape_char(): ", function() + it("escapes backslash characters in a string", function() + local input = [[hello \\ world]] + local expected = [[hello \\\\ world]] + eq(expected, this.escape_char(input)) + end) + + it("returns the same string if no escape characters", function() + local input = [['hello/ ~^'*$"world%]] + local expected = [['hello/ ~^'*$"world%]] + eq(expected, this.escape_char(input)) + end) + + it("handles an empty string", function() + local input = "" + local expected = "" + eq(expected, this.escape_char(input)) + end) +end)