diff mbox series

[RFC,17/21] tests: fetch: adapt npmsw test cases

Message ID 20241220112613.22647-18-stefan.herbrechtsmeier-oss@weidmueller.com
State New
Headers show
Series Concept for tightly coupled package manager (Node.js, Go, Rust) | expand

Commit Message

Stefan Herbrechtsmeier Dec. 20, 2024, 11:26 a.m. UTC
From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>

Adapt the npmsw test cases to the reworked npmsw fetcher:
* Remove decorator for npm binary check
* Define excepted file names
* Remove test cases for (pre)mirrors, network and invalid urls because
  the reworked class uses the npm, wget and git fetcher.

Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
---

 lib/bb/tests/fetch.py | 255 +++++++++++-------------------------------
 1 file changed, 65 insertions(+), 190 deletions(-)
diff mbox series

Patch

diff --git a/lib/bb/tests/fetch.py b/lib/bb/tests/fetch.py
index 903095746..437571f1c 100644
--- a/lib/bb/tests/fetch.py
+++ b/lib/bb/tests/fetch.py
@@ -2683,24 +2683,22 @@  class NPMTest(FetcherTest):
             fetcher = bb.fetch.Fetch(urls, self.d)
 
 class NPMSWTest(FetcherTest):
-    def skipIfNoNpm():
-        import shutil
-        if not shutil.which("npm"):
-            return unittest.skip("npm not installed")
-        return lambda f: f
+    def setUp(self):
+        super().setUp()
+        self.localsrcdir = os.path.join(self.tempdir, "localsrc")
+        os.makedirs(self.localsrcdir)
+        self.d.setVar("FILESPATH", self.localsrcdir)
 
     def create_shrinkwrap_file(self, data):
         import json
-        datadir = os.path.join(self.tempdir, "data")
-        swfile = os.path.join(datadir, "npm-shrinkwrap.json")
-        bb.utils.mkdirhier(datadir)
-        with open(swfile, "w") as f:
+        filename = "npm-shrinkwrap.json"
+        with open(os.path.join(self.localsrcdir, filename), 'w') as f:
             json.dump(data, f)
-        return swfile
+        return filename
 
     @skipIfNoNetwork()
     def test_npmsw(self):
-        swfile = self.create_shrinkwrap_file({
+        filename = self.create_shrinkwrap_file({
             "packages": {
                 "node_modules/array-flatten": {
                     "version": "1.1.1",
@@ -2720,13 +2718,19 @@  class NPMSWTest(FetcherTest):
                 },
                 "node_modules/array-flatten/node_modules/content-type/node_modules/cookie": {
                     "resolved": "git+https://github.com/jshttp/cookie.git#aec1177c7da67e3b3273df96cf476824dbc9ae09"
+                },
+                "node_modules/karma": {
+                    "resolved": "",
+                    "link": True
                 }
             }
         })
-        fetcher = bb.fetch.Fetch(["npmsw://" + swfile], self.d)
+        fetcher = bb.fetch.Fetch([f"npmsw://{filename}"], self.d)
         fetcher.download()
         self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/array-flatten-1.1.1.tgz")))
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/array-flatten-1.1.1.tgz.done")))
         self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/content-type-1.0.4.tgz")))
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/content-type-1.0.4.tgz.done")))
         self.assertTrue(os.path.exists(os.path.join(self.dldir, "git2/github.com.jshttp.cookie.git")))
         fetcher.unpack(self.unpackdir)
         self.assertTrue(os.path.exists(os.path.join(self.unpackdir, "npm-shrinkwrap.json")))
@@ -2735,21 +2739,8 @@  class NPMSWTest(FetcherTest):
         self.assertTrue(os.path.exists(os.path.join(self.unpackdir, "node_modules/array-flatten/node_modules/content-type/node_modules/cookie/package.json")))
 
     @skipIfNoNetwork()
-    def test_npmsw_git(self):
-        swfile = self.create_shrinkwrap_file({
-            "packages": {
-                "node_modules/cookie": {
-                    "resolved": "git+https://github.com/jshttp/cookie.git#aec1177c7da67e3b3273df96cf476824dbc9ae09"
-                }
-            }
-        })
-        fetcher = bb.fetch.Fetch(["npmsw://" + swfile], self.d)
-        fetcher.download()
-        self.assertTrue(os.path.exists(os.path.join(self.dldir, "git2/github.com.jshttp.cookie.git")))
-
-    @skipIfNoNetwork()
-    def test_npmsw_dev(self):
-        swfile = self.create_shrinkwrap_file({
+    def test_npmsw_resolve_dev(self):
+        filename = self.create_shrinkwrap_file({
             "packages": {
                 "node_modules/array-flatten": {
                     "version": "1.1.1",
@@ -2765,19 +2756,23 @@  class NPMSWTest(FetcherTest):
             }
         })
         # Fetch with dev disabled
-        fetcher = bb.fetch.Fetch(["npmsw://" + swfile], self.d)
+        fetcher = bb.fetch.Fetch([f"npmsw://{filename}"], self.d)
         fetcher.download()
         self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/array-flatten-1.1.1.tgz")))
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/array-flatten-1.1.1.tgz.done")))
         self.assertFalse(os.path.exists(os.path.join(self.dldir, "npm2/content-type-1.0.4.tgz")))
+        self.assertFalse(os.path.exists(os.path.join(self.dldir, "npm2/content-type-1.0.4.tgz.done")))
         # Fetch with dev enabled
-        fetcher = bb.fetch.Fetch(["npmsw://" + swfile + ";dev=1"], self.d)
+        fetcher = bb.fetch.Fetch([f"npmsw://{filename};dev=1"], self.d)
         fetcher.download()
         self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/array-flatten-1.1.1.tgz")))
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/array-flatten-1.1.1.tgz.done")))
         self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/content-type-1.0.4.tgz")))
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/content-type-1.0.4.tgz.done")))
 
     @skipIfNoNetwork()
-    def test_npmsw_destsuffix(self):
-        swfile = self.create_shrinkwrap_file({
+    def test_npmsw_subdir(self):
+        filename = self.create_shrinkwrap_file({
             "packages": {
                 "node_modules/array-flatten": {
                     "version": "1.1.1",
@@ -2786,170 +2781,17 @@  class NPMSWTest(FetcherTest):
                 }
             }
         })
-        fetcher = bb.fetch.Fetch(["npmsw://" + swfile + ";destsuffix=foo/bar"], self.d)
+        fetcher = bb.fetch.Fetch([f"npmsw://{filename};subdir=foo/bar"], self.d)
         fetcher.download()
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/array-flatten-1.1.1.tgz")))
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/array-flatten-1.1.1.tgz.done")))
         fetcher.unpack(self.unpackdir)
+        self.assertTrue(os.path.exists(os.path.join(self.unpackdir, "foo/bar/npm-shrinkwrap.json")))
         self.assertTrue(os.path.exists(os.path.join(self.unpackdir, "foo/bar/node_modules/array-flatten/package.json")))
 
-    def test_npmsw_no_network_no_tarball(self):
-        swfile = self.create_shrinkwrap_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="
-                }
-            }
-        })
-        self.d.setVar("BB_NO_NETWORK", "1")
-        fetcher = bb.fetch.Fetch(["npmsw://" + swfile], self.d)
-        with self.assertRaises(bb.fetch2.NetworkAccess):
-            fetcher.download()
-
-    @skipIfNoNpm()
-    @skipIfNoNetwork()
-    def test_npmsw_no_network_with_tarball(self):
-        # Fetch once to get a tarball
-        fetcher = bb.fetch.Fetch(["npm://registry.npmjs.org;package=array-flatten;version=1.1.1"], self.d)
-        fetcher.download()
-        # Disable network access
-        self.d.setVar("BB_NO_NETWORK", "1")
-        # Fetch again
-        swfile = self.create_shrinkwrap_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="
-                }
-            }
-        })
-        fetcher = bb.fetch.Fetch(["npmsw://" + swfile], self.d)
-        fetcher.download()
-        fetcher.unpack(self.unpackdir)
-        self.assertTrue(os.path.exists(os.path.join(self.unpackdir, "node_modules/array-flatten/package.json")))
-
-    @skipIfNoNetwork()
-    def test_npmsw_npm_reusability(self):
-        # Fetch once with npmsw
-        swfile = self.create_shrinkwrap_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="
-                }
-            }
-        })
-        fetcher = bb.fetch.Fetch(["npmsw://" + swfile], self.d)
-        fetcher.download()
-        # Disable network access
-        self.d.setVar("BB_NO_NETWORK", "1")
-        # Fetch again with npm
-        fetcher = bb.fetch.Fetch(["npm://registry.npmjs.org;package=array-flatten;version=1.1.1"], self.d)
-        fetcher.download()
-        fetcher.unpack(self.unpackdir)
-        self.assertTrue(os.path.exists(os.path.join(self.unpackdir, "npm/package.json")))
-
-    @skipIfNoNetwork()
-    def test_npmsw_bad_checksum(self):
-        # Try to fetch with bad checksum
-        swfile = self.create_shrinkwrap_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-gfNEp2hqgLTFKT6P3AsBYMgsBqg="
-                }
-            }
-        })
-        fetcher = bb.fetch.Fetch(["npmsw://" + swfile], self.d)
-        with self.assertRaises(bb.fetch2.FetchError):
-            fetcher.download()
-        # Fetch correctly to get a tarball
-        swfile = self.create_shrinkwrap_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="
-                }
-            }
-        })
-        fetcher = bb.fetch.Fetch(["npmsw://" + swfile], self.d)
-        fetcher.download()
-        localpath = os.path.join(self.dldir, "npm2/array-flatten-1.1.1.tgz")
-        self.assertTrue(os.path.exists(localpath))
-        # Modify the tarball
-        bad = b"bad checksum"
-        with open(localpath, "wb") as f:
-            f.write(bad)
-        # Verify that the tarball is fetched again
-        fetcher.download()
-        badsum = hashlib.sha1(bad).hexdigest()
-        self.assertTrue(os.path.exists(localpath + "_bad-checksum_" + badsum))
-        self.assertTrue(os.path.exists(localpath))
-
-    @skipIfNoNpm()
-    @skipIfNoNetwork()
-    def test_npmsw_premirrors(self):
-        # Fetch once to get a tarball
-        fetcher = bb.fetch.Fetch(["npm://registry.npmjs.org;package=array-flatten;version=1.1.1"], self.d)
-        ud = fetcher.ud[fetcher.urls[0]]
-        fetcher.download()
-        self.assertTrue(os.path.exists(ud.localpath))
-        # Setup the mirror
-        mirrordir = os.path.join(self.tempdir, "mirror")
-        bb.utils.mkdirhier(mirrordir)
-        os.replace(ud.localpath, os.path.join(mirrordir, os.path.basename(ud.localpath)))
-        self.d.setVar("PREMIRRORS", "https?$://.*/.* file://%s/" % mirrordir)
-        self.d.setVar("BB_FETCH_PREMIRRORONLY", "1")
-        # Fetch again
-        self.assertFalse(os.path.exists(ud.localpath))
-        swfile = self.create_shrinkwrap_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="
-                }
-            }
-        })
-        fetcher = bb.fetch.Fetch(["npmsw://" + swfile], self.d)
-        fetcher.download()
-        self.assertTrue(os.path.exists(ud.localpath))
-
-    @skipIfNoNpm()
-    @skipIfNoNetwork()
-    def test_npmsw_mirrors(self):
-        # Fetch once to get a tarball
-        fetcher = bb.fetch.Fetch(["npm://registry.npmjs.org;package=array-flatten;version=1.1.1"], self.d)
-        ud = fetcher.ud[fetcher.urls[0]]
-        fetcher.download()
-        self.assertTrue(os.path.exists(ud.localpath))
-        # Setup the mirror
-        mirrordir = os.path.join(self.tempdir, "mirror")
-        bb.utils.mkdirhier(mirrordir)
-        os.replace(ud.localpath, os.path.join(mirrordir, os.path.basename(ud.localpath)))
-        self.d.setVar("MIRRORS", "https?$://.*/.* file://%s/" % mirrordir)
-        # Fetch again with invalid url
-        self.assertFalse(os.path.exists(ud.localpath))
-        swfile = self.create_shrinkwrap_file({
-            "packages": {
-                "node_modules/array-flatten": {
-                    "version": "1.1.1",
-                    "resolved": "https://invalid",
-                    "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
-                }
-            }
-        })
-        fetcher = bb.fetch.Fetch(["npmsw://" + swfile], self.d)
-        fetcher.download()
-        self.assertTrue(os.path.exists(ud.localpath))
-
     @skipIfNoNetwork()
     def test_npmsw_bundled(self):
-        swfile = self.create_shrinkwrap_file({
+        filename = self.create_shrinkwrap_file({
             "packages": {
                 "node_modules/array-flatten": {
                     "version": "1.1.1",
@@ -2964,10 +2806,43 @@  class NPMSWTest(FetcherTest):
                 }
             }
         })
-        fetcher = bb.fetch.Fetch(["npmsw://" + swfile], self.d)
+        fetcher = bb.fetch.Fetch([f"npmsw://{filename}"], self.d)
         fetcher.download()
         self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/array-flatten-1.1.1.tgz")))
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/array-flatten-1.1.1.tgz.done")))
         self.assertFalse(os.path.exists(os.path.join(self.dldir, "npm2/content-type-1.0.4.tgz")))
+        self.assertFalse(os.path.exists(os.path.join(self.dldir, "npm2/content-type-1.0.4.tgz.done")))
+
+    @skipIfNoNetwork()
+    def test_npmsw_git(self):
+        urls = [
+            "npmsw+git://github.com/karma-runner/karma.git;protocol=https;"
+            "rev=84f85e7016efc2266fa6b3465f494a3fa151c85c"
+        ]
+        fetcher = bb.fetch.Fetch(urls, self.d)
+        fetcher.download()
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "git2/github.com.karma-runner.karma.git")))
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/http-proxy-1.18.1.tgz")))
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/http-proxy-1.18.1.tgz.done")))
+        fetcher.unpack(self.unpackdir)
+        self.assertTrue(os.path.exists(os.path.join(self.unpackdir, "git/package.json")))
+        self.assertTrue(os.path.exists(os.path.join(self.unpackdir, "git/node_modules/http-proxy")))
+
+    @skipIfNoNetwork()
+    def test_npmswhttps(self):
+        urls = [
+            "npmsw+https://github.com/karma-runner/karma/archive/refs/tags/v6.4.4.tar.gz;"
+            "striplevel=1;subdir=karma-6.4.4;"
+            "sha256sum=3cbd3b72da3b0b8aa650a90ac3a97aa5d0995ad2415989b9b8b59d09c460a6bc"
+        ]
+        fetcher = bb.fetch.Fetch(urls, self.d)
+        fetcher.download()
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "v6.4.4.tar.gz")))
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "v6.4.4.tar.gz.done")))
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/http-proxy-1.18.1.tgz")))
+        fetcher.unpack(self.unpackdir)
+        self.assertTrue(os.path.exists(os.path.join(self.unpackdir, "karma-6.4.4/package.json")))
+        self.assertTrue(os.path.exists(os.path.join(self.unpackdir, "karma-6.4.4/node_modules/http-proxy")))
 
 class GitSharedTest(FetcherTest):
     def setUp(self):