Skip to content

main.py runs at import time, so its render loop cannot be unit tested #18

Description

@dmccoystephenson

Observation

main.py runs work at import time. Its argument parsing, its LocationService /
EnvironmentService construction and the bare main() call at the bottom of the file
all execute as soon as the module is imported:

locationService = LocationService(url, port)
environmentService = EnvironmentService(url, port)
...
main()

There is no if __name__ == "__main__": guard.

Consequence

The module cannot be imported by a test without the whole program running, so nothing
in main.py can be covered by the tests/ suite introduced in PR #10 — including the
RenderWindow-driven render loop that PR refactors, the environments.json cache-key
logic, and the --exit-after-create early-return path. The refactor in PR #10 was
therefore validated only by a hand-built headless harness that stubbed the Viron
services and injected the module chain into sys.modules, which is not something a
reviewer can re-run from the repository.

Suggested change

  • Guard the entry point with if __name__ == "__main__": so importing the module has
    no side effects. python main.py is unaffected, since the module is __main__ in
    that case, and create_environments.bat invokes it the same way.
  • Move the module-level argument parsing and service construction into a function
    (for example parse_args() and a main(gridSize, exitAfterCreate, ...) signature),
    so a test can drive main() with mocked services.
  • Add tests/test_main.py covering, at minimum, the grid-size fallback on unparseable
    input, the environments.json cache key, and loop termination on QUIT.

Scope note

This is a refactor of the entry point rather than a behavior change, and it touches the
live render loop, so the project's live-Viron manual-validation anchor is expected to be
required for it.

This issue body was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions