Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Better Integration for Testing #46

Open

Description

@cjgratacos

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.

Activity

  1. soc commented on Dec 7, 2022

    @soc
    Collaborator

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

    I'd be all for it.

  2. cjgratacos commented on Dec 12, 2022

    @cjgratacos
    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()
        }
    }
  3. tgross35 commented on Jan 6, 2024

    @tgross35

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions