Documentation
¶
Overview ¶
Package cmdtest runs ytrack commands in-process against fake IO, a temp config dir, go-keyring's mock and a fake YouTrack server. Test-only.
Index ¶
- func Golden(t *testing.T, name, got string)
- func ShowANSI(s string) string
- type Env
- type FakeYouTrack
- func (yt *FakeYouTrack) Key() string
- func (yt *FakeYouTrack) LastRequest(t *testing.T) Request
- func (yt *FakeYouTrack) RedirectUploads()
- func (yt *FakeYouTrack) Requests() []Request
- func (yt *FakeYouTrack) SeedIssues(n int)
- func (yt *FakeYouTrack) SeedSampleIssues()
- func (yt *FakeYouTrack) SeedSampleProjects()
- func (yt *FakeYouTrack) SeedSampleWorkItems()
- func (yt *FakeYouTrack) URL() string
- type Projection
- type Request
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Env ¶
type Env struct {
IO *iostreams.IOStreams
Stdin *bytes.Buffer
Stdout *bytes.Buffer
Stderr *bytes.Buffer
Browser *browser.Stub
ConfigDir string
Factory *cmdutil.Factory
// contains filtered or unexported fields
}
Env is an isolated ytrack environment.
func New ¶
New returns an Env with no TTYs, an empty config dir, an empty mock keyring and every YTRACK_* variable cleared. Tests using it must not run in parallel (the keyring mock and environment are process-global).
func (*Env) Interactive ¶
func (e *Env) Interactive()
Interactive marks stdin and stdout as terminals so prompts are allowed.
func (*Env) IsUsageError ¶
IsUsageError reports whether a run failed as a usage error: exit 1 (as in gh) with the --help hint on stderr.
func (*Env) Login ¶
func (e *Env) Login(yt *FakeYouTrack)
Login stores token for the fake server's host as `auth login --with-token` would.
func (*Env) RouteAllTo ¶
func (e *Env) RouteAllTo(yt *FakeYouTrack)
RouteAllTo sends every HTTP connection to the fake server, whatever host the URL names, so tests can use non-loopback host names.
type FakeYouTrack ¶
type FakeYouTrack struct {
Server *httptest.Server
Prefix string
Token string
Login string
FullName string
Issues []map[string]any
// Comments holds each issue's comments by readable ID, oldest first.
Comments map[string][]map[string]any
// Attachments holds each issue's issue-level attachments by readable ID.
Attachments map[string][]map[string]any
// WorkItems holds each issue's work items by readable ID.
WorkItems map[string][]map[string]any
// TimeTracking holds project time tracking settings by project ID.
TimeTracking map[string]map[string]any
// Projects, ProjectFields (by project ID) and Tags back issue writes.
Projects []map[string]any
ProjectFields map[string][]map[string]any
Tags []map[string]any
// MaxTop caps $top like a server limit would (0 means no cap).
MaxTop int
// HubTokenPage makes the instance's token page 404, as on a Server
// install with an external Hub.
HubTokenPage bool
// CommandHTML makes command previews describe each part in HTML, as
// YouTrack does: values HTML-escaped inside <span class="bold">.
CommandHTML bool
// CommandDescriptions, when set, replaces the description of each parsed
// command part, in order, exactly as given (for hostile previews).
CommandDescriptions []string
// contains filtered or unexported fields
}
FakeYouTrack is a minimal YouTrack REST API:
GET /api/users/me the token's user
GET /api/issues Issues, paged with $skip/$top (default $top 42)
and restricted by customFields= when given
POST /api/issues creates an issue (see SeedSampleProjects); echoes
the JSON body back when no projects are seeded
POST /api/issues/{id} updates summary, description and customFields
POST|DELETE /api/issues/{id}/tags[/{tag}] tags and untags an issue
GET /api/admin/projects Projects, paged
GET /api/admin/projects/{id}/customFields ProjectFields[project id], paged
GET /api/admin/projects/{id}/customFields/{field} one of them
GET /api/tags Tags, paged
POST /api/commands[/assist] applies or previews a small subset of the
YouTrack command language (see command)
GET /api/issues/{id} the issue in Issues with that idReadable or id,
with commentsCount; 404 with a YouTrack body if none
GET /api/issues/{id}/comments Comments[idReadable], paged with $skip/$top
POST /api/issues/{id}/comments appends {"text", "attachments"} to Comments and
returns it; an attachment needs a name and a
data URI base64Content (400 otherwise)
POST /api/issues/{id}/attachments stores multipart files in Attachments and
returns them
GET /api/issues/{id}/timeTracking/workItems WorkItems[idReadable], paged
POST /api/issues/{id}/timeTracking/workItems creates a work item from
{duration{minutes}, date, text, type{id}}; 400 for
a missing duration or a type the project lacks
GET|POST|DELETE /api/issues/{id}/timeTracking/workItems/{item}
reads, updates (merging the body) or deletes one,
found by ID across all issues (any {id})
GET /api/admin/projects/{id}/timeTrackingSettings TimeTracking[project id]
GET /api/plain a text/plain body
GET /users/me the web token page (no auth; 404 if HubTokenPage)
Like YouTrack, a request with a body that is not JSON (or multipart for attachments) is a 415.
Like YouTrack, JSON responses keep only the attributes named in fields= (plus $type), when fields= is given. Every /api route requires "Authorization: Bearer <Token>".
func NewFakeYouTrack ¶
func NewFakeYouTrack(t *testing.T, prefix string) *FakeYouTrack
NewFakeYouTrack starts a fake server mounted at prefix ("" or e.g. "/youtrack").
func (*FakeYouTrack) Key ¶
func (yt *FakeYouTrack) Key() string
Key is the host key ytrack derives from URL.
func (*FakeYouTrack) LastRequest ¶
func (yt *FakeYouTrack) LastRequest(t *testing.T) Request
LastRequest returns the most recent request.
func (*FakeYouTrack) RedirectUploads ¶ added in v0.5.0
func (yt *FakeYouTrack) RedirectUploads()
RedirectUploads makes yt answer an issue upload with a 307 to the same URL, as a proxy that wants the request resent, unless it was already redirected.
func (*FakeYouTrack) Requests ¶
func (yt *FakeYouTrack) Requests() []Request
Requests returns the requests received so far.
func (*FakeYouTrack) SeedIssues ¶
func (yt *FakeYouTrack) SeedIssues(n int)
SeedIssues fills Issues with n issues APP-1..APP-n.
func (*FakeYouTrack) SeedSampleIssues ¶
func (yt *FakeYouTrack) SeedSampleIssues()
SeedSampleIssues replaces Issues with NSR-40 and NSR-41 and gives NSR-40 three comments.
func (*FakeYouTrack) SeedSampleProjects ¶
func (yt *FakeYouTrack) SeedSampleProjects()
SeedSampleProjects adds the NSR, OPS and (archived) OLD projects, NSR's custom field definitions and three tags. With Projects set, POST /api/issues creates issues instead of echoing the body.
func (*FakeYouTrack) SeedSampleWorkItems ¶
func (yt *FakeYouTrack) SeedSampleWorkItems()
SeedSampleWorkItems gives NSR-40 three work items and the NSR project (0-12) time tracking with the types Development, Testing and Documentation. Seed the issues first.
func (*FakeYouTrack) URL ¶
func (yt *FakeYouTrack) URL() string
URL is the service URL, including the prefix.
type Projection ¶
type Projection map[string]Projection
Projection is a parsed fields= value: attribute name to its nested projection (nil for a leaf).
func ParseProjection ¶
func ParseProjection(s string) (Projection, error)
ParseProjection parses YouTrack's fields syntax, e.g. "id,project(name)".