Skip to content

Commit fb156bc

Browse files
fcollonvalpre-commit-ci[bot]blink1073
authored
Add test for repository.url (#539)
* add test for repository.url * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix test and lint * fix windows handling --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Steven Silvester <steven.silvester@ieee.org>
1 parent 5f6c9f6 commit fb156bc

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

jupyter_releaser/cli.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,14 @@ def main(force):
183183
click.option("--version-cmd", envvar="RH_VERSION_COMMAND", help="The version command")
184184
]
185185

186+
repo_options: t.Any = [
187+
click.option("--repo", envvar="RH_REPOSITORY", help="The git repo"),
188+
]
186189

187-
branch_options: t.Any = [
190+
branch_options: t.Any = [ # noqa: RUF005
188191
click.option("--ref", envvar="RH_REF", help="The source reference"),
189192
click.option("--branch", envvar="RH_BRANCH", help="The target branch"),
190-
click.option("--repo", envvar="RH_REPOSITORY", help="The git repo"),
191-
]
193+
] + repo_options
192194

193195
auth_options: t.Any = [
194196
click.option("--auth", envvar="GITHUB_ACCESS_TOKEN", help="The GitHub auth token"),
@@ -512,13 +514,14 @@ def build_npm(package, dist_dir):
512514
@main.command()
513515
@add_options(dist_dir_options)
514516
@add_options(npm_install_options)
517+
@add_options(repo_options)
515518
@use_checkout_dir()
516-
def check_npm(dist_dir, npm_install_options):
519+
def check_npm(dist_dir, npm_install_options, repo):
517520
"""Check npm package"""
518521
if not osp.exists("./package.json"):
519522
util.log("Skipping check-npm since there is no package.json file")
520523
return
521-
npm.check_dist(dist_dir, npm_install_options)
524+
npm.check_dist(dist_dir, npm_install_options, repo)
522525

523526

524527
@main.command()

jupyter_releaser/npm.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ def build_dist(package, dist_dir):
5555
)
5656

5757

58-
def extract_dist(dist_dir, target):
59-
"""Extract dist files from a dist_dir into a target dir"""
58+
def extract_dist(dist_dir, target, repo=""):
59+
"""Extract dist files from a dist_dir into a target dir
60+
61+
62+
If `repo` is provided, check that the repository URL is ending by it.
63+
"""
6064
names = []
6165
paths = sorted(glob(f"{dist_dir}/*.tgz"))
6266
util.log(f"Extracting {len(paths)} packages...")
@@ -67,6 +71,14 @@ def extract_dist(dist_dir, target):
6771
data = extract_package(path)
6872
name = data["name"]
6973

74+
if repo and os.name != "nt":
75+
url = data.get("repository", {}).get("url", "")
76+
if url.endswith(".git"):
77+
url = url[:-4]
78+
if not url.endswith(repo):
79+
msg = f"package.json for '{name}' does not define a 'repository.url' matching the cloned repository '{repo}'."
80+
raise ValueError(msg)
81+
7082
# Skip if it is a private package
7183
if data.get("private", False): # pragma: no cover
7284
util.log(f"Skipping private package {name}")
@@ -93,14 +105,16 @@ def extract_dist(dist_dir, target):
93105
return names
94106

95107

96-
def check_dist(dist_dir, install_options):
108+
def check_dist(dist_dir, install_options, repo):
97109
"""Check npm dist file(s) in a dist dir"""
110+
repo = repo or util.get_repo()
111+
98112
with TemporaryDirectory() as td:
99113
util.run("npm init -y", cwd=td, quiet=True)
100114
names = []
101115
staging = Path(td) / "staging"
102116

103-
names = extract_dist(dist_dir, staging)
117+
names = extract_dist(dist_dir, staging, repo)
104118

105119
install_str = " ".join(f"./staging/{name}" for name in names)
106120

jupyter_releaser/tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,19 @@ def workspace_package(npm_package):
112112
sub_data = json.loads(pkg_json.read_text(encoding="utf-8"))
113113
sub_data["dependencies"] = dict(bar="*")
114114
sub_data["main"] = "index.js"
115+
sub_data["repository"] = dict(url=str(npm_package))
115116
pkg_json.write_text(json.dumps(sub_data), encoding="utf-8")
116117
elif name == "baz":
117118
pkg_json = new_dir / "package.json"
118119
sub_data = json.loads(pkg_json.read_text(encoding="utf-8"))
119120
sub_data["dependencies"] = dict(foo="*")
120121
sub_data["main"] = "index.js"
122+
sub_data["repository"] = dict(url=str(npm_package))
123+
pkg_json.write_text(json.dumps(sub_data), encoding="utf-8")
124+
elif name == "bar":
125+
pkg_json = new_dir / "package.json"
126+
sub_data = json.loads(pkg_json.read_text(encoding="utf-8"))
127+
sub_data["repository"] = dict(url=str(npm_package))
121128
pkg_json.write_text(json.dumps(sub_data), encoding="utf-8")
122129
os.chdir(prev_dir)
123130
util.run("git add .")

jupyter_releaser/tests/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
3+
import json
34
import os
45
import shutil
56
import tempfile
@@ -178,6 +179,14 @@ def create_npm_package(git_repo):
178179
npm = util.normalize_path(shutil.which("npm"))
179180
run(f"{npm} init -y")
180181

182+
# Add the npm provenance info.
183+
pack_json = Path(git_repo / "package.json")
184+
with pack_json.open() as fid:
185+
data = json.load(fid)
186+
data["repository"] = dict(url=str(git_repo))
187+
with pack_json.open("w") as fid:
188+
json.dump(data, fid)
189+
181190
git_repo.joinpath("index.js").write_text('console.log("hello");\n', encoding="utf-8")
182191

183192
run("git add .")

0 commit comments

Comments
 (0)