-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration test for running pre-commit (#41)
Dependency issues in .pre-commit-hooks.yaml previously caused hooks other than verify-alpha-spec to fail to install. Add an integration test where we actually run pre-commit with every hook to avoid such issues in the future.
- Loading branch information
1 parent
0c34e31
commit 24f5dc0
Showing
13 changed files
with
144 additions
and
2 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
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
6 changes: 6 additions & 0 deletions
6
test/examples/verify-alpha-spec/fail/master/dependencies.yaml
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,6 @@ | ||
dependencies: | ||
test: | ||
common: | ||
- output_types: pyproject | ||
packages: | ||
- cudf>=24.04,<24.06 |
6 changes: 6 additions & 0 deletions
6
test/examples/verify-alpha-spec/pass/master/dependencies.yaml
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,6 @@ | ||
dependencies: | ||
test: | ||
common: | ||
- output_types: pyproject | ||
packages: | ||
- cudf>=24.04,<24.06,>=0.0.0a0 |
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 @@ | ||
conda install pkg1 |
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 @@ | ||
conda install -y pkg1 |
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,3 @@ | ||
# Copyright (c) 2023, NVIDIA CORPORATION. | ||
|
||
print("New code") |
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 @@ | ||
# Copyright (c) 2023, NVIDIA CORPORATION. |
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,3 @@ | ||
# Copyright (c) 2023-2024, NVIDIA CORPORATION. | ||
|
||
print("New code") |
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 @@ | ||
# Copyright (c) 2023, NVIDIA CORPORATION. |
2 changes: 2 additions & 0 deletions
2
test/examples/verify-pyproject-license/fail/master/pyproject.toml
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,2 @@ | ||
[project] | ||
license = { text = "Apache-2.0" } |
2 changes: 2 additions & 0 deletions
2
test/examples/verify-pyproject-license/pass/master/pyproject.toml
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,2 @@ | ||
[project] | ||
license = { text = "Apache 2.0" } |
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,114 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import contextlib | ||
import datetime | ||
import os.path | ||
import shutil | ||
import subprocess | ||
import sys | ||
from functools import cache | ||
|
||
import git | ||
import pytest | ||
import yaml | ||
from packaging.version import Version | ||
from rapids_metadata.remote import fetch_latest | ||
|
||
REPO_DIR = os.path.join(os.path.dirname(__file__), "..") | ||
with open(os.path.join(REPO_DIR, ".pre-commit-hooks.yaml")) as f: | ||
ALL_HOOKS = [hook["id"] for hook in yaml.safe_load(f)] | ||
EXAMPLES_DIR = os.path.join(os.path.dirname(__file__), "examples") | ||
|
||
|
||
@cache | ||
def all_metadata(): | ||
return fetch_latest() | ||
|
||
|
||
@contextlib.contextmanager | ||
def set_cwd(cwd): | ||
old_cwd = os.getcwd() | ||
os.chdir(cwd) | ||
try: | ||
yield | ||
finally: | ||
os.chdir(old_cwd) | ||
|
||
|
||
@pytest.fixture | ||
def git_repo(tmp_path): | ||
repo = git.Repo.init(tmp_path) | ||
with repo.config_writer() as w: | ||
w.set_value("user", "name", "RAPIDS Test Fixtures") | ||
w.set_value("user", "email", "[email protected]") | ||
return repo | ||
|
||
|
||
def run_pre_commit(git_repo, hook_name, expected_status, exc): | ||
def list_files(top): | ||
for dirpath, _, filenames in os.walk(top): | ||
for filename in filenames: | ||
yield filename if top == dirpath else os.path.join( | ||
os.path.relpath(top, dirpath), filename | ||
) | ||
|
||
example_dir = os.path.join(EXAMPLES_DIR, hook_name, expected_status) | ||
master_dir = os.path.join(example_dir, "master") | ||
shutil.copytree(master_dir, git_repo.working_tree_dir, dirs_exist_ok=True) | ||
|
||
with open(os.path.join(git_repo.working_tree_dir, "VERSION"), "w") as f: | ||
f.write(f"{max(all_metadata().versions.keys(), key=Version)}\n") | ||
git_repo.index.add("VERSION") | ||
|
||
git_repo.index.add(list_files(master_dir)) | ||
git_repo.index.commit( | ||
"Initial commit", | ||
commit_date=datetime.datetime(2023, 2, 1, tzinfo=datetime.timezone.utc), | ||
) | ||
|
||
branch_dir = os.path.join(example_dir, "branch") | ||
if os.path.exists(branch_dir): | ||
git_repo.head.reference = git_repo.create_head("branch", git_repo.head.commit) | ||
git_repo.index.remove(list_files(master_dir), working_tree=True) | ||
shutil.copytree(branch_dir, git_repo.working_tree_dir, dirs_exist_ok=True) | ||
git_repo.index.add(list_files(branch_dir)) | ||
git_repo.index.commit( | ||
"Make some changes", | ||
commit_date=datetime.datetime(2024, 2, 1, tzinfo=datetime.timezone.utc), | ||
) | ||
|
||
with set_cwd(git_repo.working_tree_dir), pytest.raises( | ||
exc | ||
) if exc else contextlib.nullcontext(): | ||
subprocess.check_call( | ||
[sys.executable, "-m", "pre_commit", "try-repo", REPO_DIR, hook_name, "-a"], | ||
env={**os.environ, "TARGET_BRANCH": "master"}, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"hook_name", | ||
ALL_HOOKS, | ||
) | ||
def test_pre_commit_pass(git_repo, hook_name): | ||
run_pre_commit(git_repo, hook_name, "pass", None) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"hook_name", | ||
ALL_HOOKS, | ||
) | ||
def test_pre_commit_fail(git_repo, hook_name): | ||
run_pre_commit(git_repo, hook_name, "fail", subprocess.CalledProcessError) |