Tests: Make a few things more deterministic (#6991)

This way we still get the passed/expected actual values in Busted's
output

Which helps not make this any more maddening than it already is to
update...
reviewable/pr6999/r1
NiLuJe 3 years ago committed by GitHub
parent a90038e4eb
commit 83e148bc9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -263,10 +263,10 @@ function ReadHistory:removeItem(item, idx)
self:ensureLastFile() self:ensureLastFile()
end end
function ReadHistory:addItem(file) function ReadHistory:addItem(file, ts)
assert(self ~= nil) assert(self ~= nil)
if file ~= nil and lfs.attributes(file, "mode") == "file" then if file ~= nil and lfs.attributes(file, "mode") == "file" then
local now = os.time() local now = ts or os.time()
table.insert(self.hist, 1, buildEntry(now, file)) table.insert(self.hist, 1, buildEntry(now, file))
--- @todo (zijiehe): We do not need to sort if we can use binary insert and --- @todo (zijiehe): We do not need to sort if we can use binary insert and
-- binary search. -- binary search.

@ -3,6 +3,11 @@ describe("Readerfooter module", function()
local purgeDir, Screen local purgeDir, Screen
local tapFooterMenu local tapFooterMenu
local function is_am()
-- Technically only an issue for 1 digit results from %-H, e.g., anything below 10:00 AM
return tonumber(os.date("%H")) < 10
end
setup(function() setup(function()
require("commonrequire") require("commonrequire")
package.unloadAll() package.unloadAll()
@ -305,20 +310,25 @@ describe("Readerfooter module", function()
local footer = readerui.view.footer local footer = readerui.view.footer
local horizontal_margin = Screen:scaleBySize(10)*2 local horizontal_margin = Screen:scaleBySize(10)*2
footer:onUpdateFooter() footer:onUpdateFooter()
assert.is.same(370, footer.text_width) -- Account for trimming of the leading 0 in the AM
local expected = is_am() and 362 or 370
assert.is.same(expected, footer.text_width)
assert.is.same(600, footer.progress_bar.width assert.is.same(600, footer.progress_bar.width
+ footer.text_width + footer.text_width
+ horizontal_margin) + horizontal_margin)
assert.is.same(210, footer.progress_bar.width) expected = is_am() and 218 or 210
assert.is.same(expected, footer.progress_bar.width)
local old_screen_getwidth = Screen.getWidth local old_screen_getwidth = Screen.getWidth
Screen.getWidth = function() return 900 end Screen.getWidth = function() return 900 end
footer:resetLayout() footer:resetLayout()
assert.is.same(370, footer.text_width) expected = is_am() and 362 or 370
assert.is.same(expected, footer.text_width)
assert.is.same(900, footer.progress_bar.width assert.is.same(900, footer.progress_bar.width
+ footer.text_width + footer.text_width
+ horizontal_margin) + horizontal_margin)
assert.is.same(510, footer.progress_bar.width) expected = is_am() and 518 or 510
assert.is.same(expected, footer.progress_bar.width)
Screen.getWidth = old_screen_getwidth Screen.getWidth = old_screen_getwidth
end) end)
@ -333,12 +343,16 @@ describe("Readerfooter module", function()
} }
local footer = readerui.view.footer local footer = readerui.view.footer
footer:onPageUpdate(1) footer:onPageUpdate(1)
assert.are.same(202, footer.progress_bar.width) local expected = is_am() and 210 or 202
assert.are.same(378, footer.text_width) assert.are.same(expected, footer.progress_bar.width)
expected = is_am() and 370 or 378
assert.are.same(expected, footer.text_width)
footer:onPageUpdate(100) footer:onPageUpdate(100)
assert.are.same(178, footer.progress_bar.width) expected = is_am() and 186 or 178
assert.are.same(402, footer.text_width) assert.are.same(expected, footer.progress_bar.width)
expected = is_am() and 394 or 402
assert.are.same(expected, footer.text_width)
end) end)
it("should support chapter markers", function() it("should support chapter markers", function()

@ -5,7 +5,8 @@ describe("ReadHistory module", function()
local mkdir local mkdir
local realpath local realpath
local reload local reload
local usleep local lfs
local now = 61
local function file(name) local function file(name)
return joinPath(DataStorage:getDataDir(), name) return joinPath(DataStorage:getDataDir(), name)
@ -32,8 +33,12 @@ describe("ReadHistory module", function()
end end
local function touch(filename) local function touch(filename)
-- Create file if need be
local f = io.open(filename, "w") local f = io.open(filename, "w")
f:close() f:close()
-- Increment by 61s every time we're called
now = now + 61
lfs.touch(filename, now, now)
end end
local function assert_item_is(h, i, name, fileRemoved) local function assert_item_is(h, i, name, fileRemoved)
@ -55,7 +60,7 @@ describe("ReadHistory module", function()
mkdir = require("libs/libkoreader-lfs").mkdir mkdir = require("libs/libkoreader-lfs").mkdir
realpath = require("ffi/util").realpath realpath = require("ffi/util").realpath
reload = function() return package.reload("readhistory") end reload = function() return package.reload("readhistory") end
usleep = require("ffi/util").usleep lfs = require("libs/libkoreader-lfs")
mkdir(joinPath(DataStorage:getDataDir(), "testdata")) mkdir(joinPath(DataStorage:getDataDir(), "testdata"))
end) end)
@ -73,9 +78,10 @@ describe("ReadHistory module", function()
rm(file("history.lua")) rm(file("history.lua"))
local h = reload() local h = reload()
touch(test_file("a")) touch(test_file("a"))
h:addItem(test_file("a")) now = now + 61
h:addItem(test_file("a"), now)
h = reload() h = reload()
assert.is.same(#h.hist, 1) assert.is.same(1, #h.hist)
assert_item_is(h, 1, "a") assert_item_is(h, 1, "a")
rm(test_file("a")) rm(test_file("a"))
end) end)
@ -85,11 +91,11 @@ describe("ReadHistory module", function()
touch(test_file("a")) touch(test_file("a"))
touch(test_file("b")) touch(test_file("b"))
local h = reload() local h = reload()
h:addItem(test_file("a")) now = now + 61
usleep(1000000) h:addItem(test_file("a"), now)
touch(legacy_history_file("b")) touch(legacy_history_file("b"))
h = reload() h = reload()
assert.is.same(#h.hist, 2) assert.is.same(2, #h.hist)
assert_item_is(h, 1, "b") assert_item_is(h, 1, "b")
assert_item_is(h, 2, "a") assert_item_is(h, 2, "a")
rm(legacy_history_file("b")) rm(legacy_history_file("b"))
@ -106,19 +112,17 @@ describe("ReadHistory module", function()
touch(test_file("e")) touch(test_file("e"))
touch(test_file("f")) touch(test_file("f"))
local h = reload() local h = reload()
h:addItem(test_file("f")) now = now + 61
usleep(1000000) h:addItem(test_file("f"), now)
touch(legacy_history_file("c")) touch(legacy_history_file("c"))
usleep(1000000)
touch(legacy_history_file("b")) touch(legacy_history_file("b"))
usleep(1000000) now = now + 61
h:addItem(test_file("d")) h:addItem(test_file("d"), now)
usleep(1000000)
touch(legacy_history_file("a")) touch(legacy_history_file("a"))
usleep(1000000) now = now + 61
h:addItem(test_file("e")) h:addItem(test_file("e"), now)
h = reload() h = reload()
assert.is.same(#h.hist, 6) assert.is.same(6, #h.hist)
assert_item_is(h, 1, "e") assert_item_is(h, 1, "e")
assert_item_is(h, 2, "a") assert_item_is(h, 2, "a")
assert_item_is(h, 3, "d") assert_item_is(h, 3, "d")
@ -141,8 +145,9 @@ describe("ReadHistory module", function()
rm(file("history.lua")) rm(file("history.lua"))
touch(test_file("a")) touch(test_file("a"))
local h = reload() local h = reload()
h:addItem(test_file("a")) now = now + 61
assert.is.same(#h.hist, 1) h:addItem(test_file("a"), now)
assert.is.same(1, #h.hist)
assert_item_is(h, 1, "a") assert_item_is(h, 1, "a")
rm(test_file("a")) rm(test_file("a"))
end) end)
@ -153,9 +158,12 @@ describe("ReadHistory module", function()
touch(test_file("b")) touch(test_file("b"))
touch(test_file("c")) touch(test_file("c"))
local h = reload() local h = reload()
h:addItem(test_file("a")) -- NOTE: Identical timestamps to neuter sorting by mtime, instead alphabetical order kicks in (c.f., ReadHistory:_sort)
h:addItem(test_file("b")) -- This goes for basically the rest of the tests.
h:addItem(test_file("c")) now = now + 61
h:addItem(test_file("a"), now)
h:addItem(test_file("b"), now)
h:addItem(test_file("c"), now)
h:removeItem(h.hist[1]) h:removeItem(h.hist[1])
assert_item_is(h, 1, "b") assert_item_is(h, 1, "b")
assert_item_is(h, 2, "c") assert_item_is(h, 2, "c")
@ -172,9 +180,10 @@ describe("ReadHistory module", function()
touch(test_file("b")) touch(test_file("b"))
touch(test_file("c")) touch(test_file("c"))
local h = reload() local h = reload()
h:addItem(test_file("a")) now = now + 61
h:addItem(test_file("b")) h:addItem(test_file("a"), now)
h:addItem(test_file("c")) h:addItem(test_file("b"), now)
h:addItem(test_file("c"), now)
h:removeItem(h.hist[2]) h:removeItem(h.hist[2])
assert_item_is(h, 1, "a") assert_item_is(h, 1, "a")
assert_item_is(h, 2, "c") assert_item_is(h, 2, "c")
@ -189,9 +198,10 @@ describe("ReadHistory module", function()
touch(test_file("b")) touch(test_file("b"))
touch(test_file("c")) touch(test_file("c"))
local h = reload() local h = reload()
h:addItem(test_file("a")) now = now + 61
h:addItem(test_file("b")) h:addItem(test_file("a"), now)
h:addItem(test_file("c")) h:addItem(test_file("b"), now)
h:addItem(test_file("c"), now)
h:removeItem(h.hist[3]) h:removeItem(h.hist[3])
assert_item_is(h, 1, "a") assert_item_is(h, 1, "a")
assert_item_is(h, 2, "b") assert_item_is(h, 2, "b")
@ -209,10 +219,11 @@ describe("ReadHistory module", function()
touch(test_file("c")) touch(test_file("c"))
touch(test_file("d")) touch(test_file("d"))
local h = reload() local h = reload()
h:addItem(test_file("a")) now = now + 61
h:addItem(test_file("b")) h:addItem(test_file("a"), now)
h:addItem(test_file("c")) h:addItem(test_file("b"), now)
h:addItem(test_file("d")) h:addItem(test_file("c"), now)
h:addItem(test_file("d"), now)
h:removeItem(h.hist[3]) -- remove c h:removeItem(h.hist[3]) -- remove c
h:removeItem(h.hist[2]) -- remove b h:removeItem(h.hist[2]) -- remove b
assert_item_is(h, 1, "a") assert_item_is(h, 1, "a")
@ -231,11 +242,12 @@ describe("ReadHistory module", function()
touch(test_file("d")) touch(test_file("d"))
touch(test_file("e")) touch(test_file("e"))
local h = reload() local h = reload()
h:addItem(test_file("a")) now = now + 61
h:addItem(test_file("b")) h:addItem(test_file("a"), now)
h:addItem(test_file("c")) h:addItem(test_file("b"), now)
h:addItem(test_file("d")) h:addItem(test_file("c"), now)
h:addItem(test_file("e")) h:addItem(test_file("d"), now)
h:addItem(test_file("e"), now)
h:removeItem(h.hist[2]) -- remove b h:removeItem(h.hist[2]) -- remove b
h:removeItem(h.hist[2]) -- remove c h:removeItem(h.hist[2]) -- remove c
h:removeItem(h.hist[3]) -- remove e h:removeItem(h.hist[3]) -- remove e
@ -253,12 +265,15 @@ describe("ReadHistory module", function()
touch(test_file("a")) touch(test_file("a"))
touch(test_file("b")) touch(test_file("b"))
local h = reload() local h = reload()
h:addItem(test_file("b")) now = now + 61
h:addItem(test_file("b")) h:addItem(test_file("b"), now)
now = now + 61
h:addItem(test_file("b"), now)
touch(legacy_history_file("a")) touch(legacy_history_file("a"))
h:addItem(test_file("a")) -- ensure a is before b now = now + 61
h:addItem(test_file("a"), now) -- ensure a is before b
h = reload() h = reload()
assert.is.same(#h.hist, 2) assert.is.same(2, #h.hist)
assert_item_is(h, 1, "a") assert_item_is(h, 1, "a")
assert_item_is(h, 2, "b") assert_item_is(h, 2, "b")
@ -293,16 +308,17 @@ describe("ReadHistory module", function()
touch(test_file("a")) touch(test_file("a"))
touch(test_file("b")) touch(test_file("b"))
local h = reload() local h = reload()
h:addItem(test_file("a")) now = now + 61
h:addItem(test_file("b")) h:addItem(test_file("a"), now)
h:addItem(test_file("b"), now)
mv(file("history.lua"), file("history.backup")) mv(file("history.lua"), file("history.backup"))
h = reload() h = reload()
assert.is.same(#h.hist, 0) assert.is.same(0, #h.hist)
mv(file("history.backup"), file("history.lua")) mv(file("history.backup"), file("history.lua"))
h:reload() h:reload()
assert.is.same(#h.hist, 2) assert.is.same(2, #h.hist)
assert_item_is(h, 1, "a") assert_item_is(h, 1, "a")
assert_item_is(h, 2, "b") assert_item_is(h, 2, "b")
@ -316,13 +332,14 @@ describe("ReadHistory module", function()
touch(test_file("a")) touch(test_file("a"))
touch(test_file("b")) touch(test_file("b"))
local h = reload() local h = reload()
h:addItem(test_file("a")) now = now + 61
h:addItem(test_file("b")) h:addItem(test_file("a"), now)
h:addItem(test_file("b"), now)
rm(test_file("a")) rm(test_file("a"))
h:reload() h:reload()
assert.is.same(#h.hist, 1) assert.is.same(1, #h.hist)
assert_item_is(h, 1, "b") assert_item_is(h, 1, "b")
rm(test_file("b")) rm(test_file("b"))
@ -334,13 +351,14 @@ describe("ReadHistory module", function()
touch(test_file("a")) touch(test_file("a"))
touch(test_file("b")) touch(test_file("b"))
local h = reload() local h = reload()
h:addItem(test_file("a")) now = now + 61
h:addItem(test_file("b")) h:addItem(test_file("a"), now)
h:addItem(test_file("b"), now)
rm(test_file("a")) rm(test_file("a"))
h:reload() h:reload()
assert.is.same(#h.hist, 2) assert.is.same(2, #h.hist)
assert_item_is(h, 1, "a", true) assert_item_is(h, 1, "a", true)
assert_item_is(h, 2, "b") assert_item_is(h, 2, "b")

Loading…
Cancel
Save