コード例 #1
0
    def test_flatten_generator(self):
        def gen():
            for _ in range(2):
                yield range(5)

        expected = [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
        test = flatten(gen())
        assert expected == test
コード例 #2
0
    def test_flatten_generator(self):
        def gen():
            for _ in range(2):
                yield range(5)

        expected = [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
        test = flatten(gen())
        assert expected == test
コード例 #3
0
    def closure_test_function(*args, **kwargs):
        idx = cache.get_index()
        matches = idx.match(
            location=test_file,
            min_score=min_score,
            # if negative, do not detect negative rules when testing negative rules
            detect_negative=detect_negative)

        if not matches:
            matches = []

        # TODO: we should expect matches properly, not with a grab bag of flat license keys
        # flattened list of all detected license keys across all matches.
        detected_licenses = functional.flatten(
            map(unicode, match.rule.licenses) for match in matches)
        try:
            if not detect_negative:
                # we skipped negative detection for a negative rule
                # we just want to ensure that the rule was matched proper
                assert matches and not expected_licenses and not detected_licenses
            else:
                assert expected_licenses == detected_licenses
        except:
            # On failure, we compare against more result data to get additional
            # failure details, including the test_file and full match details
            match_failure_trace = []

            if trace_text:
                for match in matches:
                    qtext, itext = get_texts(match,
                                             location=test_file,
                                             idx=idx)
                    rule_text_file = match.rule.text_file
                    rule_data_file = match.rule.data_file
                    match_failure_trace.extend([
                        '', '', '======= MATCH ====', match,
                        '======= Matched Query Text for:',
                        'file://{test_file}'.format(**locals())
                    ])
                    if test_data_file:
                        match_failure_trace.append(
                            'file://{test_data_file}'.format(**locals()))
                    match_failure_trace.append(qtext.splitlines())
                    match_failure_trace.extend([
                        '',
                        '======= Matched Rule Text for:'
                        'file://{rule_text_file}'.format(**locals()),
                        'file://{rule_data_file}'.format(**locals()),
                        itext.splitlines(),
                    ])
            # this assert will always fail and provide a detailed failure trace
            assert expected_licenses == detected_licenses + [
                test_name, 'test file: file://' + test_file
            ] + match_failure_trace
コード例 #4
0
def check_license(location=None, query_string=None, expected=(), test_data_dir=None):
    if query_string:
        idx = cache.get_index()
        matches = idx.match(location=location, query_string=query_string)
        results = functional.flatten(map(unicode, match.rule.licenses) for match in matches)
        assert expected == results
    else:
        test_name = python_safe_name('test_' + location.replace(test_data_dir, ''))
        tester = make_license_test_function(
            expected_licenses=expected, test_file=location,
            test_data_file=None, test_name=test_name,
            trace_text=True)
        tester()
コード例 #5
0
def check_rule_or_license_can_be_self_detected_exactly(rule):
    idx = cache.get_index()
    matches = idx.match(
        location=rule.text_file,
        _skip_hash_match=True,
        deadline=10,
    )
    expected = [rule.identifier, '100']
    results = flatten(
        (m.rule.identifier, str(int(m.coverage()))) for m in matches)

    try:
        assert results == expected
    except:

        from licensedcode.tracing import get_texts
        data_file = rule.data_file
        if not data_file:
            data_file = rule.text_file.replace('.LICENSE', '.yml')
        text_file = rule.text_file
        # On failure, we compare againto get additional failure details such as
        # a clickable text_file path
        failure_trace = ['======= TEST ====']
        failure_trace.extend(results)
        failure_trace.extend([
            '',
            f'file://{data_file}',
            f'file://{text_file}',
            '======================',
        ])

        for i, match in enumerate(matches):
            qtext, itext = get_texts(match)
            m_text_file = match.rule.text_file

            if match.rule.is_from_license:
                m_data_file = m_text_file.replace('LICENSE', '.yml')
            else:
                m_data_file = match.rule.data_file

            failure_trace.extend([
                '', f'======= MATCH {i} ====',
                repr(match), f'file://{m_data_file}', f'file://{m_text_file}',
                '======= Matched Query Text:', '', qtext, ''
                '======= Matched Rule Text:', '', itext
            ])

        # this assert will always fail and provide a detailed failure trace
        assert '\n'.join(failure_trace) == '\n'.join(expected)
コード例 #6
0
    def closure_test_function(*args, **kwargs):
        idx = cache.get_index()
        matches = idx.match(location=test_file, min_score=min_score,
                            # if negative, do not detect negative rules when testing negative rules
                            detect_negative=detect_negative)

        if not matches:
            matches = []

        # TODO: we should expect matches properly, not with a grab bag of flat license keys
        # flattened list of all detected license keys across all matches.
        detected_licenses = functional.flatten(map(unicode, match.rule.licenses) for match in matches)
        try:
            if not detect_negative:
                # we skipped negative detection for a negative rule
                # we just want to ensure that the rule was matched proper
                assert matches and not expected_licenses and not detected_licenses
            else:
                assert expected_licenses == detected_licenses
        except:
            # On failure, we compare against more result data to get additional
            # failure details, including the test_file and full match details
            match_failure_trace = []

            if trace_text:
                for match in matches:
                    qtext, itext = get_texts(match, location=test_file, idx=idx)
                    rule_text_file = match.rule.text_file
                    rule_data_file = match.rule.data_file
                    match_failure_trace.extend(['', '',
                        '======= MATCH ====', match,
                        '======= Matched Query Text for:',
                        'file://{test_file}'.format(**locals())
                    ])
                    if test_data_file:
                        match_failure_trace.append('file://{test_data_file}'.format(**locals()))
                    match_failure_trace.append(qtext.splitlines())
                    match_failure_trace.extend(['',
                        '======= Matched Rule Text for:'
                        'file://{rule_text_file}'.format(**locals()),
                        'file://{rule_data_file}'.format(**locals()),
                        itext.splitlines(),
                    ])
            # this assert will always fail and provide a detailed failure trace
            assert expected_licenses == detected_licenses + [test_name, 'test file: file://' + test_file] + match_failure_trace
コード例 #7
0
def check_license(location=None,
                  query_string=None,
                  expected=(),
                  test_data_dir=None):
    if query_string:
        idx = cache.get_index()
        matches = idx.match(location=location, query_string=query_string)
        results = functional.flatten(
            map(unicode, match.rule.licenses) for match in matches)
        assert expected == results
    else:
        test_name = python_safe_name('test_' +
                                     location.replace(test_data_dir, ''))
        tester = make_license_test_function(expected_licenses=expected,
                                            test_file=location,
                                            test_data_file=None,
                                            test_name=test_name,
                                            trace_text=True)
        tester()
コード例 #8
0
 def test_flatten(self):
     expected = [7, 6, 5, 4, 'a', 3, 3, 2, 1]
     test = flatten([7, (6, [5, [4, ["a"], 3]], 3), 2, 1])
     assert expected == test
コード例 #9
0
def flat_keys(matches):
    """
    Return a flattened list of detected license keys, sorted by position and then rule order.
    """
    return functional.flatten(match.rule.licenses for match in matches)
コード例 #10
0
def flat_keys(matches):
    """
    Return a flattened list of detected license keys, sorted by position and then rule order.
    """
    return functional.flatten(match.rule.licenses for match in matches)
コード例 #11
0
 def test_flatten_empties(self):
     expected = ['a']
     test = flatten([[], (), ['a']])
     assert expected == test
コード例 #12
0
 def test_flatten(self):
     expected = [7, 6, 5, 4, 'a', 3, 3, 2, 1]
     test = flatten([7, (6, [5, [4, ["a"], 3]], 3), 2, 1])
     assert expected == test
コード例 #13
0
 def symbols(self):
     glocal = flatten([self.local_functions, self.global_functions])
     return sorted(glocal)
コード例 #14
0
 def test_flatten_empties(self):
     expected = ['a']
     test = flatten([[], (), ['a']])
     self.assertEqual(expected, test)
コード例 #15
0
 def test_flatten(self):
     expected = [7, 6, 5, 4, 'a', 3, 3, 2, 1]
     test = flatten([7, (6, [5, [4, ["a"], 3]], 3), 2, 1])
     self.assertEqual(expected, test)
コード例 #16
0
 def test_flatten_empties(self):
     expected = ['a']
     test = flatten([[], (), ['a']])
     assert expected == test
コード例 #17
0
 def symbols(self):
     return sorted(flatten([list(self.symbols_section.local_functions),
                     list(self.symbols_section.global_functions)]))