]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Fix tests on windows
authorCaselIT <cfederico87@gmail.com>
Fri, 27 Dec 2019 17:06:39 +0000 (12:06 -0500)
committersqla-tester <sqla-tester@sqlalchemy.org>
Fri, 27 Dec 2019 17:06:39 +0000 (12:06 -0500)
<!-- Provide a general summary of your proposed changes in the Title field above -->
Update the tests to make them run on windows
### Description
<!-- Describe your changes in detail -->
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
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)

-->

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: #<issue number>` 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: #<issue number>` 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

alembic/testing/env.py
tests/test_command.py
tests/test_script_production.py

index 15f777720c882e409b7c0cbc0678f0a75ef09c7e..473c73e7a37d57e4062bbb17c9c34fa27004bac0 100644 (file)
@@ -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)
 
 
index af8272d3fc852956f27379662add57fd3b66269e..b866d63bb0f62665891b782a7469067488d0b6b1 100644 (file)
@@ -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):
index ae2a2b85ca2bc22ac01cd6430286a89f798321af..aca16a3f02abcacc2d3ed941303b2c83ca5394d0 100644 (file)
@@ -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_(