tests/test-check-module-imports.t
changeset 31451 d3a56bb268ee
parent 30444 b86a448a2965
child 33128 6c113a7dec52
equal deleted inserted replaced
31450:80c8a6db450d 31451:d3a56bb268ee
     1 #require test-repo
     1 #require test-repo
     2 
     2 
     3   $ . "$TESTDIR/helpers-testrepo.sh"
     3   $ . "$TESTDIR/helpers-testrepo.sh"
     4   $ import_checker="$TESTDIR"/../contrib/import-checker.py
     4   $ import_checker="$TESTDIR"/../contrib/import-checker.py
     5 
       
     6 Run the doctests from the import checker, and make sure
       
     7 it's working correctly.
       
     8   $ TERM=dumb
       
     9   $ export TERM
       
    10   $ python -m doctest $import_checker
       
    11 
       
    12 Run additional tests for the import checker
       
    13 
       
    14   $ mkdir testpackage
       
    15   $ touch testpackage/__init__.py
       
    16 
       
    17   $ cat > testpackage/multiple.py << EOF
       
    18   > from __future__ import absolute_import
       
    19   > import os, sys
       
    20   > EOF
       
    21 
       
    22   $ cat > testpackage/unsorted.py << EOF
       
    23   > from __future__ import absolute_import
       
    24   > import sys
       
    25   > import os
       
    26   > EOF
       
    27 
       
    28   $ cat > testpackage/stdafterlocal.py << EOF
       
    29   > from __future__ import absolute_import
       
    30   > from . import unsorted
       
    31   > import os
       
    32   > EOF
       
    33 
       
    34   $ cat > testpackage/requirerelative.py << EOF
       
    35   > from __future__ import absolute_import
       
    36   > import testpackage.unsorted
       
    37   > EOF
       
    38 
       
    39   $ cat > testpackage/importalias.py << EOF
       
    40   > from __future__ import absolute_import
       
    41   > import ui
       
    42   > EOF
       
    43 
       
    44   $ cat > testpackage/relativestdlib.py << EOF
       
    45   > from __future__ import absolute_import
       
    46   > from .. import os
       
    47   > EOF
       
    48 
       
    49   $ cat > testpackage/symbolimport.py << EOF
       
    50   > from __future__ import absolute_import
       
    51   > from .unsorted import foo
       
    52   > EOF
       
    53 
       
    54   $ cat > testpackage/latesymbolimport.py << EOF
       
    55   > from __future__ import absolute_import
       
    56   > from . import unsorted
       
    57   > from mercurial.node import hex
       
    58   > EOF
       
    59 
       
    60   $ cat > testpackage/multiplegroups.py << EOF
       
    61   > from __future__ import absolute_import
       
    62   > from . import unsorted
       
    63   > from . import more
       
    64   > EOF
       
    65 
       
    66   $ mkdir testpackage/subpackage
       
    67   $ cat > testpackage/subpackage/levelpriority.py << EOF
       
    68   > from __future__ import absolute_import
       
    69   > from . import foo
       
    70   > from .. import parent
       
    71   > EOF
       
    72 
       
    73   $ touch testpackage/subpackage/foo.py
       
    74   $ cat > testpackage/subpackage/__init__.py << EOF
       
    75   > from __future__ import absolute_import
       
    76   > from . import levelpriority  # should not cause cycle
       
    77   > EOF
       
    78 
       
    79   $ cat > testpackage/subpackage/localimport.py << EOF
       
    80   > from __future__ import absolute_import
       
    81   > from . import foo
       
    82   > def bar():
       
    83   >     # should not cause "higher-level import should come first"
       
    84   >     from .. import unsorted
       
    85   >     # but other errors should be detected
       
    86   >     from .. import more
       
    87   >     import testpackage.subpackage.levelpriority
       
    88   > EOF
       
    89 
       
    90   $ cat > testpackage/importmodulefromsub.py << EOF
       
    91   > from __future__ import absolute_import
       
    92   > from .subpackage import foo  # not a "direct symbol import"
       
    93   > EOF
       
    94 
       
    95   $ cat > testpackage/importsymbolfromsub.py << EOF
       
    96   > from __future__ import absolute_import
       
    97   > from .subpackage import foo, nonmodule
       
    98   > EOF
       
    99 
       
   100   $ cat > testpackage/sortedentries.py << EOF
       
   101   > from __future__ import absolute_import
       
   102   > from . import (
       
   103   >     foo,
       
   104   >     bar,
       
   105   > )
       
   106   > EOF
       
   107 
       
   108   $ cat > testpackage/importfromalias.py << EOF
       
   109   > from __future__ import absolute_import
       
   110   > from . import ui
       
   111   > EOF
       
   112 
       
   113   $ cat > testpackage/importfromrelative.py << EOF
       
   114   > from __future__ import absolute_import
       
   115   > from testpackage.unsorted import foo
       
   116   > EOF
       
   117 
       
   118   $ mkdir testpackage2
       
   119   $ touch testpackage2/__init__.py
       
   120 
       
   121   $ cat > testpackage2/latesymbolimport.py << EOF
       
   122   > from __future__ import absolute_import
       
   123   > from testpackage import unsorted
       
   124   > from mercurial.node import hex
       
   125   > EOF
       
   126 
       
   127   $ python "$import_checker" testpackage*/*.py testpackage/subpackage/*.py
       
   128   testpackage/importalias.py:2: ui module must be "as" aliased to uimod
       
   129   testpackage/importfromalias.py:2: ui from testpackage must be "as" aliased to uimod
       
   130   testpackage/importfromrelative.py:2: import should be relative: testpackage.unsorted
       
   131   testpackage/importfromrelative.py:2: direct symbol import foo from testpackage.unsorted
       
   132   testpackage/importsymbolfromsub.py:2: direct symbol import nonmodule from testpackage.subpackage
       
   133   testpackage/latesymbolimport.py:3: symbol import follows non-symbol import: mercurial.node
       
   134   testpackage/multiple.py:2: multiple imported names: os, sys
       
   135   testpackage/multiplegroups.py:3: multiple "from . import" statements
       
   136   testpackage/relativestdlib.py:2: relative import of stdlib module
       
   137   testpackage/requirerelative.py:2: import should be relative: testpackage.unsorted
       
   138   testpackage/sortedentries.py:2: imports from testpackage not lexically sorted: bar < foo
       
   139   testpackage/stdafterlocal.py:3: stdlib import "os" follows local import: testpackage
       
   140   testpackage/subpackage/levelpriority.py:3: higher-level import should come first: testpackage
       
   141   testpackage/subpackage/localimport.py:7: multiple "from .. import" statements
       
   142   testpackage/subpackage/localimport.py:8: import should be relative: testpackage.subpackage.levelpriority
       
   143   testpackage/symbolimport.py:2: direct symbol import foo from testpackage.unsorted
       
   144   testpackage/unsorted.py:3: imports not lexically sorted: os < sys
       
   145   testpackage2/latesymbolimport.py:3: symbol import follows non-symbol import: mercurial.node
       
   146   [1]
       
   147 
     5 
   148   $ cd "$TESTDIR"/..
     6   $ cd "$TESTDIR"/..
   149 
     7 
   150 There are a handful of cases here that require renaming a module so it
     8 There are a handful of cases here that require renaming a module so it
   151 doesn't overlap with a stdlib module name. There are also some cycles
     9 doesn't overlap with a stdlib module name. There are also some cycles
   169   > -X tests/test-ctxmanager.py \
    27   > -X tests/test-ctxmanager.py \
   170   > -X tests/test-lock.py \
    28   > -X tests/test-lock.py \
   171   > -X tests/test-verify-repo-operations.py \
    29   > -X tests/test-verify-repo-operations.py \
   172   > -X tests/test-hook.t \
    30   > -X tests/test-hook.t \
   173   > -X tests/test-import.t \
    31   > -X tests/test-import.t \
   174   > -X tests/test-check-module-imports.t \
    32   > -X tests/test-imports-checker.t \
   175   > -X tests/test-commit-interactive.t \
    33   > -X tests/test-commit-interactive.t \
   176   > -X tests/test-contrib-check-code.t \
    34   > -X tests/test-contrib-check-code.t \
   177   > -X tests/test-extension.t \
    35   > -X tests/test-extension.t \
   178   > -X tests/test-hghave.t \
    36   > -X tests/test-hghave.t \
   179   > -X tests/test-hgweb-no-path-info.t \
    37   > -X tests/test-hgweb-no-path-info.t \