Example #1
0
def test_spdx_info_of_directory(empty_directory):
    """Raise IsADirectoryError when calling spdx_info_of on a directory."""
    (empty_directory / "src").mkdir()

    project = Project(empty_directory)
    with pytest.raises(IsADirectoryError):
        project.spdx_info_of(empty_directory / "src")
Example #2
0
def test_lint_submodule(submodule_repository):
    """Extremely simple test for lint with an ignored submodule."""
    project = Project(submodule_repository)
    (submodule_repository / "submodule/foo.c").touch()
    report = ProjectReport.generate(project)
    result = lint(report)
    assert result
Example #3
0
def test_all_files_ignore_shl_license(empty_directory):
    """SHL-2.1 contains an SPDX tag referencing itself. It should be skipped."""
    (empty_directory / "SHL-2.1").write_text("foo")
    (empty_directory / "SHL-2.1.txt").write_text("foo")

    project = Project(empty_directory)
    assert not list(project.all_files())
Example #4
0
def test_spdx_info_of_unlicensed_file(fake_repository):
    """Return an empty SpdxInfo object when asking for the SPDX information
    of a file that has no SPDX information.
    """
    (fake_repository / "foo.py").touch()
    project = Project(fake_repository)
    assert not any(project.spdx_info_of("foo.py"))
Example #5
0
def test_licenses_duplicate(empty_directory):
    """Raise a RuntimeError if multiple files resolve to the same license."""
    text = "Valid-License-Identifier: MIT"
    (empty_directory / "COPYING").write_text(text)
    (empty_directory / "LICENSE").write_text(text)
    with pytest.raises(RuntimeError):
        Project(empty_directory)
Example #6
0
def test_licenses_empty(empty_directory):
    """If the identifier of a license could not be identified, silently carry
    on."""
    (empty_directory / "LICENSES").mkdir()
    (empty_directory / "LICENSES/foo.txt").touch()
    project = Project(empty_directory)
    assert "foo" in project.licenses
Example #7
0
def test_licenses_empty(empty_directory, license_file):
    """If the identifier of a license could not be identified, silently carry
    on."""
    (empty_directory / "LICENSES").mkdir()
    (empty_directory / license_file).touch()
    project = Project(empty_directory)
    assert "LicenseRef-Unknown0" in project.licenses
Example #8
0
def test_all_files(empty_directory):
    """Given a directory with some files, yield all files."""
    (empty_directory / "foo").touch()
    (empty_directory / "bar").touch()

    project = Project(empty_directory)
    assert {file_.name for file_ in project.all_files()} == {"foo", "bar"}
Example #9
0
def test_detect_all_licenses(empty_directory):
    """In a directory where licenses are marked through various means, detect
    all of them.
    """
    license_files = ["COPYING", "LICENSE", "COPYRIGHT", "LICENSES/foo"]
    (empty_directory / "LICENSES").mkdir()

    counter = 1
    for lic in license_files:
        (empty_directory / lic).write_text(
            "Valid-License-Identifier: LicenseRef-{}".format(counter))
        counter += 1

    (empty_directory / "LICENSES/MIT.txt").write_text("nothing")
    (empty_directory /
     "LICENSES/LicenseRef-{}.txt".format(counter)).write_text("nothing")

    project = Project(empty_directory)

    assert len(project.licenses) == 6
    assert all(
        [Path(lic) in project.licenses.values() for lic in license_files])
    assert Path("LICENSES/MIT.txt") in project.licenses.values()
    assert (Path("LICENSES/LicenseRef-{}.txt".format(counter))
            in project.licenses.values())
Example #10
0
def test_all_files_ignore_dot_license(empty_directory):
    """When file and file.license are present, only yield file."""
    (empty_directory / "foo").touch()
    (empty_directory / "foo.license").touch()

    project = Project(empty_directory)
    assert {file_.name for file_ in project.all_files()} == {"foo"}
Example #11
0
def test_all_files_ignore_git(empty_directory):
    """When the git directory is present, ignore it."""
    (empty_directory / ".git").mkdir()
    (empty_directory / ".git/config").touch()

    project = Project(empty_directory)
    assert not list(project.all_files())
Example #12
0
def test_lint_submodule_included(submodule_repository):
    """Extremely simple test for lint with an included submodule."""
    project = Project(submodule_repository, include_submodules=True)
    (submodule_repository / "submodule/foo.c").write_text("foo")
    report = ProjectReport.generate(project)
    result = lint(report)
    assert not result
Example #13
0
def test_spdx_info_of_file_does_not_exist(fake_repository):
    """Raise FileNotFoundError when asking for the SPDX info of a file that
    does not exist.
    """
    project = Project(fake_repository)
    with pytest.raises(FileNotFoundError):
        project.spdx_info_of(fake_repository / "does_not_exist")
Example #14
0
def test_lint_unused_licenses(fake_repository, stringio):
    """An unused license is detected."""
    (fake_repository / "LICENSES/MIT.txt").write_text("foo")
    project = Project(fake_repository)
    report = ProjectReport.generate(project)
    lint_summary(report, out=stringio)

    assert "MIT" in stringio.getvalue()
Example #15
0
def test_generate_project_report_bad_license_in_file(fake_repository):
    """Bad licenses in files are detected."""
    (fake_repository / "foo.py").write_text("SPDX" "-License-Identifier: bad")

    project = Project(fake_repository)
    result = ProjectReport.generate(project)

    assert "bad" in result.bad_licenses
Example #16
0
def test_spdx_info_of_binary_succeeds(fake_repository):
    """spdx_info_of succeeds when the target is covered by dep5."""
    shutil.copy(RESOURCES_DIRECTORY / "fsfe.png",
                fake_repository / "doc/fsfe.png")

    project = Project(fake_repository)
    spdx_info = project.spdx_info_of("doc/fsfe.png")
    assert LicenseSymbol("CC0-1.0") in spdx_info.spdx_expressions
Example #17
0
def test_lint_files_without_copyright_and_licensing(fake_repository, stringio):
    """A file without copyright and licensing is detected."""
    (fake_repository / "foo.py").write_text("foo")
    project = Project(fake_repository)
    report = ProjectReport.generate(project)
    result = lint_files_without_copyright_and_licensing(report, out=stringio)

    assert "foo.py" in str(list(result)[0])
    assert "foo.py" in stringio.getvalue()
Example #18
0
def test_generate_project_report_bad_license(fake_repository):
    """Bad licenses are detected."""
    (fake_repository / "LICENSES/bad.txt").touch()

    project = Project(fake_repository)
    result = ProjectReport.generate(project)

    assert result.bad_licenses
    assert not result.missing_licenses
Example #19
0
def test_all_files_git_ignored_different_cwd(git_repository):
    """Given a Git repository where some files are ignored, do not yield those
    files.

    Be in a different CWD during the above.
    """
    os.chdir(git_repository / "LICENSES")
    project = Project(git_repository)
    assert Path("build/hello.py").absolute() not in project.all_files()
Example #20
0
def test_generate_project_report_bad_license(fake_repository, multiprocessing):
    """Bad licenses are detected."""
    (fake_repository / "LICENSES/bad.txt").write_text("foo")

    project = Project(fake_repository)
    result = ProjectReport.generate(project, multiprocessing=multiprocessing)

    assert result.bad_licenses
    assert not result.missing_licenses
Example #21
0
def test_generate_project_report_unused_license(fake_repository,
                                                multiprocessing):
    """Unused licenses are detected."""
    (fake_repository / "LICENSES/MIT.txt").write_text("foo")

    project = Project(fake_repository)
    result = ProjectReport.generate(project, multiprocessing=multiprocessing)

    assert result.unused_licenses == {"MIT"}
Example #22
0
def test_licenses_mismatch_license(empty_directory):
    """Raise a RuntimeError if there is a mismatch between the filename and
    Valid-License-Identifier tag.
    """
    (empty_directory / "LICENSES").mkdir()
    (empty_directory / "LICENSES/MIT.txt"
     ).write_text("Valid-License-Identifier: GPL-3.0-or-later")
    with pytest.raises(RuntimeError):
        Project(empty_directory)
Example #23
0
def test_all_files_submodule_is_ignored(submodule_repository):
    """If a submodule is ignored, all_files should not raise an Exception."""
    (submodule_repository / "submodule/foo.py").write_text("foo")
    gitignore = submodule_repository / ".gitignore"
    contents = gitignore.read_text()
    contents += "\nsubmodule/\n"
    gitignore.write_text(contents)
    project = Project(submodule_repository)
    assert Path("submodule/foo.py").absolute() not in project.all_files()
Example #24
0
def test_lint_read_errors(fake_repository, stringio):
    """A read error is detected."""
    (fake_repository / "foo.py").symlink_to("does_not_exist")
    project = Project(fake_repository)
    report = ProjectReport.generate(project)
    result = lint_read_errors(report, out=stringio)

    assert "foo.py" in str(list(result)[0])
    assert "foo.py" in stringio.getvalue()
Example #25
0
def test_generate_project_report_read_error(fake_repository):
    """Files that cannot be read are added to the read error list."""
    (fake_repository / "bad").symlink_to("does_not_exist")

    project = Project(fake_repository)
    result = ProjectReport.generate(project)

    # pylint: disable=superfluous-parens
    assert (fake_repository / "bad") in result.read_errors
Example #26
0
def test_lint_files_without_licenses(fake_repository, stringio):
    """A file without license is detected."""
    (fake_repository / "foo.py").touch()
    project = Project(fake_repository)
    report = ProjectReport.generate(project)
    result = lint_files_without_licenses(report, out=stringio)

    assert "foo.py" in str(list(result)[0])
    assert "foo.py" in stringio.getvalue()
Example #27
0
def test_generate_project_report_missing_license(fake_repository):
    """Missing licenses are detected."""
    (fake_repository / "LICENSES/GPL-3.0-or-later.txt").unlink()

    project = Project(fake_repository)
    result = ProjectReport.generate(project)

    assert "GPL-3.0-or-later" in result.missing_licenses
    assert not result.bad_licenses
Example #28
0
def test_generate_project_report_simple(fake_repository):
    """Simple generate test, just to see if it sort of works."""
    project = Project(fake_repository)
    result = ProjectReport.generate(project)

    assert not result.missing_licenses
    assert not result.bad_licenses
    assert not result.read_errors
    assert result.file_reports
Example #29
0
def test_lint_unused_licenses(fake_repository, stringio):
    """An unused license is detected."""
    (fake_repository / "LICENSES/MIT.txt").touch()
    project = Project(fake_repository)
    report = ProjectReport.generate(project)
    result = lint_unused_licenses(report, out=stringio)

    assert "MIT" in result
    assert "MIT" in stringio.getvalue()
Example #30
0
def test_generate_file_report_file_bad_license(fake_repository):
    """Simple generate test with a bad license."""
    (fake_repository / "foo.py").write_text("SPDX"
                                            "-License-Identifier: fakelicense")
    project = Project(fake_repository)
    result = FileReport.generate(project, "foo.py")

    assert result.file_report.spdxfile.copyright == ""
    assert result.bad_licenses == {"fakelicense"}
    assert not result.missing_licenses