Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/hatch/template/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(self, *args, **kwargs):
def initialize_config(self, config):
# Default values
config["readme_file_path"] = "README.md"
config["gitignore_file_path"] = ".gitignore"
config["package_metadata_file_path"] = f"src/{config['package_name']}/__about__.py"

license_data = {}
Expand Down
13 changes: 13 additions & 0 deletions src/hatch/template/files_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ def __init__(
super().__init__(Path(template_config["package_name"], "__about__.py"), '__version__ = "0.0.1"\n')


class GitIgnore(File):
TEMPLATE = """__pycache__/
dist/
"""

def __init__(
self,
template_config: dict,
plugin_config: dict, # noqa: ARG002
):
super().__init__(Path(template_config["gitignore_file_path"]), self.TEMPLATE)


class Readme(File):
TEMPLATE = """\
# {project_name}
Expand Down
8 changes: 6 additions & 2 deletions tests/backend/builders/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -1336,8 +1336,12 @@ def test_default_vcs_git_exclusion_files(self, hatch, helpers, temp_dir, config_

project_path = temp_dir / "my-app"

vcs_ignore_file = temp_dir / ".gitignore"
vcs_ignore_file.write_text("*.pyc\n*.so\n*.h\n")
outside_vcs_ignore_file = temp_dir / ".gitignore"
outside_vcs_ignore_file.write_text("*.so\n")

inside_vcs_ignore_file = project_path / ".gitignore"
with inside_vcs_ignore_file.open("a") as f:
f.write("*.h\n")

(project_path / "my_app" / "lib.so").touch()
(project_path / "my_app" / "lib.h").touch()
Expand Down
14 changes: 14 additions & 0 deletions tests/cli/new/test_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def test_default(hatch, helpers, temp_dir):
│ └── __init__.py
├── tests
│ └── __init__.py
├── .gitignore
├── LICENSE.txt
├── README.md
└── pyproject.toml
Expand All @@ -97,6 +98,7 @@ def test_default_explicit_path(hatch, helpers, temp_dir):
└── __init__.py
tests
└── __init__.py
.gitignore
LICENSE.txt
README.md
pyproject.toml
Expand Down Expand Up @@ -127,6 +129,7 @@ def test_default_empty_plugins_table(hatch, helpers, config_file, temp_dir):
│ └── __init__.py
├── tests
│ └── __init__.py
├── .gitignore
├── LICENSE.txt
├── README.md
└── pyproject.toml
Expand Down Expand Up @@ -158,6 +161,7 @@ def test_default_no_license_cache(hatch, helpers, temp_dir):
│ └── __init__.py
├── tests
│ └── __init__.py
├── .gitignore
├── LICENSE.txt
├── README.md
└── pyproject.toml
Expand Down Expand Up @@ -191,6 +195,7 @@ def test_licenses_multiple(hatch, helpers, config_file, temp_dir):
│ └── __init__.py
├── tests
│ └── __init__.py
├── .gitignore
├── README.md
└── pyproject.toml
"""
Expand Down Expand Up @@ -220,6 +225,7 @@ def test_licenses_empty(hatch, helpers, config_file, temp_dir):
│ └── __init__.py
├── tests
│ └── __init__.py
├── .gitignore
├── README.md
└── pyproject.toml
"""
Expand Down Expand Up @@ -253,6 +259,7 @@ def test_projects_urls_space_in_label(hatch, helpers, config_file, temp_dir):
│ └── __init__.py
├── tests
│ └── __init__.py
├── .gitignore
├── LICENSE.txt
├── README.md
└── pyproject.toml
Expand Down Expand Up @@ -283,6 +290,7 @@ def test_projects_urls_empty(hatch, helpers, config_file, temp_dir):
│ └── __init__.py
├── tests
│ └── __init__.py
├── .gitignore
├── LICENSE.txt
├── README.md
└── pyproject.toml
Expand Down Expand Up @@ -314,6 +322,7 @@ def test_feature_cli(hatch, helpers, temp_dir):
│ └── __main__.py
├── tests
│ └── __init__.py
├── .gitignore
├── LICENSE.txt
├── README.md
└── pyproject.toml
Expand Down Expand Up @@ -348,6 +357,7 @@ def test_feature_ci(hatch, helpers, config_file, temp_dir):
│ └── __init__.py
├── tests
│ └── __init__.py
├── .gitignore
├── LICENSE.txt
├── README.md
└── pyproject.toml
Expand Down Expand Up @@ -378,6 +388,7 @@ def test_feature_no_src_layout(hatch, helpers, config_file, temp_dir):
│ └── __init__.py
├── tests
│ └── __init__.py
├── .gitignore
├── LICENSE.txt
├── README.md
└── pyproject.toml
Expand Down Expand Up @@ -407,6 +418,7 @@ def test_feature_tests_disable(hatch, helpers, config_file, temp_dir):
│ └── my_app
│ ├── __about__.py
│ └── __init__.py
├── .gitignore
├── LICENSE.txt
├── README.md
└── pyproject.toml
Expand Down Expand Up @@ -451,6 +463,7 @@ def test_interactive(hatch, helpers, temp_dir):
│ └── __init__.py
├── tests
│ └── __init__.py
├── .gitignore
├── LICENSE.txt
├── README.md
└── pyproject.toml
Expand Down Expand Up @@ -483,6 +496,7 @@ def test_no_project_name_enables_interactive(hatch, helpers, temp_dir):
│ └── __init__.py
├── tests
│ └── __init__.py
├── .gitignore
├── LICENSE.txt
├── README.md
└── pyproject.toml
Expand Down
6 changes: 6 additions & 0 deletions tests/helpers/templates/new/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def get_files(**kwargs):
"<copyright holders>", f"{kwargs['author']} <{kwargs['email']}>", 1
),
),
File(
Path(".gitignore"),
"""__pycache__/
dist/
""",
),
File(
Path("src", kwargs["package_name"], "__init__.py"),
f"""\
Expand Down
6 changes: 6 additions & 0 deletions tests/helpers/templates/new/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def get_files(**kwargs):
"<copyright holders>", f"{kwargs['author']} <{kwargs['email']}>", 1
),
),
File(
Path(".gitignore"),
"""__pycache__/
dist/
""",
),
File(
Path("src", kwargs["package_name"], "__init__.py"),
f"""\
Expand Down
6 changes: 6 additions & 0 deletions tests/helpers/templates/new/feature_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def get_files(**kwargs):
"<copyright holders>", f"{kwargs['author']} <{kwargs['email']}>", 1
),
),
File(
Path(".gitignore"),
"""__pycache__/
dist/
""",
),
File(
Path("src", kwargs["package_name"], "__init__.py"),
f"""\
Expand Down
6 changes: 6 additions & 0 deletions tests/helpers/templates/new/feature_no_src_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def get_files(**kwargs):
"<copyright holders>", f"{kwargs['author']} <{kwargs['email']}>", 1
),
),
File(
Path(".gitignore"),
"""__pycache__/
dist/
""",
),
File(
Path(kwargs["package_name"], "__init__.py"),
f"""\
Expand Down
6 changes: 6 additions & 0 deletions tests/helpers/templates/new/licenses_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ def get_files(**kwargs):
File(Path("src", kwargs["package_name"], "__init__.py")),
File(Path("src", kwargs["package_name"], "__about__.py"), '__version__ = "0.0.1"\n'),
File(Path("tests", "__init__.py")),
File(
Path(".gitignore"),
"""__pycache__/
dist/
""",
),
File(
Path("README.md"),
f"""\
Expand Down
6 changes: 6 additions & 0 deletions tests/helpers/templates/new/licenses_multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def get_files(**kwargs):
"<copyright holders>", f"{kwargs['author']} <{kwargs['email']}>", 1
),
),
File(
Path(".gitignore"),
"""__pycache__/
dist/
""",
),
File(
Path("src", kwargs["package_name"], "__init__.py"),
f"""\
Expand Down
6 changes: 6 additions & 0 deletions tests/helpers/templates/new/projects_urls_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def get_files(**kwargs):
"<copyright holders>", f"{kwargs['author']} <{kwargs['email']}>", 1
),
),
File(
Path(".gitignore"),
"""__pycache__/
dist/
""",
),
File(
Path("src", kwargs["package_name"], "__init__.py"),
f"""\
Expand Down
6 changes: 6 additions & 0 deletions tests/helpers/templates/new/projects_urls_space_in_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def get_files(**kwargs):
"<copyright holders>", f"{kwargs['author']} <{kwargs['email']}>", 1
),
),
File(
Path(".gitignore"),
"""__pycache__/
dist/
""",
),
File(
Path("src", kwargs["package_name"], "__init__.py"),
f"""\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ def get_files(**kwargs):
relative_root = kwargs.get("relative_root", "")

files = [File(Path(relative_root, f.path), f.contents) for f in get_template_files(**kwargs)]

gitignore_files = [file for file in files if file.path == Path(relative_root, ".gitignore")]
assert len(gitignore_files) == 1, "Expected exactly one .gitignore file"
gitignore_file = gitignore_files[0]
files.remove(gitignore_file)
gitignore_file.contents += "*.h\n"

files.extend((
gitignore_file,
File(Path(relative_root, kwargs["package_name"], "lib.so"), ""),
File(
Path(relative_root, ".gitignore"),
"""\
*.pyc
*.so
*.h
""",
),
File(
Path(relative_root, "PKG-INFO"),
f"""\
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/templates/sdist/standard_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def get_files(**kwargs):
files = []
for f in get_template_files(**kwargs):
part = f.path.parts[0]
if part in {"my_app", "pyproject.toml", "README.md", "LICENSE.txt"}:
if part in {"my_app", "pyproject.toml", "README.md", "LICENSE.txt", ".gitignore"}:
files.append(File(Path(relative_root, f.path), f.contents))

files.append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def get_files(**kwargs):
files = []
for f in get_template_files(**kwargs):
part = f.path.parts[0]
if part in {"my_app", "pyproject.toml", "README.md", "LICENSE.txt"}:
if part in {"my_app", "pyproject.toml", "README.md", "LICENSE.txt", ".gitignore"}:
files.append(File(Path(relative_root, f.path), f.contents))

files.extend((
Expand Down
Loading