aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArthur Zamarin <arthurzam@gentoo.org>2023-06-21 09:28:48 +0300
committerArthur Zamarin <arthurzam@gentoo.org>2023-06-21 21:38:40 +0300
commita4d84c864d68c7bfb2ff24c7a8f00c0eb3877818 (patch)
tree823f8180d15c94b8ca72d65e168764539c3f77ea
parentdocs/config: explain when repo config is used (diff)
downloadpkgcheck-a4d84c864d68c7bfb2ff24c7a8f00c0eb3877818.tar.gz
pkgcheck-a4d84c864d68c7bfb2ff24c7a8f00c0eb3877818.tar.bz2
pkgcheck-a4d84c864d68c7bfb2ff24c7a8f00c0eb3877818.zip
RustCheck: check for suboptimal - CRATES separator
Resolves: https://github.com/pkgcore/pkgcheck/issues/586 Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
-rw-r--r--src/pkgcheck/checks/rust.py50
-rw-r--r--testdata/data/repos/standalone/RustCheck/SuboptimalCratesSeparator/expected.json1
-rw-r--r--testdata/data/repos/standalone/RustCheck/SuboptimalCratesSeparator/fix.patch14
-rw-r--r--testdata/repos/standalone/RustCheck/SuboptimalCratesSeparator/SuboptimalCratesSeparator-0.ebuild12
-rw-r--r--testdata/repos/standalone/eclass/cargo.eclass1
5 files changed, 78 insertions, 0 deletions
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="
+- random@0.10.0 snakeoil-0.10.0
+- pkgcore-0.10.0
+- pkgcheck-0.10.0
++ random@0.10.0 snakeoil@0.10.0
++ pkgcore@0.10.0
++ pkgcheck@0.10.0
+ "
+
+ 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="
+ random@0.10.0 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