Skip to content

Commit

Permalink
Remove param parser dependency on fixtures (#1029)
Browse files Browse the repository at this point in the history
* refactor parser logics

* refactor fixture parser

* fix go imports

* remove extra binary files
  • Loading branch information
etsai-stripe authored Feb 13, 2023
1 parent e054342 commit 02a9962
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 216 deletions.
42 changes: 5 additions & 37 deletions pkg/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -17,6 +16,7 @@ import (
"github.com/tidwall/gjson"

"github.com/stripe/stripe-cli/pkg/git"
"github.com/stripe/stripe-cli/pkg/parsers"
"github.com/stripe/stripe-cli/pkg/requests"
)

Expand Down Expand Up @@ -47,14 +47,6 @@ type FixtureRequest struct {
Context string `json:"context,omitempty"`
}

// FixtureQuery describes the query in fixture request
type FixtureQuery struct {
Match string // The substring that matched the query pattern regex
Name string
Query string
DefaultValue string
}

// Fixture contains a mapping of an individual fixtures responses for querying
type Fixture struct {
Fs afero.Fs
Expand Down Expand Up @@ -341,7 +333,7 @@ func (fxt *Fixture) makeRequest(ctx context.Context, data FixtureRequest, apiVer
APIBaseURL: fxt.BaseURL,
}

path, err := fxt.ParsePath(data)
path, err := parsers.ParsePath(data.Path, fxt.Responses)

if err != nil {
return make([]byte, 0), err
Expand All @@ -353,7 +345,7 @@ func (fxt *Fixture) makeRequest(ctx context.Context, data FixtureRequest, apiVer
}

if data.IdempotencyKey != "" {
idempotencyKey, err := fxt.ParseQuery(data.IdempotencyKey)
idempotencyKey, err := parsers.ParseQuery(data.IdempotencyKey, fxt.Responses)
if err != nil {
return nil, fmt.Errorf("error parsing idempotency_key field: %w", err)
}
Expand All @@ -365,7 +357,7 @@ func (fxt *Fixture) makeRequest(ctx context.Context, data FixtureRequest, apiVer

func (fxt *Fixture) createParams(params interface{}, apiVersion string) (*requests.RequestParameters, error) {
requestParams := requests.RequestParameters{}
parsed, err := fxt.ParseInterface(params)
parsed, err := parsers.ParseInterface(params, fxt.Responses)
if err != nil {
return &requestParams, err
}
Expand All @@ -380,30 +372,6 @@ func (fxt *Fixture) createParams(params interface{}, apiVersion string) (*reques
return &requestParams, nil
}

func getEnvVar(query FixtureQuery) (string, error) {
key := query.Query
// Check if env variable is present
envValue := os.Getenv(key)
if envValue == "" {
// Try to load from .env file
dir, err := os.Getwd()
if err != nil {
dir = ""
}
err = godotenv.Load(path.Join(dir, ".env"))
if err != nil {
return "", nil
}
envValue = os.Getenv(key)
}
if envValue == "" {
fmt.Printf("No value for env var: %s\n", key)
return "", nil
}

return envValue, nil
}

func (fxt *Fixture) updateEnv(env map[string]string) error {
dir, err := os.Getwd()
if err != nil {
Expand All @@ -429,7 +397,7 @@ func (fxt *Fixture) updateEnv(env map[string]string) error {
}

for key, value := range env {
parsed, err := fxt.ParseQuery(value)
parsed, err := parsers.ParseQuery(value, fxt.Responses)
if err != nil {
return err
}
Expand Down
60 changes: 0 additions & 60 deletions pkg/fixtures/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,66 +368,6 @@ CUST_ID="char_12345"`
assert.Equal(t, expected, string(output))
}

func TestToFixtureQuery(t *testing.T) {
tests := []struct {
input string
expected FixtureQuery
didMatch bool
}{
{
"/v1/charges",
FixtureQuery{},
false,
},
{
"/v1/charges/${char_bender:id}/capture",
FixtureQuery{"${char_bender:id}", "char_bender", "id", ""},
true,
},
{
"${.env:PHONE_NOT_SET|+1234567890}",
FixtureQuery{"${.env:PHONE_NOT_SET|+1234567890}", ".env", "PHONE_NOT_SET", "+1234567890"},
true,
},
{
"/v1/customers/${.env:CUST_ID}",
FixtureQuery{"${.env:CUST_ID}", ".env", "CUST_ID", ""},
true,
},
{
"${.env:CUST_ID}",
FixtureQuery{"${.env:CUST_ID}", ".env", "CUST_ID", ""},
true,
},
{
"${cust_bender:subscriptions.data.[0].id}",
FixtureQuery{"${cust_bender:subscriptions.data.[0].id}", "cust_bender", "subscriptions.data.[0].id", ""},
true,
},
{
"${cust_bender:subscriptions.data.[0].name|Unknown Person}",
FixtureQuery{"${cust_bender:subscriptions.data.[0].name|Unknown Person}", "cust_bender", "subscriptions.data.[0].name", "Unknown Person"},
true,
},
{
"${cust_bender:billing_details.address.country}",
FixtureQuery{"${cust_bender:billing_details.address.country}", "cust_bender", "billing_details.address.country", ""},
true,
},
{
"${cust_bender:billing_details.address.country|San Mateo}",
FixtureQuery{"${cust_bender:billing_details.address.country|San Mateo}", "cust_bender", "billing_details.address.country", "San Mateo"},
true,
},
}

for _, test := range tests {
actualQuery, actualDidMatch := ToFixtureQuery(test.input)
assert.Equal(t, test.expected, actualQuery)
assert.Equal(t, test.didMatch, actualDidMatch)
}
}

func TestExecuteReturnsRequestNames(t *testing.T) {
fs := afero.NewMemMapFs()
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
Expand Down
Loading

0 comments on commit 02a9962

Please sign in to comment.