commit:     a4d84c864d68c7bfb2ff24c7a8f00c0eb3877818
Author:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
AuthorDate: Wed Jun 21 06:28:48 2023 +0000
Commit:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
CommitDate: Wed Jun 21 18:38:40 2023 +0000
URL:        
https://gitweb.gentoo.org/proj/pkgcore/pkgcheck.git/commit/?id=a4d84c86

RustCheck: check for suboptimal - CRATES separator

Resolves: https://github.com/pkgcore/pkgcheck/issues/586
Signed-off-by: Arthur Zamarin <arthurzam <AT> gentoo.org>

 src/pkgcheck/checks/rust.py                        | 50 ++++++++++++++++++++++
 .../SuboptimalCratesSeparator/expected.json        |  1 +
 .../RustCheck/SuboptimalCratesSeparator/fix.patch  | 14 ++++++
 .../SuboptimalCratesSeparator-0.ebuild             | 12 ++++++
 testdata/repos/standalone/eclass/cargo.eclass      |  1 +
 5 files changed, 78 insertions(+)

diff --git a/src/pkgcheck/checks/rust.py b/src/pkgcheck/checks/rust.py
new file mode 100644
index 00000000..75197e56
--- /dev/null
+++ b/src/pkgcheck/checks/rust.py
@@ -0,0 +1,50 @@
+from .. import bash, results, sources
+from . import Check
+
+
+class SuboptimalCratesSeparator(results.LineResult, results.Warning):
+    """Using ``-`` as name-version separator in ``CRATES`` is suboptimal.
+
+    The ``CRATES`` variable is a space separated list of crates. The eclass
+    supports specifying the crate name and version as ``name@version`` and as
+    ``name-version``. The latter is suboptimal as it's slower.
+
+    It is recommended to use ``pycargoebuild`` 0.7+ to generate new ``CRATES``.
+    """
+
+    @property
+    def desc(self):
+        return f"line: {self.lineno}: using - as name-version separator in 
CRATES is suboptimal, use name@version instead"
+
+
+class RustCheck(Check):
+    """Checks for rust related issues."""
+
+    _source = sources.EbuildParseRepoSource
+    known_results = frozenset(
+        {
+            SuboptimalCratesSeparator,
+        }
+    )
+
+    def _verify_crates(self, pkg: bash.ParseTree):
+        for node in pkg.global_query(bash.var_assign_query):
+            name = pkg.node_str(node.child_by_field_name("name"))
+            if name == "CRATES":
+                val_node = node.children[-1]
+                row, _ = val_node.start_point
+                val_str = pkg.node_str(val_node).strip("'\"")
+                for lineno, line in enumerate(val_str.splitlines(), start=row 
+ 1):
+                    for token in line.split():
+                        if "@" not in token:
+                            yield SuboptimalCratesSeparator(
+                                lineno=lineno,
+                                line=token,
+                                pkg=pkg,
+                            )
+                            return
+
+    def feed(self, pkg: bash.ParseTree):
+        if "cargo" not in pkg.inherited:
+            return
+        yield from self._verify_crates(pkg)

diff --git 
a/testdata/data/repos/standalone/RustCheck/SuboptimalCratesSeparator/expected.json
 
b/testdata/data/repos/standalone/RustCheck/SuboptimalCratesSeparator/expected.json
new file mode 100644
index 00000000..8e579657
--- /dev/null
+++ 
b/testdata/data/repos/standalone/RustCheck/SuboptimalCratesSeparator/expected.json
@@ -0,0 +1 @@
+{"__class__": "SuboptimalCratesSeparator", "category": "RustCheck", "package": 
"SuboptimalCratesSeparator", "version": "0", "line": "snakeoil-0.10.0", 
"lineno": 2}

diff --git 
a/testdata/data/repos/standalone/RustCheck/SuboptimalCratesSeparator/fix.patch 
b/testdata/data/repos/standalone/RustCheck/SuboptimalCratesSeparator/fix.patch
new file mode 100644
index 00000000..e400178c
--- /dev/null
+++ 
b/testdata/data/repos/standalone/RustCheck/SuboptimalCratesSeparator/fix.patch
@@ -0,0 +1,14 @@
+diff -Naur 
standalone/RustCheck/SuboptimalCratesSeparator/SuboptimalCratesSeparator-0.ebuild
 fixed/RustCheck/SuboptimalCratesSeparator/SuboptimalCratesSeparator-0.ebuild
+--- 
standalone/RustCheck/SuboptimalCratesSeparator/SuboptimalCratesSeparator-0.ebuild
++++ 
fixed/RustCheck/SuboptimalCratesSeparator/SuboptimalCratesSeparator-0.ebuild
+@@ -1,7 +1,7 @@
+ CRATES="
+-      [email protected] snakeoil-0.10.0
+-      pkgcore-0.10.0
+-      pkgcheck-0.10.0
++      [email protected] [email protected]
++      [email protected]
++      [email protected]
+ "
+
+ inherit cargo

diff --git 
a/testdata/repos/standalone/RustCheck/SuboptimalCratesSeparator/SuboptimalCratesSeparator-0.ebuild
 
b/testdata/repos/standalone/RustCheck/SuboptimalCratesSeparator/SuboptimalCratesSeparator-0.ebuild
new file mode 100644
index 00000000..d1da3ecc
--- /dev/null
+++ 
b/testdata/repos/standalone/RustCheck/SuboptimalCratesSeparator/SuboptimalCratesSeparator-0.ebuild
@@ -0,0 +1,12 @@
+CRATES="
+       [email protected] snakeoil-0.10.0
+       pkgcore-0.10.0
+       pkgcheck-0.10.0
+"
+
+inherit cargo
+
+DESCRIPTION="Ebuild with suboptimal CRATES separator"
+HOMEPAGE="https://github.com/pkgcore/pkgcheck";
+SLOT="0"
+LICENSE="BSD"

diff --git a/testdata/repos/standalone/eclass/cargo.eclass 
b/testdata/repos/standalone/eclass/cargo.eclass
new file mode 100644
index 00000000..f83c98d3
--- /dev/null
+++ b/testdata/repos/standalone/eclass/cargo.eclass
@@ -0,0 +1 @@
+# cargo eclass

Reply via email to