[chore] Ignore empty files and tables in DocSettings (#3348)

pull/3349/head
Hzj_jie 7 years ago committed by Frans de Jonge
parent 0fa090ee47
commit 7461e396dd

@ -15,6 +15,7 @@ local DocSettings = {}
local HISTORY_DIR = DataStorage:getHistoryDir()
local function buildCandidate(file_path)
-- Ignore empty files.
if lfs.attributes(file_path, "mode") == "file" then
return { file_path, lfs.attributes(file_path, "modification") }
else
@ -92,7 +93,6 @@ end
function DocSettings:open(docfile)
-- TODO(zijiehe): Remove history_path, use only sidecar.
local new = {}
local ok, stored
new.history_file = self:getHistoryPath(docfile)
local sidecar = self:getSidecarDir(docfile)
@ -130,15 +130,19 @@ function DocSettings:open(docfile)
return l[2] > r[2]
end
end)
local ok, stored
for _, k in pairs(candidates) do
ok, stored = pcall(dofile, k[1])
if ok then
logger.dbg("data is read from ", k[1])
break
else
logger.dbg(k[1], " is invalid, remove.")
os.remove(k[1])
-- Ignore empty files
if lfs.attributes(k[1], "size") > 0 then
ok, stored = pcall(dofile, k[1])
-- Ignore the empty table.
if ok and next(stored) ~= nil then
logger.dbg("data is read from ", k[1])
break
end
end
logger.dbg(k[1], " is invalid, remove.")
os.remove(k[1])
end
if ok and stored then
new.data = stored

@ -143,4 +143,67 @@ describe("docsettings module", function()
assert.Equals("file", lfs.attributes(d.sidecar_file, "mode"))
assert.Equals("file", lfs.attributes(d.sidecar_file .. ".old", "mode"))
end)
describe("ignore empty sidecar file", function()
it("should ignore empty file", function()
local file = "file.pdf"
local d = docsettings:open(file)
d:saveSetting("a", "a")
d:flush()
-- metadata.pdf.lua should be generated.
assert.Equals("file", lfs.attributes(d.sidecar_file, "mode"))
d:close()
-- metadata.pdf.lua and metadata.pdf.lua.old should be generated.
assert.Equals("file", lfs.attributes(d.sidecar_file, "mode"))
assert.Equals("file", lfs.attributes(d.sidecar_file .. ".old", "mode"))
-- reset the sidecar_file to an empty file.
local f_out = io.open(d.sidecar_file, "w")
f_out:close()
d = docsettings:open(file)
-- metadata.pdf.lua should be removed.
assert.are.not_equal("file", lfs.attributes(d.sidecar_file, "mode"))
assert.Equals("file", lfs.attributes(d.sidecar_file .. ".old", "mode"))
assert.Equals("a", d:readSetting("a"))
d:saveSetting("a", "b")
d:close()
-- metadata.pdf.lua should be generated.
assert.Equals("file", lfs.attributes(d.sidecar_file, "mode"))
assert.Equals("file", lfs.attributes(d.sidecar_file .. ".old", "mode"))
-- The contents in sidecar_file and sidecar_file.old are different.
-- a:b v.s. a:a
end)
it("should ignore empty table", function()
local file = "file.pdf"
local d = docsettings:open(file)
d:saveSetting("a", "a")
d:flush()
-- metadata.pdf.lua should be generated.
assert.Equals("file", lfs.attributes(d.sidecar_file, "mode"))
d:close()
-- metadata.pdf.lua and metadata.pdf.lua.old should be generated.
assert.Equals("file", lfs.attributes(d.sidecar_file, "mode"))
assert.Equals("file", lfs.attributes(d.sidecar_file .. ".old", "mode"))
-- reset the sidecar_file to an empty file.
local f_out = io.open(d.sidecar_file, "w")
f_out:write("{ } ")
f_out:close()
d = docsettings:open(file)
-- metadata.pdf.lua should be removed.
assert.are.not_equal("file", lfs.attributes(d.sidecar_file, "mode"))
assert.Equals("file", lfs.attributes(d.sidecar_file .. ".old", "mode"))
assert.Equals("a", d:readSetting("a"))
d:saveSetting("a", "b")
d:close()
-- metadata.pdf.lua should be generated.
assert.Equals("file", lfs.attributes(d.sidecar_file, "mode"))
assert.Equals("file", lfs.attributes(d.sidecar_file .. ".old", "mode"))
-- The contents in sidecar_file and sidecar_file.old are different.
-- a:b v.s. a:a
end)
end)
end)

Loading…
Cancel
Save