Skip to content

Busted's test isolation leaks previously loaded modules into test scope #643

Description

@alerque

See also: lunarmodules/Penlight#363

I recently had the bright idea of using Busted as our test framework for the Penlight project. This seemed to go well at first, until I managed to do something that should have passed our local test specs but happened to break something unrelated to the current test. At this point Busted itself broke. Confused, I did something that should have broken the local test and it passed. Eventually I realized Busted was itself loading Penlight as a dependency and creating a race condition.

While the automatic --auto-isolate feature is enabled and the describe() function is clearing out the loaded module environment so that nothing is in scope, it is not always possible to get a clean module load inside the test. Instead when you load anything that happened to be loaded by Busted previously, you get the cached version of the module not a fresh load!

Of course I want Busted to depend on whatever stable release of Penlight in installed as its dependency, but I also want to be able to load Penlight modules from the local working directory inside test specs to test Penlight itself.

In order to visualize this, I put together a demo repository. There are two Lua modules it creates, a dummy pl.utils and a dummy pl.sip. A .busted file correctly configures the Lua path so that the current project should be tested. Cloning the repo and running busted should just work. The two tests it runs look like so:

https://github.com/alerque/lua-busted-isolation-test/blob/master/spec/isolate_spec.lua#L1-L22

Instead the second test using the dummy pl.sip works fine while trying to use the pl.utils fails because it actually passes the Penlight module previously loaded by Busted for its own internal use. Since Busted doesn't happen to use the real pl.sip module it is not loaded previously and hence loads from the correct place on the first go.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions