Add a test and cover edge case with parens
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Keyan Pishdadian · committed by Keyan Pishdadian · parent 1479cf8 · Browse files · 2 files·+80 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+80 added−6 removed
- other:+80 added−6 removed
Provenance
- Commit
9cbe83e- Tree
f405938- Author
Keyan Pishdadian <kpishdadian@gmail.com>- Committer
Keyan Pishdadian <kpishdadian@gmail.com>- Parent
1479cf8
2 files changed+80 added−6 removed
modified:scripts/flaskext_migrate.py+29 added-6 removed
48 unmodified lines49 # Case 150 if len(node.value) == 3:51 package = values[2].value52 modules = node.modules()53+ module_string = _get_modules(modules)54 if len(modules) > 1:54- r = "{}," * len(modules)55 node.replace("from flask_%s import %s"56- % (package, r.format(*modules)[:-1]))56+ % (package, module_string))57 else:58 name = node.names()[0]59 node.replace("from flask_%s import %s as %s"60- % (package, modules.pop(), name))60+ % (package, module_string, name))61 # Case 262 else:63 module = node.modules()[0]64 node.replace("import flask_%s as %s"22 unmodified lines87 88 return red89 90 91+def _get_modules(module):92+ """93+ Takes a list of modules and converts into a string94+95+ The module list can include parens, this function checks each element in96+ the list, if there is a paren then it does not add a comma before the next97+ element. Otherwise a comma and space is added. This is to preserve module98+ imports which are multi-line and/or occur within parens. While also not99+ affecting imports which are not enclosed.100+ """101+ modules_string = [cur + ', ' if cur.isalnum() and next.isalnum()102+ else cur103+ for (cur, next) in zip(module, module[1:]+[''])]104+105+ return ''.join(modules_string)106+107+108+def check_user_input():109+ """Exits and gives error message if no argument is passed in the shell."""110+ if len(sys.argv) < 2:111+ sys.exit("No filename was included, please try again.")112+113+114 def fix(ast):92- """Wrapper which allows for testing when not running from shell"""115+ """Wrapper which allows for testing when not running from shell."""116 return fix_imports(ast).dumps()117 118+119 if __name__ == "__main__":96- if len(sys.argv) < 2:97- sys.exit("No filename was included, please try again.")120+ check_user_input()121 input_file = sys.argv[1]122 ast = read_source(input_file)123 ast = fix_imports(ast)124 write_source(ast, input_file)added:scripts/test_import_migration.py+51 added
1+# Tester for the flaskext_migrate.py module located in flask/scripts/2+#3+# Author: Keyan Pishdadian4+import pytest5+from redbaron import RedBaron6+import flaskext_migrate as migrate7+8+9+def test_simple_from_import():10+ red = RedBaron("from flask.ext import foo")11+ output = migrate.fix(red)12+ assert output == "import flask_foo as foo"13+14+15+def test_from_to_from_import():16+ red = RedBaron("from flask.ext.foo import bar")17+ output = migrate.fix(red)18+ assert output == "from flask_foo import bar as bar"19+20+21+def test_multiple_import():22+ red = RedBaron("from flask.ext.foo import bar, foobar, something")23+ output = migrate.fix(red)24+ assert output == "from flask_foo import bar, foobar, something"25+26+27+def test_multiline_import():28+ red = RedBaron("from flask.ext.foo import \29+ bar,\30+ foobar,\31+ something")32+ output = migrate.fix(red)33+ assert output == "from flask_foo import bar, foobar, something"34+35+36+def test_module_import():37+ red = RedBaron("import flask.ext.foo")38+ output = migrate.fix(red)39+ assert output == "import flask_foo"40+41+42+def test_module_import():43+ red = RedBaron("from flask.ext.foo import bar as baz")44+ output = migrate.fix(red)45+ assert output == "from flask_foo import bar as baz"46+47+48+def test_parens_import():49+ red = RedBaron("from flask.ext.foo import (bar, foo, foobar)")50+ output = migrate.fix(red)51+ assert output == "from flask_foo import (bar, foo, foobar)"