Project

General

Profile

« Previous | Next » 

Revision bd588eb9

Added by koszko over 1 year ago

add missing english translations and verify message texts of raised exceptions in tests

View differences:

tests/test_local_apt.py
290 290
    """
291 291
    sources_list = local_apt.SourcesList(['deb-src sth', 'deb sth'])
292 292

  
293
    with pytest.raises(local_apt.AptError) as excinfo:
293
    with pytest.raises(local_apt.AptError,
294
                       match='^couldnt_execute_apt-get_is_it_installed$'):
294 295
        with local_apt.local_apt(sources_list, local_apt.default_keys) as apt:
295 296
            pass
296 297

  
297
    assert len(excinfo.value.args) == 1
298
    assert isinstance(excinfo.value.args[0], str)
299
    assert '\n' not in excinfo.value.args[0]
300

  
301 298
@pytest.mark.subprocess_run(local_apt, make_run_apt_get(update_code=1))
302 299
@pytest.mark.usefixtures('mock_subprocess_run', 'mock_gnupg_import')
303 300
def test_local_apt_update_fail(mock_cache_dir):
......
307 304
    """
308 305
    sources_list = local_apt.SourcesList(['deb-src sth', 'deb sth'])
309 306

  
310
    with pytest.raises(local_apt.AptError) as excinfo:
311
        with local_apt.local_apt(sources_list, local_apt.default_keys) as apt:
312
            pass
307
    error_regex = """^\
308
command_apt-get -c \\S+ update_failed
309

  
310
STDOUT_OUTPUT_heading
311

  
312
some output
313 313

  
314
    assert len(excinfo.value.args) == 1
314
STDERR_OUTPUT_heading
315 315

  
316
    assert re.match(r'.*\n\n.*\n\nsome output\n\n.*\n\nsome error output',
317
                    excinfo.value.args[0])
316
some error output\
317
$\
318
"""
319

  
320
    with pytest.raises(local_apt.AptError, match=error_regex):
321
        with local_apt.local_apt(sources_list, local_apt.default_keys) as apt:
322
            pass
318 323

  
319 324
@pytest.mark.subprocess_run(local_apt, make_run_apt_get())
320 325
@pytest.mark.usefixtures('mock_subprocess_run', 'mock_gnupg_import')
......
359 364
    destination = mock_cache_dir / 'destination'
360 365
    destination.mkdir()
361 366

  
362
    with pytest.raises(local_apt.AptError) as excinfo:
367
    error_regex = f"""^\
368
command_apt-get -c \\S+ install --yes --just-print libjs-mathjax_failed
369

  
370
STDOUT_OUTPUT_heading
371

  
372
{re.escape(sample_install_stdout)}
373

  
374
STDERR_OUTPUT_heading
375

  
376
some error output\
377
$\
378
"""
379

  
380
    with pytest.raises(local_apt.AptError, match=error_regex):
363 381
        local_apt.download_apt_packages(sources_list, local_apt.default_keys,
364 382
                                        ['libjs-mathjax'], destination,
365 383
                                        with_deps=True)
366 384

  
367
    assert len(excinfo.value.args) == 1
368

  
369
    assert re.match(r'^.*\n\n.*\n\n', excinfo.value.args[0])
370
    assert re.search(r'\n\nsome error output$', excinfo.value.args[0])
371
    assert sample_install_stdout in excinfo.value.args[0]
372

  
373 385
    assert [*destination.iterdir()] == []
374 386

  
375 387
@pytest.mark.subprocess_run(local_apt, make_run_apt_get(download_code=1))
......
383 395
    destination = mock_cache_dir / 'destination'
384 396
    destination.mkdir()
385 397

  
386
    with pytest.raises(local_apt.AptError) as excinfo:
398
    error_regex = """^\
399
command_apt-get -c \\S+ download libjs-mathjax_failed
400

  
401
STDOUT_OUTPUT_heading
402

  
403
some output
404

  
405
STDERR_OUTPUT_heading
406

  
407
some error output\
408
$\
409
"""
410

  
411
    with pytest.raises(local_apt.AptError, match=error_regex):
387 412
        local_apt.download_apt_packages(sources_list, local_apt.default_keys,
388 413
                                        ['libjs-mathjax'], destination)
389 414

  
390
    assert len(excinfo.value.args) == 1
415
    assert [*destination.iterdir()] == []
391 416

  
392
    assert re.match(r'.*\n\n.*\n\nsome output\n\n.*\n\nsome error output',
393
                    excinfo.value.args[0])
417
@pytest.fixture
418
def mock_bad_deb_file(monkeypatch, mock_subprocess_run):
419
    """
420
    Make mocked 'apt-get download' command produce an incorrectly-named file.
421
    """
422
    old_run = local_apt.subprocess.run
423

  
424
    def twice_mocked_run(command, **kwargs):
425
        """
426
        Create an evil file if needed; then act just like the run() function
427
        that got replaced by this one.
428
        """
429
        if 'download' in command:
430
            destination = Path(kwargs.get('cwd') or Path.cwd())
431
            (destination / 'arbitrary-name').write_text('anything')
432

  
433
        return old_run(command, **kwargs)
434

  
435
    monkeypatch.setattr(local_apt.subprocess, 'run', twice_mocked_run)
436

  
437
@pytest.mark.subprocess_run(local_apt, make_run_apt_get())
438
@pytest.mark.usefixtures('mock_subprocess_run', 'mock_gnupg_import',
439
                         'mock_bad_deb_file')
440
def test_local_apt_download_bad_filename(mock_cache_dir):
441
    """
442
    Verify that the download_apt_packages() function raises a proper error when
443
    'apt-get download' command produces an incorrectly-named file.
444
    """
445
    sources_list = local_apt.SourcesList([], 'nabia')
446
    destination = mock_cache_dir / 'destination'
447
    destination.mkdir()
448

  
449
    error_regex = """^\
450
apt_download_gave_bad_filename_arbitrary-name
451

  
452
STDOUT_OUTPUT_heading
453

  
454
some output
455

  
456
STDERR_OUTPUT_heading
457

  
458
some error output\
459
$\
460
"""
461

  
462
    with pytest.raises(local_apt.AptError, match=error_regex):
463
        local_apt.download_apt_packages(sources_list, local_apt.default_keys,
464
                                        ['libjs-mathjax'], destination)
394 465

  
395 466
    assert [*destination.iterdir()] == []
396 467

  
......
405 476
    destination = mock_cache_dir / 'destination'
406 477
    destination.mkdir()
407 478

  
408
    with pytest.raises(local_apt.AptError) as excinfo:
409
        local_apt.download_apt_packages(sources_list, local_apt.default_keys,
410
                                        ['libjs-mathjax'], destination)
479
    error_regex = """^\
480
command_apt-get -c \\S* source --download-only \\S+_failed
481

  
482
STDOUT_OUTPUT_heading
411 483

  
412
    assert len(excinfo.value.args) == 1
484
some output
413 485

  
414
    assert re.match(r'.*\n\n.*\n\nsome output\n\n.*\n\nsome error output',
415
                    excinfo.value.args[0])
486
STDERR_OUTPUT_heading
487

  
488
some error output\
489
$\
490
"""
491

  
492
    with pytest.raises(local_apt.AptError, match=error_regex):
493
        local_apt.download_apt_packages(sources_list, local_apt.default_keys,
494
                                        ['libjs-mathjax'], destination)
416 495

  
417 496
    assert [*destination.iterdir()] == []
418 497

  
......
421 500
    list = local_apt.SourcesList([], 'nabia')
422 501
    assert list.identity() == 'nabia'
423 502

  
424
    with pytest.raises(local_apt.DistroError):
503
    with pytest.raises(local_apt.DistroError, match='^distro_nabiał_unknown$'):
425 504
        local_apt.SourcesList([], 'nabiał')
426 505

  
427 506
    list = local_apt.SourcesList(['deb sth', 'deb-src sth'], 'nabia')
......
557 636
        assert piggybacked.resolve_file(PurePosixPath('a/b/c')) == None
558 637
        assert piggybacked.resolve_file(PurePosixPath('')) == None
559 638

  
560
        with pytest.raises(FileReferenceError):
639
        output_text = 'loading_.apt-root/a/../../../b_outside_piggybacked_dir'
640
        with pytest.raises(FileReferenceError,
641
                           match=f'^{re.escape(output_text)}$'):
561 642
            piggybacked.resolve_file(PurePosixPath('.apt-root/a/../../../b'))
562 643

  
563 644
        root = piggybacked.resolve_file(PurePosixPath('.apt-root/dummy')).parent
......
615 696
    Verify that the piggybacked_system() function raises a proper error when
616 697
    'dpkg-deb' is missing.
617 698
    """
618
    with pytest.raises(local_apt.AptError) as excinfo:
699
    with pytest.raises(local_apt.AptError,
700
                       match='^couldnt_execute_dpkg-deb_is_it_installed$'):
619 701
        with local_apt.piggybacked_system({
620 702
                'system': 'apt',
621 703
                'distribution': 'nabia',
......
624 706
        }, None) as piggybacked:
625 707
            pass
626 708

  
627
    assert len(excinfo.value.args) == 1
628

  
629
    assert '\n' not in excinfo.value.args[0]
630

  
631

  
632 709
@pytest.mark.subprocess_run(local_apt, lambda c, **kw: run_dpkg_deb(c, 1, **kw))
633 710
@pytest.mark.usefixtures('mock_download_packages', 'mock_subprocess_run')
634 711
def test_piggybacked_system_fail():
......
636 713
    Verify that the piggybacked_system() function raises a proper error when
637 714
    'dpkg-deb -x' command returns non-0.
638 715
    """
639
    with pytest.raises(local_apt.AptError) as excinfo:
716
    error_regex = """^\
717
command_dpkg-deb -x \\S+\\.deb \\S+_failed
718

  
719
STDOUT_OUTPUT_heading
720

  
721
some output
722

  
723
STDERR_OUTPUT_heading
724

  
725
some error output\
726
$\
727
"""
728

  
729
    with pytest.raises(local_apt.AptError, match=error_regex):
640 730
        with local_apt.piggybacked_system({
641 731
                'system': 'apt',
642 732
                'distribution': 'nabia',
......
644 734
                'dependencies': False
645 735
        }, None) as piggybacked:
646 736
            pass
647

  
648
    assert len(excinfo.value.args) == 1
649

  
650
    assert re.match(r'.*\n\n.*\n\nsome output\n\n.*\n\nsome error output',
651
                    excinfo.value.args[0])

Also available in: Unified diff