new file mode 100644
@@ -0,0 +1,237 @@
+#
+# Copyright OpenEmbedded Contributors
+#
+# SPDX-License-Identifier: MIT
+#
+
+import io
+import tempfile
+
+from unittest.case import TestCase
+from oe.vendor import cargo, go, npm
+
+class VendorTestCase(TestCase):
+ def setUp(self):
+ self._t = tempfile.TemporaryDirectory()
+ self.tmpdir = self._t.name
+ self.addCleanup(self._t.cleanup)
+
+class TestCargoVendor(VendorTestCase):
+ def create_cargo_lock_file(self, data):
+ import tomllib
+ filename = os.path.join(self.tmpdir, "Cargo.lock")
+ with open(filename, "w") as f:
+ for package in data.get("package", []):
+ f.write("\n[[package]]\n")
+ for key in package.keys():
+ f.write(f'{key} = "{package[key]}"\n')
+ return filename
+
+ def test_valid(self):
+ filename = self.create_cargo_lock_file({
+ "package": [
+ {
+ "name": "regex",
+ "version": "1.4.0",
+ "source": "registry+https://github.com/rust-lang/crates.io-index",
+ "checksum": "36f45b719a674bf4b828ff318906d6c133264c793eff7a41e30074a45b5099e2"
+ }, {
+ "name": "regex",
+ "version": "1.5.0",
+ "source": "git+https://github.com/rust-lang/regex.git#9f9f693768c584971a4d53bc3c586c33ed3a6831"
+ }
+ ]
+ })
+ expected_uris = [
+ "https://proxy.com/cargo/api/v1/crates/regex/1.4.0/download;"
+ "name=regex;"
+ "version=1.4.0;"
+ "type=cargo;"
+ "subdir=dummy-4.5.6/vendor;"
+ "downloadfilename=cargo/regex-1.4.0.crate;"
+ "sha256sum=36f45b719a674bf4b828ff318906d6c133264c793eff7a41e30074a45b5099e2",
+ "git://github.com/rust-lang/regex.git;"
+ "protocol=https;"
+ "nobranch=1;"
+ "subdir=dummy-4.5.6/vendor/regex-1.5.0;"
+ "rev=9f9f693768c584971a4d53bc3c586c33ed3a6831"
+ ]
+ uris = cargo.resolve_src_uris(filename, "https://proxy.com/cargo",
+ "dummy-4.5.6", "vendor")
+ self.assertListEqual(uris, expected_uris)
+
+#
+# Go
+#
+class TestGoVendor(VendorTestCase):
+ def create_go_sum_file(self, data):
+ filename = os.path.join(self.tmpdir, "go.sum")
+ with open(filename, 'w') as f:
+ for module_path, version, hash in data:
+ f.write(f"{module_path} {version} {hash}\n")
+ return filename
+
+ def test_resolve_src_uris(self):
+ filename = self.create_go_sum_file([
+ (
+ "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob",
+ "v1.0.0",
+ "h1:u/LLAOFgsMv7HmNL4Qufg58y+qElGOt5qv0z1mURkRY="
+ ), (
+ "gopkg.in/ini.v1",
+ "v1.67.0/go.mod",
+ "h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k="
+ )
+ ])
+ expected_src_uris = [
+ "https://proxy.com/go/github.com/!azure/azure-sdk-for-go/sdk/storage/azblob/@v/v1.0.0.zip;"
+ "name=github.com/Azure/azure-sdk-for-go/sdk/storage/azblob;"
+ "version=v1.0.0;"
+ "vendor=go;"
+ "subdir=dummy-cache/cache/download/github.com/!azure/azure-sdk-for-go/sdk/storage/azblob/@v;"
+ "downloadfilename=go/github.com/!azure/azure-sdk-for-go/sdk/storage/azblob/@v/v1.0.0.zip;"
+ "unpack=0;"
+ "goh1sum=bbf2cb00e160b0cbfb1e634be10b9f839f32faa12518eb79aafd33d665119116",
+ "https://proxy.com/go/gopkg.in/ini.v1/@v/v1.67.0.mod;"
+ "name=gopkg.in/ini.v1;"
+ "version=v1.67.0;"
+ "subdir=dummy-cache/cache/download/gopkg.in/ini.v1/@v;"
+ "downloadfilename=go/gopkg.in/ini.v1/@v/v1.67.0.mod;"
+ "unpack=0;"
+ "goh1sum=a4d2dff16522c8d12d423baee46e6f4e6d3a4c4bfdb6c220780bcaf213ab3f89"
+ ]
+ src_uris = go.resolve_src_uris(filename, "https://proxy.com/go",
+ "dummy-cache")
+ self.assertListEqual(src_uris, expected_src_uris)
+
+#
+# npm
+#
+class TestNpmVendor(VendorTestCase):
+ def create_package_lock_file(self, data):
+ import json
+ filename = os.path.join(self.tmpdir, "package-lock.json")
+ with open(filename, 'w') as f:
+ json.dump(data, f)
+ return filename
+
+ def test_resolve_src_uris(self):
+ filename = self.create_package_lock_file({
+ "packages": {
+ "node_modules/array-flatten": {
+ "version": "1.1.1",
+ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
+ "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
+ },
+ "node_modules/array-flatten/node_modules/@nodelib/fs.stat": {
+ "version": "2.0.5",
+ "resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.5.tgz",
+ "integrity": "sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A=="
+ },
+ "node_modules/cookie": {
+ "resolved": "git+https://github.com/jshttp/cookie.git#aec1177c7da67e3b3273df96cf476824dbc9ae09"
+ },
+ "node_modules/jsdoc-nr-template": {
+ "version": "1.0.0",
+ "resolved": "git+ssh://git@github.com/node-red/jsdoc-nr-template.git#3c7c8f96d585c7c5918a2e63519310e1297e162d"
+ },
+ "node_modules/accepts": {
+ "version": "1.3.8",
+ "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz",
+ "integrity": "sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==",
+ "inBundle": True
+ },
+ "node_modules/string-width-cjs": {
+ "name": "string-width",
+ "version": "4.2.3",
+ "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz",
+ "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g=="
+ },
+ "node_modules/example-package": {
+ "name": "example-package",
+ "version": "1.2.3",
+ "resolved": "https://example.com/npm/example-package/-/example-package-1.2.3.tgz",
+ "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g=="
+ },
+ "node_modules/tarball": {
+ "name": "tarball",
+ "resolved": "https://example.com/tarball.tgz",
+ "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g=="
+ },
+ "node_modules/content-type": {
+ "version": "1.0.4",
+ "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz",
+ "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==",
+ "dev": True
+ },
+ "node_modules/karma": {
+ "resolved": "",
+ "link": True
+ },
+ }
+ })
+ expected_common_src_uris = [
+ "https://registry.com/npm/array-flatten/-/array-flatten-1.1.1.tgz;"
+ "name=array-flatten;"
+ "version=1.1.1;"
+ "vendor=npm;"
+ "subdir=dummy-4.5.6/node_modules/array-flatten;"
+ "downloadfilename=npm/array-flatten-1.1.1.tgz;"
+ "striplevel=1;"
+ "sha1sum=9a5f699051b1e7073328f2a008968b64ea2955d2",
+ "https://registry.com/npm/@nodelib/fs.stat/-/fs.stat-2.0.5.tgz;"
+ "name=@nodelib/fs.stat;"
+ "version=2.0.5;"
+ "vendor=npm;"
+ "subdir=dummy-4.5.6/node_modules/array-flatten/node_modules/@nodelib/fs.stat;"
+ "downloadfilename=npm/@nodelib-fs.stat-2.0.5.tgz;"
+ "striplevel=1;"
+ "sha512sum=46484f3e9db3aea0c0400ff68cd867ced70f025bfae17761229edaef8e78039a2f23b06e93182decc5fbb9dc00bb7ce0d437293d4d2bcf7555d5279aaaf638f8",
+ "git://github.com/jshttp/cookie.git;"
+ "protocol=https;nobranch=1;"
+ "subdir=dummy-4.5.6/node_modules/cookie;"
+ "rev=aec1177c7da67e3b3273df96cf476824dbc9ae09",
+ "git://github.com/node-red/jsdoc-nr-template.git;"
+ "protocol=https;nobranch=1;"
+ "subdir=dummy-4.5.6/node_modules/jsdoc-nr-template;"
+ "rev=3c7c8f96d585c7c5918a2e63519310e1297e162d",
+ "https://registry.com/npm/string-width/-/string-width-4.2.3.tgz;"
+ "name=string-width;"
+ "version=4.2.3;"
+ "vendor=npm;"
+ "subdir=dummy-4.5.6/node_modules/string-width-cjs;"
+ "downloadfilename=npm/string-width-4.2.3.tgz;"
+ "striplevel=1;"
+ "sha512sum=c0ac90450a63274b08a7ad84ad265d1ac8cc256b1aa79a1136284786ee86ec954effd8c807a5327af2feb57b8eaab9e0f23fdcc4a4d6c96530bd24eb8a2673fe",
+ "https://example.com/npm/example-package/-/example-package-1.2.3.tgz;"
+ "name=example-package;"
+ "version=1.2.3;"
+ "vendor=npm;"
+ "subdir=dummy-4.5.6/node_modules/example-package;"
+ "downloadfilename=npm/example-package-1.2.3.tgz;"
+ "striplevel=1;"
+ "sha512sum=c0ac90450a63274b08a7ad84ad265d1ac8cc256b1aa79a1136284786ee86ec954effd8c807a5327af2feb57b8eaab9e0f23fdcc4a4d6c96530bd24eb8a2673fe",
+ "https://example.com/tarball.tgz;"
+ "name=tarball;"
+ "subdir=dummy-4.5.6/node_modules/tarball;"
+ "striplevel=1;"
+ "sha512sum=c0ac90450a63274b08a7ad84ad265d1ac8cc256b1aa79a1136284786ee86ec954effd8c807a5327af2feb57b8eaab9e0f23fdcc4a4d6c96530bd24eb8a2673fe"
+ ]
+ expected_dev_src_uris = [
+ "https://registry.com/npm/content-type/-/content-type-1.0.4.tgz;"
+ "name=content-type;"
+ "version=1.0.4;"
+ "vendor=npm;"
+ "subdir=dummy-4.5.6/node_modules/content-type;"
+ "downloadfilename=npm/content-type-1.0.4.tgz;"
+ "striplevel=1;"
+ "sha512sum=8483f71043ecf2d07d013d4bf8d52ab70380a6ce269366686fcf4c5973078c75a0f668a517f8f8a2c9e740b5c108114193fb6f206fed51cf663942623c184f5c"
+ ]
+ for dev in [False, True]:
+ src_uris = npm.resolve_src_uris(filename,
+ "https://registry.com/npm",
+ "dummy-4.5.6", dev)
+ expected_src_uris = expected_common_src_uris
+ if dev:
+ expected_src_uris += expected_dev_src_uris
+ self.assertListEqual(src_uris, expected_src_uris)