|
| 1 | +import hashlib |
| 2 | +import json |
1 | 3 | from pathlib import Path |
2 | 4 |
|
3 | 5 | import pytest |
| 6 | +from conftest import RequestsController |
4 | 7 |
|
5 | 8 | from flatpak_node_generator.integrity import Integrity |
| 9 | +from flatpak_node_generator.manifest import ManifestGenerator |
6 | 10 | from flatpak_node_generator.package import ( |
7 | 11 | GitSource, |
8 | 12 | LocalSource, |
9 | 13 | Lockfile, |
10 | 14 | Package, |
11 | 15 | ResolvedSource, |
12 | 16 | ) |
13 | | -from flatpak_node_generator.providers.pnpm import PnpmLockfileProvider |
| 17 | +from flatpak_node_generator.providers.pnpm import ( |
| 18 | + PnpmLockfileProvider, |
| 19 | + PnpmModuleProvider, |
| 20 | +) |
| 21 | +from flatpak_node_generator.providers.special import SpecialSourceProvider |
14 | 22 |
|
15 | 23 | TEST_LOCKFILE_V9 = """ |
16 | 24 | lockfileVersion: '9.0' |
@@ -325,3 +333,123 @@ def test_lockfile_v9_git_and_local(tmp_path: Path) -> None: |
325 | 333 | source=LocalSource(path='../local-pkg'), |
326 | 334 | ), |
327 | 335 | ] |
| 336 | + |
| 337 | + |
| 338 | +def test_pnpm_module_provider_tarball_url(tmp_path: Path) -> None: |
| 339 | + gen = ManifestGenerator() |
| 340 | + special = SpecialSourceProvider( |
| 341 | + gen, |
| 342 | + SpecialSourceProvider.Options( |
| 343 | + node_chromedriver_from_electron=None, |
| 344 | + electron_ffmpeg=None, |
| 345 | + electron_node_headers=False, |
| 346 | + nwjs_version=None, |
| 347 | + nwjs_node_headers=False, |
| 348 | + nwjs_ffmpeg=False, |
| 349 | + xdg_layout=True, |
| 350 | + node_sdk_extension=None, |
| 351 | + ), |
| 352 | + ) |
| 353 | + provider = PnpmModuleProvider(gen, special, tmp_path) |
| 354 | + |
| 355 | + provider._store_version = 'v3' |
| 356 | + provider._tarballs = [ |
| 357 | + PnpmModuleProvider._TarballInfo( |
| 358 | + tarball_name='normal-pkg-1.0.0.tgz', |
| 359 | + name='normal-pkg', |
| 360 | + version='1.0.0', |
| 361 | + integrity=Integrity('sha512', 'abc123def456'), |
| 362 | + ), |
| 363 | + PnpmModuleProvider._TarballInfo( |
| 364 | + tarball_name='url-pkg-http-123.tgz', |
| 365 | + name='url-pkg', |
| 366 | + version='http://example.com/url-pkg.tgz', |
| 367 | + integrity=Integrity('sha512', 'fedcba654'), |
| 368 | + ), |
| 369 | + PnpmModuleProvider._TarballInfo( |
| 370 | + tarball_name='url-pkg-https-123.tgz', |
| 371 | + name='url-pkg-2', |
| 372 | + version='https://example.com/url-pkg-2.tgz', |
| 373 | + integrity=Integrity('sha512', '99999999'), |
| 374 | + ), |
| 375 | + ] |
| 376 | + |
| 377 | + provider._add_store_population_script() |
| 378 | + |
| 379 | + # Manifest data source should have been added to gen._sources |
| 380 | + manifest_source_dict = next( |
| 381 | + dict(s) |
| 382 | + for s in gen._sources |
| 383 | + if dict(s).get('dest-filename') == 'pnpm-manifest.json' |
| 384 | + ) |
| 385 | + assert manifest_source_dict is not None |
| 386 | + |
| 387 | + manifest_data = json.loads(manifest_source_dict['contents']) |
| 388 | + packages = manifest_data['packages'] |
| 389 | + |
| 390 | + # Check each package based on original tarballs for correct handling |
| 391 | + for tarball in provider._tarballs: |
| 392 | + pkg = packages[tarball.tarball_name] |
| 393 | + assert pkg['version'] == tarball.version |
| 394 | + if tarball.version.startswith(('http://', 'https://')): |
| 395 | + assert pkg['tarball_url'] == tarball.version |
| 396 | + else: |
| 397 | + assert 'tarball_url' not in pkg |
| 398 | + |
| 399 | + |
| 400 | +@pytest.mark.asyncio |
| 401 | +async def test_pnpm_module_provider_missing_integrity( |
| 402 | + tmp_path: Path, requests: RequestsController |
| 403 | +) -> None: |
| 404 | + |
| 405 | + gen = ManifestGenerator() |
| 406 | + special = SpecialSourceProvider( |
| 407 | + gen, |
| 408 | + SpecialSourceProvider.Options( |
| 409 | + node_chromedriver_from_electron=None, |
| 410 | + electron_ffmpeg=None, |
| 411 | + electron_node_headers=False, |
| 412 | + nwjs_version=None, |
| 413 | + nwjs_node_headers=False, |
| 414 | + nwjs_ffmpeg=False, |
| 415 | + xdg_layout=True, |
| 416 | + node_sdk_extension=None, |
| 417 | + ), |
| 418 | + ) |
| 419 | + |
| 420 | + provider = PnpmModuleProvider(gen, special, tmp_path) |
| 421 | + provider._store_version = 'v3' |
| 422 | + |
| 423 | + lockfile = Lockfile(tmp_path / 'pnpm-lock.yaml', 9) |
| 424 | + |
| 425 | + test_data = b'dummy tarball content' |
| 426 | + test_digest = hashlib.sha256(test_data).hexdigest() |
| 427 | + expected_integrity = Integrity('sha256', test_digest) |
| 428 | + |
| 429 | + requests.server.expect_oneshot_request( |
| 430 | + '/test-pkg-1.0.0.tgz', 'GET' |
| 431 | + ).respond_with_data(test_data) |
| 432 | + |
| 433 | + source = ResolvedSource( |
| 434 | + resolved=requests.url_for('/test-pkg-1.0.0.tgz'), |
| 435 | + integrity=None, |
| 436 | + ) |
| 437 | + |
| 438 | + pkg = Package( |
| 439 | + lockfile=lockfile, |
| 440 | + name='test-pkg', |
| 441 | + version='1.0.0', |
| 442 | + source=source, |
| 443 | + ) |
| 444 | + |
| 445 | + await provider.generate_package(pkg) |
| 446 | + |
| 447 | + # Assert tarball was added with computed integrity |
| 448 | + assert len(provider._tarballs) == 1 |
| 449 | + assert provider._tarballs[0].integrity == expected_integrity |
| 450 | + |
| 451 | + # Assert it was added to manifest generator with the right integrity |
| 452 | + tarball_source = next( |
| 453 | + dict(s) for s in gen._sources if dict(s).get('url') == source.resolved |
| 454 | + ) |
| 455 | + assert tarball_source['sha256'] == expected_integrity.digest |
0 commit comments