Skip to content

Commit

Permalink
fix: expose internal config type (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkampitakis authored Aug 3, 2024
1 parent 2fadb85 commit 09cd270
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions snaps/matchJSON.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ validators or placeholders for data that might change on each invocation e.g. da
MatchJSON(t, User{created: time.Now(), email: "mock-email"}, match.Any("created"))
*/
func (c *config) MatchJSON(t testingT, input any, matchers ...match.JSONMatcher) {
func (c *Config) MatchJSON(t testingT, input any, matchers ...match.JSONMatcher) {
t.Helper()

matchJSON(c, t, input, matchers...)
Expand All @@ -60,7 +60,7 @@ func MatchJSON(t testingT, input any, matchers ...match.JSONMatcher) {
matchJSON(&defaultConfig, t, input, matchers...)
}

func matchJSON(c *config, t testingT, input any, matchers ...match.JSONMatcher) {
func matchJSON(c *Config, t testingT, input any, matchers ...match.JSONMatcher) {
t.Helper()

snapPath, snapPathRel := snapshotPath(c, t.Name(), false)
Expand Down
4 changes: 2 additions & 2 deletions snaps/matchSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ or call MatchSnapshot multiples times inside a test
The difference is the latter will create multiple entries.
*/
func (c *config) MatchSnapshot(t testingT, values ...any) {
func (c *Config) MatchSnapshot(t testingT, values ...any) {
t.Helper()

matchSnapshot(c, t, values...)
Expand All @@ -46,7 +46,7 @@ func MatchSnapshot(t testingT, values ...any) {
matchSnapshot(&defaultConfig, t, values...)
}

func matchSnapshot(c *config, t testingT, values ...any) {
func matchSnapshot(c *Config, t testingT, values ...any) {
t.Helper()

if len(values) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions snaps/matchStandaloneSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ MatchStandaloneSnapshot creates one snapshot file per call.
You can call MatchStandaloneSnapshot multiple times inside a test.
It will create multiple snapshot files at `__snapshots__` folder by default.
*/
func (c *config) MatchStandaloneSnapshot(t testingT, value any) {
func (c *Config) MatchStandaloneSnapshot(t testingT, value any) {
t.Helper()

matchStandaloneSnapshot(c, t, value)
Expand All @@ -38,7 +38,7 @@ func MatchStandaloneSnapshot(t testingT, value any) {
matchStandaloneSnapshot(&defaultConfig, t, value)
}

func matchStandaloneSnapshot(c *config, t testingT, value any) {
func matchStandaloneSnapshot(c *Config, t testingT, value any) {
t.Helper()

genericPathSnap, genericSnapPathRel := snapshotPath(c, t.Name(), true)
Expand Down
24 changes: 12 additions & 12 deletions snaps/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
updatedMsg = colors.Sprint(colors.Green, updateSymbol+"Snapshot updated")
)

type config struct {
type Config struct {
filename string
snapsDir string
extension string
Expand All @@ -35,8 +35,8 @@ type config struct {
// Update determines whether to update snapshots or not
//
// It respects if running on CI.
func Update(u bool) func(*config) {
return func(c *config) {
func Update(u bool) func(*Config) {
return func(c *Config) {
c.update = &u
}
}
Expand All @@ -46,8 +46,8 @@ func Update(u bool) func(*config) {
// default: __snapshots__
//
// this doesn't change the file extension see `snap.Ext`
func Filename(name string) func(*config) {
return func(c *config) {
func Filename(name string) func(*Config) {
return func(c *Config) {
c.filename = name
}
}
Expand All @@ -57,8 +57,8 @@ func Filename(name string) func(*config) {
// default: __snapshots__
//
// Accepts absolute paths
func Dir(dir string) func(*config) {
return func(c *config) {
func Dir(dir string) func(*Config) {
return func(c *Config) {
c.snapsDir = dir
}
}
Expand All @@ -69,16 +69,16 @@ func Dir(dir string) func(*config) {
//
// Note: even if you specify a different extension the file still contain .snap
// e.g. if you specify .txt the file will be .snap.txt
func Ext(ext string) func(*config) {
return func(c *config) {
func Ext(ext string) func(*Config) {
return func(c *Config) {
c.extension = ext
}
}

// Create snaps with configuration
//
// e.g snaps.WithConfig(snaps.Filename("my_test")).MatchSnapshot(t, "hello world")
func WithConfig(args ...func(*config)) *config {
func WithConfig(args ...func(*Config)) *Config {
s := defaultConfig

for _, arg := range args {
Expand Down Expand Up @@ -313,7 +313,7 @@ func getPrevStandaloneSnapshot(snapPath string) (string, error) {
// - if it's standalone snapshot we also append an integer (_%d) in the filename (even before `.snap`)
//
// Returns the relative path of the caller and the snapshot path.
func snapshotPath(c *config, tName string, isStandalone bool) (string, string) {
func snapshotPath(c *Config, tName string, isStandalone bool) (string, string) {
// skips current func, the wrapper match* and the exported Match* func
callerFilename := baseCaller(3)

Expand All @@ -328,7 +328,7 @@ func snapshotPath(c *config, tName string, isStandalone bool) (string, string) {
return snapPath, snapPathRel
}

func constructFilename(c *config, callerFilename, tName string, isStandalone bool) string {
func constructFilename(c *Config, callerFilename, tName string, isStandalone bool) string {
filename := c.filename
if filename == "" {
base := filepath.Base(callerFilename)
Expand Down
12 changes: 6 additions & 6 deletions snaps/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestAddNewSnapshot(t *testing.T) {
}

func TestSnapshotPath(t *testing.T) {
snapshotPathWrapper := func(c *config, tName string, isStandalone bool) (snapPath, snapPathRel string) {
snapshotPathWrapper := func(c *Config, tName string, isStandalone bool) (snapPath, snapPathRel string) {
// This is for emulating being called from a func so we can find the correct file
// of the caller
func() {
Expand All @@ -224,7 +224,7 @@ func TestSnapshotPath(t *testing.T) {
})

t.Run("should return path and file from config", func(t *testing.T) {
snapPath, snapPathRel := snapshotPathWrapper(&config{
snapPath, snapPathRel := snapshotPathWrapper(&Config{
filename: "my_file",
snapsDir: "my_snapshot_dir",
}, "", false)
Expand All @@ -235,7 +235,7 @@ func TestSnapshotPath(t *testing.T) {
})

t.Run("should return absolute path", func(t *testing.T) {
snapPath, snapPathRel := snapshotPathWrapper(&config{
snapPath, snapPathRel := snapshotPathWrapper(&Config{
filename: "my_file",
snapsDir: "/path_to/my_snapshot_dir",
}, "", false)
Expand All @@ -250,7 +250,7 @@ func TestSnapshotPath(t *testing.T) {
})

t.Run("should add extension to filename", func(t *testing.T) {
snapPath, snapPathRel := snapshotPathWrapper(&config{
snapPath, snapPathRel := snapshotPathWrapper(&Config{
filename: "my_file",
snapsDir: "my_snapshot_dir",
extension: ".txt",
Expand Down Expand Up @@ -291,7 +291,7 @@ func TestSnapshotPath(t *testing.T) {
})

t.Run("should return standalone snapPath with overridden filename", func(t *testing.T) {
snapPath, snapPathRel := snapshotPathWrapper(&config{
snapPath, snapPathRel := snapshotPathWrapper(&Config{
filename: "my_file",
snapsDir: "my_snapshot_dir",
}, "my_test", true)
Expand All @@ -303,7 +303,7 @@ func TestSnapshotPath(t *testing.T) {
t.Run(
"should return standalone snapPath with overridden filename and extension",
func(t *testing.T) {
snapPath, snapPathRel := snapshotPathWrapper(&config{
snapPath, snapPathRel := snapshotPathWrapper(&Config{
filename: "my_file",
snapsDir: "my_snapshot_dir",
extension: ".txt",
Expand Down
2 changes: 1 addition & 1 deletion snaps/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
isCI = ciinfo.IsCI
updateVAR = os.Getenv("UPDATE_SNAPS")
shouldClean = updateVAR == "true" || updateVAR == "clean"
defaultConfig = config{
defaultConfig = Config{
snapsDir: "__snapshots__",
}
)
Expand Down

0 comments on commit 09cd270

Please sign in to comment.