def test_incomplete_package(self):
        """If a Rez package is completely invalid, exit early."""
        directory = packaging.make_fake_source_package("some_package", "")
        self.delete_item_later(os.path.dirname(directory))

        with self.assertRaises(exceptions.NoPackageFound):
            cli.lint(directory)
    def test_unresolved_dependency(self):
        """Let a package go through ``rez_lint`` even if there's a problem with it."""
        directory = packaging.make_fake_source_package(
            "some_package",
            textwrap.dedent("""\
                name = "some_package"
                version = "1.0.0"
                requires = ["a_dependency_that_does_not_resolve"]
                """),
        )
        self.delete_item_later(os.path.dirname(directory))

        cli.lint(directory)
    def _test_found(self, name, code):
        """Run a test that assumes that there is "no-help" issue.

        Args:
            name (str): The name of the fake Rez source package to create.
            code (str): The source code used to create a package definition.

        Raises:
            AssertionError: If `code` actually does not return a "no-help" issue.

        """
        directory = packaging.make_fake_source_package(name, code)
        self.delete_item_later(os.path.dirname(directory))

        results = cli.lint(directory)

        issues = [
            description
            for description in results
            if description.get_summary()[0]
            == "The help attribute is undefined or empty"
        ]

        self.assertEqual(1, len(issues))

        first_non_summary_line = issues[0].get_message(verbose=True)[1].lstrip()

        self.assertTrue(
            first_non_summary_line
            == "Every Rez package should always point to some documentation."
        )
    def test_is(self):
        """When the version is not X.Y.Z (semantic), report an issue."""
        directory = packaging.make_fake_source_package(
            "some_package",
            textwrap.dedent(
                """\
                name = "some_package"
                version = "something.1.non_standard"
                """
            ),
        )
        self.delete_item_later(os.path.dirname(directory))

        results = cli.lint(directory)

        issues = [
            description
            for description in results
            if description.get_summary()[0]
            == "Package is not X.Y.Z semantic versioning."
        ]

        self.assertEqual(1, len(issues))

        expected = 'Package "{path}" has version "something.1.non_standard".'.format(
            path=os.path.join(directory, "package.py")
        )

        self.assertEqual(issues[0].get_message(verbose=True)[1].lstrip(), expected)
    def test_undefined(self):
        """If the version is undefined, don't report an issue."""
        directory = packaging.make_fake_source_package(
            "some_package",
            textwrap.dedent(
                """\
                name = "some_package"
                """
            ),
        )
        self.delete_item_later(os.path.dirname(directory))

        results = cli.lint(directory)

        issues = [
            description
            for description in results
            if description.get_summary()[0] == "Package has no version"
        ]

        self.assertEqual(1, len(issues))
        expected = 'Package {path}" has no version. Please define one.'.format(
            path=os.path.join(directory, "package.py")
        )

        self.assertEqual(issues[0].get_message(verbose=True)[1].lstrip(), expected)
    def test_no_changelog(self):
        """Report a missing CHANGELOG file if none exists."""
        directory = self._make_generic_installed_package()

        results = cli.lint(directory)
        issue = self._get_no_changelog_message(directory)

        self.assertTrue(issue in results)
    def test_no_readme(self):
        """Report a missing README file if none exists."""
        directory = self._make_generic_installed_package()

        results = cli.lint(directory)
        issue = self._get_no_readme_message(directory)

        self.assertTrue(issue in results)
    def test_has_changelog(self):
        """Report a missing CHANGELOG file if none exists."""
        directory = self._make_generic_installed_package()
        open(os.path.join(directory, "CHANGELOG.rst"), "a").close()

        results = cli.lint(directory)
        issue = self._get_no_changelog_message(directory)

        self.assertTrue(issue not in results)
    def test_has_readme(self):
        """Report a missing README file if none exists."""
        directory = self._make_generic_installed_package()
        open(os.path.join(directory, "README.md"), "a").close()

        results = cli.lint(directory)
        issue = self._get_no_readme_message(directory)

        self.assertTrue(issue not in results)
    def test_has_documentation(self):
        """Report a missing Sphinx documentation conf.py if none exists."""
        directory = self._make_generic_installed_package()
        documentation = os.path.join(directory, "documentation", "source")
        os.makedirs(documentation)
        open(os.path.join(documentation, "conf.py"), "a").close()

        results = cli.lint(directory)
        issue = self._get_no_documentation_message(directory)

        self.assertTrue(issue not in results)
    def test_no_documentation(self):
        """Report a missing Sphinx documentation conf.py file if none exists."""
        directory = self._make_generic_installed_package()

        with mock.patch(
            "rez_lint.plugins.checkers.explains._has_context_python_package"
        ) as patched:
            patched.return_value = True
            results = cli.lint(directory)

        issue = self._get_no_documentation_message(directory)

        self.assertTrue(issue in results)
    def test_schema_recursive(self):
        """Stop linting on a package if it's invalid."""
        files = [
            # Invalid Rez packages
            textwrap.dedent("""\
                name = "my_package"
                version = "1.0.0"
                build_command = "echo 'foo'"
                uuid = 8
                """),
            textwrap.dedent("""\
                name = "my_package"
                version = "1.0.0"
                build_command = "echo 'foo'"
                uuid = 8
                """),
            # A valid Rez package
            textwrap.dedent("""\
                name = "my_package"
                version = "1.0.0"
                build_command = "echo 'foo'"
                uuid = "54f0a93c-a9db-4026-b856-def2d0b6e956"
                """),
        ]

        directory = tempfile.mkdtemp(
            prefix="rez_lint_InvalidPackages_test_schema_recursive_")
        atexit.register(functools.partial(shutil.rmtree, directory))

        for index, code in enumerate(files):
            destination = os.path.join(directory,
                                       "folder_{index}".format(index=index))
            os.makedirs(destination)

            with open(os.path.join(destination, "package.py"), "w") as handler:
                handler.write(code)

        results = cli.lint(directory, recursive=True)

        issues = [
            description for description in results
            if description.get_code().long_name == "invalid-schema"
        ]

        self.assertEqual(2, len(issues))
    def test_schema(self):
        """Stop linting on a package if it's invalid."""
        code = textwrap.dedent("""\
            name = "my_package"
            version = "1.0.0"
            build_command = "echo 'foo'"
            uuid = 8
            """)
        directory = packaging.make_fake_source_package("my_package", code)

        results = cli.lint(directory)

        issues = [
            description for description in results
            if description.get_code().long_name == "invalid-schema"
        ]

        self.assertNotEqual([], issues)
    def test_not(self):
        """When the version is X.Y.Z (semantic), don't report an issue."""
        directory = packaging.make_fake_source_package(
            "some_package",
            textwrap.dedent(
                """\
                name = "some_package"
                version = "2.10.3"
                """
            ),
        )
        self.delete_item_later(os.path.dirname(directory))

        results = cli.lint(directory)

        issues = [
            description
            for description in results
            if description.get_summary()[0]
            == "Package is not X.Y.Z semantic versioning."
        ]

        self.assertEqual([], issues)
    def _test_not_found(self, name, code):
        """Run a test that assumes that there is no "no-help" issue.

        Args:
            name (str): The name of the fake Rez source package to create.
            code (str): The source code used to create a package definition.

        Raises:
            AssertionError: If `code` actually does return a "no-help" issue.

        """
        directory = packaging.make_fake_source_package(name, code)
        self.delete_item_later(os.path.dirname(directory))

        results = cli.lint(directory)

        issues = [
            description
            for description in results
            if description.get_summary()[0]
            == "The help attribute is undefined or empty"
        ]

        self.assertEqual([], issues)