Skip to content

Commit

Permalink
Merge pull request #30 from MaKaNu/fix-license-file-creation
Browse files Browse the repository at this point in the history
Fix license file creation
  • Loading branch information
fpgmaas authored Dec 19, 2024
2 parents 73a7083 + 52b3e99 commit 1e083e8
Show file tree
Hide file tree
Showing 9 changed files with 1,940 additions and 991 deletions.
46 changes: 46 additions & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def remove_dir(filepath: str) -> None:
shutil.rmtree(os.path.join(PROJECT_DIRECTORY, filepath))


def move_file(filepath: str, target: str) -> None:
os.rename(os.path.join(PROJECT_DIRECTORY, filepath), os.path.join(PROJECT_DIRECTORY, target))


if __name__ == "__main__":
if "{{cookiecutter.include_github_actions}}" != "y":
remove_dir(".github")
Expand All @@ -36,3 +40,45 @@ def remove_dir(filepath: str) -> None:

if "{{cookiecutter.devcontainer}}" != "y":
remove_dir(".devcontainer")

if "{{cookiecutter.open_source_license}}" == "MIT license":
move_file("LICENSE_MIT", "LICENSE")
remove_file("LICENSE_BSD")
remove_file("LICENSE_ISC")
remove_file("LICENSE_APACHE")
remove_file("LICENSE_GPL")

if "{{cookiecutter.open_source_license}}" == "BSD license":
move_file("LICENSE_BSD", "LICENSE")
remove_file("LICENSE_MIT")
remove_file("LICENSE_ISC")
remove_file("LICENSE_APACHE")
remove_file("LICENSE_GPL")

if "{{cookiecutter.open_source_license}}" == "ISC license":
move_file("LICENSE_ISC", "LICENSE")
remove_file("LICENSE_MIT")
remove_file("LICENSE_BSD")
remove_file("LICENSE_APACHE")
remove_file("LICENSE_GPL")

if "{{cookiecutter.open_source_license}}" == "Apache Software License 2.0":
move_file("LICENSE_APACHE", "LICENSE")
remove_file("LICENSE_MIT")
remove_file("LICENSE_BSD")
remove_file("LICENSE_ISC")
remove_file("LICENSE_GPL")

if "{{cookiecutter.open_source_license}}" == "GNU General Public License v3":
move_file("LICENSE_GPL", "LICENSE")
remove_file("LICENSE_MIT")
remove_file("LICENSE_BSD")
remove_file("LICENSE_ISC")
remove_file("LICENSE_APACHE")

if "{{cookiecutter.open_source_license}}" == "Not open source":
remove_file("LICENSE_GPL")
remove_file("LICENSE_MIT")
remove_file("LICENSE_BSD")
remove_file("LICENSE_ISC")
remove_file("LICENSE_APACHE")
82 changes: 82 additions & 0 deletions tests/test_cookiecutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,85 @@ def test_remove_release_workflow(cookies, tmp_path):
result = cookies.bake(extra_context={"publish_to_pypi": "n", "mkdocs": "n"})
assert result.exit_code == 0
assert not os.path.isfile(f"{result.project_path}/.github/workflows/on-release-main.yml")


def test_license_mit(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"open_source_license": "MIT license"})
assert result.exit_code == 0
assert os.path.isfile(f"{result.project_path}/LICENSE")
assert not os.path.isfile(f"{result.project_path}/LICENSE_BSD")
assert not os.path.isfile(f"{result.project_path}/LICENSE_ISC")
assert not os.path.isfile(f"{result.project_path}/LICENSE_APACHE")
assert not os.path.isfile(f"{result.project_path}/LICENSE_GPL")
with open(f"{result.project_path}/LICENSE", encoding="utf8") as licfile:
content = licfile.readlines()
assert len(content) == 21


def test_license_bsd(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"open_source_license": "BSD license"})
assert result.exit_code == 0
assert os.path.isfile(f"{result.project_path}/LICENSE")
assert not os.path.isfile(f"{result.project_path}/LICENSE_MIT")
assert not os.path.isfile(f"{result.project_path}/LICENSE_ISC")
assert not os.path.isfile(f"{result.project_path}/LICENSE_APACHE")
assert not os.path.isfile(f"{result.project_path}/LICENSE_GPL")
with open(f"{result.project_path}/LICENSE", encoding="utf8") as licfile:
content = licfile.readlines()
assert len(content) == 28


def test_license_isc(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"open_source_license": "ISC license"})
assert result.exit_code == 0
assert os.path.isfile(f"{result.project_path}/LICENSE")
assert not os.path.isfile(f"{result.project_path}/LICENSE_MIT")
assert not os.path.isfile(f"{result.project_path}/LICENSE_BSD")
assert not os.path.isfile(f"{result.project_path}/LICENSE_APACHE")
assert not os.path.isfile(f"{result.project_path}/LICENSE_GPL")
with open(f"{result.project_path}/LICENSE", encoding="utf8") as licfile:
content = licfile.readlines()
assert len(content) == 7


def test_license_apache(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"open_source_license": "Apache Software License 2.0"})
assert result.exit_code == 0
assert os.path.isfile(f"{result.project_path}/LICENSE")
assert not os.path.isfile(f"{result.project_path}/LICENSE_MIT")
assert not os.path.isfile(f"{result.project_path}/LICENSE_BSD")
assert not os.path.isfile(f"{result.project_path}/LICENSE_ISC")
assert not os.path.isfile(f"{result.project_path}/LICENSE_GPL")
with open(f"{result.project_path}/LICENSE", encoding="utf8") as licfile:
content = licfile.readlines()
assert len(content) == 202


def test_license_gplv3(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"open_source_license": "GNU General Public License v3"})
assert result.exit_code == 0
assert os.path.isfile(f"{result.project_path}/LICENSE")
assert not os.path.isfile(f"{result.project_path}/LICENSE_MIT")
assert not os.path.isfile(f"{result.project_path}/LICENSE_BSD")
assert not os.path.isfile(f"{result.project_path}/LICENSE_ISC")
assert not os.path.isfile(f"{result.project_path}/LICENSE_APACHE")
with open(f"{result.project_path}/LICENSE", encoding="utf8") as licfile:
content = licfile.readlines()
assert len(content) == 674


def test_license_no_license(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"open_source_license": "Not open source"})
assert result.exit_code == 0
assert not os.path.isfile(f"{result.project_path}/LICENSE")
assert not os.path.isfile(f"{result.project_path}/LICENSE_MIT")
assert not os.path.isfile(f"{result.project_path}/LICENSE_BSD")
assert not os.path.isfile(f"{result.project_path}/LICENSE_ISC")
assert not os.path.isfile(f"{result.project_path}/LICENSE_APACHE")
assert not os.path.isfile(f"{result.project_path}/LICENSE_GPL")
Loading

0 comments on commit 1e083e8

Please sign in to comment.