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

feat: support configurable snapshot dir and filename #60

Merged
merged 3 commits into from
May 24, 2023

Conversation

gkampitakis
Copy link
Owner

@gkampitakis gkampitakis commented May 14, 2023

This pull request adds back support for allowing configurable snapshot filename and directory name. [ pr tha dropped config ]

Action Items:

  • Make sure we are backward compatible
  • Make sure this doesn't affect the Clean functionality
  • Update docs
  • Extend unit tests

closes #58

Example usage of current api

t.Run("should allow setting filename", func(t *testing.T) {
	snaps.WithConfig(snaps.Filename("my_custom_name")).MatchSnapshot(t, "Hello Word")
	snaps.WithConfig(snaps.Dir("my_dir")).MatchSnapshot(t, 10, 20, 30)

	snaps.WithConfig(snaps.Filename("my_custom_name")).MatchSnapshot(t, "Hello Word")
	snaps.WithConfig(snaps.Dir("my_dir")).MatchSnapshot(t, 10, 20, 30)

	snaps.WithConfig(snaps.Dir("my_dir"), snaps.Filename("json_file")).MatchJSON(t, `{"hello":"world"}`)
	snaps.WithConfig(snaps.Dir("json_dir"), snaps.Filename("blue_file")).MatchJSON(t, `{"hello":"world"}`)
})

@gkampitakis gkampitakis force-pushed the support_config branch 4 times, most recently from 77c84f1 to 282a669 Compare May 16, 2023 21:56
callerPath := baseCaller(2)
base := filepath.Base(callerPath)
dir := filepath.Join(filepath.Dir(callerPath), c.snapsDir)
Copy link
Contributor

Choose a reason for hiding this comment

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

For my use case I'd like to specify the directory in full, i.e. not to be influenced by the callerPath. There are two cases I encountered for this, one is when ussing go-snaps in a goroutine, the base is $GOROOT/src/runtime; and the second when I'm using a testing framework that runs a single go test -- in my case godog, the base is $GOPATH/pkg/mod/github.com/cucumber/godog@v0.12.6.

I'd prefer if the given snapsDir was used as is, perhaps if absolute path was given?

Suggested change
dir := filepath.Join(filepath.Dir(callerPath), c.snapsDir)
var dir string
if filepath.IsAbs(c.snapsDir) {
dir = c.snapsDir
} else {
dir = filepath.Join(filepath.Dir(callerPath), c.snapsDir)
}

Copy link
Owner Author

Choose a reason for hiding this comment

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

Hey 👋 Thanks a lot for the feedback and using go-snaps. The issue with the file at least for goroutines was fixed #44. I was able to verify this should not be an issue from v0.3.7.

As for providing the directory in full I thought about it but I was a bit hesitant on how it would work with the Clean. I like idea of the absolute and I ll play a bit with it as I can see people wanting to use it.

As for the godog unfortunately I am not really familiar with it. I would appreciate if you can share a snippet so I can potentially try and support it regardless of the snapsDir configuration.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hello, thanks for considering this!

I've created an example if I run go test example_test this is what I get:

go test example_test.go 
Feature: example feature

  Scenario: example scenario     # features/example.feature:3
    Then expect snapshot "hello" # example_test.go:40 -> command-line-arguments.verifySnapshot
    mkdir /home/zregvart/.asdf/installs/golang/1.19.5/packages/pkg/mod/github.com/cucumber/godog@v0.12.6/__snapshots__: permission denied

--- Failed steps:

  Scenario: example scenario # features/example.feature:3
    Then expect snapshot "hello" # features/example.feature:4
      Error: mkdir /home/zregvart/.asdf/installs/golang/1.19.5/packages/pkg/mod/github.com/cucumber/godog@v0.12.6/__snapshots__: permission denied


1 scenarios (1 failed)
1 steps (1 failed)
217.55µs
--- FAIL: TestFeatures (0.00s)
    --- FAIL: TestFeatures/example_scenario (0.00s)
        suite.go:451: mkdir /home/zregvart/.asdf/installs/golang/1.19.5/packages/pkg/mod/github.com/cucumber/godog@v0.12.6/__snapshots__: permission denied
    example_test.go:61: non-zero status returned, failed to run feature tests
FAIL
FAIL	command-line-arguments	0.007s
FAIL

It seems to me supporting godog, and in general other similar testing frameworks that abstract away the testing framework might be a challenge -- so thank you for taking an interest and investing time into this.

For my use case, I've configured to use the version from this branch and I've resorted to using tricks to point to the directory where I want the snapshots to be created.

snaps/snapshot.go Outdated Show resolved Hide resolved
@gkampitakis gkampitakis force-pushed the support_config branch 2 times, most recently from 0abcdce to a0686d6 Compare May 19, 2023 22:01
@gkampitakis gkampitakis force-pushed the support_config branch 2 times, most recently from 4b5e674 to e7824fc Compare May 22, 2023 18:21
@gkampitakis gkampitakis marked this pull request as ready for review May 22, 2023 19:10
@gkampitakis gkampitakis merged commit f8620a8 into main May 24, 2023
@gkampitakis gkampitakis deleted the support_config branch May 24, 2023 18:31
mergify bot referenced this pull request in dbsystel/cdk-sops-secrets May 24, 2023
)

[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [github.com/gkampitakis/go-snaps](https://togithub.com/gkampitakis/go-snaps) | require | patch | `v0.4.4` -> `v0.4.5` |

---

### Release Notes

<details>
<summary>gkampitakis/go-snaps</summary>

### [`v0.4.5`](https://togithub.com/gkampitakis/go-snaps/releases/tag/v0.4.5)

[Compare Source](https://togithub.com/gkampitakis/go-snaps/compare/v0.4.4...v0.4.5)

#### What's Changed

-   feat: support configurable snapshot dir and filename by [@&#8203;gkampitakis](https://togithub.com/gkampitakis) in [https://github.com/gkampitakis/go-snaps/pull/60](https://togithub.com/gkampitakis/go-snaps/pull/60)

**Full Changelog**: gkampitakis/go-snaps@v0.4.4...v0.4.5

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/markussiebert/cdk-sops-secrets).
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.

[Feature Request ]: Configurable snapshot file
2 participants