Skip to content

Commit

Permalink
Rewrite test to use common fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
lieryan committed Jan 16, 2023
1 parent 1ac2b63 commit 31935b8
Showing 1 changed file with 16 additions and 37 deletions.
53 changes: 16 additions & 37 deletions ropetest/contrib/autoimport/autoimporttest.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,34 @@
# Special cases, easier to express in pytest
import os
from pathlib import Path
from textwrap import dedent

import pytest

from rope.base.project import Project
from rope.base.resources import Resource
from rope.base.resources import File, Folder
from rope.contrib.autoimport.sqlite import AutoImport


@pytest.fixture
def project(tmp_path: Path):
return Project(tmp_path)


@pytest.fixture
def autoimport(project: Project):
a = AutoImport(project)
yield a
a.close()


@pytest.fixture
def mod1_folder(project: Project, tmp_path: Path):
s = "mod1"
p = tmp_path / s
p.mkdir()
assert p.exists()
yield Resource(project, s)


@pytest.fixture
def mod2_file(project: Project, tmp_path: Path):
s = "mod2.py"
p = tmp_path / s
p.touch()
assert p.exists()
yield Resource(project, s)


def test_init_py(
autoimport: AutoImport, project: Project, mod1_folder: Resource, mod2_file: Resource
autoimport: AutoImport,
project: Project,
pkg1: Folder,
mod1: File,
):
s = "__init__.py"
i = mod1_folder.pathlib / s
with i.open(mode="x") as f:
f.write("def foo():\n")
f.write("\tpass\n")
with mod2_file.pathlib.open(mode="w") as f:
f.write("foo")
autoimport.generate_cache([Resource(project, os.path.join("mod1", s))])
mod1_init = pkg1.get_child("__init__.py")
mod1_init.write(dedent("""\
def foo():
pass
"""))
mod1.write(dedent("""\
foo
"""))
autoimport.generate_cache([mod1_init])
results = autoimport.search("foo", True)
print(results)
assert [("from mod1 import foo", "foo")] == results
assert [("from pkg1 import foo", "foo")] == results

0 comments on commit 31935b8

Please sign in to comment.