Skip to content

Commit

Permalink
refactor: simplify E2E test struct
Browse files Browse the repository at this point in the history
Signed-off-by: lvlcn-t <[email protected]>
  • Loading branch information
lvlcn-t committed Nov 17, 2024
1 parent eea9e17 commit 88c63e3
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions test/framework/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,16 @@ var _ Runner = (*E2E)(nil)

// E2E is an end-to-end test.
type E2E struct {
t *testing.T
config config.Config
server *http.Server
buf bytes.Buffer
sparrow *sparrow.Sparrow
t *testing.T
checks map[string]builder.Check
buf bytes.Buffer
path string
server *http.Server
mu sync.Mutex
running bool
}

// WithConfigFile sets the path to the config file.
func (t *E2E) WithConfigFile(path string) *E2E {
t.path = path
return t
}

// WithChecks sets the checks in the test.
func (t *E2E) WithChecks(builders ...builder.Check) *E2E {
for _, b := range builders {
Expand Down Expand Up @@ -93,10 +86,6 @@ func (t *E2E) Run(ctx context.Context) error {
t.t.Fatal("E2E.Run must be called once")
}

if t.path == "" {
t.path = "testdata/checks.yaml"
}

if t.server != nil {
go func() {
err := t.server.ListenAndServe()
Expand Down Expand Up @@ -195,14 +184,15 @@ func (t *E2E) AwaitChecks() *E2E {
// writeCheckConfig writes the check config to a file at the provided path.
func (t *E2E) writeCheckConfig() error {
const fileMode = 0o755
err := os.MkdirAll(filepath.Dir(t.path), fileMode)
path := "testdata/checks.yaml"
err := os.MkdirAll(filepath.Dir(path), fileMode)
if err != nil {
return fmt.Errorf("failed to create %q: %w", filepath.Dir(t.path), err)
return fmt.Errorf("failed to create %q: %w", filepath.Dir(path), err)
}

err = os.WriteFile(t.path, t.buf.Bytes(), fileMode)
err = os.WriteFile(path, t.buf.Bytes(), fileMode)
if err != nil {
return fmt.Errorf("failed to write %q: %w", t.path, err)
return fmt.Errorf("failed to write %q: %w", path, err)
}
return nil
}
Expand Down Expand Up @@ -233,6 +223,7 @@ type e2eHttpAsserter struct {
router routers.Router
}

// e2eResponseAsserter is a response asserter for end-to-end tests.
type e2eResponseAsserter struct {
want any
asserter func(r *http.Response) error
Expand Down Expand Up @@ -376,7 +367,7 @@ func (a *e2eHttpAsserter) assertSchema(req *http.Request, resp *http.Response) e
return errors.New("no media type defined in OpenAPI schema for Content-Type 'application/json'")
}

var body any
var body map[string]any
if err = json.Unmarshal(data, &body); err != nil {
return fmt.Errorf("failed to unmarshal response body: %w", err)
}
Expand Down

0 comments on commit 88c63e3

Please sign in to comment.