Mercurial > hg
comparison tests/test-module-imports.t @ 25703:1a6a117d0b95
import-checker: establish modern import convention
We introduce a new convention for declaring imports and enforce it via
the import checker script.
The new convention is only active when absolute imports are used, which is
currently nowhere. Keying off "from __future__ import absolute_import" to
engage the new import convention seems like the easiest solution. It is
also beneficial for Mercurial to use this mode because it means less work
and ambiguity for the importer and potentially better performance due to
fewer stat() system calls because the importer won't look for modules in
relative paths unless explicitly asked.
Once all files are converted to use absolute import, we can refactor
this code to again only have a single import convention and we can
require use of absolute import in the style checker.
The rules for the new convention are documented in the docstring of the
added function. Tests have been added to test-module-imports.t. Some
tests are sensitive to newlines and source column position, which makes
docstring testing difficult and/or impossible.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sun, 28 Jun 2015 12:46:34 -0700 |
parents | 7a5335ed7e1a |
children | cd1daab5d036 |
comparison
equal
deleted
inserted
replaced
25702:ab2c5163900e | 25703:1a6a117d0b95 |
---|---|
5 Run the doctests from the import checker, and make sure | 5 Run the doctests from the import checker, and make sure |
6 it's working correctly. | 6 it's working correctly. |
7 $ TERM=dumb | 7 $ TERM=dumb |
8 $ export TERM | 8 $ export TERM |
9 $ python -m doctest $import_checker | 9 $ python -m doctest $import_checker |
10 | |
11 Run additional tests for the import checker | |
12 | |
13 $ mkdir testpackage | |
14 | |
15 $ cat > testpackage/multiple.py << EOF | |
16 > from __future__ import absolute_import | |
17 > import os, sys | |
18 > EOF | |
19 | |
20 $ cat > testpackage/unsorted.py << EOF | |
21 > from __future__ import absolute_import | |
22 > import sys | |
23 > import os | |
24 > EOF | |
25 | |
26 $ cat > testpackage/stdafterlocal.py << EOF | |
27 > from __future__ import absolute_import | |
28 > from . import unsorted | |
29 > import os | |
30 > EOF | |
31 | |
32 $ cat > testpackage/requirerelative.py << EOF | |
33 > from __future__ import absolute_import | |
34 > import testpackage.unsorted | |
35 > EOF | |
36 | |
37 $ cat > testpackage/importalias.py << EOF | |
38 > from __future__ import absolute_import | |
39 > import ui | |
40 > EOF | |
41 | |
42 $ cat > testpackage/relativestdlib.py << EOF | |
43 > from __future__ import absolute_import | |
44 > from .. import os | |
45 > EOF | |
46 | |
47 $ cat > testpackage/symbolimport.py << EOF | |
48 > from __future__ import absolute_import | |
49 > from .unsorted import foo | |
50 > EOF | |
51 | |
52 $ cat > testpackage/latesymbolimport.py << EOF | |
53 > from __future__ import absolute_import | |
54 > from . import unsorted | |
55 > from mercurial.node import hex | |
56 > EOF | |
57 | |
58 $ cat > testpackage/multiplegroups.py << EOF | |
59 > from __future__ import absolute_import | |
60 > from . import unsorted | |
61 > from . import more | |
62 > EOF | |
63 | |
64 $ mkdir testpackage/subpackage | |
65 $ cat > testpackage/subpackage/levelpriority.py << EOF | |
66 > from __future__ import absolute_import | |
67 > from . import foo | |
68 > from .. import parent | |
69 > EOF | |
70 | |
71 $ cat > testpackage/sortedentries.py << EOF | |
72 > from __future__ import absolute_import | |
73 > from . import ( | |
74 > foo, | |
75 > bar, | |
76 > ) | |
77 > EOF | |
78 | |
79 $ cat > testpackage/importfromalias.py << EOF | |
80 > from __future__ import absolute_import | |
81 > from . import ui | |
82 > EOF | |
83 | |
84 $ cat > testpackage/importfromrelative.py << EOF | |
85 > from __future__ import absolute_import | |
86 > from testpackage.unsorted import foo | |
87 > EOF | |
88 | |
89 $ python "$import_checker" testpackage/*.py testpackage/subpackage/*.py | |
90 testpackage/importalias.py ui module must be "as" aliased to uimod | |
91 testpackage/importfromalias.py ui from testpackage must be "as" aliased to uimod | |
92 testpackage/importfromrelative.py import should be relative: testpackage.unsorted | |
93 testpackage/importfromrelative.py direct symbol import from testpackage.unsorted | |
94 testpackage/latesymbolimport.py symbol import follows non-symbol import: mercurial.node | |
95 testpackage/multiple.py multiple imported names: os, sys | |
96 testpackage/multiplegroups.py multiple "from . import" statements | |
97 testpackage/relativestdlib.py relative import of stdlib module | |
98 testpackage/requirerelative.py import should be relative: testpackage.unsorted | |
99 testpackage/sortedentries.py imports from testpackage not lexically sorted: bar < foo | |
100 testpackage/stdafterlocal.py stdlib import follows local import: os | |
101 testpackage/subpackage/levelpriority.py higher-level import should come first: testpackage | |
102 testpackage/symbolimport.py direct symbol import from testpackage.unsorted | |
103 testpackage/unsorted.py imports not lexically sorted: os < sys | |
10 | 104 |
11 $ cd "$TESTDIR"/.. | 105 $ cd "$TESTDIR"/.. |
12 | 106 |
13 There are a handful of cases here that require renaming a module so it | 107 There are a handful of cases here that require renaming a module so it |
14 doesn't overlap with a stdlib module name. There are also some cycles | 108 doesn't overlap with a stdlib module name. There are also some cycles |