Repository: starship/starship
File: src/modules/gradle.rs (Rust)

Code before:
        .split_whitespace()
        .next()?;
    Some(version.to_string())
}

fn parse_gradle_version_from_properties(wrapper_properties: &str) -> Option<String> {
    // example gradle.properties content
    /*
        distributionBase=GRADLE_USER_HOME
        distributionPath=wrapper/dists
        distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
        zipStoreBase=GRADLE_USER_HOME
        zipStorePath=wrapper/dists
    */
    let version = wrapper_properties
        .split_once("distributionUrl=")?
        .1
        .split_once("gradle-")?
        .1
        .split_once('-')?
        .0;
    Some(version.to_string())
}

/// Tries to find the gradle-wrapper.properties file recursively.
fn find_wrapper_properties_file(directory: &Path) -> Option<PathBuf> {
    let properties_path = directory.join("gradle/wrapper/gradle-wrapper.properties");
    if properties_path.exists() {
        return Some(properties_path);
    }

    find_wrapper_properties_file(directory.parent()?)
}

#[cfg(test)]
mod tests {
    use nu_ansi_term::Color;

    use super::*;
    use crate::test::ModuleRenderer;
    use std::fs::File;
    use std::io::{self, Write};

    #[test]
    fn folder_without_gradle_files() -> io::Result<()> {
        let dir = tempfile::tempdir()?;

        let actual = ModuleRenderer::new("gradle").path(dir.path()).collect();

        let expected = None;
        assert_eq!(expected, actual);

Review comment (suggestion):
Please avoid searching files recursively, unless the module is configured to do this. For the base level, please try checking the scan results if a `gradle` folder exists.

Code after:
    if recursive && properties.is_none() {
        for dir in context.current_dir.ancestors().skip(1) {
            properties =
                utils::read_file(&dir.join("gradle/wrapper/gradle-wrapper.properties")).ok();
            if properties.is_some() {
                break;
            }
        }
    }
    properties
}

#[cfg(test)]
mod tests {
    use nu_ansi_term::Color;

    use super::*;
    use crate::test::ModuleRenderer;
    use std::fs::{self, File};
    use std::io::{self, Write};

    #[test]
    fn folder_without_gradle_files() -> io::Result<()> {
        let dir = tempfile::tempdir()?;

        let actual = ModuleRenderer::new("gradle").path(dir.path()).collect();

        let expected = None;
        assert_eq!(expected, actual);
        dir.close()
    }

    #[test]
    fn folder_with_gradle_wrapper_properties() -> io::Result<()> {
        let dir = tempfile::tempdir()?;
        let properties = dir
            .path()
            .join("gradle")
            .join("wrapper")
            .join("gradle-wrapper.properties");
        // create gradle/wrapper/ directories
        fs::create_dir_all(properties.parent().unwrap())?;
        // create build.gradle file to mark it as a gradle project
        File::create(dir.path().join("build.gradle"))?.sync_all()?;
        let mut file = File::create(properties)?;
        file.write_all(
            b"\
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME