-
-
Notifications
You must be signed in to change notification settings - Fork 27
Similarity Matching by Fingerprint Comparison. #128
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
Merged
steven-esser
merged 10 commits into
aboutcode-org:develop
from
arnav-mandal1234:gsoc_similarity_matching
Aug 28, 2019
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c9a338b
Basic prototype.
arnav-mandal1234 5f77aea
Edits __init__.py
arnav-mandal1234 18b70eb
Fixes minor bug
arnav-mandal1234 06f7c67
Edits tests
arnav-mandal1234 f30ed61
Adds tests files
arnav-mandal1234 8c476ac
Adds new tests
arnav-mandal1234 a2abce2
Fixes bugs
arnav-mandal1234 98db37a
Refactors code and adds tests
arnav-mandal1234 de61d43
Refactors code
arnav-mandal1234 628fea1
Edits tests
arnav-mandal1234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,170 @@ | ||
| # | ||
| # Copyright (c) nexB Inc. and others. All rights reserved. | ||
| # http://nexb.com and https://github.com/nexB/deltacode/ | ||
| # The DeltaCode software is licensed under the Apache License version 2.0. | ||
| # Data generated with DeltaCode require an acknowledgment. | ||
| # DeltaCode is a trademark of nexB Inc. | ||
| # | ||
| # 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 DeltaCode or any DeltaCode | ||
| # derivative work, you must accompany this data with the following acknowledgment: | ||
| # | ||
| # Generated with DeltaCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
| # DeltaCode should be considered or used as legal advice. Consult an Attorney | ||
| # for any legal advice. | ||
| # DeltaCode is a free and open source software analysis tool from nexB Inc. and others. | ||
| # Visit https://github.com/nexB/deltacode/ for support and download. | ||
| # | ||
|
|
||
| from __future__ import absolute_import | ||
| from __future__ import print_function | ||
| from __future__ import division | ||
| from __future__ import unicode_literals | ||
|
|
||
| from collections import OrderedDict | ||
|
|
||
| import io | ||
| import json | ||
|
|
||
| from commoncode.system import on_windows | ||
|
|
||
|
|
||
| def run_scan_click(options, monkeypatch=None, test_mode=True, expected_rc=0, env=None): | ||
| """ | ||
| Run a scan as a Click-controlled subprocess | ||
| If monkeypatch is provided, a tty with a size (80, 43) is mocked. | ||
| Return a click.testing.Result object. | ||
| """ | ||
| import click | ||
| from click.testing import CliRunner | ||
| from deltacode import cli | ||
|
|
||
| options = add_windows_extra_timeout(options) | ||
|
|
||
| if monkeypatch: | ||
| monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True) | ||
| monkeypatch.setattr(click , 'get_terminal_size', lambda : (80, 43,)) | ||
| runner = CliRunner() | ||
|
|
||
| result = runner.invoke(cli.cli, options, catch_exceptions=False, env=env) | ||
|
|
||
| output = result.output | ||
| if result.exit_code != expected_rc: | ||
| opts = get_opts(options) | ||
| error = ''' | ||
| Failure to run: deltacode %(opts)s | ||
| output: | ||
| %(output)s | ||
| ''' % locals() | ||
| assert result.exit_code == expected_rc, error | ||
| return result | ||
|
|
||
|
|
||
| def get_opts(options): | ||
| try: | ||
| return ' '.join(options) | ||
| except: | ||
| try: | ||
| return b' '.join(options) | ||
| except: | ||
| return b' '.join(map(repr, options)) | ||
|
|
||
|
|
||
| WINDOWS_CI_TIMEOUT = '222.2' | ||
|
|
||
|
|
||
| def add_windows_extra_timeout(options, timeout=WINDOWS_CI_TIMEOUT): | ||
| """ | ||
| Add a timeout to an options list if on Windows. | ||
| """ | ||
| if on_windows and '--timeout' not in options: | ||
| # somehow the Appevyor windows CI is now much slower and timeouts at 120 secs | ||
| options += ['--timeout', timeout] | ||
| return options | ||
|
|
||
|
|
||
| def check_json_scan(expected_file, result_file, regen=False, remove_file_date=False, ignore_headers=False): | ||
|
steven-esser marked this conversation as resolved.
|
||
| """ | ||
| Check the scan `result_file` JSON results against the `expected_file` | ||
| expected JSON results. | ||
|
|
||
| If `regen` is True the expected_file WILL BE overwritten with the new scan | ||
| results from `results_file`. This is convenient for updating tests | ||
| expectations. But use with caution. | ||
|
|
||
| if `remove_file_date` is True, the file.date attribute is removed. | ||
| """ | ||
| results = load_json_result(result_file, remove_file_date) | ||
| if regen: | ||
| with open(expected_file, 'wb') as reg: | ||
| json.dump(results, reg, indent=2, separators=(',', ': ')) | ||
|
|
||
| expected = load_json_result(expected_file, remove_file_date) | ||
|
|
||
| if ignore_headers: | ||
| results.pop('headers', None) | ||
| expected.pop('headers', None) | ||
|
|
||
| # NOTE we redump the JSON as a string for a more efficient display of the | ||
| # failures comparison/diff | ||
| # TODO: remove sort, this should no longer be needed | ||
| expected = json.dumps(expected, indent=2, sort_keys=True, separators=(',', ': ')) | ||
| results = json.dumps(results, indent=2, sort_keys=True, separators=(',', ': ')) | ||
| assert expected == results | ||
|
|
||
|
|
||
| def load_json_result(location, remove_file_date=False): | ||
| """ | ||
| Load the JSON scan results file at `location` location as UTF-8 JSON. | ||
|
|
||
| To help with test resilience against small changes some attributes are | ||
| removed or streamlined such as the "tool_version" and scan "errors". | ||
|
|
||
| To optionally also remove date attributes from "files" and "headers" | ||
| entries, set the `remove_file_date` argument to True. | ||
| """ | ||
| with io.open(location, encoding='utf-8') as res: | ||
| scan_results = res.read() | ||
| return load_json_result_from_string(scan_results, remove_file_date) | ||
|
|
||
|
|
||
| def load_json_result_from_string(string, remove_file_date=False): | ||
| """ | ||
| Load the JSON scan results `string` as UTF-8 JSON. | ||
| """ | ||
| scan_results = json.loads(string, object_pairs_hook=OrderedDict) | ||
| # clean new headers attributes | ||
| streamline_headers(scan_results) | ||
|
|
||
| scan_results['deltas'].sort(key=lambda x: x['factors'], reverse=False) | ||
| scan_results['deltas'].sort(key=lambda x: x['score'], reverse=True) | ||
| return scan_results | ||
|
|
||
|
|
||
| def streamline_errors(errors): | ||
| """ | ||
| Modify the `errors` list in place to make it easier to test | ||
| """ | ||
| for i, error in enumerate(errors[:]): | ||
| error_lines = error.splitlines(True) | ||
| if len(error_lines) <= 1: | ||
| continue | ||
| # keep only first and last line | ||
| cleaned_error = ''.join([error_lines[0] + error_lines[-1]]) | ||
| errors[i] = cleaned_error | ||
|
|
||
|
|
||
| def streamline_headers(headers): | ||
| """ | ||
| Modify the `headers` list of mappings in place to make it easier to test. | ||
| """ | ||
| headers.pop('deltacode_version', None) | ||
| headers.pop('deltacode_options', None) | ||
| streamline_errors(headers['deltacode_errors']) | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.