Skip to content

Commit

Permalink
maint: bump reuse to v3.0.2 in add-license-headers (#163)
Browse files Browse the repository at this point in the history
Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
klmcadams and pyansys-ci-bot authored Apr 16, 2024
1 parent b6315e3 commit fad76f5
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 97 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
- uses: ansys/actions/doc-style@v5
with:
token: ${{ secrets.GITHUB_TOKEN }}
vale-version: "3.4.1"

smoke-tests:
name: "Build and Smoke tests"
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 ANSYS, Inc. and/or its affiliates.
Copyright (c) 2023 - 2024 ANSYS, Inc. and/or its affiliates.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
2 changes: 2 additions & 0 deletions doc/.vale.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ Vocab = ANSYS

# Apply the following styles
BasedOnStyles = Vale, Google

Vale.Terms = NO
1 change: 1 addition & 0 deletions doc/changelog.d/163.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
maint: bump reuse to v3.0.2 in add-license-headers
4 changes: 2 additions & 2 deletions doc/styles/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*
!Vocab
!Vocab/**
!vocabularies
!vocabularies/**
!.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ API
ANSYS
Ansys
ansys
maint
File renamed without changes.
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ classifiers = [
]
dependencies = [
"importlib-metadata==7.1.0",
"reuse==2.1.0",
"reuse==3.0.2",
"GitPython==3.1.43",
]

[project.optional-dependencies]
tests = [
"pytest==8.1.1",
"pytest-cov==5.0.0",
"reuse==2.1.0",
"GitPython==3.1.43",
]
doc = [
"ansys-sphinx-theme[autoapi]==0.15.2",
Expand Down
44 changes: 16 additions & 28 deletions src/ansys/pre_commit_hooks/add_license_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from tempfile import NamedTemporaryFile

import git
from reuse import _util, header, lint, project
from reuse import _annotate, _util, lint, project

DEFAULT_TEMPLATE = "ansys"
"""Default template to use for license headers."""
Expand Down Expand Up @@ -200,12 +200,6 @@ def list_noncompliant_files(args: argparse.Namespace, proj: project.Project) ->
missing_licensing_info = set(lint_json["non_compliant"]["missing_licensing_info"])
missing_headers = missing_headers.union(missing_licensing_info)

if lint_json["non_compliant"]["missing_licenses"]:
missing_licenses = set(
lint_json["non_compliant"]["missing_licenses"][args.custom_license]
)
missing_headers = missing_headers.union(missing_licenses)

# Remove temporary file
os.remove(filename)

Expand Down Expand Up @@ -244,9 +238,9 @@ def set_header_args(
# Provide values for license header arguments
args = parser.parse_args([file_path])
if start_year == current_year:
args.year = [str(current_year)]
args.year = [current_year]
else:
args.year = [start_year, str(current_year)]
args.year = [int(start_year), current_year]
args.copyright_style = "string-c"
args.copyright = [copyright]
args.merge_copyrights = True
Expand Down Expand Up @@ -296,48 +290,41 @@ def check_exists(
copyright = values["copyright"]
template = values["template"]

if i < len(files):
for file in files:
args = set_header_args(parser, start_year, current_year, file, copyright, template)
# If the committed file is in missing_headers
if (files[i] in missing_headers) or (os.path.getsize(files[i]) == 0):
if (file in missing_headers) or (os.path.getsize(file) == 0):
changed_headers = 1
# Run REUSE on the file
args = set_header_args(parser, start_year, current_year, files[i], copyright, template)
if not args.ignore_license_check:
args.license = [values["license"]]
header.run(args, proj)

# Check if the next file is in missing_headers
return check_exists(changed_headers, parser, values, proj, missing_headers, i + 1)
_annotate.run(args, proj)
else:
# Save current copy of files[i]
# Save current copy of file
before_hook = NamedTemporaryFile(mode="w", delete=False).name
shutil.copyfile(files[i], before_hook)
shutil.copyfile(file, before_hook)

# Update the header
# tmp captures the stdout of the header.run() function
with NamedTemporaryFile(mode="w", delete=True) as tmp:
args = set_header_args(
parser, start_year, current_year, files[i], copyright, template
)
header.run(args, proj, tmp)
_annotate.run(args, proj, tmp)

# Check if the file before add-license-headers was run is the same as the one
# after add-license-headers was run. If not, apply the syntax changes
# from other hooks before add-license-headers was run to the file
if check_same_content(before_hook, files[i]) == False:
add_hook_changes(before_hook, files[i])
if check_same_content(before_hook, file) == False:
add_hook_changes(before_hook, file)

# Check if the file content before add-license-headers was run has been changed
# Assuming the syntax was fixed in the above if statement, this check is
# solely for the file's content
if check_same_content(before_hook, files[i]) == False:
if check_same_content(before_hook, file) == False:
changed_headers = 1
print(f"Successfully changed header of {files[i]}")
print(f"Successfully changed header of {file}")

os.remove(before_hook)

return check_exists(changed_headers, parser, values, proj, missing_headers, i + 1)

return changed_headers


Expand Down Expand Up @@ -634,7 +621,8 @@ def find_files_missing_header() -> int:
# year, style, copyright-style, template, exclude-year, merge-copyrights, single-line,
# multi-line, explicit-license, force-dot-license, recursive, no-replace,
# skip-unrecognized, and skip-existing
header.add_arguments(parser)
# header.add_arguments(parser)
_annotate.add_arguments(parser)

# Link the default template and/or license from the assets folder to your git repo.
link_assets(assets, os_git_root, args)
Expand Down
74 changes: 11 additions & 63 deletions tests/test_reuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

git_repo = git.Repo(os.getcwd(), search_parent_directories=True)
REPO_PATH = git_repo.git.rev_parse("--show-toplevel")
START_YEAR = "2023"


def set_up_repo(tmp_path, template_path, template_name, license_path, license_name):
Expand Down Expand Up @@ -143,7 +144,7 @@ def test_custom_start_year(tmp_path: pytest.TempPathFactory):

# Set up git repository in tmp_path with temporary file
repo, tmp_file = set_up_repo(tmp_path, template_path, template_name, license_path, license_name)
custom_args = [tmp_file, "--start_year=2023"]
custom_args = [tmp_file, f"--start_year={START_YEAR}"]

# Assert the hook fails because it added the header
assert add_argv_run(repo, tmp_file, custom_args) == 1
Expand All @@ -155,7 +156,7 @@ def test_custom_start_year(tmp_path: pytest.TempPathFactory):
# Assert the copyright line's time range is
# from 2023 to the current year
if count == 1:
assert f"2023 - {dt.today().year}" in line
assert f"{START_YEAR} - {dt.today().year}" in line
if count > 1:
break
file.close()
Expand Down Expand Up @@ -282,8 +283,9 @@ def test_main_fails(tmp_path: pytest.TempPathFactory):

# Set up git repository in tmp_path with temporary file
repo, tmp_file = set_up_repo(tmp_path, template_path, template_name, license_path, license_name)
custom_args = [tmp_file, f"--start_year={START_YEAR}"]

assert add_argv_run(repo, tmp_file, [tmp_file]) == 1
assert add_argv_run(repo, tmp_file, custom_args) == 1

check_ansys_header(tmp_file)

Expand All @@ -292,7 +294,7 @@ def test_main_fails(tmp_path: pytest.TempPathFactory):

def test_main_passes():
"""Test all files are compliant."""
sys.argv[1:] = []
sys.argv[1:] = [f"--start_year={START_YEAR}"]

# Assert main runs successfully
assert hook.main() == 0
Expand Down Expand Up @@ -442,60 +444,6 @@ def test_update_changed_header(tmp_path: pytest.TempPathFactory):
os.chdir(REPO_PATH)


def test_missing_licenses(tmp_path: pytest.TempPathFactory):
"""Test that LICENSES folder is required."""
# Set template and license names
template_name = "test_template.jinja2"
license_name = "ECL-1.0.txt"
template_path = os.path.join(REPO_PATH, "tests", "test_reuse_files", "templates", template_name)
license_path = os.path.join(REPO_PATH, "tests", "test_reuse_files", "LICENSES", license_name)

# Set up git repository in tmp_path with temporary file
repo, tmp_file = set_up_repo(tmp_path, template_path, template_name, license_path, license_name)

# Remove LICENSES/ECL-1.0.txt file from tmp_path
os.remove(os.path.join(tmp_path, "LICENSES", license_name))

custom_args = [
tmp_file,
"--custom_template=test_template",
"--custom_license=ECL-1.0",
]

# Add header to tmp_file
add_argv_run(repo, tmp_file, custom_args)

custom_args = [
tmp_file,
"--custom_template=test_template",
"--custom_license=ECL-1.0",
]

# If LICENSES/license_name.txt file is missing, then it will fail
# and add another SPDX line. This shows you need the
# license_name.txt (MIT.txt, for example) in LICENSES or else it
# will fail
assert add_argv_run(repo, tmp_file, custom_args) == 1

# Assert two SPDX-License lines are found in the file if
# the LICENSES/ECL-1.0.txt file does not exist.
file = open(tmp_file, "r")
count = 0
for line in file:
count += 1
if count == 6:
assert "SPDX-License" in line
# Ensure header was updated correctly and didn't add
# an extra SPDX-Identifier line
if count == 7:
assert "SPDX-License" in line
if count > 7:
break
file.close()

os.chdir(REPO_PATH)


def test_copy_assets(tmp_path: pytest.TempPathFactory):
"""Test .reuse and LICENSES folders are copied."""
# List of files to be git added
Expand Down Expand Up @@ -588,7 +536,7 @@ def test_index_exception(tmp_path: pytest.TempPathFactory):
"--custom_copyright=ANSYS, Inc. Unauthorized use, distribution, \
or duplication is prohibited.",
"--ignore_license_check",
"--start_year=2023",
f"--start_year={START_YEAR}",
]

# Assert the hook failed
Expand Down Expand Up @@ -652,16 +600,16 @@ def test_license_year_update(tmp_path: pytest.TempPathFactory):
values = {
"copyright": args.custom_copyright,
"license": args.custom_license,
"start_year": args.start_year,
"start_year": START_YEAR,
"current_year": dt.today().year,
"git_repo": git_repo,
}

# Years to update the LICENSE file
years = [values["start_year"], "2022", dt.today().year]
years = ["2022", dt.today().year, values["start_year"]]

# Check the copyright line has "2023 - {current_year}", "2022 - {current_year}",
# and "{current_year}"
# Check the copyright line has "2022 - {current_year}", "{current_year}"
# and "2023 - {current_year}"
for year in years:
values["start_year"] = year
hook.update_license_file(values)
Expand Down

0 comments on commit fad76f5

Please sign in to comment.