reformat python files with ruff

This commit is contained in:
InsanePrawn 2025-03-29 19:54:42 +01:00
parent 4cd1b0bf2e
commit 4f4d8cb479
58 changed files with 4460 additions and 2197 deletions

View file

@ -14,7 +14,7 @@ TEMPDIR_MODE = 0o755
@dataclass
class TempdirFillInfo():
class TempdirFillInfo:
path: str
files: dict[str, str]
@ -26,13 +26,13 @@ def _get_tempdir():
def remove_dir(d):
run_root_cmd(['rm', '-rf', d]).check_returncode()
run_root_cmd(["rm", "-rf", d]).check_returncode()
def create_file(filepath, owner='root', group='root'):
def create_file(filepath, owner="root", group="root"):
assert not os.path.exists(filepath)
run_root_cmd(['touch', filepath]).check_returncode()
run_root_cmd(['chown', f'{owner}:{group}', filepath]).check_returncode()
run_root_cmd(["touch", filepath]).check_returncode()
run_root_cmd(["chown", f"{owner}:{group}", filepath]).check_returncode()
@pytest.fixture
@ -53,13 +53,13 @@ def test_get_tempdir(tempdir):
def tempdir_filled() -> Generator[TempdirFillInfo, None, None]:
d = _get_tempdir()
contents = {
'rootfile': {
'owner': 'root',
'group': 'root',
"rootfile": {
"owner": "root",
"group": "root",
},
'userfile': {
'owner': 'nobody',
'group': 'nobody',
"userfile": {
"owner": "nobody",
"group": "nobody",
},
}
res = TempdirFillInfo(path=d, files={})
@ -87,11 +87,13 @@ def verify_mode(filepath, mode: int = TEMPDIR_MODE):
def verify_content(filepath, content):
assert os.path.exists(filepath)
with open(filepath, 'r') as f:
with open(filepath, "r") as f:
assert f.read().strip() == content.strip()
@pytest.mark.parametrize("user,group", [('root', 'root'), ('nobody', 'nobody')])
@pytest.mark.parametrize(
"user,group", [("root", "root"), ("nobody", "nobody")]
)
def test_chown(tempdir: str, user: str, group: str):
assert os.path.exists(tempdir)
target_uid = get_uid(user)
@ -110,16 +112,16 @@ def test_chmod(tempdir_filled, mode: int):
def test_tempdir_filled_fixture(tempdir_filled: TempdirFillInfo):
files = tempdir_filled.files
assert files
assert 'rootfile' in files
assert 'userfile' in files
verify_ownership(files['rootfile'], 'root', 'root')
verify_ownership(files['userfile'], 'nobody', 'nobody')
assert "rootfile" in files
assert "userfile" in files
verify_ownership(files["rootfile"], "root", "root")
verify_ownership(files["userfile"], "nobody", "nobody")
def test_write_new_file_naive(tempdir: str):
assert os.path.exists(tempdir)
new = os.path.join(tempdir, 'newfiletest')
content = 'test12345'
new = os.path.join(tempdir, "newfiletest")
content = "test12345"
assert not os.path.exists(new)
write_file(new, content)
verify_content(new, content)
@ -128,20 +130,20 @@ def test_write_new_file_naive(tempdir: str):
def test_write_new_file_root(tempdir: str):
assert os.path.exists(tempdir)
new = os.path.join(tempdir, 'newfiletest')
content = 'test12345'
new = os.path.join(tempdir, "newfiletest")
content = "test12345"
assert not os.path.exists(new)
write_file(new, content, user='root', group='root')
write_file(new, content, user="root", group="root")
verify_content(new, content)
verify_ownership(new, user=0, group=0)
def test_write_new_file_user(tempdir: str):
user = 'nobody'
group = 'nobody'
user = "nobody"
group = "nobody"
assert os.path.exists(tempdir)
new = os.path.join(tempdir, 'newfiletest')
content = 'test12345'
new = os.path.join(tempdir, "newfiletest")
content = "test12345"
assert not os.path.exists(new)
write_file(new, content, user=user, group=group)
assert os.path.exists(new)
@ -151,31 +153,35 @@ def test_write_new_file_user(tempdir: str):
def test_write_new_file_user_in_root_dir(tempdir: str):
assert os.path.exists(tempdir)
chown(tempdir, user='root', group='root')
verify_ownership(tempdir, 'root', 'root')
chown(tempdir, user="root", group="root")
verify_ownership(tempdir, "root", "root")
test_write_new_file_user(tempdir)
def test_write_rootfile_naive(tempdir_filled: TempdirFillInfo):
files = tempdir_filled.files
assert 'rootfile' in files
p = files['rootfile']
assert "rootfile" in files
p = files["rootfile"]
assert os.path.exists(p)
verify_ownership(p, 'root', 'root')
content = 'test123'
verify_ownership(p, "root", "root")
content = "test123"
write_file(p, content)
verify_content(p, 'test123')
verify_ownership(p, 'root', 'root')
verify_content(p, "test123")
verify_ownership(p, "root", "root")
@pytest.mark.parametrize("user,group", [('root', 'root'), ('nobody', 'nobody')])
def test_write_rootfile(tempdir_filled: TempdirFillInfo, user: str, group: str):
@pytest.mark.parametrize(
"user,group", [("root", "root"), ("nobody", "nobody")]
)
def test_write_rootfile(
tempdir_filled: TempdirFillInfo, user: str, group: str
):
files = tempdir_filled.files
assert 'rootfile' in files
p = files['rootfile']
assert "rootfile" in files
p = files["rootfile"]
assert os.path.exists(p)
verify_ownership(p, 'root', 'root')
content = 'test123'
verify_ownership(p, "root", "root")
content = "test123"
write_file(p, content)
verify_content(p, 'test123')
verify_ownership(p, 'root', 'root')
verify_content(p, "test123")
verify_ownership(p, "root", "root")