Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add providing location for fetch #56

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

quepop
Copy link

@quepop quepop commented Jun 11, 2021

-extends #54
-Added location providing and filename deduction for every scheme
-Added a bunch of tests for the new functionality

Signed-off-by: Mateusz Perc m.perc@samsung.com

@quepop quepop closed this Jun 11, 2021
@quepop quepop reopened this Jun 11, 2021
Copy link
Member

@pombredanne pombredanne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
Do you think you could add a test?

@quepop quepop marked this pull request as draft June 14, 2021 00:37
@quepop quepop force-pushed the master branch 4 times, most recently from 3193c01 to a6b3f6c Compare June 24, 2021 00:20
@quepop quepop marked this pull request as ready for review June 24, 2021 00:43
@quepop quepop requested a review from pombredanne June 25, 2021 17:21
 -extends aboutcode-org#54
 -Added filename deduction (content-disposition/URL)
 -Fetch and its helper functions now use pathlib's Path

Signed-off-by: Mateusz Perc <m.perc@samsung.com>
 -Testing filename deduction for http/https/ftp schemes

Signed-off-by: Mateusz Perc <m.perc@samsung.com>
import os
import tempfile
from pathlib import Path
from pathlib import PurePosixPath
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just PurePath? It will be PurePosixPath on posix anyway and ci runs tests on {linux,macos,windows}

Copy link
Author

@quepop quepop Aug 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use PurePosixPaths only to handle URLs so i think it is more explicit that way.

from urllib.parse import urlparse
from kiss_headers import parse_it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -14,52 +14,103 @@
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

from fetchcode import fetch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to move this import, it was correctly placed: https://www.python.org/dev/peps/pep-0008/#imports

mock_get.return_value.headers = {"content-disposition": f"attachment; {parameters}"}
with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file:
location = Path.home()
response = fetch(HTTP_URL + FILENAMES[2], location)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FILENAMES[2] is confusing. I assume this is to test 3rd input set where name is inferred from url?
It could be made more readable - add url_filename parameter to tests and name those inputs (see comment above) so its clear what is tested here

],
)
@mock.patch("fetchcode.requests.get")
def test_fetch_http_with_filename_deduction(mock_get, parameters, expected_filename):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider changing parameters name to something more fitting, maybe content_disposition or filename?

@pytest.mark.parametrize(
"parameters, expected_filename",
[
pytest.param(f'filename*="{FILENAMES[0]}"; filename=""', FILENAMES[0]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider adding few test cases:

Suggested change
pytest.param(f'filename*="{FILENAMES[0]}"; filename=""', FILENAMES[0]),
pytest.param(f'filename*="{FILENAMES["a"]}"', FILENAMES["a"], id='should_choose_filename*'),
pytest.param(f'filename="{FILENAMES["a"]}"', FILENAMES["a"], id='should_choose_filename'),
pytest.param(f'filename*="{FILENAMES["a"]}"; filename={FILENAMES["b"]}', FILENAMES["a"], id='filename*_should_take_precedence'),
pytest.param(f'filename*="{FILENAMES["a"]}"; filename=""', FILENAMES["a"], id='should_choose_non_empty_filename*'),
pytest.param(f'filename*=""; filename="{FILENAMES["a"]}"', FILENAMES["a"], id='should_choose_non_empty_filename'),

this could be more readable if filename in url would also be param here and explicitly tested

location = Path.home()
mock_get.return_value.headers = {}
mock_tmp.return_value.name = location / FILENAMES[0]
with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why name it when the name is not used?


with mock.patch('fetchcode.open', mock.mock_open()) as mocked_file:
url = 'https://raw.githubusercontent.com/TG1999/converge/master/assets/Group%2022.png'
with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to name it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats not my test (black reformatted it)

assert mock_ftp.retrbinary.called


@mock.patch("fetchcode.FTP")
def test_fetch_ftp_with_filename_deduction(mock_ftp):
with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ibidem


FILENAMES = ["a.ext", "b.ext", "c.ext"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider switching to dict

Suggested change
FILENAMES = ["a.ext", "b.ext", "c.ext"]
FILENAMES = {"a": "a.ext",
"b": "b.ext",
"c": "c.ext"}

so you can access them as FILENAMES["a"] making it a little bit clearer than positionals

pombredanne added a commit that referenced this pull request May 10, 2022
Add black codestyle test for skeleton
@keshav-space keshav-space marked this pull request as draft September 19, 2024 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants