diff options
author | Jean-Marie LEMETAYER <jean-marie.lemetayer@savoirfairelinux.com> | 2020-01-24 18:08:12 +0100 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2020-01-27 16:44:22 +0000 |
commit | b166bd3cc6cc1ca63e885319091f17daaaaa2537 (patch) | |
tree | c9560fb0977701837a837314b29b562558c31415 /lib/bb/tests | |
parent | 0f451cdc43130d503ada53ed1b4fc5a24943f6ef (diff) | |
download | bitbake-b166bd3cc6cc1ca63e885319091f17daaaaa2537.tar.gz |
tests/fetch: add npm tests
This commit adds some tests to validate the npm fetcher:
- bb.tests.fetch.NPMTest.test_npm
- bb.tests.fetch.NPMTest.test_npm_bad_checksum
- bb.tests.fetch.NPMTest.test_npm_destsuffix_downloadfilename
- bb.tests.fetch.NPMTest.test_npm_mirrors
- bb.tests.fetch.NPMTest.test_npm_no_network_no_tarball
- bb.tests.fetch.NPMTest.test_npm_no_network_with_tarball
- bb.tests.fetch.NPMTest.test_npm_package_invalid
- bb.tests.fetch.NPMTest.test_npm_package_none
- bb.tests.fetch.NPMTest.test_npm_premirrors
- bb.tests.fetch.NPMTest.test_npm_registry_alternate
- bb.tests.fetch.NPMTest.test_npm_registry_invalid
- bb.tests.fetch.NPMTest.test_npm_registry_none
- bb.tests.fetch.NPMTest.test_npm_version_invalid
- bb.tests.fetch.NPMTest.test_npm_version_latest
- bb.tests.fetch.NPMTest.test_npm_version_none
Signed-off-by: Jean-Marie LEMETAYER <jean-marie.lemetayer@savoirfairelinux.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'lib/bb/tests')
-rw-r--r-- | lib/bb/tests/fetch.py | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/lib/bb/tests/fetch.py b/lib/bb/tests/fetch.py index fa0c5c5ae..5eeb64c51 100644 --- a/lib/bb/tests/fetch.py +++ b/lib/bb/tests/fetch.py @@ -2037,3 +2037,186 @@ class GitLfsTest(FetcherTest): ud.method._find_git_lfs = lambda d: False shutil.rmtree(self.gitdir, ignore_errors=True) fetcher.unpack(self.d.getVar('WORKDIR')) + +class NPMTest(FetcherTest): + def skipIfNoNpm(): + import shutil + if not shutil.which('npm'): + return unittest.skip('npm not installed, tests being skipped') + return lambda f: f + + @skipIfNoNpm() + @skipIfNoNetwork() + def test_npm(self): + url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0' + fetcher = bb.fetch.Fetch([url], 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)) + 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): + url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0' + # Fetch once to get a tarball + fetcher = bb.fetch.Fetch([url], 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): + url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0' + # Fetch once to get a tarball + fetcher = bb.fetch.Fetch([url], 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/\n' % mirrordir) + 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 + url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0' + fetcher = bb.fetch.Fetch([url], 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/\n' % 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)) + + @skipIfNoNpm() + @skipIfNoNetwork() + def test_npm_destsuffix_downloadfilename(self): + url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0;destsuffix=foo/bar;downloadfilename=foo-bar.tgz' + fetcher = bb.fetch.Fetch([url], self.d) + fetcher.download() + self.assertTrue(os.path.exists(os.path.join(self.dldir, '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): + url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0' + self.d.setVar('BB_NO_NETWORK', '1') + fetcher = bb.fetch.Fetch([url], self.d) + with self.assertRaises(bb.fetch2.NetworkAccess): + fetcher.download() + + @skipIfNoNpm() + @skipIfNoNetwork() + def test_npm_no_network_with_tarball(self): + url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0' + # Fetch once to get a tarball + fetcher = bb.fetch.Fetch([url], 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'))) + + @skipIfNoNpm() + @skipIfNoNetwork() + def test_npm_registry_alternate(self): + url = 'npm://registry.freajs.org;package=@savoirfairelinux/node-server-example;version=1.0.0' + fetcher = bb.fetch.Fetch([url], 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([url], 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): + url = 'npm://registry.invalid.org;package=@savoirfairelinux/node-server-example;version=1.0.0' + fetcher = bb.fetch.Fetch([url], self.d) + with self.assertRaises(bb.fetch2.FetchError): + fetcher.download() + + @skipIfNoNpm() + @skipIfNoNetwork() + def test_npm_package_invalid(self): + url = 'npm://registry.npmjs.org;package=@savoirfairelinux/invalid;version=1.0.0' + fetcher = bb.fetch.Fetch([url], self.d) + with self.assertRaises(bb.fetch2.FetchError): + fetcher.download() + + @skipIfNoNpm() + @skipIfNoNetwork() + def test_npm_version_invalid(self): + url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=invalid' + with self.assertRaises(bb.fetch2.ParameterError): + fetcher = bb.fetch.Fetch([url], self.d) + + @skipIfNoNpm() + @skipIfNoNetwork() + def test_npm_registry_none(self): + url = 'npm://;package=@savoirfairelinux/node-server-example;version=1.0.0' + with self.assertRaises(bb.fetch2.MalformedUrl): + fetcher = bb.fetch.Fetch([url], self.d) + + @skipIfNoNpm() + @skipIfNoNetwork() + def test_npm_package_none(self): + url = 'npm://registry.npmjs.org;version=1.0.0' + with self.assertRaises(bb.fetch2.MissingParameterError): + fetcher = bb.fetch.Fetch([url], self.d) + + @skipIfNoNpm() + @skipIfNoNetwork() + def test_npm_version_none(self): + url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example' + with self.assertRaises(bb.fetch2.MissingParameterError): + fetcher = bb.fetch.Fetch([url], self.d) |