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

Specify a target filepath for included files #9

Open
LukeMathWalker opened this issue Mar 11, 2022 · 1 comment · May be fixed by #11
Open

Specify a target filepath for included files #9

LukeMathWalker opened this issue Mar 11, 2022 · 1 comment · May be fixed by #11

Comments

@LukeMathWalker
Copy link

LukeMathWalker commented Mar 11, 2022

I've been playing around with assay today for a side project and it has been 🔥

The only thing I am struggling with, right now, is including files.
My project folder layout follows this structure:

src
  lib.rs
  module_with_tests.rs
  ...
  test_files
    test_name_1
      file_to_include_1
      ...

test_name_1 is a test that lives in module_with_tests.rs and I'd like to mount all files under test_files/test_name_1 in the temporary directory created for test_name_1.
This doesn't seem possible at the moment based on my understanding of the docs.
I can do:

    #[assay]
    #[assay(include = ["src/test_files/test_name_1/a_file_in_there.txt"])]
    fn test_name_1() {
        // [...]
    }

while what I want is

    #[assay]
    #[assay(include = [("src/test_files/test_name_1/a_file_in_there.txt", "a_file_in_there.txt"])]
    fn test_name_1() {
        // [...]
    }

Is there an easy layout that I could use to keep the files related to different tests separated? Would you be interested to support this functionality/review a PR for adding it?

Along the same lines, some kind of globbing would be quite handy - i.e. "mount all the files in this directory at the root of the temporary filesystem for the test".

@mgattozzi
Copy link
Owner

Hi @LukeMathWalker Yes I'd absolutely love this functionality as I've run into this issue myself. Currently it will copy over, as you noticed, the path relative to the root into the temporary directory. This means if you want things to be just a_file_in_there.txt it has to be in the project root which is far from ideal.

I'd accept a PR that would change include to do the following:

  • Make it so that it drops it into the root of the temp dir as the default as this is what I usually want as well and I don't think you're the only one
  • Add the option to give a specific path to place it at with:
    #[assay(include = [("src/test_files/test_name_1/a_file_in_there.txt", "relative/path/a_file_in_there.txt"])]

This would necessitate a 0.2 release but I don't mind doing that. If you have a PR I'm more than happy to review it and I appreciate you asking before just doing it.

As some pointers as to what will need to change and I don't expect it to be easy:

  • This parsing code to get the values for the paths here will need to change ->
    "include" => {
    let _: Token![=] = input.parse()?;
    let array: ExprArray = input.parse()?;
    include = Some(
    array
    .elems
    .into_iter()
    .filter_map(|e| match e {
    Expr::Lit(ExprLit {
    lit: Lit::Str(lit_str),
    ..
    }) => Some(lit_str.value()),
    _ => None,
    })
    .collect(),
  • This code will also need to change, probably a secondary parameter that's an Option for renaming ->

    assay/src/lib.rs

    Lines 52 to 87 in 7678772

    pub fn include(&self, path: impl AsRef<Path>) -> Result<(), Box<dyn Error>> {
    // Get our pathbuf to the file to include
    let mut inner_path = path.as_ref().to_owned();
    // If the path given is not absolute then it's relative to the dir we
    // ran the test from
    let is_relative = inner_path.is_relative();
    if is_relative {
    inner_path = self.ran_from.join(&path);
    }
    // Get our working directory
    let dir = self.directory.path().to_owned();
    // Make the relative path of the file in relation to our temp file
    // system based on if it was absolute or not
    let relative = if !is_relative {
    inner_path
    .components()
    .filter(|c| *c != Component::RootDir)
    .collect::<PathBuf>()
    } else {
    path.as_ref().into()
    };
    // If the relative path to the file includes parent directories create
    // them
    if let Some(parent) = relative.parent() {
    create_dir_all(dir.join(parent))?;
    }
    // Copy the file over from the file system into the temp file system
    copy(inner_path, dir.join(relative))?;
    Ok(())
    }

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

Successfully merging a pull request may close this issue.

2 participants