diff options
author | Aviskar KC <aviskarkc10@gmail.com> | 2019-07-04 14:31:03 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-07-18 14:26:20 -0400 |
commit | 02a1bf3454acb7b02942e246c19326630a8f9175 (patch) | |
tree | 316d647c5a0ac589b601579094a79ca5bd7d08d3 /tests/test_command.py | |
parent | 7cc28f542f4b1addd6b5a55d3b774ac74de6bed4 (diff) | |
download | alembic-02a1bf3454acb7b02942e246c19326630a8f9175.tar.gz |
Allow init into existing but empty directory
The "alembic init" command will now proceed if the target directory exists
as long as it's still empty. Previously, it would not proceed if the
directory existed. The new behavior is modeled from what git does, to
accommodate for container or other deployments where an Alembic target
directory may need to be already mounted instead of being created with
alembic init. Pull request courtesy Aviskar KC.
Fixes: #571
Closes: #574
Pull-request: https://github.com/sqlalchemy/alembic/pull/574
Pull-request-sha: ab7b8f342a89c752460d171cd50d1f958c7d59c9
Change-Id: I6b0cff9a7e940d9d878aa339891bda33744a2a4c
Diffstat (limited to 'tests/test_command.py')
-rw-r--r-- | tests/test_command.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/test_command.py b/tests/test_command.py index 653028f..746471b 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -800,3 +800,57 @@ class CommandLineTest(TestBase): # not too long assert len(help_text) < 80 assert not commands, "Commands without help text: %s" % commands + + def test_init_file_exists_and_is_not_empty(self): + with mock.patch( + "alembic.command.os.listdir", return_value=["file1", "file2"] + ), mock.patch("alembic.command.os.access", return_value=True): + directory = "alembic" + assert_raises_message( + util.CommandError, + "Directory %s already exists and is not empty" % directory, + command.init, + self.cfg, + directory=directory, + ) + + def test_init_file_exists_and_is_empty(self): + def access_(path, mode): + if "generic" in path or path == "foobar": + return True + else: + return False + + def listdir_(path): + if path == "foobar": + return [] + else: + return ["file1", "file2", "alembic.ini.mako"] + + with mock.patch( + "alembic.command.os.access", side_effect=access_ + ), mock.patch("alembic.command.os.makedirs") as makedirs, mock.patch( + "alembic.command.os.listdir", side_effect=listdir_ + ), mock.patch( + "alembic.command.ScriptDirectory" + ): + command.init(self.cfg, directory="foobar") + eq_(makedirs.mock_calls, [mock.call("foobar/versions")]) + + def test_init_file_doesnt_exist(self): + def access_(path, mode): + if "generic" in path: + return True + else: + return False + + with mock.patch( + "alembic.command.os.access", side_effect=access_ + ), mock.patch("alembic.command.os.makedirs") as makedirs, mock.patch( + "alembic.command.ScriptDirectory" + ): + command.init(self.cfg, directory="foobar") + eq_( + makedirs.mock_calls, + [mock.call("foobar"), mock.call("foobar/versions")], + ) |