1- import importlib
2- import io
31import os
42import shlex
53import shutil
64import subprocess
75import tempfile
86import textwrap
9- import typing
107from collections import defaultdict
118from functools import partial
129from io import StringIO
1310from operator import attrgetter
1411from unittest .mock import patch
1512
1613import pytest
14+ from pkgcheck import __title__ as project
15+ from pkgcheck import base
16+ from pkgcheck import checks as checks_mod
17+ from pkgcheck import const , objects , reporters , scan
18+ from pkgcheck .scripts import run
1719from pkgcore import const as pkgcore_const
1820from pkgcore .ebuild import atom , restricts
1921from pkgcore .restrictions import packages
2224from snakeoil .formatters import PlainTextFormatter
2325from snakeoil .osutils import pjoin
2426
25- from pkgcheck import __title__ as project
26- from pkgcheck import base , const , objects , reporters , scan
27- from pkgcheck import checks as checks_mod
28- from pkgcheck .results import Result
29- from pkgcheck .scripts import run
30-
3127from ..misc import Profile
3228
3329
@@ -591,45 +587,23 @@ def _scan_results(self, repo, tmp_path, verbosity):
591587 assert len (results ) == len (results_set )
592588 return results_set
593589
594- def _load_expected_results (
595- self , path : str
596- ) -> tuple [typing .Callable [[typing .Set [Result ]], bool ] | None , set [Result ]]:
590+ def _get_results (self , path : str ):
597591 """Return the set of result objects from a given json stream file."""
598- explicit = []
599- custom = None
600- expected_path = self .repos_data / path / "expected.json"
601- custom_path = self .repos_data / path / "custom.py"
602592 try :
603- with expected_path .open () as f :
604- explicit = list (reporters .JsonStream .from_iter (f ))
593+ with ( self . repos_data / path ) .open () as f :
594+ return set (reporters .JsonStream .from_iter (f ))
605595 except FileNotFoundError :
606- pass
607- except Exception as e :
608- raise Exception (f"failed loading { expected_path } " ) from e
609- try :
610- with custom_path .open () as f :
611- # we have to use eval since the import pathway isn't valid import lookup
612- module = eval (f .read ())
613- custom = getattr (module , "handle" )
614- except FileNotFoundError :
615- pass
616- except Exception as e :
617- raise Exception (f"failed loading { custom_path } " ) from e
618- s_explicit = set (explicit )
619- assert len (s_explicit ) == len (explicit ), f"results of { expected_path } are not unique"
620- assert custom is not None or len (explicit ), (
621- f"{ (self .repos_data / path )!r} contains no expected.json nor custom.py"
622- )
623- return custom , set (explicit )
596+ return set ()
624597
625- def _render_results (self , results , ** kwargs ) -> str :
598+ def _render_results (self , results , ** kwargs ):
626599 """Render a given set of result objects into their related string form."""
627- # with tempfile.TemporaryFile() as f:
628- with io .BytesIO () as f :
600+ with tempfile .TemporaryFile () as f :
629601 with reporters .FancyReporter (out = PlainTextFormatter (f ), ** kwargs ) as reporter :
630602 for result in sorted (results ):
631603 reporter .report (result )
632- return f .getvalue ().decode ()
604+ f .seek (0 )
605+ output = f .read ().decode ()
606+ return output
633607
634608 @pytest .mark .parametrize ("repo" , repos )
635609 def test_scan_repo (self , repo , tmp_path_factory ):
@@ -641,16 +615,13 @@ def test_scan_repo(self, repo, tmp_path_factory):
641615 for check , keywords in self ._checks [repo ].items ():
642616 for keyword in keywords :
643617 # verify the expected results were seen during the repo scans
644- custom_check , expected_results = self ._load_expected_results (
645- f"{ repo } /{ check } /{ keyword } "
646- )
647- if custom_check :
648- custom_check (scan_results )
618+ expected_results = self ._get_results (f"{ repo } /{ check } /{ keyword } /expected.json" )
619+ assert expected_results , "regular results must always exist"
649620 assert self ._render_results (expected_results ), "failed rendering results"
650621 results .update (expected_results )
651622
652623 # when expected verbose results exist use them, otherwise fallback to using the regular ones
653- expected_verbose_results = self ._load_expected_results (
624+ expected_verbose_results = self ._get_results (
654625 f"{ repo } /{ check } /{ keyword } /expected-verbose.json"
655626 )
656627 if expected_verbose_results :
0 commit comments