Skip to content

Commit

Permalink
chore: use write_text in tests
Browse files Browse the repository at this point in the history
Fixes issues where test files are not the same depending on the OS
  • Loading branch information
gg-mmill committed Dec 9, 2024
1 parent 76f7711 commit 8ae13a4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
41 changes: 21 additions & 20 deletions tests/unit/cmd/scan/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
assert_invoke_exited_with,
assert_invoke_ok,
my_vcr,
write_text,
)


def create_normally_ignored_file() -> Path:
path = Path("node_modules", "test.js")
path.parent.mkdir()
path.write_text("// Test")
write_text(path, "// Test")
return path


Expand All @@ -34,13 +35,13 @@ class TestPathScan:
"""

def create_files(self):
Path("file1").write_text("This is a file with no secrets.")
Path("file2").write_text("This is a file with no secrets.")
write_text(Path("file1"), "This is a file with no secrets.")
write_text(Path("file2"), "This is a file with no secrets.")

@my_vcr.use_cassette("test_scan_file")
@pytest.mark.parametrize("verbose", [True, False])
def test_scan_file(self, cli_fs_runner, verbose):
Path("file").write_text("This is a file with no secrets.")
write_text(Path("file"), "This is a file with no secrets.")
assert os.path.isfile("file")

if verbose:
Expand All @@ -58,7 +59,7 @@ def test_scan_file_secret(self, cli_fs_runner):
THEN the secret is reported
AND the exit code is not 0
"""
Path("file_secret").write_text(UNCHECKED_SECRET_PATCH)
write_text(Path("file_secret"), UNCHECKED_SECRET_PATCH)
assert os.path.isfile("file_secret")

cmd = ["secret", "scan", "path", "file_secret"]
Expand All @@ -79,7 +80,7 @@ def test_scan_file_secret(self, cli_fs_runner):
)

def test_scan_file_secret_with_validity(self, cli_fs_runner):
Path("file_secret").write_text(VALID_SECRET_PATCH)
write_text(Path("file_secret"), VALID_SECRET_PATCH)
assert os.path.isfile("file_secret")

with my_vcr.use_cassette("test_scan_path_file_secret_with_validity"):
Expand All @@ -102,7 +103,7 @@ def test_scan_file_secret_with_validity(self, cli_fs_runner):
@pytest.mark.parametrize("validity", [True, False])
def test_scan_file_secret_json_with_validity(self, cli_fs_runner, validity):
secret = VALID_SECRET_PATCH if validity else UNCHECKED_SECRET_PATCH
Path("file_secret").write_text(secret)
write_text(Path("file_secret"), secret)
assert os.path.isfile("file_secret")

cassette_name = f"test_scan_file_secret-{validity}"
Expand All @@ -122,7 +123,7 @@ def test_scan_file_secret_json_with_validity(self, cli_fs_runner, validity):

@pytest.mark.parametrize("json_output", [False, True])
def test_scan_file_secret_exit_zero(self, cli_fs_runner, json_output):
Path("file_secret").write_text(UNCHECKED_SECRET_PATCH)
write_text(Path("file_secret"), UNCHECKED_SECRET_PATCH)
assert os.path.isfile("file_secret")

with my_vcr.use_cassette("test_scan_file_secret"):
Expand Down Expand Up @@ -207,7 +208,7 @@ def test_scan_ignored_file(self, scan_mock, cli_fs_runner):
- "file1"
"""
Path(".gitguardian.yaml").write_text(config)
write_text(Path(".gitguardian.yaml"), config)

result = cli_fs_runner.invoke(
cli, ["secret", "scan", "path", "file1", "file2", "-y"]
Expand Down Expand Up @@ -261,10 +262,10 @@ def path_line(path_str):
def create_files(self):
os.makedirs("dir", exist_ok=True)
os.makedirs("dir/subdir", exist_ok=True)
Path("file1").write_text("This is a file with no secrets.")
Path("dir/file2").write_text("This is a file with no secrets.")
Path("dir/subdir/file3").write_text("This is a file with no secrets.")
Path("dir/subdir/file4").write_text("This is a file with no secrets.")
write_text(Path("file1"), "This is a file with no secrets.")
write_text(Path("dir/file2"), "This is a file with no secrets.")
write_text(Path("dir/subdir/file3"), "This is a file with no secrets.")
write_text(Path("dir/subdir/file4"), "This is a file with no secrets.")

def test_directory_error(self, cli_fs_runner):
result = cli_fs_runner.invoke(
Expand Down Expand Up @@ -415,17 +416,17 @@ def test_scan_path_use_gitignore(
# files in repo
ignored_secret = local_repo.path / "ignored_file_secret"
found_secret = local_repo.path / "found_file_secret"
ignored_secret.write_text(VALID_SECRET_PATCH)
found_secret.write_text(VALID_SECRET_PATCH)
write_text(ignored_secret, VALID_SECRET_PATCH)
write_text(found_secret, VALID_SECRET_PATCH)

gitignore = local_repo.path / ".gitignore"
gitignore.write_text("ignored_file_secret")
write_text(gitignore, "ignored_file_secret")

# Submodule
submodule_path = tmp_path / "submodule_repo"
local_submodule = Repository.create(submodule_path)
staged_sm_file = local_submodule.path / "committed_sm_file"
staged_sm_file.write_text("This is a file with no secrets.")
write_text(staged_sm_file, "This is a file with no secrets.")
local_submodule.git("add", str(staged_sm_file))
local_submodule.create_commit(message="Initial commit")

Expand All @@ -434,7 +435,7 @@ def test_scan_path_use_gitignore(

# Unstaged file in the submodule
submodule_unstaged_file = local_submodule.path / "unstaged_sm_file"
submodule_unstaged_file.write_text("This is a file with no secrets.")
write_text(submodule_unstaged_file, "This is a file with no secrets.")

# Scan with --use-gitignore
with cli_runner.isolated_filesystem(temp_dir=tmp_path):
Expand Down Expand Up @@ -478,7 +479,7 @@ def test_scan_path_use_gitignore(
def test_ignore_detectors(
self, cli_fs_runner, ignored_detectors, nb_secret, nb_ignored, all_secrets
):
Path("file_secret").write_text(_ONE_LINE_AND_MULTILINE_PATCH)
write_text(Path("file_secret"), _ONE_LINE_AND_MULTILINE_PATCH)
all_secrets_option = ["--all-secrets"] if all_secrets else []
with my_vcr.use_cassette("test_scan_path_file_one_line_and_multiline_patch"):
result = cli_fs_runner.invoke(
Expand Down Expand Up @@ -527,7 +528,7 @@ def test_scan_context_repository(
local_repo.git("remote", "add", "origin", remote_url)

file = local_repo.path / "file_secret"
file.write_text("Hello")
write_text(file, "Hello")
local_repo.add(file)
local_repo.create_commit()

Expand Down
9 changes: 7 additions & 2 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,15 @@ def isolated_fs(fs):

def write_text(filename: Union[str, Path], content: str):
"""Create a text file named `filename` with content `content.
Create any missing dirs if necessary."""
Create any missing dirs if necessary.
Note that using `write_bytes(content.encode())` ensures the
same content is created, independently of the OS
(whereas using Path.write_text creates different line ends depending on the OS)
"""
path = Path(filename)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content)
path.write_bytes(content.encode())


def write_yaml(filename: Union[str, Path], data: Any):
Expand Down

0 comments on commit 8ae13a4

Please sign in to comment.