Example #1
0
    def test_file_cache_construction(self, tmpdir):
        base_path = Path(str(tmpdir))
        builder_path = base_path / '.builder'

        assert not builder_path.exists()

        # Make sure the directory is properly created.
        cache = FileCache(base_path)

        # noinspection PyProtectedMember
        assert cache._base_file_cache_path == builder_path
        assert builder_path.exists()

        # Make sure no errors occur if the directory already exists.
        cache = FileCache(base_path)

        # noinspection PyProtectedMember
        assert cache._base_file_cache_path == builder_path
        assert builder_path.exists()
Example #2
0
    def test_download_file_non_existent(self):
        with patch('builder.file_cache.FileCache._get_download_file_size'
                   ) as mock_dl_size:
            mock_dl_size.return_value = (None, False)

            # noinspection PyProtectedMember
            assert FileCache._download_file('the-url',
                                            Path('the-path')) is False

        assert mock_dl_size.mock_calls == [call('the-url')]
Example #3
0
    def test_resolve_file_existing_file_with_force_works(self, tmpdir):
        base_path = Path(str(tmpdir))
        the_file = Path('the.file')
        path = base_path / '.builder' / the_file
        mock_download = MagicMock()

        # Make sure the path already exists.
        path.parent.mkdir()
        path.touch()

        cache = FileCache(base_path)

        cache._download_file = mock_download
        mock_download.return_value = True

        with patch('builder.file_cache.global_options.force_remote_fetch'
                   ) as mock_frf:
            mock_frf.return_value = True

            assert cache.resolve_file('the-url', the_file) == path
            assert mock_download.mock_calls == [call('the-url', path)]
Example #4
0
    def test_get_download_file_size_no_file(self):
        response = FakeResponse(404, msg='Run away!')

        with patch('builder.file_cache.requests') as mock_requests:
            mock_requests.head.return_value = response

            length, exists = FileCache._get_download_file_size('the-url')

        assert mock_requests.head.mock_calls == [
            call('the-url', allow_redirects=True)
        ]
        assert length is None
        assert exists is False
Example #5
0
    def test_get_download_file_size_good_content_length(self):
        response = FakeResponse(200, content_length=7)

        with patch('builder.file_cache.requests') as mock_requests:
            mock_requests.head.return_value = response

            length, exists = FileCache._get_download_file_size('the-url')

        assert mock_requests.head.mock_calls == [
            call('the-url', allow_redirects=True)
        ]
        assert length == 7
        assert exists is True
Example #6
0
    def test_download_file(self, tmpdir):
        path = Path(str(tmpdir)) / 'path' / 'to' / 'file.txt'
        expected = 'The quick brown fox, blah, blah'
        response = FakeResponse(200, content=expected)

        with patch('builder.file_cache.FileCache._get_download_file_size'
                   ) as mock_dl_size:
            mock_dl_size.return_value = (len(expected), True)

            with patch('builder.file_cache.requests') as mock_requests:
                mock_requests.get.return_value = response

                # noinspection PyProtectedMember
                assert FileCache._download_file('the-url', path) is True

        assert path.exists()
        assert path.read_text(encoding='utf-8') == expected
Example #7
0
    def test_download_file_http_error(self):
        response = FakeResponse(502, msg='Run away!')

        with patch('builder.file_cache.FileCache._get_download_file_size'
                   ) as mock_dl_size:
            mock_dl_size.return_value = (7, True)

            with patch('builder.file_cache.requests') as mock_requests:
                mock_requests.get.return_value = response

                with pytest.raises(HTTPError) as he:
                    # noinspection PyProtectedMember
                    _ = FileCache._download_file('the-url', Path('the-path'))

        assert mock_dl_size.mock_calls == [call('the-url')]
        assert mock_requests.get.mock_calls == [
            call('the-url', allow_redirects=True)
        ]
        assert he.value.response == response
        assert he.value.args[0] == 'Run away!'