diff mbox series

[RFC,13/21] tests: fetch: adapt npm test cases

Message ID 20241220112613.22647-14-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 npm test cases to the reworked npm fetcher:
* Add test case for latest version check
* Remove decorator for npm binary check
* Use common npm package for test cases
* Define excepted file names
* Remove test cases for (pre)mirrors, network and invalid urls because
  the reworked class is based on the wget fetcher.

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

 lib/bb/tests/fetch.py | 221 +++++++-----------------------------------
 1 file changed, 36 insertions(+), 185 deletions(-)
diff mbox series

Patch

diff --git a/lib/bb/tests/fetch.py b/lib/bb/tests/fetch.py
index 3fd4c82cd..09f493f8a 100644
--- a/lib/bb/tests/fetch.py
+++ b/lib/bb/tests/fetch.py
@@ -1507,6 +1507,14 @@  class FetchLatestVersionTest(FetcherTest):
             : "0.9.29"
    }
 
+    test_npm_uris = {
+        # basic example; version pattern "A.B.C"
+        (
+            "types-node",
+            "npm://registry.npmjs.org;package=@types/node;version=16.0.0"
+        ) : "22.10.2"
+   }
+
     @skipIfNoNetwork()
     def test_git_latest_versionstring(self):
         for k, v in self.test_git_uris.items():
@@ -1557,6 +1565,17 @@  class FetchLatestVersionTest(FetcherTest):
             r = bb.utils.vercmp_string(v, verstring)
             self.assertTrue(r == -1 or r == 0, msg="Package %s, version: %s <= %s" % (k[0], v, verstring))
 
+    @skipIfNoNetwork()
+    def test_npm_latest_versionstring(self):
+        for k, v in self.test_npm_uris.items():
+            self.d.setVar("PN", k[0])
+            ud = bb.fetch2.FetchData(k[1], self.d)
+            pupver = ud.method.latest_versionstring(ud, self.d)
+            verstring = pupver[0]
+            self.assertTrue(verstring, msg="Could not find upstream version for %s" % k[0])
+            r = bb.utils.vercmp_string(v, verstring)
+            self.assertTrue(r == -1 or r == 0, msg="Package %s, version: %s <= %s" % (k[0], v, verstring))
+
 class FetchCheckStatusTest(FetcherTest):
     test_wget_uris = ["https://downloads.yoctoproject.org/releases/sato/sato-engine-0.1.tar.gz",
                       "https://downloads.yoctoproject.org/releases/sato/sato-engine-0.2.tar.gz",
@@ -2618,216 +2637,48 @@  class CrateTest(FetcherTest):
             fetcher.download()
 
 class NPMTest(FetcherTest):
-    def skipIfNoNpm():
-        import shutil
-        if not shutil.which("npm"):
-            return unittest.skip("npm not installed")
-        return lambda f: f
-
-    @skipIfNoNpm()
     @skipIfNoNetwork()
     def test_npm(self):
-        urls = ["npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0"]
+        urls = [
+            "npm://registry.npmjs.org;package=@types/node;version=22.10.2;"
+            "sha256sum=9dad888e5280e9969393d7410e26d4edf726a828ee4762318c8ddf6fcfee793e"
+        ]
         fetcher = bb.fetch.Fetch(urls, self.d)
         ud = fetcher.ud[fetcher.urls[0]]
         fetcher.download()
-        self.assertTrue(os.path.exists(ud.localpath))
-        self.assertTrue(os.path.exists(ud.localpath + ".done"))
-        self.assertTrue(os.path.exists(ud.resolvefile))
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/@types-node-22.10.2.tgz")))
+        self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/@types-node-22.10.2.tgz.done")))
         fetcher.unpack(self.unpackdir)
-        unpackdir = os.path.join(self.unpackdir, "npm")
-        self.assertTrue(os.path.exists(os.path.join(unpackdir, "package.json")))
-
-    @skipIfNoNpm()
-    @skipIfNoNetwork()
-    def test_npm_bad_checksum(self):
-        urls = ["npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0"]
-        # Fetch once to get a tarball
-        fetcher = bb.fetch.Fetch(urls, self.d)
-        ud = fetcher.ud[fetcher.urls[0]]
-        fetcher.download()
-        self.assertTrue(os.path.exists(ud.localpath))
-        # Modify the tarball
-        bad = b"bad checksum"
-        with open(ud.localpath, "wb") as f:
-            f.write(bad)
-        # Verify that the tarball is fetched again
-        fetcher.download()
-        badsum = hashlib.sha512(bad).hexdigest()
-        self.assertTrue(os.path.exists(ud.localpath + "_bad-checksum_" + badsum))
-        self.assertTrue(os.path.exists(ud.localpath))
-
-    @skipIfNoNpm()
-    @skipIfNoNetwork()
-    def test_npm_premirrors(self):
-        urls = ["npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0"]
-        # Fetch once to get a tarball
-        fetcher = bb.fetch.Fetch(urls, self.d)
-        ud = fetcher.ud[fetcher.urls[0]]
-        fetcher.download()
-        self.assertTrue(os.path.exists(ud.localpath))
-
-        # Setup the mirror by renaming the download directory
-        mirrordir = os.path.join(self.tempdir, "mirror")
-        bb.utils.rename(self.dldir, mirrordir)
-        os.mkdir(self.dldir)
-
-        # Configure the premirror to be used
-        self.d.setVar("PREMIRRORS", "https?$://.*/.* file://%s/npm2" % mirrordir)
-        self.d.setVar("BB_FETCH_PREMIRRORONLY", "1")
-
-        # Fetch again
-        self.assertFalse(os.path.exists(ud.localpath))
-        # The npm fetcher doesn"t handle that the .resolved file disappears
-        # while the fetcher object exists, which it does when we rename the
-        # download directory to "mirror" above. Thus we need a new fetcher to go
-        # with the now empty download directory.
-        fetcher = bb.fetch.Fetch(urls, self.d)
-        ud = fetcher.ud[fetcher.urls[0]]
-        fetcher.download()
-        self.assertTrue(os.path.exists(ud.localpath))
-
-    @skipIfNoNpm()
-    @skipIfNoNetwork()
-    def test_npm_premirrors_with_specified_filename(self):
-        urls = ["npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0"]
-        # Fetch once to get a tarball
-        fetcher = bb.fetch.Fetch(urls, 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)
-        mirrorfilename = os.path.join(mirrordir, os.path.basename(ud.localpath))
-        os.replace(ud.localpath, mirrorfilename)
-        self.d.setVar("PREMIRRORS", "https?$://.*/.* file://%s" % mirrorfilename)
-        self.d.setVar("BB_FETCH_PREMIRRORONLY", "1")
-        # Fetch again
-        self.assertFalse(os.path.exists(ud.localpath))
-        fetcher.download()
-        self.assertTrue(os.path.exists(ud.localpath))
-
-    @skipIfNoNpm()
-    @skipIfNoNetwork()
-    def test_npm_mirrors(self):
-        # Fetch once to get a tarball
-        urls = ["npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0"]
-        fetcher = bb.fetch.Fetch(urls, 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)
-        # Update the resolved url to an invalid url
-        with open(ud.resolvefile, "r") as f:
-            url = f.read()
-        uri = URI(url)
-        uri.path = "/invalid"
-        with open(ud.resolvefile, "w") as f:
-            f.write(str(uri))
-        # Fetch again
-        self.assertFalse(os.path.exists(ud.localpath))
-        fetcher.download()
-        self.assertTrue(os.path.exists(ud.localpath))
+        self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'npm/package.json')))
 
-    @skipIfNoNpm()
     @skipIfNoNetwork()
     def test_npm_destsuffix_downloadfilename(self):
-        urls = ["npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0;destsuffix=foo/bar;downloadfilename=foo-bar.tgz"]
+        urls = [
+            "npm://registry.npmjs.org;package=@types/node;version=22.10.2;"
+            "destsuffix=foo/bar;downloadfilename=npm2/foo-bar.tgz;"
+            "sha256sum=9dad888e5280e9969393d7410e26d4edf726a828ee4762318c8ddf6fcfee793e"
+        ]
         fetcher = bb.fetch.Fetch(urls, self.d)
         fetcher.download()
         self.assertTrue(os.path.exists(os.path.join(self.dldir, "npm2/foo-bar.tgz")))
         fetcher.unpack(self.unpackdir)
-        unpackdir = os.path.join(self.unpackdir, "foo", "bar")
-        self.assertTrue(os.path.exists(os.path.join(unpackdir, "package.json")))
-
-    def test_npm_no_network_no_tarball(self):
-        urls = ["npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0"]
-        self.d.setVar("BB_NO_NETWORK", "1")
-        fetcher = bb.fetch.Fetch(urls, self.d)
-        with self.assertRaises(bb.fetch2.NetworkAccess):
-            fetcher.download()
-
-    @skipIfNoNpm()
-    @skipIfNoNetwork()
-    def test_npm_no_network_with_tarball(self):
-        urls = ["npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0"]
-        # Fetch once to get a tarball
-        fetcher = bb.fetch.Fetch(urls, self.d)
-        fetcher.download()
-        # Disable network access
-        self.d.setVar("BB_NO_NETWORK", "1")
-        # Fetch again
-        fetcher.download()
-        fetcher.unpack(self.unpackdir)
-        unpackdir = os.path.join(self.unpackdir, "npm")
-        self.assertTrue(os.path.exists(os.path.join(unpackdir, "package.json")))
+        self.assertTrue(os.path.exists(os.path.join(self.unpackdir, "foo/bar/package.json")))
 
-    @skipIfNoNpm()
-    @skipIfNoNetwork()
-    def test_npm_registry_alternate(self):
-        urls = ["npm://skimdb.npmjs.com;package=@savoirfairelinux/node-server-example;version=1.0.0"]
-        fetcher = bb.fetch.Fetch(urls, self.d)
-        fetcher.download()
-        fetcher.unpack(self.unpackdir)
-        unpackdir = os.path.join(self.unpackdir, "npm")
-        self.assertTrue(os.path.exists(os.path.join(unpackdir, "package.json")))
-
-    @skipIfNoNpm()
     @skipIfNoNetwork()
     def test_npm_version_latest(self):
-        url = ["npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=latest"]
-        fetcher = bb.fetch.Fetch(urls, self.d)
-        fetcher.download()
-        fetcher.unpack(self.unpackdir)
-        unpackdir = os.path.join(self.unpackdir, "npm")
-        self.assertTrue(os.path.exists(os.path.join(unpackdir, "package.json")))
-
-    @skipIfNoNpm()
-    @skipIfNoNetwork()
-    def test_npm_registry_invalid(self):
-        urls = ["npm://registry.invalid.org;package=@savoirfairelinux/node-server-example;version=1.0.0"]
-        fetcher = bb.fetch.Fetch(urls, self.d)
-        with self.assertRaises(bb.fetch2.FetchError):
-            fetcher.download()
-
-    @skipIfNoNpm()
-    @skipIfNoNetwork()
-    def test_npm_package_invalid(self):
-        urls = ["npm://registry.npmjs.org;package=@savoirfairelinux/invalid;version=1.0.0"]
-        fetcher = bb.fetch.Fetch(urls, self.d)
-        with self.assertRaises(bb.fetch2.FetchError):
-            fetcher.download()
-
-    @skipIfNoNpm()
-    @skipIfNoNetwork()
-    def test_npm_version_invalid(self):
-        urls = ["npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=invalid"]
+        urls = ["npm://registry.npmjs.org;package=@types/node;version=latest"]
         with self.assertRaises(bb.fetch2.ParameterError):
-            fetcher = bb.fetch.Fetch(urls, self.d)
-
-    @skipIfNoNpm()
-    @skipIfNoNetwork()
-    def test_npm_registry_none(self):
-        urls = ["npm://;package=@savoirfairelinux/node-server-example;version=1.0.0"]
-        with self.assertRaises(bb.fetch2.MalformedUrl):
-            fetcher = bb.fetch.Fetch(urls, self.d)
+            bb.fetch.Fetch(urls, self.d)
 
-    @skipIfNoNpm()
     @skipIfNoNetwork()
     def test_npm_package_none(self):
-        urls = ["npm://registry.npmjs.org;version=1.0.0"]
+        urls = ["npm://registry.npmjs.org;version=22.10.2"]
         with self.assertRaises(bb.fetch2.MissingParameterError):
             fetcher = bb.fetch.Fetch(urls, self.d)
 
-    @skipIfNoNpm()
     @skipIfNoNetwork()
     def test_npm_version_none(self):
-        urls = ["npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example"]
+        urls = ["npm://registry.npmjs.org;package=@types/node"]
         with self.assertRaises(bb.fetch2.MissingParameterError):
             fetcher = bb.fetch.Fetch(urls, self.d)