Add tag support for GitHub URLs - #12
Conversation
|
@pombredanne @MaJuRG, please check this PR :) |
|
I ran it for https://github.com/nexb/scancode-toolkit and got this |
steven-esser
left a comment
There was a problem hiding this comment.
You need to add more tests here. Convert the test you did above locally into an actual test case as a first step.
|
Okay should I match both the arrays @MaJuRG the one that I shown in comments with the test case? |
|
@TG1999 Assuming the list does not change over time, this is fine. Otherwise you can simply check for the presence of a few tag values that we know will continue to exists. |
|
Okay I think I can match the last element of the array, since it won't change over time |
|
@MaJuRG done the changes as per your comments :) |
| def test_github_tags_for_not_empty_list(): | ||
| url = 'https://github.com/nexb/scancode-toolkit' | ||
| response = github_tags.get_tags(url=url) | ||
| assert (response[-1] == 'v1.4.2') |
There was a problem hiding this comment.
You should change this to assert 'v1.4.2' in response. This is easier to read.
Additionally, we should test for two additional items:
- Another version number that is a valid tag in
response - A version number that IS NOT in
response
steven-esser
left a comment
There was a problem hiding this comment.
Are you accessing the internet in your test cases? To me, it seems you are (which is something we do not want to do). Just want to make sure
|
Yes, I am since using it since I have to acess GitHub api |
steven-esser
left a comment
There was a problem hiding this comment.
We need to mock these API requests then. We do not want tests that rely on accessing potentially changing data on the web.
|
Cool, pushing changes in a while |
|
I have mocked the responses now you can merge it :) |
| @mock.patch('fetchcode.github_tags.requests.get') | ||
| def test_github_tags_for_not_empty_list(mock_get): | ||
| url = 'https://github.com/nexb/scancode-toolkit' | ||
| mock_get.return_value.json.return_value =[ |
There was a problem hiding this comment.
Can you move this big thing to a separate json file?
steven-esser
left a comment
There was a problem hiding this comment.
Since we are going to (probably) have multiple test files in the future, please:
- Create a new directory
tests/data/ - Rename this JSON file to
gh-scancode-tags-response.jsonor similar.
|
Okay making changes in 5 minutes |
|
Done @MaJuRG :) |
pombredanne
left a comment
There was a problem hiding this comment.
Thanks!
See some of my comments inline
| """ | ||
| Builds URL according to GitHub API | ||
| """ | ||
| return base_url + path + '/tags' |
There was a problem hiding this comment.
Can you describe what base_url and path are ? what would examples of values? Also using + sounds rather britlle...
What about using format or a f string instead? and does base_url ever changes?
There was a problem hiding this comment.
And also is this function really warranted since you are not even testing it?
There was a problem hiding this comment.
I have tested the get_tags() function that is using this function so should I also write unit test for this function 😅
|
|
||
| def build_url(base_url, path): | ||
| """ | ||
| Builds URL according to GitHub API |
There was a problem hiding this comment.
Do you mind using imperative style like in commit message?
e.g. Build URL according to GitHub API
or rather:
Return a URL to access the list of tags using the GitHub API
|
|
||
| def get_tags(url): | ||
| """ | ||
| Takes Github VCS URL as input and provides |
There was a problem hiding this comment.
Same comment as above: use imperative instead.
Return a list of git tags given the `url` to a Github repository
| print('Not a GitHub URL') | ||
|
|
||
| else: | ||
| final_url = build_url(base_url=base_url, path=url_parts.path) |
There was a problem hiding this comment.
See my comments on build_url
| else: | ||
| final_url = build_url(base_url=base_url, path=url_parts.path) | ||
| resp = requests.get(final_url) | ||
| print(resp) |
| print(resp) | ||
| tags = [] | ||
| for res in resp.json(): | ||
| tags.append(res['name']) |
There was a problem hiding this comment.
Are you sure 'name' is always there?
pombredanne
left a comment
There was a problem hiding this comment.
Thank you for the updates! see my comments
| url_parts = urlparse(url) | ||
|
|
||
| if not(url_parts.netloc == 'github.com'): | ||
| print('Not a GitHub URL') |
There was a problem hiding this comment.
Why print anything? You do not print things in a library: you return values or you raise Exceptions.
| # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
| # CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations under the License. | ||
| from unittest import mock |
There was a problem hiding this comment.
Please organize your imports:
import xxx <-std lib imports, one per line, sorted
empty line
import zzz <-third-party imports, one per line, sorted
empty line
import foo <-own modules imports, one per line, sorted
empty line
empty line
There was a problem hiding this comment.
Got this :), will take care for future too
| @mock.patch('fetchcode.github_tags.requests.get') | ||
| def test_github_tags_for_not_empty_list(mock_get): | ||
| url = 'https://github.com/nexb/scancode-toolkit' | ||
| with open('tests/data/gh_tags_response.json','r') as file: |
There was a problem hiding this comment.
Why r mode? this is the default.
| data = file.read() | ||
| mock_get.return_value.json.return_value = json.loads(data) | ||
| response = github_tags.get_tags(url=url) | ||
| assert 'v1.4.2' in response |
There was a problem hiding this comment.
Why assert only three values and not everything?
|
|
||
| def test_build_url_with_conventional_URL(): | ||
| url = 'https://github.com/nexb/fetchcode' | ||
| supposed_build_url = 'https://api.github.com/repos/nexb/fetchcode/tags' |
There was a problem hiding this comment.
supposed would likely be best as "expected" ?
| url = 'https://github.com/nexb/fetchcode' | ||
| supposed_build_url = 'https://api.github.com/repos/nexb/fetchcode/tags' | ||
| final_url = github_tags.build_url(url) | ||
| assert final_url == supposed_build_url |
There was a problem hiding this comment.
We typically write asserts the other ways as assert "expected value" == "obtained value"...
This ensures that in diffs the expected values are before or on the left and the actual obtained values are on the right or below... which is helpful as a convention when there are failure traces.
There was a problem hiding this comment.
Will remember this for future ref. too
| assert 'v3.1.1' in response | ||
| assert 'v3.1.0' in response | ||
| assert 'v3.0.2' in response | ||
| assert 'v3.0.1' in response | ||
| assert 'v3.0.0' in response | ||
| assert 'v2.9.9' in response | ||
| assert 'v2.9.8' in response | ||
| assert 'v2.9.7' in response | ||
| assert 'v2.9.6' in response | ||
| assert 'v2.9.5' in response | ||
| assert 'v2.9.4' in response | ||
| assert 'v2.9.3' in response | ||
| assert 'v2.9.2' in response | ||
| assert 'v2.9.1' in response | ||
| assert 'v2.9.0b1' in response | ||
| assert 'v2.2.1' in response | ||
| assert 'v2.2.0' in response | ||
| assert 'v2.1.0' in response | ||
| assert 'v2.0.1' in response | ||
| assert 'v2.0.0' in response | ||
| assert 'v2.0.0.rc3' in response | ||
| assert 'v2.0.0.rc2' in response | ||
| assert 'v2.0.0.rc1' in response | ||
| assert 'v1.6.3' in response | ||
| assert 'v1.6.2' in response | ||
| assert 'v1.6.1' in response | ||
| assert 'v1.6.0' in response | ||
| assert 'v1.5.0' in response | ||
| assert 'v1.4.3' in response | ||
| assert 'v1.4.2' in response | ||
| assert 'v5.0.1' not in response |
There was a problem hiding this comment.
Can you add these values to a single list and iterate thru them? There is no need to take up many lines here.
There was a problem hiding this comment.
Okay cool, doing changes in 10 minutes
|
Done @MaJuRG 😄 |
|
Thanks @MaJuRG now waiting for @pombredanne |
Signed-off-by: TG1999 <tushar.goel.dav@gmail.com>
|
Thanks! all merged. |
Solves issue #6 for GitHub URLs
Signed-off-by: TG1999 tushar.goel.dav@gmail.com