From 8cc4a396f53caad98d02552692af4ab070bd5b1b Mon Sep 17 00:00:00 2001 From: Frank O'Brien Date: Fri, 5 Apr 2024 17:54:57 -0700 Subject: [PATCH 1/5] add test to check that modules are cached correctly --- src/tests.rs | 1 + tests/require/tests/state.luau | 16 ++++++++++++++++ tests/require/tests/state_module.luau | 11 +++++++++++ tests/require/tests/state_second.luau | 5 +++++ 4 files changed, 33 insertions(+) create mode 100644 tests/require/tests/state.luau create mode 100644 tests/require/tests/state_module.luau create mode 100644 tests/require/tests/state_second.luau diff --git a/src/tests.rs b/src/tests.rs index f4af8874..fad6026b 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -95,6 +95,7 @@ create_tests! { require_nested: "require/tests/nested", require_parents: "require/tests/parents", require_siblings: "require/tests/siblings", + require_state: "require/tests/state", global_g_table: "globals/_G", global_version: "globals/_VERSION", diff --git a/tests/require/tests/state.luau b/tests/require/tests/state.luau new file mode 100644 index 00000000..8c5cc3a0 --- /dev/null +++ b/tests/require/tests/state.luau @@ -0,0 +1,16 @@ +-- the idea of this test is that state_module stores some state in one of its local variable +-- we also set a global in the module to ensure the change we would have expected happens +local state_module = require("./state_module") + +-- we confirm that without anything happening, the initial value is what we ecpect +assert(state_module.state == 10) +assert(_state_test_global == 10) + +-- this second file also requires state_module and calls a function that changes both the local state and the global to 11 +require("./state_second") + +-- with correct module caching, we should see the change done in state_secone reflected here +assert(state_module.state == 11) +-- if this also fails then there is either a problem with globals, or state_second is not doing what we expect it to +assert(_state_test_global == 11) + diff --git a/tests/require/tests/state_module.luau b/tests/require/tests/state_module.luau new file mode 100644 index 00000000..c9460082 --- /dev/null +++ b/tests/require/tests/state_module.luau @@ -0,0 +1,11 @@ +local M = {} + +M.state = 10 +_state_test_global = 10 + +function M.set_state(n: number) + M.state = n + _state_test_global = n +end + +return M diff --git a/tests/require/tests/state_second.luau b/tests/require/tests/state_second.luau new file mode 100644 index 00000000..24ca3a45 --- /dev/null +++ b/tests/require/tests/state_second.luau @@ -0,0 +1,5 @@ +local state_module = require("./state_module") + +state_module.set_state(11) + +return {} From d252be46b3ef09f0ed98e25a19ca478e5d99ce9a Mon Sep 17 00:00:00 2001 From: Frank O'Brien Date: Fri, 5 Apr 2024 17:58:49 -0700 Subject: [PATCH 2/5] reorder the asserts so we check the global first --- tests/require/tests/state.luau | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/require/tests/state.luau b/tests/require/tests/state.luau index 8c5cc3a0..b73dd90a 100644 --- a/tests/require/tests/state.luau +++ b/tests/require/tests/state.luau @@ -3,14 +3,14 @@ local state_module = require("./state_module") -- we confirm that without anything happening, the initial value is what we ecpect -assert(state_module.state == 10) assert(_state_test_global == 10) +assert(state_module.state == 10) -- this second file also requires state_module and calls a function that changes both the local state and the global to 11 require("./state_second") --- with correct module caching, we should see the change done in state_secone reflected here -assert(state_module.state == 11) -- if this also fails then there is either a problem with globals, or state_second is not doing what we expect it to assert(_state_test_global == 11) +-- with correct module caching, we should see the change done in state_secone reflected here +assert(state_module.state == 11) From 9804a6abfed6b6864998bff10943d4f4b29d65fd Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 5 Apr 2024 23:41:34 -0700 Subject: [PATCH 3/5] Swap order of returned tuple fields in resolve_paths --- src/lune/globals/require/context.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lune/globals/require/context.rs b/src/lune/globals/require/context.rs index 0c95d802..7f018c61 100644 --- a/src/lune/globals/require/context.rs +++ b/src/lune/globals/require/context.rs @@ -70,7 +70,7 @@ impl RequireContext { CWD.join(&rel_path) }; - Ok((rel_path, abs_path)) + Ok((abs_path, rel_path)) } /** From 5c983b56b5cf24d2eef0380714429e4aa337c0b9 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 5 Apr 2024 23:45:32 -0700 Subject: [PATCH 4/5] Remove global from state_module test --- tests/require/tests/state.luau | 14 ++++++-------- tests/require/tests/state_module.luau | 2 -- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/require/tests/state.luau b/tests/require/tests/state.luau index b73dd90a..fce92cb2 100644 --- a/tests/require/tests/state.luau +++ b/tests/require/tests/state.luau @@ -1,16 +1,14 @@ --- the idea of this test is that state_module stores some state in one of its local variable --- we also set a global in the module to ensure the change we would have expected happens +-- the idea of this test is that state_module stores some state in one of its local +-- variable local state_module = require("./state_module") -- we confirm that without anything happening, the initial value is what we ecpect -assert(_state_test_global == 10) assert(state_module.state == 10) --- this second file also requires state_module and calls a function that changes both the local state and the global to 11 +-- this second file also requires state_module and calls a function that changes the local +-- state to 11 require("./state_second") --- if this also fails then there is either a problem with globals, or state_second is not doing what we expect it to -assert(_state_test_global == 11) --- with correct module caching, we should see the change done in state_secone reflected here +-- with correct module caching, we should see the change done in state_secone reflected +-- here assert(state_module.state == 11) - diff --git a/tests/require/tests/state_module.luau b/tests/require/tests/state_module.luau index c9460082..9b6135e2 100644 --- a/tests/require/tests/state_module.luau +++ b/tests/require/tests/state_module.luau @@ -1,11 +1,9 @@ local M = {} M.state = 10 -_state_test_global = 10 function M.set_state(n: number) M.state = n - _state_test_global = n end return M From cc5305fe105ea4b674b4d4bf4fe1d884859def5a Mon Sep 17 00:00:00 2001 From: Kenneth Loeffler Date: Sat, 6 Apr 2024 14:24:52 -0700 Subject: [PATCH 5/5] Update tests/require/tests/state.luau Co-authored-by: vocksel --- tests/require/tests/state.luau | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/require/tests/state.luau b/tests/require/tests/state.luau index fce92cb2..d1b2b462 100644 --- a/tests/require/tests/state.luau +++ b/tests/require/tests/state.luau @@ -2,7 +2,7 @@ -- variable local state_module = require("./state_module") --- we confirm that without anything happening, the initial value is what we ecpect +-- we confirm that without anything happening, the initial value is what we expect assert(state_module.state == 10) -- this second file also requires state_module and calls a function that changes the local