|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from rsconnect import shiny_express as express |
| 4 | + |
| 5 | +def test_is_express_app(tmp_path: Path): |
| 6 | + tmp_file = str(tmp_path / "app.py") |
| 7 | + |
| 8 | + def write_tmp_file(s: str): |
| 9 | + with open(tmp_file, "w") as f: |
| 10 | + f.write(s) |
| 11 | + |
| 12 | + write_tmp_file("import shiny.express") |
| 13 | + assert express.is_express_app(tmp_file, None) |
| 14 | + # Check that it works when passing in app_path |
| 15 | + assert express.is_express_app("app.py", str(tmp_path)) |
| 16 | + |
| 17 | + write_tmp_file("# comment\nimport sys\n\nimport shiny.express") |
| 18 | + assert express.is_express_app(tmp_file, None) |
| 19 | + |
| 20 | + write_tmp_file("import sys\n\nfrom shiny import App, express") |
| 21 | + assert express.is_express_app(tmp_file, None) |
| 22 | + |
| 23 | + write_tmp_file("import sys\n\nfrom shiny.express import layout, input") |
| 24 | + assert express.is_express_app(tmp_file, None) |
| 25 | + |
| 26 | + # Shouldn't find in comment |
| 27 | + write_tmp_file("# import shiny.express") |
| 28 | + assert not express.is_express_app(tmp_file, None) |
| 29 | + |
| 30 | + # Shouldn't find in a string, even if it looks like an import |
| 31 | + write_tmp_file('"""\nimport shiny.express\n"""') |
| 32 | + assert not express.is_express_app(tmp_file, None) |
| 33 | + |
| 34 | + # Shouldn't recurse into with, if, for, def, etc. |
| 35 | + write_tmp_file("with f:\n from shiny import express") |
| 36 | + assert not express.is_express_app(tmp_file, None) |
| 37 | + |
| 38 | + write_tmp_file("if True:\n import shiny.express") |
| 39 | + assert not express.is_express_app(tmp_file, None) |
| 40 | + |
| 41 | + write_tmp_file("for i in range(2):\n import shiny.express") |
| 42 | + assert not express.is_express_app(tmp_file, None) |
| 43 | + |
| 44 | + write_tmp_file("def f():\n import shiny.express") |
| 45 | + assert not express.is_express_app(tmp_file, None) |
| 46 | + |
| 47 | + # Look for magic comment - should override import detection |
| 48 | + write_tmp_file("\n#shiny_mode: core\nfrom shiny.express import ui") |
| 49 | + assert not express.is_express_app(tmp_file, None) |
| 50 | + |
| 51 | + write_tmp_file("#shiny_mode: express\nfrom shiny import ui") |
| 52 | + assert express.is_express_app(tmp_file, None) |
0 commit comments