Skip to content

Commit e596184

Browse files
asmacdopre-commit-ci[bot]viniciusdc
authored
bf: do not clobber existing .gitignore (#3105)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Vinicius D. Cerutti <[email protected]>
1 parent 63802e4 commit e596184

File tree

3 files changed

+89
-7
lines changed

3 files changed

+89
-7
lines changed

src/_nebari/stages/bootstrap/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313

1414
def gen_gitignore():
1515
"""
16-
Generate `.gitignore` file.
17-
Add files as needed.
16+
Generate `.gitignore` file if not present.
1817
"""
18+
gitignore_path = pathlib.Path(".gitignore")
19+
20+
# If .gitignore already exists, don't overwrite it
21+
if gitignore_path.exists():
22+
return {}
23+
1924
filestoignore = """
2025
# ignore terraform state
2126
.terraform
@@ -26,7 +31,7 @@ def gen_gitignore():
2631
# python
2732
__pycache__
2833
"""
29-
return {pathlib.Path(".gitignore"): cleandoc(filestoignore)}
34+
return {gitignore_path: cleandoc(filestoignore)}
3035

3136

3237
def gen_cicd(config: schema.Main):

tests/tests_unit/conftest.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from pathlib import Path
23
from unittest.mock import Mock
34

@@ -182,10 +183,19 @@ def nebari_stages():
182183
def nebari_render(nebari_config, nebari_stages, tmp_path):
183184
NEBARI_CONFIG_FN = "nebari-config.yaml"
184185

185-
config_filename = tmp_path / NEBARI_CONFIG_FN
186-
write_configuration(config_filename, nebari_config)
187-
render_template(tmp_path, nebari_config, nebari_stages)
188-
return tmp_path, config_filename
186+
# Save original cwd and change to tmp_path so files that should be generated
187+
# in the repo root are put in the right place
188+
original_cwd = Path.cwd()
189+
os.chdir(tmp_path)
190+
191+
try:
192+
config_filename = tmp_path / NEBARI_CONFIG_FN
193+
write_configuration(config_filename, nebari_config)
194+
render_template(tmp_path, nebari_config, nebari_stages)
195+
return tmp_path, config_filename
196+
finally:
197+
# Always restore original cwd
198+
os.chdir(original_cwd)
189199

190200

191201
@pytest.fixture

tests/tests_unit/test_bootstrap.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
import pathlib
3+
from unittest.mock import patch
4+
5+
from _nebari.stages.bootstrap import gen_gitignore
6+
7+
8+
class TestGenGitignore:
9+
"""Test the gen_gitignore function."""
10+
11+
def test_gen_gitignore_creates_file_when_not_exists(self, tmp_path):
12+
"""Test that gen_gitignore returns gitignore content when file doesn't exist."""
13+
with patch("pathlib.Path.exists", return_value=False):
14+
result = gen_gitignore()
15+
16+
assert pathlib.Path(".gitignore") in result
17+
assert "terraform.tfstate" in result[pathlib.Path(".gitignore")]
18+
assert "__pycache__" in result[pathlib.Path(".gitignore")]
19+
20+
def test_gen_gitignore_skips_when_exists(self, tmp_path):
21+
"""Test that gen_gitignore returns empty dict when .gitignore already exists."""
22+
with patch("pathlib.Path.exists", return_value=True):
23+
result = gen_gitignore()
24+
25+
assert result == {}
26+
27+
def test_gen_gitignore_content_format(self, tmp_path):
28+
"""Test that gen_gitignore returns properly formatted content."""
29+
with patch("pathlib.Path.exists", return_value=False):
30+
result = gen_gitignore()
31+
32+
content = result[pathlib.Path(".gitignore")]
33+
expected_patterns = [
34+
"# ignore terraform state",
35+
".terraform",
36+
"terraform.tfstate",
37+
"terraform.tfstate.backup",
38+
".terraform.tfstate.lock.info",
39+
"# python",
40+
"__pycache__",
41+
]
42+
43+
for pattern in expected_patterns:
44+
assert pattern in content
45+
46+
def test_gen_gitignore_file_system_integration(self, tmp_path):
47+
"""Test gen_gitignore behavior with actual file system."""
48+
# Change to tmp directory
49+
original_cwd = pathlib.Path.cwd()
50+
os.chdir(tmp_path)
51+
52+
try:
53+
# Test when .gitignore doesn't exist
54+
result = gen_gitignore()
55+
assert pathlib.Path(".gitignore") in result
56+
57+
# Create .gitignore file
58+
gitignore_path = tmp_path / ".gitignore"
59+
gitignore_path.write_text("existing content")
60+
61+
# Test when .gitignore exists
62+
result = gen_gitignore()
63+
assert result == {}
64+
65+
finally:
66+
# Restore original working directory
67+
os.chdir(original_cwd)

0 commit comments

Comments
 (0)