-
-
Notifications
You must be signed in to change notification settings - Fork 3
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 an OS agnostic path assertion #5
base: main
Are you sure you want to change the base?
Conversation
Haven't checked the code and why Style ci complains as I'm on vacation. But how about The OS agnostic naming is super specific but makes the method relatively long and not so easy to read. |
In train right now so a few minutes for review. 🙈
|
CRAP. This is a big oversight on my part - as this is totally possible. Just not something I needed to test for.
The general idea behind not normalizing the For instance, imagine a library only created and tested on Linux that uses paths a lot. If the code producing a path with Linux style paths caused a bug when actually run on Windows, then the app should change to fit that. However if we normalized the
This is a really good train of thought IMO. I do have concerns about if it may confuse folks having these "os aware" assertions mixed with "vanilla" assertions. So that made me wonder if it could be worth making a new trait just for these more "magic" assertions - since they do more than a basic one. However even that seems overkill - I'll keep playing around with the idea and maybe even change to a Docs Driven Development mindset to find what would look best from a Docs/end-user point of view. |
Yeah, I had the same idea with a dedicated class. This one or utilizing the equals and same naming for OS agnostic and exact matching. Okay, one case for converting the actual value but I wouldn't add actual normalization because of it but make the normalizing method public. I'm also pretty sure that this OS agnostic testing is only possible for relative paths. As Unix always starts with Memo to myself: assertions for "is relative" and "is absolute" paths. |
I've been one of those rare devs who tests their packages on windows too (well a good deal of packages here and there). So I was just reviewing some of my varying packages that are tested on windows and looking over changes I made to fix tests on windows before creating helper methods. Almost all of them do have one thing in common that's particularly relevant to your point about relative vs absolute paths. While they actual assertions they use are comparing absolute paths, they are all creating the
So based on that workflow alone, a more optimal assertion signature might be:
Ultimately while this enables full path assertions, you hit the nail on the head that only the relative path can be normalized to the OS platform it's running on. One neat aspect of this tactic would be that we could even add "training wheels" onto this assertion; for instance it could safety check the This could allow me to change a test from: self::assertEquals($expectingBaseDir . DIRECTORY_SEPARATOR . "json.txt", $jsonFile->getRealPath()); To: OsAwarePathAssertions::assertEquals($expectingBaseDir, "/json.txt", $jsonFile->getRealPath()); |
Okay, so after thinking about all these I have this API in mind: PathAssertions::assertEquals(string $base, string $expected, mixed $actual); // $base.$expected == $base.$actual
PathAssertions::assertEndsWith(string $expected, mixed $actual); // str_ends_with($actual, $expected) For sure with normalization and for me also important for great DX - automatically trimming the But one thing/idea: everything around paths isn't that well/natively supported in PHP, even worse when it gets to OS agnostic. So how about an enhanced node.js |
One thing to clarify my example - the |
🤝 I'm 100% into this idea. Once a stable version of that package exists then -in theory- providing test assertion helpers like I had in mind here should be easier too. Heck at that point it'd be super easy to just pull that library in and make all the path assertions work without care for OS like you suggested earlier. |
@Gummibeer - How would you want to start on the port project? Would you like to setup a repo for us to work in? Or should I maybe give a first pass at getting something built and we can move it under the Astrotomic name later? |
Use Case
Test the expectations of methods that return paths constructed based on PHP constants that will vary by OS platforms. For instance, any PHP constant related to paths will vary from Unix(mac/linux/etc) and Windows due to use of differing directory seperators; even if they are the same equivalent folder.
Packages that seek to have their test suites pass on both Unix and Windows platforms with the same tests will quickly run into test that may pass on linux but fail on windows.
Example
Per usual, you would provide the expected value first and the actual value second.
This is a quite literal example where I'm providing literal windows paths for the actual. However consider an application with functions that return paths to resources managed by the app. (laravels resource_path and similar) My Kickflip project would also be a similar project that could use this like:
Current:
In this case the
getRelativePath
method will derive the relative path based on the full file path. It will just return the relative part based on the "source root folder". So the expected path is predictable however will vary based on OS directory separator - hence the perfect use-case for an assertion like this!LMK if you have any questions!