Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Integration for Testing #46

Open
cjgratacos opened this issue Dec 7, 2022 · 3 comments
Open

Better Integration for Testing #46

cjgratacos opened this issue Dec 7, 2022 · 3 comments

Comments

@cjgratacos
Copy link

cjgratacos commented Dec 7, 2022

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.

@soc
Copy link
Collaborator

soc commented Dec 7, 2022

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

I'd be all for it.

@cjgratacos
Copy link
Author

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()
    }
}

@tgross35
Copy link

tgross35 commented Jan 6, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants