chore: Add test helpers to improve coverage (#166)

* chore: Rename ProjectHistory to ProjectVersion

Version more accurately represents version storage. This
forks from the WorkspaceHistory name, but I think it's
easier to understand Workspace history.

* Rename files

* Standardize tests a bit more

* Remove Server struct from coderdtest

* Improve test coverage for workspace history

* Fix linting errors

* Fix coderd test leak

* Fix coderd test leak

* Improve workspace history logs

* Standardize test structure for codersdk

* Fix linting errors

* Fix WebSocket compression

* Update coderd/workspaces.go

Co-authored-by: Bryan <bryan@coder.com>

* Add test for listing project parameters

* Cache npm dependencies with setup node

* Remove windows npm cache key

Co-authored-by: Bryan <bryan@coder.com>
This commit is contained in:
Kyle Carberry
2022-02-05 18:24:51 -06:00
committed by GitHub
parent f19770b2c6
commit 1796dc6c2f
38 changed files with 1575 additions and 1166 deletions

View File

@ -10,6 +10,7 @@ import (
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"golang.org/x/xerrors"
@ -20,14 +21,15 @@ import (
// New creates a Coder client for the provided URL.
func New(serverURL *url.URL) *Client {
return &Client{
url: serverURL,
URL: serverURL,
httpClient: &http.Client{},
}
}
// Client is an HTTP caller for methods to the Coder API.
type Client struct {
url *url.URL
URL *url.URL
httpClient *http.Client
}
@ -40,7 +42,7 @@ func (c *Client) SetSessionToken(token string) error {
return err
}
}
c.httpClient.Jar.SetCookies(c.url, []*http.Cookie{{
c.httpClient.Jar.SetCookies(c.URL, []*http.Cookie{{
Name: httpmw.AuthCookie,
Value: token,
}})
@ -50,7 +52,7 @@ func (c *Client) SetSessionToken(token string) error {
// request performs an HTTP request with the body provided.
// The caller is responsible for closing the response body.
func (c *Client) request(ctx context.Context, method, path string, body interface{}) (*http.Response, error) {
serverURL, err := c.url.Parse(path)
serverURL, err := c.URL.Parse(path)
if err != nil {
return nil, xerrors.Errorf("parse url: %w", err)
}
@ -112,5 +114,10 @@ func (e *Error) StatusCode() int {
}
func (e *Error) Error() string {
return fmt.Sprintf("status code %d: %s", e.statusCode, e.Message)
var builder strings.Builder
_, _ = fmt.Fprintf(&builder, "status code %d: %s", e.statusCode, e.Message)
for _, err := range e.Errors {
_, _ = fmt.Fprintf(&builder, "\n\t%s: %s", err.Field, err.Code)
}
return builder.String()
}