2016-01-06 05:28:26 +00:00
|
|
|
describe("docsettings module", function()
|
2016-09-13 03:02:48 +00:00
|
|
|
local docsettings, lfs, util
|
2016-06-06 15:56:07 +00:00
|
|
|
|
2016-06-04 05:05:14 +00:00
|
|
|
setup(function()
|
|
|
|
require("commonrequire")
|
|
|
|
docsettings = require("docsettings")
|
2016-06-06 17:05:16 +00:00
|
|
|
lfs = require("libs/libkoreader-lfs")
|
2016-09-13 03:02:48 +00:00
|
|
|
util = require("ffi/util")
|
2016-06-04 05:05:14 +00:00
|
|
|
end)
|
2016-09-13 03:02:48 +00:00
|
|
|
|
2016-01-06 05:28:26 +00:00
|
|
|
it("should generate sidecar directory path", function()
|
2016-06-04 05:05:14 +00:00
|
|
|
assert.Equals("../../foo.sdr", docsettings:getSidecarDir("../../foo.pdf"))
|
|
|
|
assert.Equals("/foo/bar.sdr", docsettings:getSidecarDir("/foo/bar.pdf"))
|
|
|
|
assert.Equals("baz.sdr", docsettings:getSidecarDir("baz.pdf"))
|
2016-01-06 05:28:26 +00:00
|
|
|
end)
|
2016-06-06 17:05:16 +00:00
|
|
|
|
2017-02-02 11:13:13 +00:00
|
|
|
it("should generate sidecar metadata file", function()
|
|
|
|
assert.Equals("../../foo.sdr/metadata.pdf.lua",
|
|
|
|
docsettings:getSidecarFile("../../foo.pdf"))
|
|
|
|
assert.Equals("/foo/bar.sdr/metadata.pdf.lua",
|
|
|
|
docsettings:getSidecarFile("/foo/bar.pdf"))
|
|
|
|
assert.Equals("baz.sdr/metadata.epub.lua",
|
|
|
|
docsettings:getSidecarFile("baz.epub"))
|
|
|
|
end)
|
|
|
|
|
2016-06-06 15:56:07 +00:00
|
|
|
it("should read legacy history file", function()
|
|
|
|
local file = "file.pdf"
|
|
|
|
local d = docsettings:open(file)
|
|
|
|
d:saveSetting("a", "b")
|
2016-06-06 17:05:16 +00:00
|
|
|
d:saveSetting("c", "d")
|
2016-06-06 15:56:07 +00:00
|
|
|
d:close()
|
|
|
|
-- Now the sidecar file should be written.
|
|
|
|
|
2016-06-06 17:05:16 +00:00
|
|
|
local legacy_files = {
|
|
|
|
d.history_file,
|
|
|
|
d.sidecar .. "/file.pdf.lua",
|
|
|
|
"file.pdf.kpdfview.lua",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f in pairs(legacy_files) do
|
|
|
|
assert.False(os.rename(d.sidecar_file, f) == nil)
|
|
|
|
d = docsettings:open(file)
|
|
|
|
assert.True(os.remove(d.sidecar_file) == nil)
|
|
|
|
-- Legacy history files should not be removed before flush has been
|
|
|
|
-- called.
|
|
|
|
assert.Equals(lfs.attributes(f, "mode"), "file")
|
|
|
|
assert.Equals(d:readSetting("a"), "b")
|
|
|
|
assert.Equals(d:readSetting("c"), "d")
|
|
|
|
assert.Equals(d:readSetting("e"), nil)
|
|
|
|
d:close()
|
|
|
|
-- legacy history files should be removed as sidecar_file is
|
|
|
|
-- preferred.
|
|
|
|
assert.True(os.remove(f) == nil)
|
|
|
|
end
|
|
|
|
|
2016-06-06 15:56:07 +00:00
|
|
|
assert.False(os.remove(d.sidecar_file) == nil)
|
2016-06-06 17:05:16 +00:00
|
|
|
d:purge()
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should respect newest history file", function()
|
|
|
|
local file = "file.pdf"
|
|
|
|
local d = docsettings:open(file)
|
|
|
|
|
|
|
|
local legacy_files = {
|
|
|
|
d.history_file,
|
|
|
|
d.sidecar .. "/file.pdf.lua",
|
|
|
|
"file.pdf.kpdfview.lua",
|
|
|
|
}
|
|
|
|
|
|
|
|
-- docsettings:flush will remove legacy files.
|
|
|
|
for i, v in pairs(legacy_files) do
|
|
|
|
d:saveSetting("a", i)
|
|
|
|
d:flush()
|
|
|
|
assert.False(os.rename(d.sidecar_file, v.."1") == nil)
|
|
|
|
end
|
2016-06-06 15:56:07 +00:00
|
|
|
|
|
|
|
d:close()
|
2016-06-06 17:05:16 +00:00
|
|
|
for _, v in pairs(legacy_files) do
|
|
|
|
assert.False(os.rename(v.."1", v) == nil)
|
|
|
|
end
|
2016-06-06 15:56:07 +00:00
|
|
|
|
|
|
|
d = docsettings:open(file)
|
2016-06-06 17:05:16 +00:00
|
|
|
assert.Equals(d:readSetting("a"), #legacy_files)
|
2016-06-06 15:56:07 +00:00
|
|
|
d:close()
|
|
|
|
d:purge()
|
2016-01-06 05:28:26 +00:00
|
|
|
end)
|
2016-09-13 03:02:48 +00:00
|
|
|
|
|
|
|
it("should build correct legacy history path", function()
|
|
|
|
local file = "/a/b/c--d/c.txt"
|
|
|
|
local history_path = util.basename(docsettings:getHistoryPath(file))
|
|
|
|
local path_from_history = docsettings:getPathFromHistory(history_path)
|
|
|
|
local name_from_history = docsettings:getNameFromHistory(history_path)
|
|
|
|
assert.is.same(file, path_from_history .. "/" .. name_from_history)
|
|
|
|
end)
|
2017-08-17 01:38:58 +00:00
|
|
|
|
|
|
|
it("should reserve last good 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"))
|
|
|
|
|
|
|
|
-- write some garbage to sidecar-file.
|
|
|
|
local f_out = io.open(d.sidecar_file, "w")
|
|
|
|
f_out:write("bla bla bla")
|
|
|
|
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
|
|
|
|
|
|
|
|
d = docsettings:open(file)
|
|
|
|
-- The content should come from sidecar_file.
|
|
|
|
assert.Equals("b", d:readSetting("a"))
|
|
|
|
-- write some garbage to sidecar-file.
|
|
|
|
f_out = io.open(d.sidecar_file, "w")
|
|
|
|
f_out:write("bla bla bla")
|
|
|
|
f_out:close()
|
|
|
|
|
|
|
|
-- do not flush the result, open docsettings again.
|
|
|
|
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"))
|
|
|
|
-- The content should come from sidecar_file.old.
|
|
|
|
assert.Equals("a", d:readSetting("a"))
|
|
|
|
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"))
|
|
|
|
end)
|
2016-01-06 05:28:26 +00:00
|
|
|
end)
|