-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #382 from oracle-samples/ODSC-49119/add_tests_to_c…
…opyright_check ODSC-49119. Added tests for check_copyright pre-commit
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: "Test Check Copyright Pre-Commit" | ||
on: | ||
workflow_dispatch: | ||
pull_request: | ||
paths: | ||
- "pre_commit_scripts/**" | ||
|
||
jobs: | ||
test: | ||
name: "Run tests for check copyright pre-commit code" | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-python@v4 | ||
python-version: "3.10" | ||
- shell: bash | ||
run: | | ||
set -x # print commands that are executed | ||
$CONDA/bin/conda init | ||
source /home/runner/.bashrc | ||
pip install pytest | ||
python -m pytest -v -p no:warnings pre_commit_scripts/test_check_copyright.py |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import datetime | ||
import sys | ||
import os | ||
|
||
import pytest | ||
|
||
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
sys.path.append(os.path.dirname(SCRIPT_DIR)) | ||
|
||
from pre_commit_scripts.check_copyright import main | ||
|
||
class TestCopyrightCheck: | ||
|
||
CURRENT_YEAR = datetime.date.today().year | ||
|
||
def test_exit(self): | ||
with pytest.raises(SystemExit) as pytest_wrapped_e: | ||
main([]) | ||
assert pytest_wrapped_e.type == SystemExit | ||
assert pytest_wrapped_e.value.code == 0 | ||
|
||
def test_copyright_check_empty(self, tmp_path, capsys): | ||
p = tmp_path / "empty.py" | ||
s = '''\ | ||
This is an empty file. | ||
''' | ||
p.write_text(s) | ||
assert p.read_text() == s | ||
with pytest.raises(SystemExit) as pytest_wrapped_e: | ||
main([p]) | ||
assert pytest_wrapped_e.value.code == 1 | ||
captured = capsys.readouterr() | ||
assert "or year last edited not correct" in captured.out | ||
|
||
def test_copyright_check_only_current_year(self, tmp_path): | ||
p = tmp_path / "only_current_year.py" | ||
s = f'''\ | ||
Copyright (c) {self.CURRENT_YEAR} Oracle and/or its affiliates | ||
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ | ||
This is a sentence. | ||
''' | ||
p.write_text(s) | ||
assert p.read_text() == s | ||
with pytest.raises(SystemExit) as pytest_wrapped_e: | ||
main([p]) | ||
assert pytest_wrapped_e.type == SystemExit | ||
assert pytest_wrapped_e.value.code == 0 | ||
|
||
def test_copyright_check_2022_and_current_year(self, tmp_path): | ||
p = tmp_path / "2022_and_current_year.py" | ||
s = f'''\ | ||
Copyright (c) 2022, {self.CURRENT_YEAR} Oracle and/or its affiliates | ||
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ | ||
This is a sentence. | ||
''' | ||
p.write_text(s) | ||
assert p.read_text() == s | ||
with pytest.raises(SystemExit) as pytest_wrapped_e: | ||
main([p]) | ||
assert pytest_wrapped_e.type == SystemExit | ||
assert pytest_wrapped_e.value.code == 0 | ||
|
||
def test_copyright_incorrect_statement(self, tmp_path, capsys): | ||
p = tmp_path / "incorrect_statement.py" | ||
s = f'''\ | ||
Copyright (c) {self.CURRENT_YEAR} Oracle and/or its affiliates | ||
Licensed under ---error in statement----- 1.0 as shown at https://oss.oracle.com/licenses/upl/ | ||
This is a sentence. | ||
''' | ||
p.write_text(s) | ||
assert p.read_text() == s | ||
with pytest.raises(SystemExit) as pytest_wrapped_e: | ||
main([p]) | ||
assert pytest_wrapped_e.value.code == 1 | ||
captured = capsys.readouterr() | ||
assert "missing or incomplete" in captured.out | ||
|
||
def test_copyright_non_copyright(self, tmp_path, capsys): | ||
p = tmp_path / "incorrect_statement.py" | ||
s = '''\ | ||
This is a sentence. | ||
This is another sentence. | ||
''' | ||
p.write_text(s) | ||
assert p.read_text() == s | ||
with pytest.raises(SystemExit) as pytest_wrapped_e: | ||
main([p]) | ||
assert pytest_wrapped_e.value.code == 1 | ||
captured = capsys.readouterr() | ||
assert "not correct" in captured.out |