Exemple #1
0
    def render_demo_config_file(directory: str, invalid_packages: List[Package]) -> str:
        if Configuration.exists_in_directory(directory):
            return ''

        licenses = BaseLicenseChecker.remove_duplicates(list(map(lambda package: package.license, invalid_packages)))
        licenses.sort()
        demo_configuration = Configuration(allowed_licenses=licenses)

        return f'''
Exemple #2
0
def test_load_external_configuration():
    with NamedTemporaryFile() as cache_file:
        with Server(ConfigurationTestServer) as ts:
            configuration = Configuration(cache_name=cache_file.name)

            loaded_configuration = configuration.load_external_configuration(
                ConfigurationInclude(url=ts.url()))
            assert loaded_configuration == Configuration(
                allowed_licenses=['MIT', 'GPL'],
                excluded_packages=['demo1234'])
Exemple #3
0
def test_load_with_partial_values():
    demo_configuration = {
        'allowedLicenses': ['MIT', 'GPL'],
    }

    with TemporaryDirectory() as directory:
        with open(Configuration.get_config_file_path(directory),
                  'w') as config_file:
            dump(demo_configuration, config_file)

        configuration = Configuration.load_from_directory(directory)
        assert configuration == Configuration(['MIT', 'GPL'])
Exemple #4
0
    def do_GET(self) -> None:
        demo_configuration = Configuration(
            allowed_licenses=['MIT', 'GPL'],
            excluded_packages=['demo1234'],
        )

        contents = demo_configuration.to_yaml().encode()
        self.send_response(200)
        self.send_header('Content-type', 'text/yaml')
        self.send_header('Content-length', len(contents))
        self.end_headers()
        self.wfile.write(contents)
Exemple #5
0
def test_invalidate_cache():
    with NamedTemporaryFile() as cache_file:
        with Server(ConfigurationTestServer) as ts:
            base_configuration = Configuration(
                allowed_licenses=['Apache 2.0'],
                includes=[ConfigurationInclude(url=ts.url())],
                cache_name=cache_file.name)
            merged_configuration = base_configuration.merge_includes()

            assert merged_configuration.session.cache.urls == [ts.url()]

            merged_configuration.invalidate_cache()

            assert merged_configuration.session.cache.urls == []
Exemple #6
0
def test_merge_includes():
    with NamedTemporaryFile() as cache_file:
        with Server(ConfigurationTestServer) as ts:
            base_configuration = Configuration(
                allowed_licenses=['Apache 2.0'],
                includes=[
                    ConfigurationInclude(url=ts.url()),
                ],
                cache_name=cache_file.name)
            merged_configuration = base_configuration.merge_includes()

            assert merged_configuration == Configuration(
                allowed_licenses=['Apache 2.0', 'MIT', 'GPL'],
                excluded_packages=['demo1234'],
                includes=[ConfigurationInclude(url=ts.url())])
Exemple #7
0
        def test_run_returns_success(self):
            Configuration(
                allowed_licenses=['BSD License', 'GPL', 'MIT License'
                                  ]).save_to_directory(self.directory.name)

            args = self.Object()
            args.filenames = [join(self.directory.name, 'package.json')]
            result = self.checker.run(args)
            assert result == 0
Exemple #8
0
def test_to_yaml():
    demo_configuration = Configuration(
        allowed_licenses=['MIT', 'GPL'],
        excluded_packages=['demo1234'],
        includes=[
            ConfigurationInclude(
                url='http://localhost:8000/license-check-node.yaml')
        ])
    expected = '''allowedLicenses:
- MIT
- GPL
excludedPackages:
- demo1234
include:
- url: http://localhost:8000/license-check-node.yaml
'''

    assert demo_configuration.to_yaml() == expected
 def test_parse_packages(self):
     configuration = Configuration()
     packages = self.checker.parse_packages(self.DEMO_LICENSE_OUTPUT, configuration, self.directory.name)
     assert packages == [
         Package('starlette', '0.14.1', 'BSD License'),
         Package('demo1234', '0.14.1', 'GPL'),
         Package('urllib3', '1.26.4', 'MIT License'),
         Package('uvicorn', '0.13.3', 'BSD License'),
         Package('zipp', '3.4.1', 'MIT License')]
Exemple #10
0
 def test_parse_packages(self):
     packages = self.checker.parse_packages(
         self.DEMO_LICENSE_OUTPUT,
         Configuration(allowed_licenses=[], excluded_packages=[]),
         self.directory.name)
     assert packages == [
         Package('xtend', '4.0.2', 'MIT'),
         Package('y18n', '4.0.0', 'ISC'),
         Package('y18n', '5.0.5', 'ISC'),
     ]
Exemple #11
0
def test_load_with_file():
    demo_configuration = {
        'allowedLicenses': ['MIT', 'GPL'],
        'excludedPackages': ['demo1234'],
        'include': [{
            'url': 'http://localhost:8000/license-check-node.yaml'
        }]
    }

    with TemporaryDirectory() as directory:
        with open(Configuration.get_config_file_path(directory),
                  'w') as config_file:
            dump(demo_configuration, config_file)

        configuration = Configuration.load_from_directory(directory)
        assert configuration == Configuration(['MIT', 'GPL'], ['demo1234'], [
            ConfigurationInclude(
                'http://localhost:8000/license-check-node.yaml')
        ])
Exemple #12
0
 def test_find_invalid_packages(self):
     packages = [
         Package('starlette', '0.14.1', 'BSD License'),
         Package('demo1234', '0.14.1', 'GPL'),
         Package('urllib3', '1.26.4', 'MIT License'),
     ]
     invalid_packages = self.checker.find_invalid_packages(
         packages,
         Configuration(allowed_licenses=['BSD License', 'MIT License']))
     assert invalid_packages == [Package('demo1234', '0.14.1', 'GPL')]
Exemple #13
0
 def test_remove_excluded_packages(self):
     packages = [
         Package('starlette', '0.14.1', 'BSD License'),
         Package('demo1234', '0.14.1', 'GPL'),
         Package('urllib3', '1.26.4', 'MIT License'),
     ]
     filtered_packages = BaseLicenseChecker.remove_excluded_packages(
         packages, Configuration(excluded_packages=['demo1234']))
     assert filtered_packages == [
         Package('starlette', '0.14.1', 'BSD License'),
         Package('urllib3', '1.26.4', 'MIT License'),
     ]
 def run(self, args) -> int:
     result = 0
     for filename in args.filenames:
         try:
             configuration = Configuration.load_from_file(
                 filename, return_empty_if_not_present=False)
             configuration.sort()
             configuration.save_to_file(filename)
         except Exception as ex:
             logger.info('Linting fails with error: %s', ex)
             result += 1
     return result
    def test_run_returns_zero_on_valid_configuration(self):
        base_configuration = Configuration(
            allowed_licenses=['MIT', 'Apache 2.0'],
            includes=[
                ConfigurationInclude(url='https://examle.com/test.yaml'),
                ConfigurationInclude(url='https://examle.com/test2.yaml'),
            ],
            cache_name=self.cache_file.name)

        base_configuration.save_to_directory(self.directory.name)

        args = Mock()
        args.filenames = [
            Path(self.directory.name, CONFIG_FILE_NAME).absolute()
        ]

        assert self.checker.run(args) == 0

        assert Configuration.load_from_directory(
            self.directory.name) == Configuration(
                allowed_licenses=['Apache 2.0', 'MIT'],
                includes=[
                    ConfigurationInclude(url='https://examle.com/test.yaml'),
                    ConfigurationInclude(url='https://examle.com/test2.yaml'),
                ],
                cache_name=self.cache_file.name)
Exemple #16
0
    def run(self, args) -> int:
        return_code = 0

        directories = self.consolidate_directories(args.filenames)

        for directory in directories:
            logger.info(f'Starting scan in %s...', directory)

            configuration = Configuration.load_from_directory(directory).merge_includes()
            logger.debug('Loaded configuration %s', configuration)
            self.prepare_directory(directory)
            installed_packages = self.load_installed_packages(directory, configuration)
            filtered_packages = self.remove_excluded_packages(installed_packages, configuration)
            invalid_packages = self.find_invalid_packages(filtered_packages, configuration)
            if len(invalid_packages) > 0:
                configuration.invalidate_cache()
                return_code = 1
                self.print_license_warning(directory, invalid_packages)

        return return_code
Exemple #17
0
    def test_parse_packages(self):
        target_directory = Path(self.directory.name, 'build', 'reports',
                                'licenses')
        target_directory.mkdir(parents=True)

        with open(join(target_directory.absolute(), 'licenseReport.json'),
                  'w') as licenses_file:
            licenses_file.write(self.LICENSE_REPORT_JSON)

        packages = self.checker.parse_packages(
            '', Configuration(allowed_licenses=[], excluded_packages=[]),
            self.directory.name)
        assert packages == [
            Package('org.apache.logging.log4j:log4j-api', '2.13.3',
                    'Apache License, Version 2.0'),
            Package('org.apache.logging.log4j:log4j-to-slf4j', '2.13.3',
                    'Apache License, Version 2.0'),
            Package('com.fasterxml.jackson.datatype:jackson-datatype-jdk8',
                    '2.11.4', 'The Apache Software License, Version 2.0'),
        ]
Exemple #18
0
    def test_parse_packages(self):
        target_directory = Path(self.directory.name, 'target',
                                'generated-resources')
        target_directory.mkdir(parents=True)

        with open(join(target_directory.absolute(), 'licenses.xml'),
                  'w') as licenses_file:
            licenses_file.write(self.LICENSES_XML)

        packages = self.checker.parse_packages(
            '', Configuration(allowed_licenses=[], excluded_packages=[]),
            self.directory.name)
        assert packages == [
            Package(
                'ch.qos.logback:logback-classic', '1.2.3',
                'Eclipse Public License - v 1.0;GNU Lesser General Public License'
            ),
            Package(
                'ch.qos.logback:logback-core', '1.2.3',
                'Eclipse Public License - v 1.0;GNU Lesser General Public License'
            ),
            Package('com.fasterxml.jackson.core:jackson-annotations', '2.11.4',
                    'The Apache Software License, Version 2.0')
        ]
Exemple #19
0
    def prepare_test_directory(self):
        base_configuration = Configuration(
            allowed_licenses=['MIT', 'Apache 2.0'], )

        base_configuration.save_to_directory(self.directory.name)
Exemple #20
0
    def test_render_demo_config_file_with_file(self):
        Configuration().save_to_directory(self.directory.name)

        text = self.checker.render_demo_config_file(
            self.directory.name, [Package('demo1234', '0.14.1', 'GPL')])
        assert text == ''
Exemple #21
0
def test_load_without_file():
    with TemporaryDirectory() as directory:
        configuration = Configuration.load_from_directory(directory)
        assert configuration == Configuration([], [])
 def test_success(self):
     Configuration(allowed_licenses=self.get_allowed_licenses(), excluded_packages=[], includes=[]).save_to_directory(
         self.directory.name)
     result = self.run_pre_commit()
     assert result.returncode == 0