uv icon indicating copy to clipboard operation
uv copied to clipboard

Eliminate dependencies on `directores` and `dirs-sys`

Open j178 opened this issue 1 year ago • 2 comments

Summary

Migrate all directory related logic to etcetera, eliminated two dependecies.

j178 avatar Oct 09 '24 15:10 j178

I added a validation test in https://github.com/astral-sh/uv/pull/8048/commits/df5066d677cce04afdb656c5f6407b34948d5571, to compare the outputs of the old and new code, and it passed successfully. This confirms that this PR keeps the directories unchanged.

#[cfg(test)]
mod tests {
    use etcetera::BaseStrategy;
    use std::path::PathBuf;

    fn dirs_before() -> (PathBuf, PathBuf, PathBuf, PathBuf) {
        let data_dir = directories::ProjectDirs::from("", "", "uv")
            .unwrap()
            .data_dir()
            .to_path_buf();

        let cache_dir = directories::ProjectDirs::from("", "", "uv")
            .unwrap()
            .cache_dir()
            .to_path_buf();

        let config_dir = {
            // On Windows, use, e.g., C:\Users\Alice\AppData\Roaming
            #[cfg(windows)]
            {
                dirs_sys::known_folder_roaming_app_data()
            }
            // On Linux and macOS, use, e.g., /home/alice/.config.
            #[cfg(not(windows))]
            {
                std::env::var_os("XDG_CONFIG_HOME")
                    .and_then(dirs_sys::is_absolute_path)
                    .or_else(|| dirs_sys::home_dir().map(|path| path.join(".config")))
            }
        };

        let home_dir = {
            #[cfg(windows)]
            {
                dirs_sys::known_folder_profile()
            }
            #[cfg(not(windows))]
            {
                dirs_sys::home_dir()
            }
        };

        (data_dir, cache_dir, config_dir.unwrap(), home_dir.unwrap())
    }

    fn dirs_after() -> (PathBuf, PathBuf, PathBuf, PathBuf) {
        let data_dir = etcetera::base_strategy::choose_native_strategy()
            .unwrap()
            .data_dir()
            .join("uv");
        let data_dir = if cfg!(windows) {
            data_dir.join("data")
        } else {
            data_dir
        };

        let cache_dir = etcetera::base_strategy::choose_native_strategy()
            .unwrap()
            .cache_dir()
            .join("uv");
        let cache_dir = if cfg!(windows) {
            cache_dir.join("cache")
        } else {
            cache_dir
        };

        let config_dir = etcetera::base_strategy::choose_base_strategy()
            .unwrap()
            .config_dir();
        let home_dir = etcetera::home_dir().unwrap();

        (data_dir, cache_dir, config_dir, home_dir)
    }

    #[test]
    fn ensure_directory_not_changed() {
        let before = dirs_before();
        let after = dirs_after();
        assert_eq!(before, after);
    }
}

j178 avatar Oct 09 '24 17:10 j178

Thanks! I can review, I'm responsible for some of the mess.

charliermarsh avatar Oct 09 '24 17:10 charliermarsh

Gonna merge into v0.5.0.

charliermarsh avatar Oct 21 '24 18:10 charliermarsh