Skip to content

Commit 2b50c1f

Browse files
committed
Don't run tree or git when running golden tests
Both tree and git have unstable output between different versions, so it makes the golden tests too flaky when they run in different environments. Since which files are being generated is already being tested by the golden tree, we don't really need to check for this. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent b2452f0 commit 2b50c1f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

cookiecutter/hooks/post_gen_project.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import configparser as _configparser
1111
import dataclasses as _dataclasses
1212
import json as _json
13+
import os
1314
import pathlib as _pathlib
1415
import shutil as _shutil
1516
import subprocess as _subprocess
@@ -189,6 +190,11 @@ def initialize_git_submodules() -> bool:
189190
Returns:
190191
Whether any git submodules were initialized.
191192
"""
193+
if is_golden_testing():
194+
# We don't use external tools when running tests because they are too flaky, as
195+
# different versions emit different outputs
196+
return False
197+
192198
gitmodules_path = _pathlib.Path(".gitmodules")
193199

194200
if not gitmodules_path.exists():
@@ -287,8 +293,14 @@ def initialize_git_repo() -> bool:
287293
Returns:
288294
Whether the project was initialized as a git repository.
289295
"""
296+
if is_golden_testing():
297+
# We don't use external tools when running tests because they are too flaky, as
298+
# different versions emit different outputs
299+
return False
300+
290301
if _pathlib.Path(".git").exists():
291302
return False
303+
292304
print()
293305
note("Initializing git repository...")
294306
try_run(
@@ -309,6 +321,11 @@ def commit_git_changes(*, first_commit: bool) -> None:
309321
Args:
310322
first_commit: Whether this is the first commit in the git repository.
311323
"""
324+
if is_golden_testing():
325+
# We don't use external tools when running tests because they are too flaky, as
326+
# different versions emit different outputs
327+
return
328+
312329
if not try_run(["git", "status", "--porcelain"]):
313330
return
314331

@@ -367,6 +384,11 @@ def is_file_empty(path: _pathlib.Path) -> bool:
367384

368385
def print_generated_tree() -> None:
369386
"""Print the generated files tree."""
387+
if is_golden_testing():
388+
# We don't use external tools when running tests because they are too flaky, as
389+
# different versions emit different outputs
390+
return
391+
370392
result = try_run(["tree"])
371393
if result is not None and result.returncode == 0:
372394
print()
@@ -487,6 +509,8 @@ def try_run(
487509
note_on_failure: If not `None`, print this note if the command fails (either
488510
because of an error or a non-zero status code).
489511
verbose: Whether to print the command before running it.
512+
shell: Whether to run the command in a shell. If `True`, `cmd` must be a
513+
string, otherwise it must be a list of strings.
490514
491515
Returns:
492516
The result of the command or `None` if the command could not be run because
@@ -513,6 +537,15 @@ def try_run(
513537
return result
514538

515539

540+
def is_golden_testing() -> bool:
541+
"""Return `True` if we are running as part of a golden testing.
542+
543+
Returns:
544+
Whether we are running as part of a golden testing.
545+
"""
546+
return os.environ.get("GOLDEN_TEST", None) is not None
547+
548+
516549
def recursive_overwrite_move(src: _pathlib.Path, dst: _pathlib.Path) -> None:
517550
"""Recursively move a directory overwriting the target files if they exist.
518551

tests/integration/test_cookiecutter_generation.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ def test_golden(
3030
request: pytest.FixtureRequest,
3131
) -> None:
3232
"""Test generation of a new repo comparing it to a golden tree."""
33+
env = os.environ.copy()
34+
env.update(
35+
# Signal to the cookiecutter template that it is running in a golden test, so
36+
# some flaky outputs can be disabled.
37+
GOLDEN_TEST="1",
38+
)
39+
3340
cwd = pathlib.Path().cwd()
3441
golden_path = (
3542
cwd
@@ -39,7 +46,7 @@ def test_golden(
3946
)
4047

4148
generated_repo_path, run_result = _generate_repo(
42-
repo_type, tmp_path, capture_output=True
49+
repo_type, tmp_path, capture_output=True, env=env
4350
)
4451
stdout, stderr = _filter_generation_output(run_result)
4552
_assert_golden_file(golden_path, "cookiecutter-stdout", stdout)

0 commit comments

Comments
 (0)