Skip to content

Commit aefe22f

Browse files
committed
🔧 Melhorando qualidade do codigo
- [Test] - Cobertura de test para as funções do arquivo utils.py - [Codacy] Corrigindo as 3 issues de possível falha de segurança no codigo
1 parent 7e05b2e commit aefe22f

7 files changed

+87
-10
lines changed

encryptdef/__main__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
from encryptdef.cli import main
44

55
if __name__ == "__main__":
6-
main()
6+
# Passando um contexto vazio
7+
main(ctx=None)

encryptdef/cli.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Módulo responsável por todos os comandos CLI"""
22

33
from importlib.metadata import version
4-
from typing import Any, List, Optional
4+
from typing import List, Optional
55

66
import rich_click as click
77

@@ -21,7 +21,7 @@
2121
@click.group(invoke_without_command=True)
2222
@click.version_option(version("encryptdef"))
2323
@click.pass_context
24-
def main(ctx: Any) -> None:
24+
def main(ctx: Optional[click.Context] = None) -> None:
2525
"""Encryptdef
2626
2727
Ferramenta de linha de comando em Python para encriptar e desencriptar
@@ -40,6 +40,9 @@ def main(ctx: Any) -> None:
4040
> **Mantenha a chave de encriptação em segredo e não a perca. Sem a chave
4141
correta, não será possível desencriptar os dados ou arquivos.**
4242
"""
43+
if ctx is None:
44+
ctx = click.Context(main)
45+
4346
if ctx.invoked_subcommand is None:
4447
core.interactive_mode()
4548

encryptdef/interactive_interface.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
TEMPLATE_INFO,
1616
TEMPLATE_LOGO,
1717
)
18+
from encryptdef.utils import clear_console
1819

1920

2021
def print_template_logo(info: bool = False) -> None:
@@ -24,7 +25,7 @@ def print_template_logo(info: bool = False) -> None:
2425
Args:
2526
info (bool): Se True, imprime também as informações da ferramenta.
2627
"""
27-
os.system("clear")
28+
clear_console()
2829
console.print(TEMPLATE_LOGO)
2930

3031
if info:
@@ -70,7 +71,7 @@ def validate_and_get_input(prompts: List[str]) -> List[str]:
7071
return inputs
7172

7273
except ValueError as e:
73-
os.system("clear")
74+
clear_console()
7475
print_template_logo()
7576
print_and_record_log(str(e), "error")
7677

@@ -102,7 +103,7 @@ def print_request_menu(menu: str) -> int:
102103
return int(choice)
103104

104105
except ValueError as e:
105-
os.system("clear")
106+
clear_console()
106107
print_template_logo(info=True)
107108
print_and_record_log(str(e), "error")
108109

encryptdef/utils.py

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import re
5+
import subprocess
56
from typing import List
67

78
from encryptdef.template import TEMPLATE_IS_DIRECTORY
@@ -85,3 +86,11 @@ def write_file(new_file_path: str, processed_lines: List[str]) -> None:
8586
"""
8687
with open(new_file_path, "w", encoding="utf-8") as file_a:
8788
file_a.writelines(processed_lines)
89+
90+
91+
def clear_console():
92+
"""Limpa o console de forma segura."""
93+
if os.name == "posix":
94+
subprocess.run(["clear"], check=False)
95+
elif os.name == "nt":
96+
subprocess.run(["cls"], shell=True, check=False)

tests/test_assigning_a_name_file.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
""" Modulo para testar a função assigning_a_name_file em utils.py"""
2+
3+
from encryptdef.utils import assigning_a_name_file
4+
5+
6+
def test_assigning_a_name_file_absolute():
7+
"""Test function assigning_a_name_file"""
8+
file = "/tmp/test/file-test-123.txt"
9+
name = "encrypt-"
10+
expected_result = "/tmp/test/encrypt-file-test-123.txt"
11+
12+
result = assigning_a_name_file(file, name)
13+
assert result == expected_result
14+
15+
16+
def test_assigning_a_name_file_relative():
17+
"""Test function assigning_a_name_file"""
18+
file = "-123-file-test.txt"
19+
name = "decrypt-"
20+
expected_result = "decrypt--123-file-test.txt"
21+
22+
result = assigning_a_name_file(file, name)
23+
assert result == expected_result

tests/test_clear_console.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
""" Modulo para testar a função clear_console em utils.py"""
2+
3+
from unittest import mock
4+
5+
from encryptdef.utils import clear_console
6+
7+
8+
@mock.patch("subprocess.run") # Substitui subprocess.run por um mock
9+
def test_clear_console_posix(mock_run):
10+
"""Test function clear_console"""
11+
with mock.patch("os.name", "posix"):
12+
clear_console()
13+
14+
# Verifica se subprocess.run foi chamado com ["cls"] e check=False
15+
mock_run.assert_called_once_with(["clear"], check=False)
16+
17+
18+
@mock.patch("subprocess.run")
19+
def test_clear_console_nt(mock_run):
20+
"""Test function clear_console"""
21+
with mock.patch("os.name", "nt"):
22+
clear_console()
23+
mock_run.assert_called_once_with(["cls"], shell=True, check=False)

tests/test_get_new_file_path.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,34 @@
22

33
import os
44

5+
import pytest
6+
57
from encryptdef.utils import get_new_file_path
68

79

810
def test_get_new_file_path_positive():
911
"""Test function get_new_file_path"""
12+
current_dir = os.getcwd()
1013
file = "file_test.txt"
1114
new_file = "encrypt_file_test.txt"
12-
current_dir = os.getcwd()
15+
expected_path = os.path.join(current_dir, new_file)
16+
1317
file_path = get_new_file_path(file, new_file, current_dir)
18+
assert file_path == expected_path
19+
20+
21+
def test_get_new_file_path_is_directory_error():
22+
"""Test function get_new_file_path"""
23+
current_dir = os.getcwd()
24+
directoty = "new_directory"
25+
directoty_path = os.path.join(current_dir, directoty)
26+
os.mkdir(directoty_path)
27+
28+
with pytest.raises(IsADirectoryError):
29+
get_new_file_path(directoty_path, "new_test.txt", current_dir)
1430

15-
assert file_path == os.path.join(os.getcwd(), new_file)
31+
with pytest.raises(IsADirectoryError):
32+
get_new_file_path("test.txt", directoty_path, current_dir)
1633

1734

1835
def test_get_new_file_path_positive_exact_file_path():
@@ -22,6 +39,6 @@ def test_get_new_file_path_positive_exact_file_path():
2239

2340
current_dir = os.getcwd()
2441
new_file_path = os.path.join(current_dir, new_file)
25-
file_path_get = get_new_file_path(file, new_file_path, current_dir)
2642

27-
assert file_path_get == new_file_path
43+
new_file_path_get = get_new_file_path(file, new_file_path, current_dir)
44+
assert new_file_path_get == new_file_path

0 commit comments

Comments
 (0)