-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Haiko Schol <hs@haikoschol.com>
- Loading branch information
1 parent
7f1e8ab
commit 87ea90a
Showing
5 changed files
with
255 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# Copyright (c) 2017 nexB Inc. and others. All rights reserved. | ||
# http://nexb.com and https://github.com/nexB/vulnerablecode/ | ||
# The VulnerableCode software is licensed under the Apache License version 2.0. | ||
# Data generated with VulnerableCode require an acknowledgment. | ||
# | ||
# You may not use this software except in compliance with the License. | ||
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software distributed | ||
# 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. | ||
# | ||
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode | ||
# derivative work, you must accompany this data with the following acknowledgment: | ||
# | ||
# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
# VulnerableCode should be considered or used as legal advice. Consult an Attorney | ||
# for any legal advice. | ||
# VulnerableCode is a free software code scanning tool from nexB Inc. and others. | ||
# Visit https://github.com/nexB/vulnerablecode/ for support and download. | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from vulnerabilities.data_source import GitDataSource | ||
from vulnerabilities.data_source import InvalidConfigError | ||
|
||
|
||
def mk_ds(**kwargs): | ||
# batch_size is a required parameter of the base class, unrelated to these tests | ||
return GitDataSource(batch_size=100, config=kwargs) | ||
|
||
|
||
def test_GitDataSource_validate_config_repository_url_required(no_mkdir, no_rmtree): | ||
ds = mk_ds() | ||
|
||
with pytest.raises(InvalidConfigError): | ||
ds.validate_config() | ||
|
||
|
||
def test_GitDataSource_validate_config_repository_url_must_be_string(no_mkdir, no_rmtree): | ||
ds = mk_ds(repository_url=True) | ||
|
||
with pytest.raises(InvalidConfigError): | ||
ds.validate_config() | ||
|
||
|
||
def test_GitDataSource_validate_config_branch_must_be_string(no_mkdir, no_rmtree): | ||
ds = mk_ds(repository_url='asdf', branch={}) | ||
|
||
with pytest.raises(InvalidConfigError): | ||
ds.validate_config() | ||
|
||
|
||
def test_GitDataSource_validate_config_working_directory_must_be_string(no_mkdir, no_rmtree): | ||
ds = mk_ds(repository_url='asdf', working_directory=[]) | ||
|
||
with pytest.raises(InvalidConfigError): | ||
ds.validate_config() | ||
|
||
|
||
def test_GitDataSource_validate_config_create_working_directory_must_be_bool(no_mkdir, no_rmtree): | ||
ds = mk_ds(repository_url='asdf', create_working_directory='maybe') | ||
|
||
with pytest.raises(InvalidConfigError): | ||
ds.validate_config() | ||
|
||
|
||
def test_GitDataSource_validate_config_remove_working_directory_must_be_bool(no_mkdir, no_rmtree): | ||
ds = mk_ds(repository_url='asdf', remove_working_directory='maybe') | ||
|
||
with pytest.raises(InvalidConfigError): | ||
ds.validate_config() | ||
|
||
|
||
def test_GitDataSource_validate_config_create_working_directory_must_be_set_when_working_directory_is_default( | ||
no_mkdir, no_rmtree): | ||
|
||
ds = mk_ds(repository_url='asdf', create_working_directory=False) | ||
|
||
with pytest.raises(InvalidConfigError): | ||
ds.validate_config() | ||
|
||
|
||
def test_GitDataSource_validate_config_remove_working_directory_must_be_set_when_working_directory_is_default( | ||
no_mkdir, no_rmtree): | ||
|
||
ds = mk_ds(repository_url='asdf', remove_working_directory=False) | ||
|
||
with pytest.raises(InvalidConfigError): | ||
ds.validate_config() | ||
|
||
|
||
def test_GitDataSource_validate_config_remove_working_directory_is_applied(no_mkdir, no_rmtree): | ||
ds = mk_ds(repository_url='asdf', remove_working_directory=False, working_directory='/some/directory') | ||
|
||
ds.validate_config() | ||
|
||
assert not ds.remove_working_directory | ||
|
||
|
||
def test_GitDataSource_validate_config_working_directory_must_exist_when_create_working_directory_is_not_set( | ||
no_mkdir, no_rmtree): | ||
|
||
ds = mk_ds(repository_url='asdf', working_directory='/does/not/exist', create_working_directory=False) | ||
|
||
with pytest.raises(InvalidConfigError): | ||
ds.validate_config() | ||
|
||
|
||
@patch('os.path.exists', return_value=False) | ||
@patch('shutil.rmtree') | ||
@patch('pygit2.clone_repository') | ||
@patch('os.mkdir') | ||
def test_GitDataSource_contextmgr_working_directory_is_created_and_removed(mkdir, clone_repository, rmtree, _): | ||
|
||
wd = '/some/working/directory' | ||
ds = mk_ds(repository_url='asdf', working_directory=wd) | ||
ds.validate_config() | ||
|
||
assert wd == ds.working_directory | ||
|
||
with ds: | ||
assert mkdir.called_with(wd) | ||
|
||
assert mkdir.called_with(wd) | ||
assert clone_repository.called_with('asdf', wd, checkout_branch=ds.branch) | ||
assert rmtree.called_with(wd) | ||
|
||
|
||
@patch('shutil.rmtree') | ||
@patch('pygit2.clone_repository') | ||
@patch('tempfile.mkdtemp', return_value='/fake/tempdir') | ||
def test_GitDataSource_contextmgr_calls_mkdtemp_if_working_directory_is_not_set(mkdtemp, clone_repository, rmtree): | ||
|
||
ds = mk_ds(repository_url='asdf') | ||
ds.validate_config() | ||
|
||
with ds: | ||
assert mkdtemp.called | ||
assert ds.working_directory == '/fake/tempdir' | ||
|
||
assert clone_repository.called_with('asdf', '/fake/tempdir', checkout_branch=ds.branch) | ||
assert rmtree.called_with('/fake/tempdir') |