From fb627568f8dd3929fdb4e49362ace32be725d106 Mon Sep 17 00:00:00 2001 From: wangzhaolin Date: Tue, 19 Nov 2019 19:45:02 +0800 Subject: [PATCH] Fix the exception when use __file__ attribute in .py config file. --- orator/commands/command.py | 2 +- .../commands/migrations/test_make_command.py | 2 +- tests/commands/test_command.py | 35 +++++++++++++++++++ tests/migrations/test_migrator.py | 2 +- 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 tests/commands/test_command.py diff --git a/orator/commands/command.py b/orator/commands/command.py index 2a114400..85a878b3 100644 --- a/orator/commands/command.py +++ b/orator/commands/command.py @@ -123,7 +123,7 @@ def _get_config(self, path=None): config = {} with open(path) as fh: - exec(fh.read(), {}, config) + exec(fh.read(), {"__file__": path}, config) else: raise RuntimeError("Config file [%s] is not supported." % path) diff --git a/tests/commands/migrations/test_make_command.py b/tests/commands/migrations/test_make_command.py index b72269eb..af0c964d 100644 --- a/tests/commands/migrations/test_make_command.py +++ b/tests/commands/migrations/test_make_command.py @@ -31,7 +31,7 @@ def test_basic_create_gives_creator_proper_arguments_when_table_is_set(self): self.run_command(command, [("name", "create_foo"), ("--table", "users")]) def test_basic_create_gives_creator_proper_arguments_when_table_is_set_with_create( - self + self, ): creator_mock = flexmock(MigrationCreator) creator_mock.should_receive("create").once().with_args( diff --git a/tests/commands/test_command.py b/tests/commands/test_command.py new file mode 100644 index 00000000..95fac783 --- /dev/null +++ b/tests/commands/test_command.py @@ -0,0 +1,35 @@ +# -*- coding:utf-8 -*- +import os +import tempfile + +from flexmock import flexmock + +from orator.commands.command import Command + +from . import OratorCommandTestCase + + +class FooCommand(Command): + """ + Test Command + """ + + name = "foo" + + def handle(self): + pass + + +class CommandTestCase(OratorCommandTestCase): + def test_get_py_config_and_require___file__(self): + filename = tempfile.mktemp(".py") + with open(filename, "w") as f: + f.write("foo = __file__") + + command = flexmock(FooCommand()) + command.should_call("_get_config").and_return({"foo": filename}) + + self.run_command(command, [("-c", filename)]) + + if os.path.exists(filename): + os.remove(filename) diff --git a/tests/migrations/test_migrator.py b/tests/migrations/test_migrator.py index c62c3d2d..07141c97 100644 --- a/tests/migrations/test_migrator.py +++ b/tests/migrations/test_migrator.py @@ -224,7 +224,7 @@ def test_last_batch_of_migrations_can_be_rolled_back(self): migrator.rollback(os.getcwd()) def test_last_batch_of_migrations_can_be_rolled_back_directly_if_transactional_is_false( - self + self, ): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive("connection").and_return({})