dirs-rs icon indicating copy to clipboard operation
dirs-rs copied to clipboard

Better Integration for Testing

Open cjgratacos opened this issue 2 years ago • 3 comments

Love the project but kinda of a pain to write tests around code that is leveraging this library. Have the team considered making a trait of the Public API's so they can be mocked or replaced with an in memory file system for testing code that uses this library.

cjgratacos avatar Dec 07 '22 15:12 cjgratacos

Is there a good approach that does not impose test-related complexity on regular users?

I'd be all for it.

soc avatar Dec 07 '22 17:12 soc

Good questions, I believe the best approach might be to have two versions of the API.

  • First one is the current system that you have, top level functions in the library. This guarantee backwards compatibility with the current users.
  • The second is having a trait that are the same functions as the the top level functions in the library. Then have an struct container where the struct just implements the trait and delegates the functionality to the top level functions in the library. So it will look something like this:
pub trait DirsLocator {
    fn home_dir() -> Option<PathBuf>;
    fn cache_dir() -> Option<PathBuf>;
    fn config_dir() -> Option<PathBuf>;
    fn data_dir() -> Option<PathBuf>;
    fn data_local_dir() -> Option<PathBuf>;
    fn executable_dir() -> Option<PathBuf>;
    fn preference_dir() -> Option<PathBuf>;
    fn runtime_dir() -> Option<PathBuf>;
    fn state_dir() -> Option<PathBuf>;
    fn audio_dir() -> Option<PathBuf>;
    fn desktop_dir() -> Option<PathBuf>;
    fn document_dir() -> Option<PathBuf>;
    fn download_dir() -> Option<PathBuf>;
    fn font_dir() -> Option<PathBuf>;
    fn picture_dir() -> Option<PathBuf>;
    fn public_dir() -> Option<PathBuf>;
    fn template_dir() -> Option<PathBuf>;
    fn video_dir() -> Option<PathBuf>;
}

struct Dirs;

impl DirsLocator for Dirs {
    fn home_dir() -> Option<PathBuf> {
        sys::home_dir()
    }
    fn cache_dir() -> Option<PathBuf> {
        sys::cache_dir()
    }
    fn config_dir() -> Option<PathBuf> {
        sys::config_dir()
    }
    fn data_dir() -> Option<PathBuf> {
        sys::data_dir()
    }
    fn data_local_dir() -> Option<PathBuf> {
        sys::data_local_dir()
    }
    fn executable_dir() -> Option<PathBuf> {
        sys::executable_dir()
    }
    fn preference_dir() -> Option<PathBuf> {
        sys::preference_dir()
    }
    fn runtime_dir() -> Option<PathBuf> {
        sys::runtime_dir()
    }
    fn state_dir() -> Option<PathBuf> {
        sys::state_dir()
    }
    fn audio_dir() -> Option<PathBuf> {
        sys::audio_dir()
    }
    fn desktop_dir() -> Option<PathBuf> {
        sys::desktop_dir()
    }
    fn document_dir() -> Option<PathBuf> {
        sys::document_dir()
    }
    fn download_dir() -> Option<PathBuf> {
        sys::download_dir()
    }
    fn font_dir() -> Option<PathBuf> {
        sys::font_dir()
    }
    fn picture_dir() -> Option<PathBuf> {
        sys::picture_dir()
    }
    fn public_dir() -> Option<PathBuf> {
        sys::public_dir()
    }
    fn template_dir() -> Option<PathBuf> {
        sys::template_dir()
    }
    fn video_dir() -> Option<PathBuf> {
        sys::video_dir()
    }
}

cjgratacos avatar Dec 12 '22 20:12 cjgratacos

Maybe this could just check for an env before running RUST_DIRS_TESTING_USE_PREFIX=/tmp/..., and use it if set.

tgross35 avatar Jan 06 '24 08:01 tgross35