Skip to content

Commit

Permalink
fix: Review threads and format with black test modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Artanias committed Aug 19, 2023
1 parent 8d8821c commit 271fd04
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 134 deletions.
16 changes: 11 additions & 5 deletions docker.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ docker-test: docker-test-image
"$(TEST_DOCKER_TAG)"

docker-autotest: docker-test-image
docker run --rm \
--volume $(PWD)/test:/usr/src/$(UTIL_NAME)/test \
--env-file .env \
"$(TEST_DOCKER_TAG)" bash -c \
"make && make autotest"
@if [ $(shell find . -maxdepth 1 -type f -name .env | wc --lines) != 1 ]; then \
echo "Requires '.env' file with provided GitHub token for running autotests."; \
exit 200; \
else \
docker run --rm \
--volume $(PWD)/test:/usr/src/$(UTIL_NAME)/test \
--env-file .env \
"$(TEST_DOCKER_TAG)" bash -c \
"make && make autotest"; \
fi

docker-build-package: docker-test-image
docker run --rm \
Expand All @@ -50,6 +55,7 @@ docker-image: docker-base-image docker-test-image
)

docker-run: docker-image
@touch .env
docker run --rm --tty --interactive \
--env-file .env \
"$(DOCKER_TAG)"
Expand Down
3 changes: 3 additions & 0 deletions src/codeplag/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,13 @@ class CompareInfo(NamedTuple):
structure: Optional[StructuresInfo] = None


# TODO: Rework it structure
class WorksReport(TypedDict):
date: str
first_path: str
second_path: str
first_modify_date: NotRequired[str]
second_modify_date: NotRequired[str]
first_heads: List[str]
second_heads: List[str]
fast: Dict[str, int] # dict from FastMetrics
Expand Down
4 changes: 4 additions & 0 deletions src/codeplag/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ def save_result(self,
fast=fast_metrics._asdict(),
structure=struct_info_dict
)
if first_work.modify_date:
report["first_modify_date"] = first_work.modify_date
if second_work.modify_date:
report["second_modify_date"] = second_work.modify_date

try:
report_file = self.reports / f'{uuid.uuid4().hex}.json'
Expand Down
34 changes: 20 additions & 14 deletions src/webparsers/async_github_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,28 @@

_GH_URL: Final[str] = 'https://github.com/'

"""
import asyncio
async def test():
async with aiohttp.ClientSession() as session:
gh_parser = AsyncGithubParser(session, token=<token>)
tasks = []
for _ in range(3):
tasks.append(asyncio.create_task(gh_parser.<some_func_call>))
async for ... in gh_parser.<some_aysnc_gen_func_call>:
...
return await asyncio.gather(*tasks)
asyncio.run(test())
"""


class AsyncGithubParser:
"""Asynchronous parser which works with GitHub REST API.
Example:
>>> import asyncio
>>> async def request():
... async with aiohttp.ClientSession() as session:
... gh_parser = AsyncGithubParser(session, token=<token>)
... tasks = []
... for _ in range(3):
... tasks.append(asyncio.create_task(gh_parser.<some_func_call>))
... return await asyncio.gather(*tasks)
...
>>> asyncio.run(test())
>>> async def loop():
... async for ... in gh_parser.<some_aysnc_gen_func_call>:
... ...
...
>>> asynio.run(loop())
"""

USER_INFO = '/users/{username}'
USER_REPOS = '/users/{username}/repos{?per_page,page}'

Expand Down
126 changes: 60 additions & 66 deletions test/auto/functional/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,74 +11,65 @@
from codeplag.types import WorksReport

CPP_FILES = [
'test/unit/codeplag/cplag/data/sample1.cpp',
'test/unit/codeplag/cplag/data/sample2.cpp'
"test/unit/codeplag/cplag/data/sample1.cpp",
"test/unit/codeplag/cplag/data/sample2.cpp",
]
PY_FILES = [
'src/codeplag/pyplag/astwalkers.py',
'setup.py'
]
CPP_DIR = 'test/unit/codeplag/cplag/data'
PY_DIRS = [
'test/unit/codeplag/cplag',
'test/unit'
]
REPO_URL = 'https://github.com/OSLL/code-plagiarism'
REPORTS_FOLDER = os.path.abspath('./reports')
PY_FILES = ["src/codeplag/pyplag/astwalkers.py", "setup.py"]
CPP_DIR = "test/unit/codeplag/cplag/data"
PY_DIRS = ["test/unit/codeplag/cplag", "test/unit"]
REPO_URL = "https://github.com/OSLL/code-plagiarism"
REPORTS_FOLDER = os.path.abspath("./reports")
CPP_GITHUB_FILES = [
f'{REPO_URL}/blob/main/test/unit/codeplag/cplag/data/sample3.cpp',
f'{REPO_URL}/blob/main/test/unit/codeplag/cplag/data/sample4.cpp'
f"{REPO_URL}/blob/main/test/unit/codeplag/cplag/data/sample3.cpp",
f"{REPO_URL}/blob/main/test/unit/codeplag/cplag/data/sample4.cpp",
]
PY_GITHUB_FILES = [
f'{REPO_URL}/blob/main/src/codeplag/pyplag/utils.py',
f'{REPO_URL}/blob/main/setup.py'
f"{REPO_URL}/blob/main/src/codeplag/pyplag/utils.py",
f"{REPO_URL}/blob/main/setup.py",
]
CPP_GITHUB_DIR = f'{REPO_URL}/tree/main/test'
PY_GITHUB_DIR = f'{REPO_URL}/blob/main/src/codeplag/pyplag'
CPP_GITHUB_DIR = f"{REPO_URL}/tree/main/test"
PY_GITHUB_DIR = f"{REPO_URL}/blob/main/src/codeplag/pyplag"


@pytest.fixture(scope='module', autouse=True)
@pytest.fixture(scope="module", autouse=True)
def setup_module():
first_cond = modify_settings(environment=".env").returncode == 0
second_cond = os.environ.get('ACCESS_TOKEN', '') != ''
second_cond = os.environ.get("ACCESS_TOKEN", "") != ""

assert first_cond or second_cond


def test_check_util_version():
result = run_util(['--version'])
result = run_util(["--version"])

assert result.returncode == SUCCESS_CODE
assert f'{UTIL_NAME} {UTIL_VERSION}' in result.stdout.decode('utf-8')
assert f"{UTIL_NAME} {UTIL_VERSION}" in result.stdout.decode("utf-8")


@pytest.mark.parametrize(
"cmd, out",
[
(['--files', *CPP_FILES], b'Getting works features from files'),
(["--files", *CPP_FILES], b"Getting works features from files"),
(
['--directories', CPP_DIR],
f'Getting works features from {CPP_DIR}'.encode('utf-8')
["--directories", CPP_DIR],
f"Getting works features from {CPP_DIR}".encode("utf-8"),
),
(
['--github-files', *CPP_GITHUB_FILES],
b'Getting works features from GitHub urls'
["--github-files", *CPP_GITHUB_FILES],
b"Getting works features from GitHub urls",
),
(
['--github-project-folders', CPP_GITHUB_DIR],
f'Getting works features from {CPP_GITHUB_DIR}'.encode('utf-8')
["--github-project-folders", CPP_GITHUB_DIR],
f"Getting works features from {CPP_GITHUB_DIR}".encode("utf-8"),
),
(
['--github-user', 'OSLL', '--repo-regexp', 'code-plag'],
f'Getting works features from {REPO_URL}'.encode('utf-8')
)
]
["--github-user", "OSLL", "--repo-regexp", "code-plag"],
f"Getting works features from {REPO_URL}".encode("utf-8"),
),
],
)
def test_compare_cpp_files(cmd, out):
result = run_check(
cmd,
extension='cpp'
)
result = run_check(cmd, extension="cpp")

assert result.returncode == SUCCESS_CODE
assert out in result.stdout
Expand All @@ -87,31 +78,28 @@ def test_compare_cpp_files(cmd, out):
@pytest.mark.parametrize(
"cmd, out",
[
(["--files", *PY_FILES], b"Getting works features from files"),
(
['--files', *PY_FILES],
b'Getting works features from files'
["--directories", *PY_DIRS],
f"Getting works features from {PY_DIRS[0]}".encode("utf-8"),
),
(
['--directories', *PY_DIRS],
f'Getting works features from {PY_DIRS[0]}'.encode('utf-8')
["--github-files", *PY_GITHUB_FILES],
b"Getting works features from GitHub urls",
),
(
['--github-files', *PY_GITHUB_FILES],
b'Getting works features from GitHub urls'
["--github-project-folders", PY_GITHUB_DIR],
f"Getting works features from {PY_GITHUB_DIR}".encode("utf-8"),
),
(
['--github-project-folders', PY_GITHUB_DIR],
f'Getting works features from {PY_GITHUB_DIR}'.encode('utf-8')
["--github-user", "OSLL", "--repo-regexp", "code-plag"],
f"Getting works features from {REPO_URL}".encode("utf-8"),
),
(
['--github-user', 'OSLL', '--repo-regexp', 'code-plag'],
f'Getting works features from {REPO_URL}'.encode('utf-8')
["--directories", *PY_DIRS, "--mode", "one_to_one"],
f"Getting works features from {PY_DIRS[0]}".encode("utf-8"),
),
(
['--directories', *PY_DIRS, '--mode', 'one_to_one'],
f'Getting works features from {PY_DIRS[0]}'.encode('utf-8'),
)
]
],
)
def test_compare_py_files(cmd, out):
result = run_check(cmd)
Expand All @@ -126,24 +114,30 @@ def test_save_reports():
assert os.path.exists(REPORTS_FOLDER)

assert modify_settings(REPORTS_FOLDER).returncode == 0
assert run_check(
[
'--directories',
'./test/auto',
'--files',
'./test/auto/utils.py',
'./test/auto/test_bugs.py'
]
).returncode == 0
assert (
run_check(
[
"--directories",
"./test/auto",
"--files",
"./test/auto/utils.py",
"./test/auto/test_bugs.py",
]
).returncode
== 0
)
reports_files = os.listdir(REPORTS_FOLDER)

assert len(reports_files) > 0
for file in reports_files:
assert re.search('.*[.]json$', file)
filepath = f'{REPORTS_FOLDER}/{file}'
with open(filepath, 'r') as f:
assert re.search(".*[.]json$", file)
filepath = f"{REPORTS_FOLDER}/{file}"
with open(filepath, "r") as f:
report = json.loads(f.read())
for key in WorksReport.__annotations__.keys():
for key in set(
WorksReport.__annotations__.keys()
- ["first_modify_date", "second_modify_date"]
):
assert key in report

shutil.rmtree(REPORTS_FOLDER)
Loading

0 comments on commit 271fd04

Please sign in to comment.