MCPcopy
hub / github.com/tornadoweb/tornado / test_reload

Method test_reload

tornado/test/autoreload_test.py:107–186  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

105 return out
106
107 def test_reload(self):
108 main = """\
109import sys
110
111# In module mode, the path is set to the parent directory and we can import testapp.
112try:
113 import testapp
114except ImportError:
115 print("import testapp failed")
116else:
117 print("import testapp succeeded")
118
119spec = getattr(sys.modules[__name__], '__spec__', None)
120print(f"Starting {__name__=}, __spec__.name={getattr(spec, 'name', None)}")
121exec(open("run_twice_magic.py", encoding="utf-8").read())
122"""
123
124 # Create temporary test application
125 self.write_files(
126 {
127 "testapp": {
128 "__init__.py": "",
129 "__main__.py": main,
130 },
131 }
132 )
133
134 # The autoreload wrapper should support all the same modes as the python interpreter.
135 # The wrapper itself should have no effect on this test so we try all modes with and
136 # without it.
137 for wrapper in [False, True]:
138 with self.subTest(wrapper=wrapper):
139 with self.subTest(mode="module"):
140 if wrapper:
141 base_args = [sys.executable, "-m", "tornado.autoreload"]
142 else:
143 base_args = [sys.executable]
144 # In module mode, the path is set to the parent directory and we can import
145 # testapp. Also, the __spec__.name is set to the fully qualified module name.
146 out = self.run_subprocess(base_args + ["-m", "testapp"])
147 self.assertEqual(
148 out,
149 (
150 "import testapp succeeded\n"
151 + "Starting __name__='__main__', __spec__.name=testapp.__main__\n"
152 )
153 * 2,
154 )
155
156 with self.subTest(mode="file"):
157 out = self.run_subprocess(base_args + ["testapp/__main__.py"])
158 # In file mode, we do not expect the path to be set so we can import testapp,
159 # but when the wrapper is used the -m argument to the python interpreter
160 # does this for us.
161 expect_import = (
162 "import testapp succeeded"
163 if wrapper
164 else "import testapp failed"

Callers

nothing calls this directly

Calls 2

write_filesMethod · 0.95
run_subprocessMethod · 0.95

Tested by

no test coverage detected