-
Notifications
You must be signed in to change notification settings - Fork 120
Added support for more test methods, including Yarn and Composer #71
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,7 +164,7 @@ def notification_settings(self): | |
def invite(self, email: str, admin: bool = False): | ||
raise SnykNotImplementedError # pragma: no cover | ||
|
||
def _test(self, path, contents=None): | ||
def _test(self, path, contents=None, additional=None): | ||
if contents: | ||
# Check for a file-like object, allows us to support files | ||
# and strings in the same interface | ||
|
@@ -176,6 +176,15 @@ def _test(self, path, contents=None): | |
"encoding": "base64", | ||
"files": {"target": {"contents": encoded}}, | ||
} | ||
|
||
# Some test methods carry a second file, often a lock file | ||
if additional: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Python is EAFP than LBYL therefore a Pythonic way of writing this code block would look like: try:
read = getattr(additional, "read", None)
additional = additional.read()
encoded = base64.b64encode(additional.encode()).decode()
post_body["files"]["additional"] = {"contents": encoded}
except:
pass I'm aware of the fact that the code block at the top of this method is LBYL and therefore this one is too, but worth noting IMHO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I refer the consistency vs having two stanzas with different styles. Not against someone refactoring the whole method mind. It requires some additional type hints I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have a point there. let's take this one separately as part of a refactor (happy to take it) |
||
read = getattr(additional, "read", None) | ||
if callable(read): | ||
additional = additional.read() | ||
encoded = base64.b64encode(additional.encode()).decode() | ||
post_body["files"]["additional"] = {"contents": encoded} | ||
|
||
resp = self.client.post(path, post_body) | ||
else: | ||
resp = self.client.get(path) | ||
|
@@ -212,9 +221,9 @@ def test_gemfilelock(self, contents): | |
path = "test/rubygems?org=%s" % self.id | ||
return self._test(path, contents) | ||
|
||
def test_packagejson(self, contents): | ||
def test_packagejson(self, contents, lock=None): | ||
path = "test/npm?org=%s" % self.id | ||
return self._test(path, contents) | ||
return self._test(path, contents, lock) | ||
|
||
def test_gradlefile(self, contents): | ||
path = "test/gradle?org=%s" % self.id | ||
|
@@ -228,6 +237,14 @@ def test_pom(self, contents): | |
path = "test/maven?org=%s" % self.id | ||
return self._test(path, contents) | ||
|
||
def test_composer(self, contents, lock): | ||
path = "test/composer?org=%s" % self.id | ||
return self._test(path, contents, lock) | ||
|
||
def test_yarn(self, contents, lock): | ||
path = "test/yarn?org=%s" % self.id | ||
return self._test(path, contents, lock) | ||
|
||
|
||
@dataclass | ||
class Integration(DataClassJSONMixin): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we add type hints for this method as well, like so:
using typing.IO will cover both binary(if necessary) and text file-like objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't quite work as is, as the interface accepts file handles or strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clarifying, API looks ambiguous to me. shouldn't the function calling
_test
be the one reading the file content and passing that on? I guess this is an inherited design smell.worth a re-factor, though I'm sure we'll have to consider backwards compatibility here.
for instance why
test_pipfile(self, content)
is acting liketest_pipfile_content(self, content)
while what I think would be cleaner is: