How to use the commoncode.testcase.FileDrivenTesting function in commoncode

To help you get started, we’ve selected a few commoncode examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nexB / scancode-toolkit / tests / scancode / test_scan_utils.py View on Github external
import click
click.disable_unicode_literals_warning = True

from click.termui import progressbar
from click.testing import CliRunner

from commoncode.testcase import FileDrivenTesting
from scancode import CommandLineOption
from scancode.cli import ScanCommand
from scancode.utils import fixed_width_file_name

import pytest
pytestmark = pytest.mark.scanpy3  # NOQA


class TestUtils(FileDrivenTesting):
    test_data_dir = os.path.join(os.path.dirname(__file__), 'data')

    def test_click_progressbar_with_labels(self):

        # test related to https://github.com/mitsuhiko/click/issues/406
        @click.command()
        def mycli():
            """Sample cmd with progress bar"""
            click.echo('Start')
            with progressbar(range(10), label='xyz') as it:
                for _ in it:
                    pass
            click.echo('End')

        runner = CliRunner()
        result = runner.invoke(mycli)
github nexB / scancode-toolkit / tests / cluecode / test_plugin_ignore_copyrights.py View on Github external
#  ScanCode is a free software code scanning tool from nexB Inc. and others.
#  Visit https://github.com/nexB/scancode-toolkit/ for support and download.

from __future__ import absolute_import
from __future__ import unicode_literals

from os.path import dirname
from os.path import join

from commoncode.testcase import FileDrivenTesting
from scancode.cli_test_utils import run_scan_click
from scancode.cli_test_utils import check_json_scan
from cluecode.plugin_ignore_copyrights import is_ignored


class TestIgnoreCopyrights(FileDrivenTesting):

    test_data_dir = join(dirname(__file__), 'data')

    def test_is_ignored(self):
        import re
        patterns = [re.compile('Berkeley'), re.compile('1993.*Californi')]
        test1 = 'The Regents of the University of California.'
        test2 ='Copyright (c) 1993 The Regents of the University of California.'
        test3 ='the University of California, Berkeley and its contributors.'

        assert not is_ignored(patterns, [test1])
        assert is_ignored(patterns, [test1, test2, test3])
        assert is_ignored(patterns, [test3])
        assert is_ignored(patterns, [test2])

    def test_ignore_holders(self):
github nexB / scancode-toolkit / tests / cluecode / test_copyrights_fosso.py View on Github external
] + expected

            assert failure_trace == copyrights

    return copyright_test_method


def build_tests(clazz):
    """
    Dynamically build test methods and attach these to the clazz test class.
    """
    for test_method, test_name in build_copyright_test_methods_with_fossology_data():
        setattr(clazz, test_name, test_method)


class TestCopyrightFossologyDataDriven(FileDrivenTesting):
    # test functions are attached to this class at module import time
    pass


build_tests(clazz=TestCopyrightFossologyDataDriven)
github nexB / scancode-toolkit / tests / scancode / test_extract_cli.py View on Github external
from __future__ import unicode_literals

import os

import click
from click.testing import CliRunner

from commoncode.fileutils import as_posixpath
from commoncode.fileutils import fsencode
from commoncode.fileutils import resource_iter
from commoncode.testcase import FileDrivenTesting
from commoncode.system import on_linux
from commoncode.system import on_windows
from scancode import extract_cli

test_env = FileDrivenTesting()
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), 'data')

"""
These CLI tests are dependent on py.test monkeypatch to  ensure we are testing
the actual command outputs as if using a TTY or not.
"""

EMPTY_STRING = b'' if on_linux else ''


def test_extractcode_command_can_take_an_empty_directory(monkeypatch):
    test_dir = test_env.get_temp_dir()
    monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)
    runner = CliRunner()
    result = runner.invoke(extract_cli.extractcode, [test_dir])
    assert result.exit_code == 0
github nexB / scancode-toolkit / tests / licensedcode / test_plugin_license_policy.py View on Github external
from __future__ import absolute_import
from __future__ import unicode_literals

from collections import OrderedDict
from os.path import dirname
from os.path import join

from commoncode.testcase import FileDrivenTesting
from licensedcode.plugin_license_policy import has_policy_duplicates
from licensedcode.plugin_license_policy import load_license_policy
from scancode.cli_test_utils import load_json_result
from scancode.cli_test_utils import run_scan_click


class TestLicensePolicy(FileDrivenTesting):

    test_data_dir = join(dirname(__file__), 'data')

    def test_process_codebase_info_license_duplicate_key_policy_file(self):
        test_dir = self.extract_test_tar('plugin_license_policy/policy-codebase.tgz')
        policy_file = self.get_test_loc('plugin_license_policy/process_codebase_info_license_duplicate_key_policy_file.yml')

        result_file = self.get_temp_file('json')

        run_scan_click(['--info', '--license', '--license-policy', policy_file, test_dir, '--json-pp', result_file])

        scan_result = load_json_result(result_file)

        for result in scan_result['files']:
            assert 'license_policy' in result.keys()
            assert {} == result['license_policy']
github nexB / scancode-toolkit / tests / formattedcode / test_output_spdx.py View on Github external
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import codecs
from collections import OrderedDict
import os
import re

import xmltodict

from commoncode.testcase import FileDrivenTesting
from scancode_cli_test_utils import run_scan_click
from scancode_cli_test_utils import run_scan_plain

test_env = FileDrivenTesting()
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), 'data')


def strip_variable_text(rdf_text):
    """
    Return rdf_text stripped from variable parts such as rdf nodeids
    """

    replace_nid = re.compile('rdf:nodeID="[^\"]*"').sub
    rdf_text = replace_nid('', rdf_text)

    replace_creation = re.compile('.*', re.DOTALL).sub
    rdf_text = replace_creation('', rdf_text)

    replace_pcc = re.compile('.*', re.DOTALL).sub
    rdf_text = replace_pcc('', rdf_text)
github nexB / scancode-toolkit / tests / scancode / test_plugin_consolidate.py View on Github external
#  for any legal advice.
#  ScanCode is a free software code scanning tool from nexB Inc. and others.
#  Visit https://github.com/nexB/scancode-toolkit/ for support and download.

from __future__ import absolute_import
from __future__ import unicode_literals

from os import path

from commoncode.testcase import FileDrivenTesting
from scancode.cli_test_utils import check_json_scan
from scancode.cli_test_utils import run_scan_click
from scancode.plugin_consolidate import is_majority


class TestConsolidate(FileDrivenTesting):
    test_data_dir = path.join(path.dirname(__file__), 'data')

    def get_scan(self, test_loc, cli_options='-clip'):
        scan_loc = self.get_test_loc(test_loc)
        scan_file = self.get_temp_file('json')
        run_scan_click(['-clip', scan_loc, '--json', scan_file])
        return scan_file

    def test_is_majority_above_threshold(self):
        files_count = 10
        src_count = 8
        assert is_majority(src_count, files_count)

    def test_is_majority_below_threshold(self):
        files_count = 10
        src_count = 7
github nexB / scancode-toolkit / tests / scancode / test_plugin_only_findings.py View on Github external
#  Visit https://github.com/nexB/scancode-toolkit/ for support and download.

from __future__ import absolute_import
from __future__ import unicode_literals

from os.path import dirname
from os.path import join

import pytest

from commoncode.testcase import FileDrivenTesting
from scancode.cli_test_utils import run_scan_click
from scancode.cli_test_utils import check_json_scan


class TestHasFindings(FileDrivenTesting):

    test_data_dir = join(dirname(__file__), 'data')

    @pytest.mark.scanslow
    def test_scan_only_findings(self):
        test_dir = self.extract_test_tar('plugin_only_findings/basic.tgz')
        result_file = self.get_temp_file('json')
        expected_file = self.get_test_loc('plugin_only_findings/expected.json')
        run_scan_click(['-clip', '--only-findings', '--json', result_file, test_dir])
        check_json_scan(expected_file, result_file, remove_file_date=True, regen=False)

    def test_scan_only_findings_with_errors(self):
        test_dir = self.get_test_loc('plugin_only_findings/errors')
        result_file = self.get_temp_file('json')
        expected_file = self.get_test_loc('plugin_only_findings/errors.expected.json')
        run_scan_click(['-ci', '--only-findings', '--timeout', '0.0001',
github nexB / scancode-toolkit / tests / scancode / test_scan_utils.py View on Github external
expected = 'drupal.js'
        assert expected == test

    def test_fixed_width_file_name_with_win_path_with_shortening(self):
        test = fixed_width_file_name('C\\:Documents_and_Settings\\Boki\\Desktop\\head\\patches\\drupal6\\012345678901234567890123.c', 25)
        expected = '0123456789...4567890123.c'
        assert expected == test

    def test_fixed_width_file_name_with_very_small_file_name_and_long_extension(self):
        test = fixed_width_file_name('abc.abcdef', 5)
        # FIXME: what is expected is TBD
        expected = ''
        assert expected == test


class TestHelpGroups(FileDrivenTesting):

    test_data_dir = os.path.join(os.path.dirname(__file__), 'data')

    def test_scan_help_group_and_sort_order_without_custom_class(self):

        @click.command(name='scan', cls=ScanCommand)
        @click.option('--opt', is_flag=True, help='Help text for option')
        def scan(opt):
            pass

        runner = CliRunner()
        result = runner.invoke(scan, ['--help'])
        from scancode import MISC_GROUP
        assert MISC_GROUP in result.output
        assert  '--opt   Help text for option' in result.output
github nexB / scancode-toolkit / tests / scancode / test_plugin_ignore.py View on Github external
]
        self.check_ProcessIgnore(test_dir, expected, ignore)

    def test_ProcessIgnore_with_multiple_ignores(self):
        test_dir = self.extract_test_tar('plugin_ignore/user.tgz')
        ignore = ('*.doc', '*/src/test/*',)
        expected = [
            'user',
            'user/src',
            'user/src/test'
        ]
        self.check_ProcessIgnore(test_dir, expected, ignore)



class TestScanPluginIgnoreFiles(FileDrivenTesting):

    test_data_dir = join(dirname(__file__), 'data')

    def test_scancode_ignore_vcs_files_and_dirs_by_default(self):
        test_dir = self.extract_test_tar('plugin_ignore/vcs.tgz')
        result_file = self.get_temp_file('json')
        args = ['--copyright', '--strip-root', test_dir, '--json', result_file]
        run_scan_click(args)
        scan_result = load_json_result(result_file)
        # a single test.tst file and its directory that is not a VCS file should
        # be listed
        assert 1 == scan_result['headers'][0]['extra_data']['files_count']
        scan_locs = [x['path'] for x in scan_result['files']]
        assert [u'vcs', u'vcs/test.txt'] == scan_locs

    def test_scancode_ignore_vcs_files_and_dirs_by_default_no_multiprocess(self):