Skip to content

Impl custom golden paths #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

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

aVikingTrex
Copy link

@aVikingTrex aVikingTrex commented Nov 30, 2024

This PR implements custom names for golden files. The crux of this change is the addition of a new macro case branch per assert for handling the new name.
The provided name is postfixed to the path produced by the current _function_path macro in place of the actual function name.

New tests have also been written for these new cases. This should resolve #2

Trent Graw added 3 commits November 30, 2024 14:36
Implement a new macro case for each assertion and the _new_goldie macros. This path extends the current _function_path
return value. What this achieves is custom named files in the same place golden tests would be previously written to.
@aVikingTrex
Copy link
Author

@rossmacarthur Could we get this looked at by any chance? I dont mind making changes if it is in-appropriate for what you want in the lib

@rossmacarthur
Copy link
Owner

rossmacarthur commented Feb 12, 2025

Hi @aVikingTrex, thanks for your contribution. I'm not convinced this is the best API for this.

If I understand correctly, your use-case is that you want a suffix to the name of the golden file. I can imagine other ways of customizing the paths and I want to make sure that we implement the suffix in a way that allows for other options without too much API churn. Examples:

  • different testdata directory
  • different function name
  • different function name + suffix

Additionally, I would prefer if we had as little macro code as possible. The macros are only required in order to get the file!() and function_name!() calls to work.

Idea:
I could imagine an api more like this:

goldie::new!().suffix("custom").assert_debug(...)

Let me know what you think, or if you have ideas?

@aVikingTrex
Copy link
Author

Appreciate the reply @rossmacarthur. When I wrote this I had the very narrow use case of interop between this and the test-case crate. Re-reading #2 and the OP's issue, I do see that configuring in a more customised way would be the way to go. In that issue is two use cases although I wonder if OP's issue is better served as the crate's default rather than an opt-in config.

I agree that a better long term solution would be expanding out the goldie struct itself. Personally I can see it being configured via builder, which looks to be what you have in your example. It would be trivially implemented via Bon but this comes at the cost of compile time macros, and it might be too heavy for such a simple use case. I am also happy to hand-write the builder for this.

From your examples and what I want out of the crate I could see:

  • Function name: Custom name
  • Test prefix: prefix-{function}.golden
  • Test suffix: {function}-suffix.golden
  • goldie_directory: Where to put the golden files, relative to crate root? Or should this be relative to the test?
    enum RelativeTo { Test, CrateRoot, Workspace  }
  • separate module sub dirs: Looking at Organising golden files #2 I think this should be default behavior
  • Anything else?

I can see it being used similar to how you have there with the difference being that new_goldie!() would return a GoldieBuilder rather than a Goldie struct:

goldie::new_goldie()
  .with_suffix("")
  .with_prefix("")
  .with_golden_dir("", RelativeTo::Test)
  .with_separate_module_dirs()
  .assert()

What do you think about this?

@aVikingTrex
Copy link
Author

@rossmacarthur just waiting on you to give your thoughts before I implement

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.

Organising golden files
2 participants