-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from pathlib import Path | ||
from pkgutil import iter_modules | ||
|
||
from abstra_internals.linter.linter import LinterFix, LinterIssue, LinterRule | ||
from abstra_internals.repositories.project.project import ProjectRepository | ||
from abstra_internals.settings import Settings | ||
|
||
|
||
class AddPreffix(LinterFix): | ||
preffix = "util_" | ||
|
||
def __init__(self, file: Path): | ||
self.file = file | ||
|
||
def fix(self): | ||
new_file = self.file.parent / f"{self.preffix}{self.file.name}" | ||
|
||
self.file.rename(new_file) | ||
|
||
project = ProjectRepository.load() | ||
|
||
for stage in project.workflow_stages: | ||
if stage.file_path == self.file: | ||
project.update_stage(stage, dict(file=str(new_file.name))) | ||
|
||
ProjectRepository.save(project) | ||
|
||
|
||
class ConflictingNameIssue(LinterIssue): | ||
type = "bug" | ||
fixes = [] | ||
|
||
def __init__(self, file: Path): | ||
self.label = f"The name of the file {file.name} is in conflict with an internal reserved name. This can cause unexpected behavior. You can either change it manually in the Editor or use the 'Fix conflicting name' button." | ||
self.fixes = [AddPreffix(file)] | ||
|
||
|
||
def reserved_names(): | ||
return [ | ||
m.name | ||
for m in iter_modules() | ||
if m not in list(iter_modules([str(Settings.root_path)])) | ||
] | ||
|
||
|
||
class ConflictingName(LinterRule): | ||
label = "Conflicting path" | ||
type = "bug" | ||
|
||
def find_issues(self): | ||
root = Settings.root_path | ||
project_py_files = set(root.glob("*.py")) | ||
_reserved_names = set(root / (n + ".py") for n in reserved_names()) | ||
|
||
issues = [ | ||
ConflictingNameIssue(file) | ||
for file in _reserved_names.intersection(project_py_files) | ||
] | ||
|
||
return issues |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from unittest import TestCase | ||
|
||
from abstra_internals.repositories.project.project import ProjectRepository, ScriptStage | ||
from tests.fixtures import clear_dir, init_dir | ||
|
||
from .conflicting_name import ConflictingName | ||
|
||
|
||
class ConflictingNameTest(TestCase): | ||
def setUp(self) -> None: | ||
self.root = init_dir() | ||
|
||
def tearDown(self) -> None: | ||
clear_dir(self.root) | ||
|
||
def test_conflicting_paths_valid(self): | ||
ok_file = self.root / "util_email.py" | ||
ok_file.write_text("ok content") | ||
|
||
rule = ConflictingName() | ||
self.assertEqual(len(rule.find_issues()), 0) | ||
|
||
def test_conflicting_paths_invalid(self): | ||
bad_file = self.root / "email.py" | ||
bad_file.write_text("bad content") | ||
|
||
rule = ConflictingName() | ||
self.assertEqual(len(rule.find_issues()), 1) | ||
|
||
fix = rule.find_issues()[0].fixes[0] | ||
fix.fix() | ||
|
||
self.assertEqual(len(rule.find_issues()), 0) | ||
|
||
self.assertFalse(bad_file.exists()) | ||
self.assertTrue((self.root / "util_email.py").exists()) | ||
|
||
def test_conflicting_paths_entrypoint(self): | ||
bad_file = self.root / "email.py" | ||
bad_file.write_text("bad content") | ||
|
||
project = ProjectRepository.load() | ||
script = ScriptStage.create( | ||
title="Email", | ||
file="email.py", | ||
) | ||
project.add_stage(script) | ||
ProjectRepository.save(project) | ||
|
||
rule = ConflictingName() | ||
self.assertEqual(len(rule.find_issues()), 1) | ||
|
||
fix = rule.find_issues()[0].fixes[0] | ||
fix.fix() | ||
|
||
self.assertEqual(len(rule.find_issues()), 0) | ||
|
||
self.assertFalse(bad_file.exists()) | ||
self.assertTrue((self.root / "util_email.py").exists()) | ||
|
||
new_script = ProjectRepository.load().get_script(script.id) | ||
assert new_script is not None | ||
self.assertEqual(new_script.file, "util_email.py") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.