Files
greptimedb/scripts/check-snafu-test.py
T
dennis zhuang 0f6a9b3d6e chore: catch unused snafu variants shadowed by other crates and remove them (#10826)
* chore: catch unused snafu variants shadowed by other crates and remove them

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix: count shorthand variant constructions in error files as uses

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix: parse nested expressions and literals when telling variant constructions from patterns

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
2026-12-02 12:18:45 +00:00

88 lines
3.1 KiB
Python

# Copyright 2023 Greptime Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import importlib.util
import textwrap
import unittest
from pathlib import Path
SCRIPT_PATH = Path(__file__).resolve().parent / "check-snafu.py"
spec = importlib.util.spec_from_file_location("check_snafu", SCRIPT_PATH)
checker = importlib.util.module_from_spec(spec)
spec.loader.exec_module(checker)
ERROR_FILE = "/repo/src/a/src/error.rs"
CRATE = ("/repo/src/a", "a")
ERROR_ENUM = """
pub enum Error {
#[snafu(display("Broken ({path})"))]
Broken { path: String, source: std::io::Error },
}
impl ErrorExt for Error {
fn status_code(&self) -> StatusCode {
match self {
Error::Broken { .. } => StatusCode::Internal,
}
}
}
"""
def is_used(error_file_extra="", **other_files):
files = {ERROR_FILE: textwrap.dedent(ERROR_ENUM + error_file_extra)}
files.update(
{path: textwrap.dedent(content) for path, content in other_files.items()}
)
return checker.is_used(ERROR_FILE, "Error", "Broken", CRATE, files)
class CheckSnafuTest(unittest.TestCase):
def test_finds_variant_whose_display_has_parentheses(self):
self.assertEqual(
list(checker.extract_variants(textwrap.dedent(ERROR_ENUM))),
[("Error", "Broken")],
)
def test_match_arm_is_not_a_use(self):
self.assertFalse(is_used())
def test_helper_in_error_file_builds_variant(self):
for construction in [
"Error::Broken { path, source }",
'Error::Broken { path: format!("failed: {reason}"), source }',
'Error::Broken { path: "../data".to_string(), source }',
"Error::Broken { path: Path { parts: vec![] }.to_string(), source }",
"read().map_err(|source| Error::Broken { path, source })",
]:
with self.subTest(construction):
self.assertTrue(is_used(f"fn broken() -> Error {{ {construction} }}\n"))
def test_or_pattern_is_not_a_use(self):
arms = "fn f(e: &Error) { match e { Error::Broken { path, source } | Error::Other => {} } }\n"
self.assertFalse(is_used(arms))
def test_same_named_selector_of_another_crate_is_not_a_use(self):
other = "use crate::error::BrokenSnafu;\nuse a::error::Result;\n"
self.assertFalse(is_used(**{"/repo/src/b/src/lib.rs": other}))
def test_selector_through_module_alias_is_a_use(self):
other = "use a::error as a_error;\nfn f() { g().context(a_error::BrokenSnafu { path }) }\n"
self.assertTrue(is_used(**{"/repo/src/b/src/lib.rs": other}))
if __name__ == "__main__":
unittest.main()