Exemplo n.º 1
0
    def test_reading(self, tmp_path: Path):
        test_file = tmp_path / 'beef.food'
        test_file.write_text('This is my test file\nIt has two lines')

        test_unit = FileTextReader(test_file)
        content = [line for line in test_unit.line_by_line()]
        assert content == ['This is my test file\n', 'It has two lines']

        # Call again on a now read file...
        for _ in test_unit.line_by_line():
            fail(' No lines should be generated from a read file')
Exemplo n.º 2
0
    def run(self, artifacts: List[Artifact]) -> List[Artifact]:
        logger = logging.getLogger(__name__)

        if len(artifacts) == 1:
            artifact = artifacts[0]
        else:
            msg = ('Header Analyser expects only one Artifact, '
                   f'but was given {len(artifacts)}')
            raise TaskException(msg)

        new_artifact = Artifact(artifact.location, artifact.filetype,
                                HeadersAnalysed)

        reader = FileTextReader(artifact.location)
        logger.debug('Looking for headers in: %s', reader.filename)
        for line in reader.line_by_line():
            include_match: Optional[Match] \
                = self._include_pattern.match(line)
            if include_match:
                include: str = include_match.group(1)
                logger.debug('Found header: %s', include)
                if include.startswith(('"', "'")):
                    include = include.strip('"').strip("'")
                    logger.debug('  * User header; adding dependency')
                    new_artifact.add_dependency(Path(self._workspace /
                                                     include))

        return [new_artifact]
Exemplo n.º 3
0
 def hash(self) -> int:
     # If this is the first access of the property calculate the hash
     # and cache it for later accesses
     if self._hash is None:
         self._hash = 1
         reader = FileTextReader(self.location)
         for line in reader.line_by_line():
             self._hash = adler32(bytes(line, encoding='utf-8'), self._hash)
     return self._hash
Exemplo n.º 4
0
    def run(self, artifacts: List[Artifact]) -> List[Artifact]:
        if len(artifacts) == 1:
            artifact = artifacts[0]
        else:
            msg = ('Header Analyser expects only one Artifact, '
                   f'but was given {len(artifacts)}')
            raise TaskException(msg)

        new_artifact = Artifact(artifact.location, artifact.filetype,
                                HeadersAnalysed)

        reader = FileTextReader(artifact.location)
        for line in reader.line_by_line():
            include_match: Optional[Match] \
                = self._include_pattern.match(line)
            if include_match:
                include: str = include_match.group(1)
                if include.startswith(('"', "'")):
                    include = include.strip('"').strip("'")
                    new_artifact.add_dependency(Path(self._workspace /
                                                     include))

        return [new_artifact]