Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
927d6a3
Added git diff
mehtamohit013 May 26, 2023
43758d8
test modified
mehtamohit013 May 26, 2023
f9977da
Fix
mehtamohit013 May 26, 2023
3a8f70a
Format
mehtamohit013 May 26, 2023
8f0a825
Changed API
mehtamohit013 May 30, 2023
ce8a4f9
Fix
mehtamohit013 May 30, 2023
8e8e409
tmp package
mehtamohit013 May 30, 2023
18a1814
Fix tests
mehtamohit013 May 30, 2023
242cf1a
Fix test
mehtamohit013 May 30, 2023
92533de
Changed args
mehtamohit013 May 31, 2023
f4de4d4
Added sys exit
mehtamohit013 May 31, 2023
c928988
Revert sys.exit
mehtamohit013 May 31, 2023
99dc484
Lint
mehtamohit013 May 31, 2023
8f880a8
Added test case
mehtamohit013 Jun 1, 2023
b2667bb
Renaming
mehtamohit013 Jun 1, 2023
8c09d1b
Added Pathlib implementation
mehtamohit013 Jun 1, 2023
34853e5
tmp deleted workdlow tag check
mehtamohit013 Jun 1, 2023
3a7a615
Re added workflow edits
mehtamohit013 Jun 1, 2023
631c27c
Fix CI
mehtamohit013 Jun 1, 2023
f867bb1
Commented Tests
mehtamohit013 Jun 1, 2023
ce3ee64
Format
mehtamohit013 Jun 1, 2023
539c7eb
Commented some more
mehtamohit013 Jun 1, 2023
0fdbc34
more comments
mehtamohit013 Jun 1, 2023
9be7f6d
Fix conftest
mehtamohit013 Jun 1, 2023
86989b9
Format
mehtamohit013 Jun 1, 2023
c03a61a
Fix conftest
mehtamohit013 Jun 2, 2023
5486626
Format
mehtamohit013 Jun 2, 2023
4a056eb
Verbose testing
mehtamohit013 Jun 2, 2023
51e382f
Revert back CI changes
mehtamohit013 Jun 2, 2023
c7e2d47
Moved to sys.exit
mehtamohit013 Jun 2, 2023
8cfd99a
Minor
mehtamohit013 Jun 2, 2023
a5998cc
Minor
mehtamohit013 Jun 2, 2023
7c264f7
Merge branch 'doc' of github.com:mehtamohit013/pkgmt into doc
mehtamohit013 Jun 2, 2023
fe85854
Minor
mehtamohit013 Jun 2, 2023
83ced20
Format
mehtamohit013 Jun 2, 2023
20c65e0
Fix
mehtamohit013 Jun 3, 2023
b7f0147
Windows CI
mehtamohit013 Jun 3, 2023
988f1d3
Revert back Windows CI
mehtamohit013 Jun 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/pkgmt/fail_if_modified.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import subprocess
import argparse
import sys


def check_modified(base_branch, exclude_path, debug=False):
# https://stackoverflow.com/questions/4380945
cmd = f"git diff --exit-code {base_branch}... -- ."

for path in exclude_path:
cmd += f" ':^{path}'"

if debug:
print(f"cmd: {cmd}")
try:
subprocess.check_output(cmd, shell=True)
return 0
except subprocess.CalledProcessError as err:
if debug:
print(
f"Path has been modified with respect to '{base_branch}'\n"
f"Excluding paths: {exclude_path}"
)
print(f"Return code: {err.returncode}")
print(f"Output: {err.output}")
return 1


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Check if a branch has modified" "anything excluding some path/dir"
)
parser.add_argument(
"-b", "--base-branch", default="main", help="Base branch to compare against"
)
parser.add_argument(
"-e",
"--exclude-path",
default=["doc"],
nargs="+",
help="Path to exclude from git diff."
"Can be used multiple times eg: -e p1 -e p2",
)
parser.add_argument("--debug", action="store_true", help="Print debug info")

args = parser.parse_args()
returncode = check_modified(args.base_branch, args.exclude_path, debug=args.debug)
sys.exit(returncode)
66 changes: 66 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,69 @@ def tmp_another_package(root, tmp_empty):
yield tmp_empty

os.chdir(old)


@pytest.fixture
def tmp_package_modi(root, tmp_empty):
old = Path.cwd()
path_to_templates = root / "tests" / "assets" / "package_name"

shutil.copytree(str(path_to_templates), "copy")
os.chdir("copy")

subprocess.run(["git", "init"])
subprocess.check_call(["git", "config", "commit.gpgsign", "false"])
subprocess.check_call(["git", "config", "user.email", "ci@ploomberio"])
subprocess.check_call(["git", "config", "user.name", "Ploomber"])
subprocess.run(["git", "checkout", "-b", "main"])
subprocess.run(["git", "add", "--all"])
subprocess.run(["git", "commit", "-m", "init-commit-message"])

subprocess.run(["git", "checkout", "-b", "test_modified_doc"])
Path.cwd().joinpath("test_doc1").mkdir(parents=True, exist_ok=True)
Path.cwd().joinpath("test_doc1", "test_modified.txt").touch()
Path.cwd().joinpath("test_doc2").mkdir(parents=True, exist_ok=True)
Path.cwd().joinpath("test_doc2", "test_modified.txt").touch()
subprocess.run(["git", "add", "."])
subprocess.run(["git", "commit", "-m", "test_modified"])
Comment thread
mehtamohit013 marked this conversation as resolved.

yield tmp_empty

os.chdir(old)


@pytest.fixture
def tmp_package_modi_2(root, tmp_empty):
old = Path.cwd()
path_to_templates = root / "tests" / "assets" / "package_name"
shutil.copytree(str(path_to_templates), "copy")
os.chdir("copy")

subprocess.run(["git", "init"])
subprocess.check_call(["git", "config", "commit.gpgsign", "false"])
subprocess.check_call(["git", "config", "user.email", "ci@ploomberio"])
subprocess.check_call(["git", "config", "user.name", "Ploomber"])
subprocess.run(["git", "checkout", "-b", "main"])
subprocess.run(["git", "add", "--all"])
subprocess.run(["git", "commit", "-m", "init-commit-message"])

Path.cwd().joinpath("doc").mkdir(parents=True, exist_ok=True)
Path.cwd().joinpath("doc", "file.txt").touch()
subprocess.run(["git", "add", "."])
subprocess.run(["git", "commit", "-m", "added doc/file.txt"])

subprocess.run(["git", "checkout", "-b", "test_modified_doc"])
Path.cwd().joinpath("doc").mkdir(parents=True, exist_ok=True)
Path.cwd().joinpath("doc", "another.txt").touch()
subprocess.run(["git", "add", "."])
subprocess.run(["git", "commit", "-m", "add doc/another.txt"])

subprocess.run(["git", "checkout", "main"])
Path.cwd().joinpath("something").mkdir(parents=True, exist_ok=True)
Path.cwd().joinpath("something", "file.txt").touch()
subprocess.run(["git", "add", "."])
subprocess.run(["git", "commit", "-m", "added something/file.txt"])

yield tmp_empty

os.chdir(old)
28 changes: 28 additions & 0 deletions tests/test_modified.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest
from pkgmt import fail_if_modified


@pytest.mark.parametrize(
"base_branch, exclude_path, returncode",
[
[
"main",
["test_doc1"],
1,
],
[
"main",
["test_doc1", "test_doc2"],
0,
],
],
)
def test_check_modified(tmp_package_modi, base_branch, exclude_path, returncode):
assert (
fail_if_modified.check_modified(base_branch, exclude_path, debug=True)
== returncode
)


def test_check_modified_2(tmp_package_modi_2):
assert fail_if_modified.check_modified("main", ["doc"], debug=True) == 0