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

Add PathBuf Arbitrary impl #362

Closed
wants to merge 1 commit into from

Conversation

maackle
Copy link
Contributor

@maackle maackle commented Aug 21, 2023

A minimal implementation of Arbitrary for PathBuf, per #272

@sunshowers
Copy link
Contributor

sunshowers commented Aug 24, 2023

(author of camino here, including the Arbitrary impl for Utf8PathBuf)

I think a useful impl should have a certain number of components separated by MAIN_SEPARATOR, and should be able to generate relative and absolute paths with 50-50 probability. (And as a stretch goal it could also generate all of the other prefix components on Windows, UNC paths and such)

https://docs.rs/camino/1.1.6/src/camino/proptest_impls.rs.html#14-42

@matthew-russo
Copy link
Member

Sorry for the delay in getting to this -- works been busy this week. I'll try to give this a look tonight.

@maackle
Copy link
Contributor Author

maackle commented Aug 24, 2023

Awesome @sunshowers, that's the kind of the feedback I was hoping for. "The best way to get something done right is to do it wrong on the internet". :)

use std::path::*;
use std::{ffi::OsString, path::*};

use crate::{arbitrary::StrategyFor, prelude::Strategy, strategy::MapInto};

// TODO: Figure out PathBuf and then Box/Rc/Box<Path>.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should edit/remove this comment once we get to a spot we're happy with

Comment on lines +20 to +22
arbitrary!(PathBuf, MapInto<StrategyFor<OsString>, Self>;
OsString::arbitrary().prop_map_into()
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed with @sunshowers comment that we should be a bit more robust in the impl here. I agree that a good starting place is to ensure we get a spread of path segments and we get coverage of absolute and relative paths. I'd be fine with merging a strategy that hits those two marks. I think more robust edge case generation could be done separately.

let me know if you'd be willing to tackle those in a follow up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I won't have bandwidth to take this on probably for quite a while, but I hope this can be a very minimal bit of inspiration for others to build upon though.

Also I might point out that even though this is not an ideal implementation in terms of covering a wide range of possibilities, it does produce an arbitrary PathBuf, so maybe in the spirit of "something is better than nothing", this could be merged with the intention to improve upon it. Maybe even with a deprecation to make it clear that this is not a good implementation, but is currently the best we have? That would get me unstuck on my own project, where I really just need a completely arbitrary PathBuf, and have some types that contain PathBufs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could either of you copy over the impl in camino? Otherwise I can try getting a PR up with that impl when I have time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ended up doing this myself, see #368.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'm on vacation until next week so may not get a chance to look at it but will try to look sometime next week if I don't find any time before then

sunshowers added a commit to sunshowers/proptest that referenced this pull request Sep 1, 2023
Followup to proptest-rs#362. Includes documentation.

Thanks to Michael for starting work on this!

Co-authored-by: Michael Dougherty <maackle.d@gmail.com>
matthew-russo pushed a commit that referenced this pull request Sep 18, 2023
* Add PathBuf Arbitrary impl

Followup to #362. Includes documentation.

Thanks to Michael for starting work on this!

Co-authored-by: Michael Dougherty <maackle.d@gmail.com>

* Use PathParams

---------

Co-authored-by: Michael Dougherty <maackle.d@gmail.com>
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 this pull request may close these issues.

3 participants