Skip to content

Commit acb5138

Browse files
Read npm licenses from the selected release
Signed-off-by: Ali Zulfiqar <codewithfourtix@gmail.com>
1 parent 27c4fc9 commit acb5138

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/fetchcode/package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def get_npm_data_from_purl(purl):
143143
vcs_url = repository.get("url")
144144
download_url = dist.get("tarball")
145145
bug_tracking_url = bugs.get("url")
146-
declared_license = license
146+
declared_license = version.get("license", license)
147147

148148
if purl.version and version_purl.version != purl.version:
149149
continue

tests/test_npm_version_license.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# fetchcode is a free software tool from nexB Inc. and others.
2+
# Visit https://github.com/aboutcode-org/fetchcode for support and download.
3+
4+
# Copyright (c) nexB Inc. and others. All rights reserved.
5+
# http://nexb.com and http://aboutcode.org
6+
7+
# This software is licensed under the Apache License version 2.0.
8+
9+
# You may not use this software except in compliance with the License.
10+
# You may obtain a copy of the License at:
11+
# http://apache.org/licenses/LICENSE-2.0
12+
# Unless required by applicable law or agreed to in writing, software distributed
13+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations under the License.
16+
17+
from fetchcode import package
18+
19+
20+
def test_npm_metadata_uses_release_license(monkeypatch):
21+
monkeypatch.setattr(
22+
package,
23+
"get_response",
24+
lambda url: {
25+
"license": "Apache-2.0",
26+
"versions": {
27+
"1.0": {"version": "1.0", "license": "MIT"},
28+
"2.0": {"version": "2.0", "license": "Apache-2.0"},
29+
"0.5": {"version": "0.5"},
30+
},
31+
},
32+
)
33+
results = list(package.get_npm_data_from_purl("pkg:npm/example"))
34+
assert {item.version: item.declared_license for item in results} == {
35+
"1.0": "MIT",
36+
"2.0": "Apache-2.0",
37+
"0.5": "Apache-2.0",
38+
}

0 commit comments

Comments
 (0)