예제 #1
0
파일: teststubgen.py 프로젝트: o11c/mypy
def test_stubgen(testcase):
    if 'stubgen-test-path' not in sys.path:
        sys.path.insert(0, 'stubgen-test-path')
    os.mkdir('stubgen-test-path')
    source = '\n'.join(testcase.input)
    handle = tempfile.NamedTemporaryFile(prefix='prog_', suffix='.py', dir='stubgen-test-path')
    assert os.path.isabs(handle.name)
    path = os.path.basename(handle.name)
    name = path[:-3]
    path = os.path.join('stubgen-test-path', path)
    out_dir = '_out'
    os.mkdir(out_dir)
    try:
        with open(path, 'w') as file:
            file.write(source)
            file.close()
            # Without this we may sometimes be unable to import the module below, as importlib
            # caches os.listdir() results in Python 3.3+ (Guido explained this to me).
            reset_importlib_caches()
            try:
                if testcase.name.endswith('_import'):
                    generate_stub_for_module(name, out_dir, quiet=True)
                else:
                    generate_stub(path, out_dir)
                a = load_output(out_dir)
            except CompileError as e:
                a = e.messages
            assert_string_arrays_equal(testcase.output, a,
                                       'Invalid output ({}, line {})'.format(
                                           testcase.file, testcase.line))
    finally:
        shutil.rmtree(out_dir)
        handle.close()
예제 #2
0
def test_stubgen(testcase):
    source = '\n'.join(testcase.input)
    name = 'prog%d' % random.randrange(1000 * 1000 * 1000)
    path = '%s.py' % name
    out_dir = '_out'
    os.mkdir(out_dir)
    try:
        with open(path, 'w') as file:
            file.write(source)
            file.close()
            # Without this we may sometimes be unable to import the module below, as importlib
            # caches os.listdir() results in Python 3.3+ (Guido explained this to me).
            reset_importlib_caches()
            try:
                if testcase.name.endswith('_import'):
                    generate_stub_for_module(name, out_dir, quiet=True)
                else:
                    generate_stub(path, out_dir)
                a = load_output(out_dir)
            except CompileError as e:
                a = e.messages
            assert_string_arrays_equal(testcase.output, a,
                                       'Invalid output ({}, line {})'.format(
                                           testcase.file, testcase.line))
    finally:
        shutil.rmtree(out_dir)
        os.remove(path)
예제 #3
0
def test_stubgen(testcase: DataDrivenTestCase) -> None:
    if 'stubgen-test-path' not in sys.path:
        sys.path.insert(0, 'stubgen-test-path')
    os.mkdir('stubgen-test-path')
    source = '\n'.join(testcase.input)
    options = parse_flags(source)
    handle = tempfile.NamedTemporaryFile(prefix='prog_',
                                         suffix='.py',
                                         dir='stubgen-test-path',
                                         delete=False)
    assert os.path.isabs(handle.name)
    path = os.path.basename(handle.name)
    name = path[:-3]
    path = os.path.join('stubgen-test-path', path)
    out_dir = '_out'
    os.mkdir(out_dir)
    try:
        handle.write(bytes(source, 'ascii'))
        handle.close()
        # Without this we may sometimes be unable to import the module below, as importlib
        # caches os.listdir() results in Python 3.3+ (Guido explained this to me).
        reset_importlib_caches()
        try:
            if testcase.name.endswith('_import'):
                generate_stub_for_module(
                    name,
                    out_dir,
                    quiet=True,
                    no_import=options.no_import,
                    include_private=options.include_private)
            else:
                generate_stub(path,
                              out_dir,
                              include_private=options.include_private)
            a = load_output(out_dir)
        except CompileError as e:
            a = e.messages
        assert_string_arrays_equal(
            testcase.output, a,
            'Invalid output ({}, line {})'.format(testcase.file,
                                                  testcase.line))
    finally:
        handle.close()
        os.unlink(handle.name)
        shutil.rmtree(out_dir)
예제 #4
0
파일: teststubgen.py 프로젝트: sixolet/mypy
def test_stubgen(testcase: DataDrivenTestCase) -> None:
    if 'stubgen-test-path' not in sys.path:
        sys.path.insert(0, 'stubgen-test-path')
    os.mkdir('stubgen-test-path')
    source = '\n'.join(testcase.input)
    options = parse_flags(source)
    handle = tempfile.NamedTemporaryFile(prefix='prog_', suffix='.py', dir='stubgen-test-path',
                                         delete=False)
    assert os.path.isabs(handle.name)
    path = os.path.basename(handle.name)
    name = path[:-3]
    path = os.path.join('stubgen-test-path', path)
    out_dir = '_out'
    os.mkdir(out_dir)
    try:
        handle.write(bytes(source, 'ascii'))
        handle.close()
        # Without this we may sometimes be unable to import the module below, as importlib
        # caches os.listdir() results in Python 3.3+ (Guido explained this to me).
        reset_importlib_cache('stubgen-test-path')
        try:
            if testcase.name.endswith('_import'):
                generate_stub_for_module(name, out_dir, quiet=True,
                                         no_import=options.no_import,
                                         include_private=options.include_private)
            else:
                generate_stub(path, out_dir, include_private=options.include_private)
            a = load_output(out_dir)
        except CompileError as e:
            a = e.messages
        assert_string_arrays_equal(testcase.output, a,
                                   'Invalid output ({}, line {})'.format(
                                       testcase.file, testcase.line))
    finally:
        handle.close()
        os.unlink(handle.name)
        shutil.rmtree(out_dir)
예제 #5
0
def test_stubgen(testcase):
    if 'stubgen-test-path' not in sys.path:
        sys.path.insert(0, 'stubgen-test-path')
    os.mkdir('stubgen-test-path')
    source = '\n'.join(testcase.input)
    handle = tempfile.NamedTemporaryFile(prefix='prog_',
                                         suffix='.py',
                                         dir='stubgen-test-path')
    assert os.path.isabs(handle.name)
    path = os.path.basename(handle.name)
    name = path[:-3]
    path = os.path.join('stubgen-test-path', path)
    out_dir = '_out'
    os.mkdir(out_dir)
    try:
        with open(path, 'w') as file:
            file.write(source)
            file.close()
            # Without this we may sometimes be unable to import the module below, as importlib
            # caches os.listdir() results in Python 3.3+ (Guido explained this to me).
            reset_importlib_caches()
            try:
                if testcase.name.endswith('_import'):
                    generate_stub_for_module(name, out_dir, quiet=True)
                else:
                    generate_stub(path, out_dir)
                a = load_output(out_dir)
            except CompileError as e:
                a = e.messages
            assert_string_arrays_equal(
                testcase.output, a,
                'Invalid output ({}, line {})'.format(testcase.file,
                                                      testcase.line))
    finally:
        shutil.rmtree(out_dir)
        handle.close()