Пример #1
0
def test_analysis_results():
    # using a physical analysis file, this tests:
    #   new source file
    #   changed source file
    #   previously analysed source file, unchanged
    #   source file no longer there

    # source folder before change
    previous_file_hashes = {
        Path('change.f90'): 111,
        Path('no_change.f90'): 222,
        Path('remove.f90'): 333,
    }

    # source folder after change
    latest_file_hashes = {
        Path('change.f90'): 123,
        Path('no_change.f90'): 222,
        Path('new.f90'): 444,
    }

    previous_results = {
        AnalysedFile(fpath=path, file_hash=file_hash)
        for path, file_hash in previous_file_hashes.items()
    }

    with TemporaryDirectory() as tmpdir:
        analyser = Analyse(root_symbol=None)

        # simulate the effect of calling run, in which the superclass sets up the _config attribute (is this too ugly?)
        analyser._config = mock.Mock(project_workspace=Path(tmpdir))

        # create the initial analysis file
        with analyser._new_analysis_file(unchanged=previous_results):
            pass

        # check it loads correctly with no changes detected
        loaded_results = analyser._load_analysis_results(previous_file_hashes)
        changed, unchanged = analyser._what_needs_reanalysing(
            prev_results=loaded_results,
            latest_file_hashes=previous_file_hashes)
        assert not changed
        assert unchanged == previous_results

        # check we correctly identify new, changed, unchanged and removed files
        loaded_results = analyser._load_analysis_results(latest_file_hashes)
        changed, unchanged = analyser._what_needs_reanalysing(
            prev_results=loaded_results, latest_file_hashes=latest_file_hashes)
        assert unchanged == {
            AnalysedFile(fpath=Path('no_change.f90'), file_hash=222)
        }
        assert changed == {
            HashedFile(fpath=Path('change.f90'), file_hash=123),
            HashedFile(fpath=Path('new.f90'), file_hash=444)
        }
Пример #2
0
    def _what_needs_reanalysing(self, prev_results: Dict[Path, AnalysedFile], latest_file_hashes: Dict[Path, int]) -> \
            Tuple[Set[HashedFile], Set[AnalysedFile]]:
        """
        Determine which files have changed since they were last analysed.

        Returns, in a tuple:
             - The changed files as a set of HashedFile
             - The unchanged files as a set of AnalysedFile

        """
        # work out what needs to be reanalysed
        changed: Set[HashedFile] = set()
        unchanged: Set[AnalysedFile] = set()
        for latest_fpath, latest_hash in latest_file_hashes.items():
            # what happened last time we analysed this file?
            prev_pu = prev_results.get(latest_fpath)
            if (not prev_pu) or prev_pu.file_hash != latest_hash:
                changed.add(HashedFile(latest_fpath, latest_hash))
            else:
                unchanged.add(prev_pu)

        logger.info(
            f"{len(unchanged)} files already analysed, {len(changed)} to analyse"
        )

        return changed, unchanged
Пример #3
0
    def test_empty_file(self):
        mock_tree = Mock(content=[None])
        with mock.patch('fab.tasks.fortran.FortranAnalyser._parse_file',
                        return_value=mock_tree):
            result = FortranAnalyser().run(
                HashedFile(fpath=None, file_hash=None))

        assert type(result) is EmptySourceFile
Пример #4
0
def test_simple_result(tmp_path):
    fpath = Path(Path(__file__).parent / "test_c_analyser.c")
    result = CAnalyser().run(HashedFile(fpath, None))

    expected = AnalysedFile(
        fpath=fpath,
        file_hash=None,
        symbol_deps={'usr_var', 'usr_func'},
        symbol_defs={
            'func_decl', 'func_def', 'var_def', 'var_extern_def', 'main'
        },
        file_deps=set(),
        mo_commented_file_deps=set(),
    )
    assert result == expected
Пример #5
0
    def test_program_file(self, module_fpath, module_expected):
        # same as test_real_file() but replacing MODULE with PROGRAM
        with NamedTemporaryFile(mode='w+t', suffix='.f90') as tmp_file:
            tmp_file.write(module_fpath.open().read().replace(
                "MODULE", "PROGRAM"))
            tmp_file.flush()
            result = FortranAnalyser().run(
                HashedFile(fpath=Path(tmp_file.name), file_hash=None))

            module_expected.fpath = Path(tmp_file.name)
            module_expected.module_defs = set()
            module_expected.symbol_defs.update(
                {'internal_sub', 'internal_func'})

            assert result == module_expected
Пример #6
0
    def test_vanilla(self, analyser):
        # ensure we know when a file has changed since we last analysed it

        prev_results = {
            Path('foo.f90'): mock.Mock(spec=AnalysedFile, file_hash=123),
            Path('bar.f90'): mock.Mock(spec=AnalysedFile, file_hash=456),
        }

        latest_file_hashes = {
            Path('foo.f90'): 999999,
            Path('bar.f90'): 456,
        }

        changed, unchanged = analyser._what_needs_reanalysing(
            prev_results=prev_results, latest_file_hashes=latest_file_hashes)

        assert changed == {HashedFile(
            Path('foo.f90'),
            999999,
        )}
        assert unchanged == {
            prev_results[Path('bar.f90')],
        }
Пример #7
0
 def fake_hasher(fpath):
     return HashedFile(fpath, hash(str(fpath)))
Пример #8
0
 def test_module_file(self, module_fpath, module_expected):
     result = FortranAnalyser().run(
         HashedFile(fpath=module_fpath, file_hash=None))
     assert result == module_expected