def test_get_package_and_deps(package_lock_deps, package_and_deps): package_lock_deps["millennium-falcon"] = { "version": "file:millennium-falcon-1.0.0.tgz", "integrity": ( "sha512-I6R70uG8HTBl4bDae8Tj4WpwRRS0RPLPDw/cZOqNFkk+qhQ241rLq8ynuC7dN4CKtihxybAvqvk+Fr" "sLau3fOA==" ), } package_and_deps["deps"].insert( 3, { "bundled": False, "dev": False, "name": "millennium-falcon", "type": "npm", "version": "file:millennium-falcon-1.0.0.tgz", "version_in_nexus": None, }, ) package_and_deps["deps"].sort(key=operator.itemgetter("name", "version")) package_lock = {"name": "han_solo", "version": "5.0.0", "dependencies": package_lock_deps} mock_open = mock.mock_open(read_data=json.dumps(package_lock)) with mock.patch("cachito.workers.pkg_managers.npm.open", mock_open): deps_info = npm.get_package_and_deps( "/tmp/cachito-bundles/1/temp/app/package.json", "/tmp/cachito-bundles/1/temp/app/package-lock.json", ) deps_info["deps"].sort(key=operator.itemgetter("name", "version")) mock_open.assert_called_once() assert deps_info == package_and_deps
def test_get_package_and_deps_dep_replacements(package_lock_deps, package_and_deps): package_lock = { "name": "star-wars", "version": "5.0.0", "dependencies": { "rxjs": { "version": "github:ReactiveX/rxjs#dfa239d41b97504312fa95e13f4d593d95b49c4b", "from": "github:ReactiveX/rxjs#dfa239d41b97504312fa95e13f4d593d95b49c4b", "requires": { "tslib": "^1.9.0" }, }, "tslib": { "version": "1.11.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.11.1.tgz", "integrity": ("sha512-aZW88SY8kQbU7gpV19lN24LtXh/yD4ZZg6qieAJDDg+YBsJcSmLGK9QpnUjAKVG/" "xefmvJGd1WUmfpT/g6AJGA=="), }, }, } package_json = { "dependencies": { "rxjs": { "version": "github:ReactiveX/rxjs#dfa239d41b97504312fa95e13f4d593d95b49c4b" }, "tslib": { "version": "1.11.1" }, } } def _mock_get_deps(_deps, file_deps_allowlist): _deps["rxjs"] = { "version": "6.5.5-external-gitcommit-dfa239d41b97504312fa95e13f4d593d95b49c4b", "resolved": ("https://nexus.domain.local/repository/cachito-js-hosted/rxjs/-/" "rxjs-6.5.5-external-gitcommit-dfa239d41b97504312fa95e13f4d593d95b49c4b.tgz" ), "integrity": ("sha512-vvAdzoVTdbr5Lq7BI2+l4R3dM4Mw7305wNKLgij8ru7sx3Fuo1W2XrsoTXWfPtIk+kxiBXxCoc8" "UX1Vb45kbRQ=="), "requires": { "tslib": "^1.9.0" }, } name_to_deps = { "rxjs": [ { "bundled": False, "dev": False, "name": "rxjs", "version": "6.5.5", "version_in_nexus": ("6.5.5-external-gitcommit-dfa239d41b97504312fa95e13f4d593d95b49c4b" ), }, ], "tslib": [ { "bundled": False, "dev": False, "name": "rxjs", "version": "1.11.1", "version_in_nexus": None, }, ], } replacements = [( "rxjs", "6.5.5-external-gitcommit-dfa239d41b97504312fa95e13f4d593d95b49c4b" )] return name_to_deps, replacements with mock.patch("cachito.workers.pkg_managers.npm._get_deps", new=_mock_get_deps): with mock.patch("cachito.workers.pkg_managers.npm.open") as mock_open: mock_open.side_effect = [ mock.mock_open( read_data=json.dumps(package_lock)).return_value, mock.mock_open( read_data=json.dumps(package_json)).return_value, ] deps_info = npm.get_package_and_deps( "/tmp/cachito-bundles/1/temp/app/package.json", "/tmp/cachito-bundles/1/temp/app/package-lock.json", ) assert deps_info == { "deps": [ { "bundled": False, "dev": False, "name": "rxjs", "version": "6.5.5", "version_in_nexus": ("6.5.5-external-gitcommit-dfa239d41b97504312fa95e13f4d593d95b49c4b" ), }, { "bundled": False, "dev": False, "name": "rxjs", "version": "1.11.1", "version_in_nexus": None, }, ], # Verify that the lock file was detected as having been modified "lock_file": { "dependencies": { "rxjs": { "integrity": ("sha512-vvAdzoVTdbr5Lq7BI2+l4R3dM4Mw7305wNKLgij8ru7sx3Fuo1W2XrsoTXWfPtIk+k" "xiBXxCoc8UX1Vb45kbRQ=="), "requires": { "tslib": "^1.9.0" }, "resolved": ("https://nexus.domain.local/repository/cachito-js-hosted/rxjs/-/" "rxjs-6.5.5-external-gitcommit-dfa239d41b97504312fa95e13f4d593d95b49c4b.tgz" ), "version": "6.5.5-external-gitcommit-dfa239d41b97504312fa95e13f4d593d95b49c4b", }, "tslib": { "integrity": ("sha512-aZW88SY8kQbU7gpV19lN24LtXh/yD4ZZg6qieAJDDg+YBsJcSmLGK9Qp" "nUjAKVG/xefmvJGd1WUmfpT/g6AJGA=="), "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.11.1.tgz", "version": "1.11.1", }, }, "name": "star-wars", "version": "5.0.0", }, "package": { "name": "star-wars", "type": "npm", "version": "5.0.0" }, "package.json": { "dependencies": { # Verify that package.json was updated with the hosted version of rxjs "rxjs": "6.5.5-external-gitcommit-dfa239d41b97504312fa95e13f4d593d95b49c4b", "tslib": { "version": "1.11.1" }, } }, }