Repository: starship/starship
File: src/context.rs (Rust)

Code before:
                let enumerated_dir = fs::read_dir(base).unwrap().enumerate();
                for entry in enumerated_dir
                    .take_while(|(n, _)| {
                        cfg!(test) // ignore timeout during tests
                        || n & 0xFF != 0 // only check timeout once every 2^8 entries
                        || start.elapsed() < *timeout
                    })
                    .filter_map(|(_, entry)| entry.ok())
                {
                    match tx.send(entry) {
                        Ok(_) => continue,
                        Err(e) => {
                            log::warn!("Error occurred while sending across mpsc: {e}");
                            break;
                        }
                    }
                }
            };

            let _ = thread::Builder::new()
                .name("from_path_with_timeout worker".into())
                .spawn(worker)?;
        }

        loop {
            match rx.recv_timeout(remaining_time) {
                Ok(entry) => {
                    let path = PathBuf::from(entry.path().strip_prefix(base_path).unwrap());

                    let is_dir = match follow_symlinks {
                        true => entry.path().is_dir(),
                        false => fs::symlink_metadata(entry.path())
                            .map(|m| m.is_dir())
                            .unwrap_or(false),
                    };

                    if is_dir {
                        folders.insert(path);
                    } else {
                        if !path.to_string_lossy().starts_with('.') {
                            // Extract the file extensions (yes, that's plural) from a filename.
                            // Why plural? Consider the case of foo.tar.gz. It's a compressed
                            // tarball (tar.gz), and it's a gzipped file (gz). We should be able
                            // to match both.

                            // find the minimal extension on a file. ie, the gz in foo.tar.gz
                            // NB the .to_string_lossy().to_string() here looks weird but is
                            // required to convert it from a Cow.
                            path.extension()
                                .map(|ext| extensions.insert(ext.to_string_lossy().to_string()));


Review comment (suggestion):
Timeouts should not happen unless specifically tested. Alternatively perhaps add a function that redirects that redirects to either the timeout/non-timeout version based on `cfg(test)` to allow for timeout-specific tests.
```suggestion
            // TODO: use `recv_deadline` instead once stable
            let msg = if cfg(test) { rx.recv() } else { rx.recv_timeout(remaining_time) };
            match msg {
```

Code after:
                let _ = enumerated_dir
                    .filter_map(|(_, entry)| entry.ok())
                    .try_for_each(|entry| tx.send(entry));
            };

            let _ = thread::Builder::new()
                .name("from_path_with_timeout worker".into())
                .spawn(worker)?;
        }

        loop {
            // TODO: use `recv_deadline` instead once stable
            let msg = if cfg!(test) {
                // recv() errors out only when the corresponding sender closes.
                // See mpsc::RecvError.
                rx.recv().map_err(|_| mpsc::RecvTimeoutError::Disconnected)
            } else {
                rx.recv_timeout(remaining_time)
            };
            match msg {
                Ok(entry) => {
                    let path = PathBuf::from(entry.path().strip_prefix(base_path).unwrap());

                    let is_dir = match follow_symlinks {
                        true => entry.path().is_dir(),
                        false => fs::symlink_metadata(entry.path())
                            .map(|m| m.is_dir())
                            .unwrap_or(false),
                    };

                    if is_dir {
                        folders.insert(path);
                    } else {
                        if !path.to_string_lossy().starts_with('.') {
                            // Extract the file extensions (yes, that's plural) from a filename.
                            // Why plural? Consider the case of foo.tar.gz. It's a compressed
                            // tarball (tar.gz), and it's a gzipped file (gz). We should be able
                            // to match both.

                            // find the minimal extension on a file. ie, the gz in foo.tar.gz
                            // NB the .to_string_lossy().to_string() here looks weird but is
                            // required to convert it from a Cow.
                            path.extension()
                                .map(|ext| extensions.insert(ext.to_string_lossy().to_string()));

                            // find the full extension on a file. ie, the tar.gz in foo.tar.gz
                            path.file_name().map(|file_name| {
                                file_name
                                    .to_string_lossy()
                                    .split_once('.')
                                    .map(|(_, after)| extensions.insert(after.to_string()))