Skip to content

Commit

Permalink
Merge pull request #382 from oracle-samples/ODSC-49119/add_tests_to_c…
Browse files Browse the repository at this point in the history
…opyright_check

ODSC-49119. Added tests for check_copyright pre-commit
  • Loading branch information
liudmylaru authored Jan 10, 2024
2 parents 5a31f8f + 305ab1e commit 864665b
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test_check_copyright.yml
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.
94 changes: 94 additions & 0 deletions pre_commit_scripts/test_check_copyright.py
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

0 comments on commit 864665b

Please sign in to comment.