From fa778f95a17f5050cff52330576e51955bcb4e1d Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 6 Dec 2023 17:34:04 -0800 Subject: [PATCH 1/4] _yet another_ bugfix to the version check X-( MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When I stopped running the version check before the tests I also stopped initializing Version, which can be used in tests to watch out for font changes across versions. As a result I started seeing a test failure with LÖVE v12. It looks like all manual tests pass now. And we're also printing the warning about version checks before running tests, which can come in handy if a new version ever causes test failures. The only thing that makes me unhappy is the fact that we're calling the version check twice. And oh, the fact that this part around initialization and version management is clearly still immature. I'll capture some desires and fragmentary thought processes around them: * If there's an error, go to the source editor. * But oh, don't go to source editor on some unactionable errors, so we include a new `Current_app` mode for them: * Unsupported version requires an expert. Just muddle through if you can or give a warning someone can send me. * A failing test might be spurious depending on the platform and font rendering scheme. So again just provide a warning someone can send me. [Source editor can be confusing for errors. Also an editor! But not showing the file you asked for!] * But our framework clears the warning after running tests: * If someone is deep in developing a new feature and quits -> restore back in the source editor. [Perhaps `Current_app` is the wrong place for this third hacky mode, since we actually want to continue running. Perhaps it's orthogonal to `Current_app`.] [Ideally I wouldn't run the tests after the version check. I'd pause, wait for a key and then resume tests? "Muddle through" is a pain to orchestrate.] * We store `Current_app` in settings. But we don't really intend to persist a `Current_app` of 'error'. Only the main app or 'source' editor. [Another vote against storing 'error' in `Current_app`.] * So we need to rerun the version check after running tests to actually show the warning. [Perhaps I need to separate out the side-effect of setting `Version` from the side-effect of changing `Current_app`. But that's not right either, because I do still want to raise an error message if the version check fails before running tests. Which brings us back to wanting to run the tests after raising the version check..] One good thing: none of the bugs so far have been about silently ignoring test failures. I thought that might be the case for a bit, which was unnerving. I grew similar muddiness in Mu's bootstrap system over time, with several surrounding modes around the core program that interacted poorly or at least unsatisfyingly with each other. On one level it just feels like this outer layer reflects muddy constraints in the real world. But perhaps there's some skill I still need to learn here.. Why am I even displaying this error if we're going to try to muddle through anyway? In (vain) hopes that someone will send me that information. It's not terribly actionable even to me. But it's really intended for when making changes. If a test fails then, you want to know. The code would be cleaner if I just threw an unrecoverable error from the version check. Historically, the way I arrived at this solution was: * I used the default love.errorhandler for a while * I added xpcall and error recovery, but now I have situations where I would rather fall back on love.errorhandler. How to tell xpcall that? But no, this whole line of thought is wrong. LÖVE has a precedent for trying to muddle through on an unexpected version. And spurious test failures don't merit a hard crash. There's some irreducible requirement here. No point making the code simplistic when the world is complex. Perhaps I should stop caching Version and just recompute it each time. It's only used once so far, hardly seems worth the global. We have two bits of irreducible complexity here: * If tests fail it might be a real failure, or it might not. * Even if it's an unexpected version, everything might be fine. And the major remaining problem happens at the intersection of these two bits. What if we get an unexpected version with some difference that causes tests to fail? But this is a hypothetical and not worth thinking about since I'll update the app fairly quickly in response to new versions. --- Manual_tests.md | 1 + app.lua | 1 + 2 files changed, 2 insertions(+) diff --git a/Manual_tests.md b/Manual_tests.md index c874610..2b8df26 100644 --- a/Manual_tests.md +++ b/Manual_tests.md @@ -18,6 +18,7 @@ Initializing settings: - no log file; switching to source works - run with an unsupported version. Error message pops up and waits for a key. The app attempts to continue, and doesn't receive the key. + - run with a LÖVE v12 release candidate. No errors; it is a supported version. All tests pass. Code loading: * run love with directory; text editor runs diff --git a/app.lua b/app.lua index e4b67cc..57533f1 100644 --- a/app.lua +++ b/app.lua @@ -8,6 +8,7 @@ -- and a source editor, while giving each the illusion of complete -- control. function love.run() + App.version_check() App.snapshot_love() -- Tests always run at the start. App.run_tests_and_initialize() From 01a26cad5f0abcd9aeb010ba91d33bcdb570256f Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 6 Dec 2023 20:14:24 -0800 Subject: [PATCH 2/4] redo version checks This is still ugly, but hopefully easier to follow. --- Manual_tests.md | 5 +++-- app.lua | 17 ++++++++++++++--- main.lua | 37 ++++++++++++++++++++----------------- reference.md | 3 +++ 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/Manual_tests.md b/Manual_tests.md index 2b8df26..12d3a66 100644 --- a/Manual_tests.md +++ b/Manual_tests.md @@ -17,7 +17,7 @@ Initializing settings: - start out editing source, move window, press ctrl+e twice; window is editing source in same position+dimensions - no log file; switching to source works - - run with an unsupported version. Error message pops up and waits for a key. The app attempts to continue, and doesn't receive the key. + - run with an untested version. Error message pops up and waits for a key. The app attempts to continue, and doesn't receive the key. - run with a LÖVE v12 release candidate. No errors; it is a supported version. All tests pass. Code loading: @@ -30,7 +30,8 @@ Code loading: - analogously, how a shape precisely looks as you draw it * start out running the text editor, press ctrl+e to edit source, make a change to the source, press ctrl+e twice to return to the source editor; the change should be preserved. -* run with an unsupported version. Error message pops up and waits for a key. The app attempts to continue, and doesn't receive the key. Press ctrl+e to edit source. Source editor opens up without checking version. +* run with an untested version. Error message pops up. Press a key. Text editor comes up, and doesn't receive the key. Press ctrl+e. Source editor opens up. Press ctrl+e. Text editor returns. +* create a couple of spuriously failing tests. Run with an untested version. Error message includes message about untested version. ### Other compromises diff --git a/app.lua b/app.lua index 57533f1..32c6f00 100644 --- a/app.lua +++ b/app.lua @@ -8,11 +8,11 @@ -- and a source editor, while giving each the illusion of complete -- control. function love.run() - App.version_check() + Version, Major_version = App.love_version() App.snapshot_love() -- Tests always run at the start. App.run_tests_and_initialize() - App.version_check() + App.love_version_check() -- hack: we want to run this just the first time and not every time we bounce between 'run' and 'source' --? print('==') love.timer.step() @@ -86,6 +86,12 @@ end App = {} +function App.love_version() + local major_version, minor_version = love.getVersion() + local version = major_version..'.'..minor_version + return version, major_version +end + -- save/restore various framework globals we care about -- only on very first load function App.snapshot_love() if Love_snapshot then return end @@ -103,7 +109,12 @@ function App.run_tests_and_initialize() Test_errors = {} App.run_tests() if #Test_errors > 0 then - error(('There were %d test failures:\n\n%s'):format(#Test_errors, table.concat(Test_errors))) + local error_message = '' + if Warning_before_tests then + error_message = Warning_before_tests..'\n\n' + end + error_message = error_message .. ('There were %d test failures:\n%s'):format(#Test_errors, table.concat(Test_errors)) + error(error_message) end App.disable_tests() App.initialize_globals() diff --git a/main.lua b/main.lua index 034953e..af53451 100644 --- a/main.lua +++ b/main.lua @@ -72,24 +72,10 @@ function App.load() end end -function App.version_check() - -- available modes: run, error - Error_message = nil - Error_count = 0 - -- we'll reuse error mode on load for an initial version check - local supported_versions = {'11.5', '11.4', '12.0'} -- put the recommended version first - local minor_version - Major_version, minor_version = love.getVersion() - Version = Major_version..'.'..minor_version - if array.find(supported_versions, Version) == nil then - Current_app = 'error' - Error_message = ("This app doesn't support version %s; please use version %s. Press any key to try it with this version anyway."):format(Version, supported_versions[1]) - print(Error_message) - -- continue initializing everything; hopefully we won't have errors during initialization - end -end - function App.initialize_globals() + Supported_versions = {'11.5', '11.4', '12.0'} -- put the recommended version first + check_love_version_for_tests() + if Current_app == 'run' then run.initialize_globals() elseif Current_app == 'source' then @@ -105,6 +91,23 @@ function App.initialize_globals() Last_resize_time = 0 end +function check_love_version_for_tests() + if array.find(Supported_versions, Version) == nil then + Unsupported_version = true + -- warning to include in an error message if any tests failed + Warning_before_tests = ("This app hasn't been tested with LÖVE version %s."):format(Version) + end +end + +function App.love_version_check() + if Unsupported_version then + Current_app = 'error' + Error_message = ("This app hasn't been tested with LÖVE version %s; please switch to version %s if you run into issues. Press any key to continue."):format(Version, Supported_versions[1]) + print(Error_message) + -- continue initializing everything; hopefully we won't have errors during initialization + end +end + function App.initialize(arg) love.keyboard.setTextInput(true) -- bring up keyboard on touch screen love.keyboard.setKeyRepeat(true) diff --git a/reference.md b/reference.md index 4dde57d..916c8f4 100644 --- a/reference.md +++ b/reference.md @@ -11,6 +11,9 @@ automatically called for you as appropriate. * `flags` -- some properties of the app window. See [`flags` in `love.graphics.getMode`](https://love2d.org/wiki/love.window.getMode) for details. +* `Version` -- the running version of LÖVE as a string, e.g. '11.4'. +* `Major_version` -- just the part before the period as an int, e.g. 11. + ## Functions that get automatically called * `App.initialize_globals()` -- called before running each test and also From 19597e7619babdf9a35efd61c9a87bdabd173569 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 6 Dec 2023 21:32:10 -0800 Subject: [PATCH 3/4] redo version checks yet again I'm starting to feel better after replacing 1 line with 20 and 2 new bits of global state. I'm now handling two scenarios more explicitly: * If I change Current_app within key_press, the corresponding text_input and key_release events go to the new app. If it's an editor it might insert the key, which is undesirable. Putting such handlers in key_release now feels overly clever, particularly since it took me forever to realize why I was getting stuck in an infinite loop. * Both 'run' and 'source' can hit the version check, so we need to be able to transition from the 'error' app to either. Which necessitates yet another global bit of state: Next_app. --- Manual_tests.md | 2 +- app.lua | 1 - main.lua | 57 ++++++++++++++++++++++++++++++++++--------------- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/Manual_tests.md b/Manual_tests.md index 12d3a66..582de20 100644 --- a/Manual_tests.md +++ b/Manual_tests.md @@ -30,7 +30,7 @@ Code loading: - analogously, how a shape precisely looks as you draw it * start out running the text editor, press ctrl+e to edit source, make a change to the source, press ctrl+e twice to return to the source editor; the change should be preserved. -* run with an untested version. Error message pops up. Press a key. Text editor comes up, and doesn't receive the key. Press ctrl+e. Source editor opens up. Press ctrl+e. Text editor returns. +* run with an untested version. Error message pops up. Press a key. Text editor comes up, and doesn't receive the key. Press ctrl+e. Error pops up. Press a key. Source editor opens up. Press ctrl+e. Error pops up. Press a key. Text editor returns. * create a couple of spuriously failing tests. Run with an untested version. Error message includes message about untested version. ### Other compromises diff --git a/app.lua b/app.lua index 32c6f00..271fdc3 100644 --- a/app.lua +++ b/app.lua @@ -12,7 +12,6 @@ function love.run() App.snapshot_love() -- Tests always run at the start. App.run_tests_and_initialize() - App.love_version_check() -- hack: we want to run this just the first time and not every time we bounce between 'run' and 'source' --? print('==') love.timer.step() diff --git a/main.lua b/main.lua index af53451..dbba863 100644 --- a/main.lua +++ b/main.lua @@ -89,25 +89,30 @@ function App.initialize_globals() Current_time = 0 Last_focus_time = 0 -- https://love2d.org/forums/viewtopic.php?p=249700 Last_resize_time = 0 + + -- Another weird bit for a class of corner cases. E.g.: + -- * I press ctrl+e, switch Current_app. I don't want the new app to receive + -- text_input and key_release events. + -- If I try to avoid text_input events by switching modes on key_release, I + -- hit a new problem: + -- * I press ctrl+e, am running an untested version, Current_app goes to + -- 'error', and immediately rolls back out of 'error' in the key_release + -- event. + -- Skip_rest_of_key_events is ugly, but feels cleaner than creating yet + -- another possible value for Current_app. + Skip_rest_of_key_events = nil + + -- Where to go from 'error' app. + Next_app = nil end function check_love_version_for_tests() if array.find(Supported_versions, Version) == nil then - Unsupported_version = true -- warning to include in an error message if any tests failed Warning_before_tests = ("This app hasn't been tested with LÖVE version %s."):format(Version) end end -function App.love_version_check() - if Unsupported_version then - Current_app = 'error' - Error_message = ("This app hasn't been tested with LÖVE version %s; please switch to version %s if you run into issues. Press any key to continue."):format(Version, Supported_versions[1]) - print(Error_message) - -- continue initializing everything; hopefully we won't have errors during initialization - end -end - function App.initialize(arg) love.keyboard.setTextInput(true) -- bring up keyboard on touch screen love.keyboard.setKeyRepeat(true) @@ -122,14 +127,25 @@ function App.initialize(arg) else assert(false, 'unknown app "'..Current_app..'"') end + + check_love_version() +end + +function check_love_version() + if array.find(Supported_versions, Version) == nil then + Next_app = Current_app + Current_app = 'error' + Error_message = ("This app hasn't been tested with LÖVE version %s; please switch to version %s if you run into issues. Press any key to continue."):format(Version, Supported_versions[1]) + -- continue initializing everything; hopefully we won't have errors during initialization + end end function App.resize(w,h) + if Current_app == 'error' then return end if Current_app == 'run' then if run.resize then run.resize(w,h) end elseif Current_app == 'source' then if source.resize then source.resize(w,h) end - elseif Current_app == 'error' then else assert(false, 'unknown app "'..Current_app..'"') end @@ -137,17 +153,18 @@ function App.resize(w,h) end function App.filedropped(file) + if Current_app == 'error' then return end if Current_app == 'run' then if run.file_drop then run.file_drop(file) end elseif Current_app == 'source' then if source.file_drop then source.file_drop(file) end - elseif Current_app == 'error' then else assert(false, 'unknown app "'..Current_app..'"') end end function App.focus(in_focus) + if Current_app == 'error' then return end if in_focus then Last_focus_time = Current_time end @@ -155,7 +172,6 @@ function App.focus(in_focus) if run.focus then run.focus(in_focus) end elseif Current_app == 'source' then if source.focus then source.focus(in_focus) end - elseif Current_app == 'error' then else assert(false, 'unknown app "'..Current_app..'"') end @@ -178,6 +194,7 @@ end function App.update(dt) Current_time = Current_time + dt + if Current_app == 'error' then return end -- some hysteresis while resizing if Current_time < Last_resize_time + 0.1 then return @@ -187,7 +204,6 @@ function App.update(dt) run.update(dt) elseif Current_app == 'source' then source.update(dt) - elseif Current_app == 'error' then else assert(false, 'unknown app "'..Current_app..'"') end @@ -199,9 +215,14 @@ function App.keychord_press(chord, key) return end -- + Skip_rest_of_key_events = nil if Current_app == 'error' then if chord == 'C-c' then love.system.setClipboardText(Error_message) + else + Current_app = Next_app + Next_app = nil + Skip_rest_of_key_events = true end return end @@ -229,6 +250,7 @@ function App.keychord_press(chord, key) load_file_from_source_or_save_directory('main.lua') App.undo_initialize() App.run_tests_and_initialize() + Skip_rest_of_key_events = true return end if Current_app == 'run' then @@ -247,6 +269,7 @@ function App.textinput(t) return end -- + if Skip_rest_of_key_events then return end if Current_app == 'run' then if run.text_input then run.text_input(t) end elseif Current_app == 'source' then @@ -257,14 +280,14 @@ function App.textinput(t) end function App.keyreleased(key, scancode) + if Current_app == 'error' then return end -- ignore events for some time after window in focus (mostly alt-tab) if Current_time < Last_focus_time + 0.01 then return end -- - if Current_app == 'error' then - Current_app = 'run' - elseif Current_app == 'run' then + if Skip_rest_of_key_events then return end + if Current_app == 'run' then if run.key_release then run.key_release(key, scancode) end elseif Current_app == 'source' then if source.key_release then source.key_release(key, scancode) end From 8c9775548097e9518bf5fcac2bde8a9594fa5b7a Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 6 Dec 2023 22:59:24 -0800 Subject: [PATCH 4/4] hide some details within the 'warning' state Renamed from the 'error' state. Now we no longer overload Error_message; it's only used for actual errors that trigger opening the source editor. I was tempted to hide Skip_rest_of_key_events inside the 'warning' state as well, but that isn't right. It applies to all Current_app transitions, not just those in and out of 'warning'. --- main.lua | 72 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/main.lua b/main.lua index dbba863..cf76a19 100644 --- a/main.lua +++ b/main.lua @@ -39,6 +39,11 @@ function App.load() Current_app = Settings.current_app end + -- Current_app = + -- | run + -- | source + -- | {name=warning message='...' next_app = run|source} + if Current_app == nil then Current_app = 'run' end @@ -66,7 +71,7 @@ function App.load() load_file_from_source_or_save_directory('colorize.lua') load_file_from_source_or_save_directory('source_text_tests.lua') load_file_from_source_or_save_directory('source_tests.lua') - elseif Current_app == 'error' then + elseif current_app_is_warning() then else assert(false, 'unknown app "'..Current_app..'"') end @@ -80,7 +85,7 @@ function App.initialize_globals() run.initialize_globals() elseif Current_app == 'source' then source.initialize_globals() - elseif Current_app == 'error' then + elseif current_app_is_warning() then else assert(false, 'unknown app "'..Current_app..'"') end @@ -96,14 +101,11 @@ function App.initialize_globals() -- If I try to avoid text_input events by switching modes on key_release, I -- hit a new problem: -- * I press ctrl+e, am running an untested version, Current_app goes to - -- 'error', and immediately rolls back out of 'error' in the key_release - -- event. + -- 'warning', and immediately rolls back out of 'warning' in the + -- key_release event. -- Skip_rest_of_key_events is ugly, but feels cleaner than creating yet -- another possible value for Current_app. Skip_rest_of_key_events = nil - - -- Where to go from 'error' app. - Next_app = nil end function check_love_version_for_tests() @@ -123,7 +125,7 @@ function App.initialize(arg) run.initialize(arg) elseif Current_app == 'source' then source.initialize(arg) - elseif Current_app == 'error' then + elseif current_app_is_warning() then else assert(false, 'unknown app "'..Current_app..'"') end @@ -133,15 +135,14 @@ end function check_love_version() if array.find(Supported_versions, Version) == nil then - Next_app = Current_app - Current_app = 'error' - Error_message = ("This app hasn't been tested with LÖVE version %s; please switch to version %s if you run into issues. Press any key to continue."):format(Version, Supported_versions[1]) + show_warning( + ("This app hasn't been tested with LÖVE version %s; please switch to version %s if you run into issues. Press any key to continue."):format(Version, Supported_versions[1])) -- continue initializing everything; hopefully we won't have errors during initialization end end function App.resize(w,h) - if Current_app == 'error' then return end + if current_app_is_warning() then return end if Current_app == 'run' then if run.resize then run.resize(w,h) end elseif Current_app == 'source' then @@ -153,7 +154,7 @@ function App.resize(w,h) end function App.filedropped(file) - if Current_app == 'error' then return end + if current_app_is_warning() then return end if Current_app == 'run' then if run.file_drop then run.file_drop(file) end elseif Current_app == 'source' then @@ -164,7 +165,7 @@ function App.filedropped(file) end function App.focus(in_focus) - if Current_app == 'error' then return end + if current_app_is_warning() then return end if in_focus then Last_focus_time = Current_time end @@ -182,11 +183,11 @@ function App.draw() run.draw() elseif Current_app == 'source' then source.draw() - elseif Current_app == 'error' then + elseif current_app_is_warning() then love.graphics.setColor(0,0,1) love.graphics.rectangle('fill', 0,0, App.screen.width, App.screen.height) love.graphics.setColor(1,1,1) - love.graphics.printf(Error_message, 40,40, 600) + love.graphics.printf(Current_app.message, 40,40, 600) else assert(false, 'unknown app "'..Current_app..'"') end @@ -194,7 +195,7 @@ end function App.update(dt) Current_time = Current_time + dt - if Current_app == 'error' then return end + if current_app_is_warning() then return end -- some hysteresis while resizing if Current_time < Last_resize_time + 0.1 then return @@ -216,12 +217,11 @@ function App.keychord_press(chord, key) end -- Skip_rest_of_key_events = nil - if Current_app == 'error' then + if current_app_is_warning() then if chord == 'C-c' then love.system.setClipboardText(Error_message) else - Current_app = Next_app - Next_app = nil + clear_warning() Skip_rest_of_key_events = true end return @@ -240,7 +240,7 @@ function App.keychord_press(chord, key) if source.quit then source.quit() end Current_app = 'run' Error_message = nil - elseif Current_app == 'error' then + elseif current_app_is_warning() then else assert(false, 'unknown app "'..Current_app..'"') end @@ -263,7 +263,7 @@ function App.keychord_press(chord, key) end function App.textinput(t) - if Current_app == 'error' then return end + if current_app_is_warning() then return end -- ignore events for some time after window in focus (mostly alt-tab) if Current_time < Last_focus_time + 0.01 then return @@ -280,7 +280,7 @@ function App.textinput(t) end function App.keyreleased(key, scancode) - if Current_app == 'error' then return end + if current_app_is_warning() then return end -- ignore events for some time after window in focus (mostly alt-tab) if Current_time < Last_focus_time + 0.01 then return @@ -297,7 +297,7 @@ function App.keyreleased(key, scancode) end function App.mousepressed(x,y, mouse_button) - if Current_app == 'error' then return end + if current_app_is_warning() then return end --? print('mouse press', x,y) if Current_app == 'run' then if run.mouse_press then run.mouse_press(x,y, mouse_button) end @@ -309,7 +309,7 @@ function App.mousepressed(x,y, mouse_button) end function App.mousereleased(x,y, mouse_button) - if Current_app == 'error' then return end + if current_app_is_warning() then return end if Current_app == 'run' then if run.mouse_release then run.mouse_release(x,y, mouse_button) end elseif Current_app == 'source' then @@ -320,7 +320,7 @@ function App.mousereleased(x,y, mouse_button) end function App.wheelmoved(dx,dy) - if Current_app == 'error' then return end + if current_app_is_warning() then return end if Current_app == 'run' then if run.mouse_wheel_move then run.mouse_wheel_move(dx,dy) end elseif Current_app == 'source' then @@ -331,7 +331,7 @@ function App.wheelmoved(dx,dy) end function love.quit() - if Current_app == 'error' then return end + if current_app_is_warning() then return end if Current_app == 'run' then local source_settings = Settings.source Settings = run.settings() @@ -349,3 +349,21 @@ function love.quit() assert(false, 'unknown app "'..Current_app..'"') end end + +function current_app_is_warning() + return type(Current_app) == 'table' and Current_app.name == 'warning' +end + +function show_warning(message) + assert(type(Current_app) == 'string') + Current_app = { + name = 'warning', + message = message, + next_app = Current_app, + } +end + +function clear_warning() + assert(type(Current_app) == 'table') + Current_app = Current_app.next_app +end