From: CaselIT Date: Fri, 27 Dec 2019 17:06:39 +0000 (-0500) Subject: Fix tests on windows X-Git-Tag: rel_1_3_3~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=98177eb24070b8845dccea387b044b21550ea1eb;p=thirdparty%2Fsqlalchemy%2Falembic.git Fix tests on windows Update the tests to make them run on windows ### Description The main changes are: - Correctly manage the paths on windows vs *nix like system. - Manage the new lines \r\n vs \n - Windows cannot remove open files, namely the sqlite db, so it keeps track of the connections and closes them before removing the files. I've tested with sqlite, postgres and mysql and all pass (on windows 10) This fixes #632 ### Checklist This pull request is: - [ ] A documentation / typographical error fix - Good to go, no issue or tests are needed - [x] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [ ] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #` in the commit message - please include tests. **Have a nice day!** Closes: #639 Pull-request: https://github.com/sqlalchemy/alembic/pull/639 Pull-request-sha: c6b7794510b5179f6ea574019df79a040e3c5f42 Change-Id: Ibbf7e1da941196d98d6a2e60d5f1bb3a253cca2b --- diff --git a/alembic/testing/env.py b/alembic/testing/env.py index 15f77772..473c73e7 100644 --- a/alembic/testing/env.py +++ b/alembic/testing/env.py @@ -60,6 +60,9 @@ def staging_env(create=True, template="generic", sourceless=False): def clear_staging_env(): + from sqlalchemy.testing import engines + + engines.testing_reaper.close_all() shutil.rmtree(_get_staging_directory(), True) diff --git a/tests/test_command.py b/tests/test_command.py index af8272d3..b866d63b 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -100,8 +100,14 @@ finally: if env_token: assert_lines.insert(0, "environment included OK") + actual = ( + buf.getvalue() + .decode("ascii", "replace") + .replace(os.linesep, "\n") + .strip() + ) eq_( - buf.getvalue().decode("ascii", "replace").strip(), + actual, "\n".join(assert_lines) .encode("ascii", "replace") .decode("ascii") @@ -879,9 +885,9 @@ class EditTest(TestBase): command.stamp(self.cfg, "base") def test_edit_head(self): - expected_call_arg = "%s/scripts/versions/%s_revision_c.py" % ( - EditTest.cfg.config_args["here"], - EditTest.c, + expected_call_arg = os.path.normpath( + "%s/scripts/versions/%s_revision_c.py" + % (EditTest.cfg.config_args["here"], EditTest.c,) ) with mock.patch("alembic.util.edit") as edit: @@ -889,9 +895,9 @@ class EditTest(TestBase): edit.assert_called_with(expected_call_arg) def test_edit_b(self): - expected_call_arg = "%s/scripts/versions/%s_revision_b.py" % ( - EditTest.cfg.config_args["here"], - EditTest.b, + expected_call_arg = os.path.normpath( + "%s/scripts/versions/%s_revision_b.py" + % (EditTest.cfg.config_args["here"], EditTest.b,) ) with mock.patch("alembic.util.edit") as edit: @@ -928,9 +934,9 @@ class EditTest(TestBase): ) def test_edit_current(self): - expected_call_arg = "%s/scripts/versions/%s_revision_b.py" % ( - EditTest.cfg.config_args["here"], - EditTest.b, + expected_call_arg = os.path.normpath( + "%s/scripts/versions/%s_revision_b.py" + % (EditTest.cfg.config_args["here"], EditTest.b,) ) command.stamp(self.cfg, self.b) @@ -1096,7 +1102,10 @@ class CommandLineTest(TestBase): "alembic.command.ScriptDirectory" ): command.init(self.cfg, directory="foobar") - eq_(makedirs.mock_calls, [mock.call("foobar/versions")]) + eq_( + makedirs.mock_calls, + [mock.call(os.path.normpath("foobar/versions"))], + ) def test_init_file_doesnt_exist(self): def access_(path, mode): @@ -1113,7 +1122,10 @@ class CommandLineTest(TestBase): command.init(self.cfg, directory="foobar") eq_( makedirs.mock_calls, - [mock.call("foobar"), mock.call("foobar/versions")], + [ + mock.call("foobar"), + mock.call(os.path.normpath("foobar/versions")), + ], ) def test_init_w_package(self): diff --git a/tests/test_script_production.py b/tests/test_script_production.py index ae2a2b85..aca16a3f 100644 --- a/tests/test_script_production.py +++ b/tests/test_script_production.py @@ -1236,9 +1236,9 @@ def downgrade(): clear_staging_env() def test_env_emits_warning(self): - with assertions.expect_warnings( + msg = ( "File %s loaded twice! ignoring. " - "Please ensure version_locations is unique" + "Please ensure version_locations is unique." % ( os.path.realpath( os.path.join( @@ -1248,7 +1248,8 @@ def downgrade(): ) ) ) - ): + ) + with assertions.expect_warnings(msg, regex=False): script = ScriptDirectory.from_config(self.cfg) script.revision_map.heads eq_(