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: implement matchYAML #114

Merged
merged 5 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: implement matchYAML
  • Loading branch information
gkampitakis committed Dec 18, 2024
commit b9152ce6298bfd8226ba199a37e3c6a4a3d83f8e
20 changes: 20 additions & 0 deletions examples/__snapshots__/matchYAML_test.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

[TestMatchYaml/should_match_string_yaml - 1]
ser: "mock-service"
name: John Doe
age: 30
email: john.doe@example.com
address: mock-address
street: mock-address

---

[TestMatchYaml/should_match_struct_yaml - 1]
ser: mock-service
name: John Doe
age: 30
email: john.doe@example.com
address: mock-address
street: mock-address

---
46 changes: 46 additions & 0 deletions examples/matchYAML_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package examples

import (
"os"
"testing"

"github.com/gkampitakis/go-snaps/match"
"github.com/gkampitakis/go-snaps/snaps"
)

func TestMatchYaml(t *testing.T) {
t.Run("should match string yaml", func(t *testing.T) {
const doc_yaml = `
ser: "mock-service"
name: John Doe
age: 30
email: john.doe@example.com
address: "123 Main St"
street: 123 Main St`

snaps.WithConfig(snaps.Update(os.Getenv("UPDATE_ONLY") == "true")).
// .address panics
MatchYAML(t, doc_yaml, match.Any("$.address", "$.street").Placeholder("mock-address"))
})

t.Run("should match struct yaml", func(t *testing.T) {
type User struct {
Service string `yaml:"ser"`
Name string `yaml:"name"`
Age int `yaml:"age"`
Email string `yaml:"email"`
Address string `yaml:"address"`
Street string `yaml:"street"`
}

snaps.WithConfig(snaps.Update(os.Getenv("UPDATE_ONLY") == "true")).
MatchYAML(t, User{
Service: "mock-service",
Name: "John Doe",
Age: 30,
Email: "john.doe@example.com",
Address: "123 Main St",
Street: "123 Main St",
}, match.Any("$.address", "$.street").Placeholder("mock-address"))
})
}
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
module github.com/gkampitakis/go-snaps

go 1.21
go 1.22

toolchain go1.23.2

require (
github.com/gkampitakis/ciinfo v0.3.0
github.com/gkampitakis/ciinfo v0.3.1
github.com/gkampitakis/go-diff v1.3.2
github.com/goccy/go-yaml v1.15.10
github.com/kr/pretty v0.3.1
github.com/maruel/natural v1.1.1
github.com/tidwall/gjson v1.17.0
github.com/tidwall/gjson v1.18.0
github.com/tidwall/pretty v1.2.1
github.com/tidwall/sjson v1.2.5
)

require (
github.com/kr/text v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
)
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/gkampitakis/ciinfo v0.3.0 h1:gWZlOC2+RYYttL0hBqcoQhM7h1qNkVqvRCV1fOvpAv8=
github.com/gkampitakis/ciinfo v0.3.0/go.mod h1:1NIwaOcFChN4fa/B0hEBdAb6npDlFL8Bwx4dfRLRqAo=
github.com/gkampitakis/ciinfo v0.3.1 h1:lzjbemlGI4Q+XimPg64ss89x8Mf3xihJqy/0Mgagapo=
github.com/gkampitakis/ciinfo v0.3.1/go.mod h1:1NIwaOcFChN4fa/B0hEBdAb6npDlFL8Bwx4dfRLRqAo=
github.com/gkampitakis/go-diff v1.3.2 h1:Qyn0J9XJSDTgnsgHRdz9Zp24RaJeKMUHg2+PDZZdC4M=
github.com/gkampitakis/go-diff v1.3.2/go.mod h1:LLgOrpqleQe26cte8s36HTWcTmMEur6OPYerdAAS9tk=
github.com/goccy/go-yaml v1.15.10 h1:9exV2CDYm/FWHPptIIgcDiPQS+X/4uTR+HEl+GF9xJU=
github.com/goccy/go-yaml v1.15.10/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -11,11 +13,11 @@ github.com/maruel/natural v1.1.1 h1:Hja7XhhmvEFhcByqDoHz9QZbkWey+COd9xWfCfn1ioo=
github.com/maruel/natural v1.1.1/go.mod h1:v+Rfd79xlw1AgVBjbO0BEQmptqb5HvL/k9GRHB7ZKEg=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
Expand Down
57 changes: 56 additions & 1 deletion match/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package match

import (
"errors"
"fmt"
"strings"

"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/parser"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

var errPathNotFound = errors.New("path does not exist")

type anyMatcher struct {
paths []string
placeholder any
Expand Down Expand Up @@ -45,6 +51,55 @@ func (a *anyMatcher) ErrOnMissingPath(e bool) *anyMatcher {
return a
}

func (a anyMatcher) YAML(s []byte) ([]byte, []MatcherError) {
var errs []MatcherError

f, err := parser.ParseBytes(s, parser.ParseComments)
if err != nil {
return s, []MatcherError{{
Reason: err,
Matcher: a.name,
Path: "*",
}}
}

for _, path := range a.paths {
p, err := yaml.PathString(path)
if err != nil {
errs = append(errs, MatcherError{
Reason: err,
Matcher: a.name,
Path: path,
})

continue
}

if a.errOnMissingPath {
if _, err = p.FilterFile(f); err != nil && errors.Is(err, yaml.ErrNotFoundNode) {
errs = append(errs, MatcherError{
Reason: errPathNotFound,
Matcher: a.name,
Path: path,
})
continue
}
}

err = p.ReplaceWithReader(f, strings.NewReader(fmt.Sprint(a.placeholder)))
if err != nil {
errs = append(errs, MatcherError{
Reason: err,
Matcher: a.name,
Path: path,
})
continue
}
}

return []byte(f.String()), errs
}

// JSON is intended to be called internally on snaps.MatchJSON for applying Any matchers
func (a anyMatcher) JSON(s []byte) ([]byte, []MatcherError) {
var errs []MatcherError
Expand All @@ -55,7 +110,7 @@ func (a anyMatcher) JSON(s []byte) ([]byte, []MatcherError) {
if !r.Exists() {
if a.errOnMissingPath {
errs = append(errs, MatcherError{
Reason: errors.New("path does not exist"),
Reason: errPathNotFound,
Matcher: a.name,
Path: path,
})
Expand Down
4 changes: 4 additions & 0 deletions match/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ type JSONMatcher interface {
JSON([]byte) ([]byte, []MatcherError)
}

type YAMLMatcher interface {
YAML([]byte) ([]byte, []MatcherError)
}

// internal Error struct returned from Matchers
type MatcherError struct {
Reason error
Expand Down
132 changes: 132 additions & 0 deletions snaps/matchYAML.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package snaps

import (
"errors"
"fmt"
"strings"

"github.com/gkampitakis/go-snaps/internal/colors"
"github.com/gkampitakis/go-snaps/match"
"github.com/goccy/go-yaml"
)

func (c *Config) MatchYAML(t testingT, input any, matchers ...match.YAMLMatcher) {
t.Helper()

matchYAML(c, t, input, matchers...)
}

func MatchYAML(t testingT, input any, matchers ...match.YAMLMatcher) {
t.Helper()

matchYAML(&defaultConfig, t, input, matchers...)
}

func matchYAML(c *Config, t testingT, input any, matchers ...match.YAMLMatcher) {
t.Helper()

snapPath, snapPathRel := snapshotPath(c, t.Name(), false)
testID := testsRegistry.getTestID(snapPath, t.Name())
t.Cleanup(func() {
testsRegistry.reset(snapPath, t.Name())
})

y, err := validateYAML(input)
if err != nil {
handleError(t, err)
return
}

y, matchersErrors := applyYAMLMatchers(y, matchers...)
if len(matchersErrors) > 0 {
s := strings.Builder{}

for _, err := range matchersErrors {
colors.Fprint(
&s,
colors.Red,
fmt.Sprintf(
"\n%smatch.%s(\"%s\") - %s",
errorSymbol,
err.Matcher,
err.Path,
err.Reason,
),
)
}

handleError(t, s.String())
return
}

snapshot := string(y)
prevSnapshot, line, err := getPrevSnapshot(testID, snapPath)
if errors.Is(err, errSnapNotFound) {
if isCI {
handleError(t, err)
return
}

err := addNewSnapshot(testID, snapshot, snapPath)
if err != nil {
handleError(t, err)
return
}

t.Log(addedMsg)
testEvents.register(added)
return
}
if err != nil {
handleError(t, err)
return
}

diff := prettyDiff(prevSnapshot, snapshot, snapPathRel, line)
if diff == "" {
testEvents.register(passed)
return
}

if !shouldUpdate(c.update) {
handleError(t, diff)
return
}

if err = updateSnapshot(testID, snapshot, snapPath); err != nil {
handleError(t, err)
return
}

t.Log(updatedMsg)
testEvents.register(updated)
}

// FIXME: validate
func validateYAML(input any) ([]byte, error) {
switch y := input.(type) {
case string:
return []byte(y), nil
case []byte:
return y, nil
default:
return yaml.Marshal(input)
}
}

// NOTE: this can be improved and pass the ast file
// NOTE: technically here we want to arrive when the []byte is valid yaml
func applyYAMLMatchers(b []byte, matchers ...match.YAMLMatcher) ([]byte, []match.MatcherError) {
errors := []match.MatcherError{}

for _, m := range matchers {
json, errs := m.YAML(b)
if len(errs) > 0 {
errors = append(errors, errs...)
continue
}
b = json
}

return b, errors
}
Loading