Skip to content

Commit

Permalink
bug with ssh git repo cloning (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
twaldear authored and austinbyers committed Jun 29, 2018
1 parent 20cac10 commit 2ae91f0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
5 changes: 4 additions & 1 deletion rules/clone_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ def _clone_repo(url: str, include: Optional[List[str]], exclude: Optional[List[s
subprocess.check_call(['git', 'clone', '--quiet', '--depth', '1', url, cloned_repo_root])

# Remove existing rules in target folder before copying (in case upstream rules were deleted).
target_repo_root = os.path.join(RULES_DIR, url.split('//')[1])
if '//' in url:
target_repo_root = os.path.join(RULES_DIR, url.split('//')[1])
else:
target_repo_root = os.path.join(RULES_DIR, url.split('@')[1].replace(':', '/', 1))
if os.path.exists(target_repo_root):
shutil.rmtree(target_repo_root)

Expand Down
18 changes: 14 additions & 4 deletions tests/rules/clone_rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def setUp(self):
{
"url": "https://github.com/test-user2/test-repo2",
"exclude": ["windows/*", "*_mobile.yara"]
},
{
"url": "[email protected]:test-user3/test-repo3",

}
]
}
Expand All @@ -98,10 +102,12 @@ def _mock_git_clone(self, args: List[str]) -> None:
if cloned_repo_root.endswith('test-repo1'):
self.fs.CreateFile(os.path.join(cloned_repo_root, 'yara', 'cloned.yara'))
self.fs.CreateFile(os.path.join(cloned_repo_root, 'not_included.yara'))
else:
elif cloned_repo_root.endswith('test-repo2'):
self.fs.CreateFile(os.path.join(cloned_repo_root, 'yara', 'cloned.yara'))
self.fs.CreateFile(os.path.join(cloned_repo_root, 'yara', 'exluded_mobile.yara'))
self.fs.CreateFile(os.path.join(cloned_repo_root, 'windows', 'excluded.yara'))
elif cloned_repo_root.endswith('test-repo3'):
self.fs.CreateFile(os.path.join(cloned_repo_root, 'yara', 'cloned.yara'))

@mock.patch.object(clone_rules, 'print')
def test_clone_remote_rules(self, mock_print: mock.MagicMock):
Expand All @@ -110,18 +116,22 @@ def test_clone_remote_rules(self, mock_print: mock.MagicMock):
clone_rules.clone_remote_rules()

mock_print.assert_has_calls([
mock.call('[1/2] Cloning https://github.com/test-user1/test-repo1... ',
mock.call('[1/3] Cloning https://github.com/test-user1/test-repo1... ',
end='', flush=True),
mock.call('1 YARA file copied'),
mock.call('[2/3] Cloning https://github.com/test-user2/test-repo2... ',
end='', flush=True),
mock.call('1 YARA file copied'),
mock.call('[2/2] Cloning https://github.com/test-user2/test-repo2... ',
mock.call('[3/3] Cloning git@github.com:test-user3/test-repo3... ',
end='', flush=True),
mock.call('1 YARA file copied'),
mock.call('Done! 2 YARA files cloned from 2 repositories.')
mock.call('Done! 3 YARA files cloned from 3 repositories.')
])

expected_files = {
'github.com/test-user1/test-repo1/yara/cloned.yara',
'github.com/test-user2/test-repo2/yara/cloned.yara',
'github.com/test-user3/test-repo3/yara/cloned.yara',
'private/private.yara'
}
self.assertEqual(expected_files, set(compile_rules._find_yara_files()))

0 comments on commit 2ae91f0

Please sign in to comment.