Beispiel #1
0
def test_import_wildcard():
  io = ImportOrder(PythonFile.from_statement("""
    from twitter.common.dirutil import *
  """))
  nits = list(io.nits())
  assert len(nits) == 1
  assert nits[0].code == 'T400'
  assert nits[0].severity == Nit.ERROR
Beispiel #2
0
def test_import_lexical_order():
  io = ImportOrder(PythonFile.from_statement("""
    from twitter.common.dirutil import safe_rmtree, safe_mkdtemp
  """))
  nits = list(io.nits())
  assert len(nits) == 1
  assert nits[0].code == 'T401'
  assert nits[0].severity == Nit.ERROR
Beispiel #3
0
def test_classify_import():
    for import_type, chunk in IMPORT_CHUNKS.items():
        io = ImportOrder(PythonFile(chunk))
        import_chunks = list(io.iter_import_chunks())
        assert len(import_chunks) == 1
        module_types, chunk_errors = io.classify_imports(import_chunks[0])
        assert len(module_types) == 1
        assert module_types.pop() == import_type
        assert chunk_errors == []
Beispiel #4
0
def test_classify_import():
  for import_type, chunk in IMPORT_CHUNKS.items():
    io = ImportOrder(PythonFile(chunk))
    import_chunks = list(io.iter_import_chunks())
    assert len(import_chunks) == 1
    module_types, chunk_errors = io.classify_imports(import_chunks[0])
    assert len(module_types) == 1
    assert module_types.pop() == import_type
    assert chunk_errors == []
Beispiel #5
0
def test_import_wildcard():
    io = ImportOrder(
        PythonFile.from_statement(
            """
    from twitter.common.dirutil import *
  """
        )
    )
    nits = list(io.nits())
    assert len(nits) == 1
    assert nits[0].code == "T400"
    assert nits[0].severity == Nit.ERROR
Beispiel #6
0
def test_import_lexical_order():
    io = ImportOrder(
        PythonFile.from_statement(
            """
    from twitter.common.dirutil import safe_rmtree, safe_mkdtemp
  """
        )
    )
    nits = list(io.nits())
    assert len(nits) == 1
    assert nits[0].code == "T401"
    assert nits[0].severity == Nit.ERROR
Beispiel #7
0
def test_multiple_imports_error():
  io = ImportOrder(PythonFile(stitch_chunks(0, ImportType.STDLIB, ImportType.TWITTER)))
  import_chunks = list(io.iter_import_chunks())
  assert len(import_chunks) == 1

  module_types, chunk_errors = io.classify_imports(import_chunks[0])
  assert len(chunk_errors) == 1
  assert chunk_errors[0].code == 'T405'
  assert chunk_errors[0].severity == Nit.ERROR
  assert set(module_types) == set([ImportType.STDLIB, ImportType.TWITTER])

  io = ImportOrder(PythonFile('import io, pkg_resources'))
  import_chunks = list(io.iter_import_chunks())
  assert len(import_chunks) == 1
  module_types, chunk_errors = io.classify_imports(import_chunks[0])
  assert len(chunk_errors) == 3
  assert set(chunk_error.code for chunk_error in chunk_errors) == set(['T403', 'T405', 'T402'])
  assert set(module_types) == set([ImportType.STDLIB, ImportType.THIRD_PARTY])
Beispiel #8
0
def test_pairwise_classify():
  for first, second in PAIRS:
    io = ImportOrder(PythonFile(stitch_chunks(1, first, second)))
    import_chunks = list(io.iter_import_chunks())
    assert len(import_chunks) == 2

    module_types, chunk_errors = io.classify_imports(import_chunks[0])
    assert len(module_types) == 1
    assert len(chunk_errors) == 0
    assert module_types.pop() == first

    module_types, chunk_errors = io.classify_imports(import_chunks[1])
    assert len(module_types) == 1
    assert len(chunk_errors) == 0
    assert module_types.pop() == second

  for second, first in PAIRS:
    io = ImportOrder(PythonFile(stitch_chunks(1, first, second)))
    import_chunks = list(io.iter_import_chunks())
    assert len(import_chunks) == 2
    nits = list(io.nits())
    assert len(nits) == 1
    assert nits[0].code == 'T406'
    assert nits[0].severity == Nit.ERROR
Beispiel #9
0
def test_multiple_imports_error():
    io = ImportOrder(PythonFile(stitch_chunks(0, ImportType.STDLIB, ImportType.TWITTER)))
    import_chunks = list(io.iter_import_chunks())
    assert len(import_chunks) == 1

    module_types, chunk_errors = io.classify_imports(import_chunks[0])
    assert len(chunk_errors) == 1
    assert chunk_errors[0].code == "T405"
    assert chunk_errors[0].severity == Nit.ERROR
    assert set(module_types) == set([ImportType.STDLIB, ImportType.TWITTER])

    io = ImportOrder(PythonFile("import io, pkg_resources"))
    import_chunks = list(io.iter_import_chunks())
    assert len(import_chunks) == 1
    module_types, chunk_errors = io.classify_imports(import_chunks[0])
    assert len(chunk_errors) == 3
    assert set(chunk_error.code for chunk_error in chunk_errors) == set(["T403", "T405", "T402"])
    assert set(module_types) == set([ImportType.STDLIB, ImportType.THIRD_PARTY])
Beispiel #10
0
def test_pairwise_classify():
    for first, second in PAIRS:
        io = ImportOrder(PythonFile(stitch_chunks(1, first, second)))
        import_chunks = list(io.iter_import_chunks())
        assert len(import_chunks) == 2

        module_types, chunk_errors = io.classify_imports(import_chunks[0])
        assert len(module_types) == 1
        assert len(chunk_errors) == 0
        assert module_types.pop() == first

        module_types, chunk_errors = io.classify_imports(import_chunks[1])
        assert len(module_types) == 1
        assert len(chunk_errors) == 0
        assert module_types.pop() == second

    for second, first in PAIRS:
        io = ImportOrder(PythonFile(stitch_chunks(1, first, second)))
        import_chunks = list(io.iter_import_chunks())
        assert len(import_chunks) == 2
        nits = list(io.nits())
        assert len(nits) == 1
        assert nits[0].code == "T406"
        assert nits[0].severity == Nit.ERROR
Beispiel #11
0
def get_import_chunk_types(import_type):
  chunks = list(ImportOrder(PythonFile(IMPORT_CHUNKS[import_type])).iter_import_chunks())
  assert len(chunks) == 1
  return tuple(map(type, chunks[0]))