import importlib.util import pathlib import tempfile import unittest SPEC = importlib.util.spec_from_file_location("java_with_env", pathlib.Path(__file__).parents[1] / "java-with-env.py") MODULE = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(MODULE) class EnvironmentTest(unittest.TestCase): def test_literals_preserve_jdbc_ampersands_and_never_execute_shell(self): with tempfile.TemporaryDirectory() as directory: path = pathlib.Path(directory) / ".env" path.write_text("DB_URL=jdbc:mysql://example/db?a=1&b=2\nDB_PASSWORD='literal$(command)`text`'\nEMPTY=\n") values = MODULE.load_environment(path, {"DB_PASSWORD": "inherited", "KEEP": "yes"}) self.assertEqual(values["DB_URL"], "jdbc:mysql://example/db?a=1&b=2") self.assertEqual(values["DB_PASSWORD"], "literal$(command)`text`") self.assertEqual(values["KEEP"], "yes") self.assertEqual(values["EMPTY"], "") def test_invalid_lines_are_rejected_without_echoing_secrets(self): with tempfile.TemporaryDirectory() as directory: path = pathlib.Path(directory) / ".env" path.write_text("PASSWORD='secret\n") with self.assertRaisesRegex(ValueError, "第 1 行") as caught: MODULE.load_environment(path, {}) self.assertNotIn("secret", str(caught.exception))