Skip to content

Commit 476156b

Browse files
authored
Merge pull request snyk-labs#71 from garethr/extend-test-methods
Added support for more test methods, including Yarn and Composer
2 parents d783696 + 2bd1366 commit 476156b

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,12 @@ As well as testing individual packages you can also test all packages found in v
151151

152152
* `test_pipfile(<file-handle-or-string>)` - returns an IssueSet for all Python dependencies in a `Pipfile`
153153
* `test_gemfilelock(<file-handle-or-string>)` - returns an IssueSet for all Ruby dependencies in a `Gemfile`
154-
* `test_packagejson(<file-handle-or-string>)` - returns an IssueSet for all Javascript dependencies in a `package.json` file
154+
* `test_packagejson(<file-handle-or-string>, (<lock-file-handle-or-string>))` - returns an IssueSet for all Javascript dependencies in a `package.json` file. Optionally takes a `package.lock` file
155155
* `test_gradlefile(<file-handle-or-string>)` - returns an IssueSet for all dependencies in a `Gradlefile`
156156
* `test_sbt(<file-handle-or-string>)` - returns an IssueSet for all dependencies defined in a `.sbt` file
157157
* `test_pom(<file-handle-or-string>)` - returns an IssueSet for all dependencies in a Maven `pom.xml` file
158+
* `test_yarn(<file-handle-or-string>, <lock-file-handle-or-string>)` - returns an IssueSet for all dependencies in Yarn `package.json` and `yarn.lock` files
159+
* `test_composer(<file-handle-or-string>, <lock-file-handle-or-string>)` - returns an IssueSet for all dependencies in Composer `composer.json` and `composer.lock` files
158160

159161
For example, here we are testing a Python `Pipfile`.
160162

snyk/models.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def invite(self, email: str, admin: bool = False) -> bool:
168168
payload = {"email": email, "isAdmin": admin}
169169
return bool(self.client.post(path, payload))
170170

171-
def _test(self, path, contents=None):
171+
def _test(self, path, contents=None, additional=None):
172172
if contents:
173173
# Check for a file-like object, allows us to support files
174174
# and strings in the same interface
@@ -180,6 +180,15 @@ def _test(self, path, contents=None):
180180
"encoding": "base64",
181181
"files": {"target": {"contents": encoded}},
182182
}
183+
184+
# Some test methods carry a second file, often a lock file
185+
if additional:
186+
read = getattr(additional, "read", None)
187+
if callable(read):
188+
additional = additional.read()
189+
encoded = base64.b64encode(additional.encode()).decode()
190+
post_body["files"]["additional"] = {"contents": encoded}
191+
183192
resp = self.client.post(path, post_body)
184193
else:
185194
resp = self.client.get(path)
@@ -216,9 +225,9 @@ def test_gemfilelock(self, contents):
216225
path = "test/rubygems?org=%s" % self.id
217226
return self._test(path, contents)
218227

219-
def test_packagejson(self, contents):
228+
def test_packagejson(self, contents, lock=None):
220229
path = "test/npm?org=%s" % self.id
221-
return self._test(path, contents)
230+
return self._test(path, contents, lock)
222231

223232
def test_gradlefile(self, contents):
224233
path = "test/gradle?org=%s" % self.id
@@ -232,6 +241,14 @@ def test_pom(self, contents):
232241
path = "test/maven?org=%s" % self.id
233242
return self._test(path, contents)
234243

244+
def test_composer(self, contents, lock):
245+
path = "test/composer?org=%s" % self.id
246+
return self._test(path, contents, lock)
247+
248+
def test_yarn(self, contents, lock):
249+
path = "test/yarn?org=%s" % self.id
250+
return self._test(path, contents, lock)
251+
235252

236253
@dataclass
237254
class Integration(DataClassJSONMixin):

snyk/test_models.py

+21
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ def test_packagejson_test_with_file(
155155
requests_mock.post("%s/test/npm" % base_url, json=blank_test)
156156
assert organization.test_packagejson(fake_file)
157157

158+
def test_packagejson_test_with_files(
159+
self, organization, base_url, blank_test, fake_file, requests_mock
160+
):
161+
162+
requests_mock.post("%s/test/npm" % base_url, json=blank_test)
163+
assert organization.test_packagejson(fake_file, fake_file)
164+
158165
def test_gradlefile_test_with_file(
159166
self, organization, base_url, blank_test, fake_file, requests_mock
160167
):
@@ -176,6 +183,20 @@ def test_pom_test_with_file(
176183
requests_mock.post("%s/test/maven" % base_url, json=blank_test)
177184
assert organization.test_pom(fake_file)
178185

186+
def test_composer_with_files(
187+
self, organization, base_url, blank_test, fake_file, requests_mock
188+
):
189+
190+
requests_mock.post("%s/test/composer" % base_url, json=blank_test)
191+
assert organization.test_composer(fake_file, fake_file)
192+
193+
def test_yarn_with_files(
194+
self, organization, base_url, blank_test, fake_file, requests_mock
195+
):
196+
197+
requests_mock.post("%s/test/yarn" % base_url, json=blank_test)
198+
assert organization.test_yarn(fake_file, fake_file)
199+
179200
def test_missing_package_test(self, organization, base_url, requests_mock):
180201
requests_mock.get("%s/test/rubygems/puppet/4.0.0" % base_url, status_code=404)
181202
with pytest.raises(SnykError):

0 commit comments

Comments
 (0)