[bitbake-devel,v3,7/7] tests/fetch.py: add npm tests

Submitted by Jean-Marie LEMETAYER on Nov. 20, 2019, 9:34 a.m. | Patch ID: 167183

Details

Message ID 20191120093412.11519-8-jean-marie.lemetayer@savoirfairelinux.com
State New
Headers show

Commit Message

Jean-Marie LEMETAYER Nov. 20, 2019, 9:34 a.m.
This commit adds some basic tests for the npm fetcher:

 - bb.tests.fetch.NPMTest.test_npm
 - bb.tests.fetch.NPMTest.test_npm_name_invalid
 - bb.tests.fetch.NPMTest.test_npm_name_none
 - 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_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>
---
 lib/bb/tests/fetch.py | 103 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 103 insertions(+)

Patch hide | download patch | download mbox

diff --git a/lib/bb/tests/fetch.py b/lib/bb/tests/fetch.py
index 83fad3ff..d1091fa7 100644
--- a/lib/bb/tests/fetch.py
+++ b/lib/bb/tests/fetch.py
@@ -2008,3 +2008,106 @@  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;name=@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_no_network_no_tarball(self):
+        url = 'npm://registry.npmjs.org;name=@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;name=@savoirfairelinux/node-server-example;version=1.0.0'
+        fetcher = bb.fetch.Fetch([url], self.d)
+        fetcher.download()
+        self.d.setVar('BB_NO_NETWORK', '1')
+        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_alternate(self):
+        url = 'npm://registry.freajs.org;name=@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;name=@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;name=@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_name_invalid(self):
+        url = 'npm://registry.npmjs.org;name=@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;name=@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://;name=@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_name_none(self):
+        url = 'npm://registry.npmjs.org;version=1.0.0'
+        with self.assertRaises(AttributeError):
+            fetcher = bb.fetch.Fetch([url], self.d)
+
+    @skipIfNoNpm()
+    @skipIfNoNetwork()
+    def test_npm_version_none(self):
+        url = 'npm://registry.npmjs.org;name=@savoirfairelinux/node-server-example'
+        with self.assertRaises(AttributeError):
+            fetcher = bb.fetch.Fetch([url], self.d)

Comments

Mark Hatle Nov. 20, 2019, 6:29 p.m.
First thanks for the tests!  A few comments inline

On 11/20/19 3:34 AM, Jean-Marie LEMETAYER wrote:
> This commit adds some basic tests for the npm fetcher:
> 
>  - bb.tests.fetch.NPMTest.test_npm
>  - bb.tests.fetch.NPMTest.test_npm_name_invalid
>  - bb.tests.fetch.NPMTest.test_npm_name_none
>  - 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_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>
> ---
>  lib/bb/tests/fetch.py | 103 ++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 103 insertions(+)
> 
> diff --git a/lib/bb/tests/fetch.py b/lib/bb/tests/fetch.py
> index 83fad3ff..d1091fa7 100644
> --- a/lib/bb/tests/fetch.py
> +++ b/lib/bb/tests/fetch.py
> @@ -2008,3 +2008,106 @@ 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;name=@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_no_network_no_tarball(self):
> +        url = 'npm://registry.npmjs.org;name=@savoirfairelinux/node-server-example;version=1.0.0'
> +        self.d.setVar('BB_NO_NETWORK', '1')

Since this is a BB_NO_NETWORK=1 case, the @skipIfNoNetwork() should not be here,
since it should not need the network to perform the test.

> +        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;name=@savoirfairelinux/node-server-example;version=1.0.0'
> +        fetcher = bb.fetch.Fetch([url], self.d)
> +        fetcher.download()
> +        self.d.setVar('BB_NO_NETWORK', '1')

Same here.

If any other the ones below work off local files, then similarly we shouldn't
skip running them on a no network config.

--Mark

> +        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_alternate(self):
> +        url = 'npm://registry.freajs.org;name=@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;name=@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;name=@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_name_invalid(self):
> +        url = 'npm://registry.npmjs.org;name=@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;name=@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://;name=@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_name_none(self):
> +        url = 'npm://registry.npmjs.org;version=1.0.0'
> +        with self.assertRaises(AttributeError):
> +            fetcher = bb.fetch.Fetch([url], self.d)
> +
> +    @skipIfNoNpm()
> +    @skipIfNoNetwork()
> +    def test_npm_version_none(self):
> +        url = 'npm://registry.npmjs.org;name=@savoirfairelinux/node-server-example'
> +        with self.assertRaises(AttributeError):
> +            fetcher = bb.fetch.Fetch([url], self.d)
>
Jean-Marie LEMETAYER Nov. 21, 2019, 12:54 p.m.
Hi Mark,

On Nov 20, 2019, at 7:29 PM, Mark Hatle mark.hatle@kernel.crashing.org wrote:
> First thanks for the tests!  A few comments inline
> 
> On 11/20/19 3:34 AM, Jean-Marie LEMETAYER wrote:
>> +
>> +    @skipIfNoNpm()
>> +    @skipIfNoNetwork()
>> +    def test_npm_no_network_no_tarball(self):
>> +        url =
>> 'npm://registry.npmjs.org;name=@savoirfairelinux/node-server-example;version=1.0.0'
>> +        self.d.setVar('BB_NO_NETWORK', '1')
> 
> Since this is a BB_NO_NETWORK=1 case, the @skipIfNoNetwork() should not be here,
> since it should not need the network to perform the test.

Yes it is correct. In fact the @skipIfNoNpm() is not useful either as the
bb.fetch2.NetworkAccess exception should be raised before any npm commands.

>> +        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;name=@savoirfairelinux/node-server-example;version=1.0.0'
>> +        fetcher = bb.fetch.Fetch([url], self.d)
>> +        fetcher.download()
>> +        self.d.setVar('BB_NO_NETWORK', '1')
> 
> Same here.
> 
> If any other the ones below work off local files, then similarly we shouldn't
> skip running them on a no network config.

As the DL_DIR point to a temporary directory created in the FetcherTest::setUp()
function when starting the test suite I think there is no way to work with local
files if you haven't download it first (which implies a network access).
Or did I miss something ?

>> +        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'))

Regards,
Jean-Marie
Mark Hatle Nov. 21, 2019, 7:18 p.m.
On 11/21/19 6:54 AM, Jean-Marie LEMETAYER wrote:
> Hi Mark,
> 
> On Nov 20, 2019, at 7:29 PM, Mark Hatle mark.hatle@kernel.crashing.org wrote:
>> First thanks for the tests!  A few comments inline
>>
>> On 11/20/19 3:34 AM, Jean-Marie LEMETAYER wrote:
>>> +
>>> +    @skipIfNoNpm()
>>> +    @skipIfNoNetwork()
>>> +    def test_npm_no_network_no_tarball(self):
>>> +        url =
>>> 'npm://registry.npmjs.org;name=@savoirfairelinux/node-server-example;version=1.0.0'
>>> +        self.d.setVar('BB_NO_NETWORK', '1')
>>
>> Since this is a BB_NO_NETWORK=1 case, the @skipIfNoNetwork() should not be here,
>> since it should not need the network to perform the test.
> 
> Yes it is correct. In fact the @skipIfNoNpm() is not useful either as the
> bb.fetch2.NetworkAccess exception should be raised before any npm commands.
> 
>>> +        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;name=@savoirfairelinux/node-server-example;version=1.0.0'
>>> +        fetcher = bb.fetch.Fetch([url], self.d)
>>> +        fetcher.download()
>>> +        self.d.setVar('BB_NO_NETWORK', '1')
>>
>> Same here.
>>
>> If any other the ones below work off local files, then similarly we shouldn't
>> skip running them on a no network config.
> 
> As the DL_DIR point to a temporary directory created in the FetcherTest::setUp()
> function when starting the test suite I think there is no way to work with local
> files if you haven't download it first (which implies a network access).
> Or did I miss something ?

I didn't notice thi one has the BB_NO_NETWORK -after- the download.  So ya, I
see how it requires the network.

--Mark

>>> +        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'))
> 
> Regards,
> Jean-Marie
>