Debian Codegraph @

tests/test-imports-checker.t

raw
1 #require test-repo
2
3 $ . "$TESTDIR/helpers-testrepo.sh"
4 $ testrepohgenv
5 $ import_checker="$TESTDIR"/../contrib/import-checker.py
6
7 Run the doctests from the import checker, and make sure
8 it's working correctly.
9 $ TERM=dumb
10 $ export TERM
11 $ "$PYTHON" -m doctest $import_checker
12
13 Run additional tests for the import checker
14
15 $ mkdir testpackage
16 $ touch testpackage/__init__.py
17
18 $ cat > testpackage/multiple.py << EOF
19 > import os, sys
20 > EOF
21
22 $ cat > testpackage/unsorted.py << EOF
23 > import sys
24 > import os
25 > EOF
26
27 $ cat > testpackage/stdafterlocal.py << EOF
28 > from . import unsorted
29 > import os
30 > EOF
31
32 $ cat > testpackage/requirerelative.py << EOF
33 > import testpackage.unsorted
34 > EOF
35
36 $ cat > testpackage/importalias.py << EOF
37 > import ui
38 > EOF
39
40 $ cat > testpackage/relativestdlib.py << EOF
41 > from .. import os
42 > EOF
43
44 $ cat > testpackage/stdlibfrom.py << EOF
45 > from collections import abc
46 > EOF
47
48 $ cat > testpackage/symbolimport.py << EOF
49 > from .unsorted import foo
50 > EOF
51
52 $ cat > testpackage/latesymbolimport.py << EOF
53 > from . import unsorted
54 > from mercurial.node import hex
55 > EOF
56
57 $ cat > testpackage/multiplegroups.py << EOF
58 > from . import unsorted
59 > from . import more
60 > EOF
61
62 $ mkdir testpackage/subpackage
63 $ cat > testpackage/subpackage/levelpriority.py << EOF
64 > from . import foo
65 > from .. import parent
66 > EOF
67
68 $ touch testpackage/subpackage/foo.py
69 $ cat > testpackage/subpackage/__init__.py << EOF
70 > from . import levelpriority # should not cause cycle
71 > EOF
72
73 $ cat > testpackage/subpackage/localimport.py << EOF
74 > from . import foo
75 > def bar():
76 > # should not cause "higher-level import should come first"
77 > from .. import unsorted
78 > # but other errors should be detected
79 > from .. import more
80 > import testpackage.subpackage.levelpriority
81 > EOF
82
83 $ cat > testpackage/importmodulefromsub.py << EOF
84 > from .subpackage import foo # not a "direct symbol import"
85 > EOF
86
87 $ cat > testpackage/importsymbolfromsub.py << EOF
88 > from .subpackage import foo, nonmodule
89 > EOF
90
91 $ cat > testpackage/sortedentries.py << EOF
92 > from . import (
93 > foo,
94 > bar,
95 > )
96 > EOF
97
98 $ cat > testpackage/importfromalias.py << EOF
99 > from . import ui
100 > EOF
101
102 $ cat > testpackage/importfromrelative.py << EOF
103 > from testpackage.unsorted import foo
104 > EOF
105
106 $ mkdir testpackage2
107 $ touch testpackage2/__init__.py
108
109 $ cat > testpackage2/latesymbolimport.py << EOF
110 > from testpackage import unsorted
111 > from mercurial.node import hex
112 > EOF
113
114 # Shadowing a stdlib module to test "relative import of stdlib module" is
115 # allowed if the module is also being checked
116
117 $ mkdir email
118 $ touch email/__init__.py
119 $ touch email/errors.py
120 $ cat > email/utils.py << EOF
121 > from . import errors
122 > EOF
123
124 $ "$PYTHON" "$import_checker" testpackage*/*.py testpackage/subpackage/*.py \
125 > email/*.py
126 testpackage/importalias.py:1: ui module must be "as" aliased to uimod
127 testpackage/importfromalias.py:1: ui from testpackage must be "as" aliased to uimod
128 testpackage/importfromrelative.py:1: import should be relative: testpackage.unsorted
129 testpackage/importfromrelative.py:1: direct symbol import foo from testpackage.unsorted
130 testpackage/importsymbolfromsub.py:1: direct symbol import nonmodule from testpackage.subpackage
131 testpackage/latesymbolimport.py:2: symbol import follows non-symbol import: mercurial.node
132 testpackage/multiple.py:1: multiple imported names: os, sys
133 testpackage/multiplegroups.py:2: multiple "from . import" statements
134 testpackage/relativestdlib.py:1: relative import of stdlib module
135 testpackage/requirerelative.py:1: import should be relative: testpackage.unsorted
136 testpackage/sortedentries.py:1: imports from testpackage not lexically sorted: bar < foo
137 testpackage/stdafterlocal.py:2: stdlib import "os" follows local import: testpackage
138 testpackage/stdlibfrom.py:1: direct symbol import abc from collections
139 testpackage/subpackage/levelpriority.py:2: higher-level import should come first: testpackage
140 testpackage/subpackage/localimport.py:6: multiple "from .. import" statements
141 testpackage/subpackage/localimport.py:7: import should be relative: testpackage.subpackage.levelpriority
142 testpackage/symbolimport.py:1: direct symbol import foo from testpackage.unsorted
143 testpackage/unsorted.py:2: imports not lexically sorted: os < sys
144 testpackage2/latesymbolimport.py:2: symbol import follows non-symbol import: mercurial.node
145 [1]