cmdtest

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FixtureIssueClosed = map[string]interface{}{
	"id":          201,
	"iid":         11,
	"title":       "Update documentation",
	"state":       "closed",
	"description": "Documentation needs to be updated with new API endpoints",
	"web_url":     "https://gitlab.com/test-owner/test-repo/-/issues/11",
	"author": map[string]interface{}{
		"id":       4,
		"username": "docs-writer",
		"name":     "Documentation Writer",
		"email":    "docs@example.com",
	},
	"assignees": []interface{}{
		map[string]interface{}{
			"id":       4,
			"username": "docs-writer",
			"name":     "Documentation Writer",
		},
	},
	"labels":     []string{"documentation"},
	"milestone":  nil,
	"created_at": "2024-01-03T14:00:00.000Z",
	"updated_at": "2024-01-05T16:00:00.000Z",
	"closed_at":  "2024-01-05T16:00:00.000Z",
	"closed_by": map[string]interface{}{
		"id":       4,
		"username": "docs-writer",
		"name":     "Documentation Writer",
	},
	"due_date":         nil,
	"upvotes":          1,
	"downvotes":        0,
	"user_notes_count": 1,
}

FixtureIssueClosed is an issue that has been closed.

View Source
var FixtureIssueOpen = map[string]interface{}{
	"id":          200,
	"iid":         10,
	"title":       "Application crashes on startup",
	"state":       "opened",
	"description": "The application crashes when starting with certain configurations",
	"web_url":     "https://gitlab.com/test-owner/test-repo/-/issues/10",
	"author": map[string]interface{}{
		"id":       1,
		"username": "test-user",
		"name":     "Test User",
		"email":    "test-user@example.com",
	},
	"assignees": []interface{}{
		map[string]interface{}{
			"id":       2,
			"username": "developer",
			"name":     "Developer User",
		},
	},
	"labels": []string{"bug", "priority::high"},
	"milestone": map[string]interface{}{
		"id":    5,
		"title": "v1.2.0",
	},
	"created_at":       "2024-01-05T09:00:00.000Z",
	"updated_at":       "2024-01-06T11:30:00.000Z",
	"closed_at":        nil,
	"due_date":         "2024-01-15",
	"upvotes":          2,
	"downvotes":        0,
	"user_notes_count": 3,
}

FixtureIssueOpen is an issue in the opened state.

View Source
var FixtureLabelBug = map[string]interface{}{
	"id":                        500,
	"name":                      "bug",
	"color":                     "#d9534f",
	"description":               "Something isn't working correctly",
	"text_color":                "#FFFFFF",
	"open_issues_count":         3,
	"closed_issues_count":       15,
	"open_merge_requests_count": 1,
	"priority":                  nil,
}

FixtureLabelBug is a bug label.

View Source
var FixtureLabelFeature = map[string]interface{}{
	"id":                        501,
	"name":                      "feature",
	"color":                     "#5cb85c",
	"description":               "New feature or enhancement",
	"text_color":                "#FFFFFF",
	"open_issues_count":         7,
	"closed_issues_count":       25,
	"open_merge_requests_count": 3,
	"priority":                  nil,
}

FixtureLabelFeature is a feature label.

View Source
var FixtureLabelPriority = map[string]interface{}{
	"id":                        502,
	"name":                      "priority::high",
	"color":                     "#f0ad4e",
	"description":               "High priority item",
	"text_color":                "#FFFFFF",
	"open_issues_count":         2,
	"closed_issues_count":       8,
	"open_merge_requests_count": 1,
	"priority":                  10,
}

FixtureLabelPriority is a priority label.

View Source
var FixtureMRClosed = map[string]interface{}{
	"id":          102,
	"iid":         3,
	"title":       "Experimental feature",
	"state":       "closed",
	"description": "This experimental feature was not accepted",
	"web_url":     "https://gitlab.com/test-owner/test-repo/-/merge_requests/3",
	"author": map[string]interface{}{
		"id":       3,
		"username": "contributor",
		"name":     "External Contributor",
		"email":    "contributor@example.com",
	},
	"assignees":     []interface{}{},
	"source_branch": "feature/experiment",
	"target_branch": "main",
	"merged_at":     nil,
	"closed_at":     "2024-01-04T12:00:00.000Z",
	"closed_by": map[string]interface{}{
		"id":       1,
		"username": "test-user",
		"name":     "Test User",
	},
	"created_at":                    "2024-01-02T08:00:00.000Z",
	"updated_at":                    "2024-01-04T12:00:00.000Z",
	"merge_status":                  "cannot_be_merged",
	"upvotes":                       0,
	"downvotes":                     2,
	"work_in_progress":              false,
	"draft":                         false,
	"has_conflicts":                 true,
	"blocking_discussions_resolved": false,
}

FixtureMRClosed is a merge request that has been closed without merging.

View Source
var FixtureMRMerged = map[string]interface{}{
	"id":          101,
	"iid":         2,
	"title":       "Fix critical bug",
	"state":       "merged",
	"description": "This MR fixes a critical bug in production",
	"web_url":     "https://gitlab.com/test-owner/test-repo/-/merge_requests/2",
	"author": map[string]interface{}{
		"id":       1,
		"username": "test-user",
		"name":     "Test User",
		"email":    "test-user@example.com",
	},
	"assignees":     []interface{}{},
	"source_branch": "bugfix/critical-fix",
	"target_branch": "main",
	"merged_at":     "2024-01-03T16:45:00.000Z",
	"merged_by": map[string]interface{}{
		"id":       2,
		"username": "reviewer",
		"name":     "Reviewer User",
	},
	"closed_at":                     nil,
	"created_at":                    "2024-01-03T09:00:00.000Z",
	"updated_at":                    "2024-01-03T16:45:00.000Z",
	"merge_status":                  "merged",
	"upvotes":                       5,
	"downvotes":                     0,
	"work_in_progress":              false,
	"draft":                         false,
	"has_conflicts":                 false,
	"blocking_discussions_resolved": true,
}

FixtureMRMerged is a merge request that has been merged.

View Source
var FixtureMROpen = map[string]interface{}{
	"id":          100,
	"iid":         1,
	"title":       "Add new feature",
	"state":       "opened",
	"description": "This MR adds a new feature to improve user experience",
	"web_url":     "https://gitlab.com/test-owner/test-repo/-/merge_requests/1",
	"author": map[string]interface{}{
		"id":       1,
		"username": "test-user",
		"name":     "Test User",
		"email":    "test-user@example.com",
	},
	"assignees": []interface{}{
		map[string]interface{}{
			"id":       2,
			"username": "reviewer",
			"name":     "Reviewer User",
		},
	},
	"source_branch":                 "feature/new-feature",
	"target_branch":                 "main",
	"merged_at":                     nil,
	"closed_at":                     nil,
	"created_at":                    "2024-01-01T10:00:00.000Z",
	"updated_at":                    "2024-01-02T14:30:00.000Z",
	"merge_status":                  "can_be_merged",
	"upvotes":                       3,
	"downvotes":                     0,
	"work_in_progress":              false,
	"draft":                         false,
	"has_conflicts":                 false,
	"blocking_discussions_resolved": true,
}

FixtureMROpen is a merge request in the opened state.

View Source
var FixturePipelineFailed = map[string]interface{}{
	"id":      301,
	"iid":     51,
	"ref":     "feature/test-failure",
	"sha":     "def456789012345678901234567890abcdef1234",
	"status":  "failed",
	"web_url": "https://gitlab.com/test-owner/test-repo/-/pipelines/301",
	"user": map[string]interface{}{
		"id":       2,
		"username": "developer",
		"name":     "Developer User",
	},
	"created_at":  "2024-01-06T11:00:00.000Z",
	"updated_at":  "2024-01-06T11:08:00.000Z",
	"started_at":  "2024-01-06T11:00:30.000Z",
	"finished_at": "2024-01-06T11:08:00.000Z",
	"duration":    450,
	"coverage":    nil,
}

FixturePipelineFailed is a pipeline that failed.

View Source
var FixturePipelinePending = map[string]interface{}{
	"id":      303,
	"iid":     53,
	"ref":     "feature/new-pipeline",
	"sha":     "567890abcdef1234567890abcdef12345678901",
	"status":  "pending",
	"web_url": "https://gitlab.com/test-owner/test-repo/-/pipelines/303",
	"user": map[string]interface{}{
		"id":       3,
		"username": "contributor",
		"name":     "External Contributor",
	},
	"created_at":  "2024-01-06T13:00:00.000Z",
	"updated_at":  "2024-01-06T13:00:00.000Z",
	"started_at":  nil,
	"finished_at": nil,
	"duration":    nil,
	"coverage":    nil,
}

FixturePipelinePending is a pipeline waiting to start.

View Source
var FixturePipelineRunning = map[string]interface{}{
	"id":      302,
	"iid":     52,
	"ref":     "develop",
	"sha":     "1234567890abcdef1234567890abcdef12345678",
	"status":  "running",
	"web_url": "https://gitlab.com/test-owner/test-repo/-/pipelines/302",
	"user": map[string]interface{}{
		"id":       1,
		"username": "test-user",
		"name":     "Test User",
	},
	"created_at":  "2024-01-06T12:00:00.000Z",
	"updated_at":  "2024-01-06T12:05:00.000Z",
	"started_at":  "2024-01-06T12:00:30.000Z",
	"finished_at": nil,
	"duration":    nil,
	"coverage":    nil,
}

FixturePipelineRunning is a pipeline currently running.

View Source
var FixturePipelineSuccess = map[string]interface{}{
	"id":      300,
	"iid":     50,
	"ref":     "main",
	"sha":     "abc123def456789012345678901234567890abcd",
	"status":  "success",
	"web_url": "https://gitlab.com/test-owner/test-repo/-/pipelines/300",
	"user": map[string]interface{}{
		"id":       1,
		"username": "test-user",
		"name":     "Test User",
	},
	"created_at":  "2024-01-06T10:00:00.000Z",
	"updated_at":  "2024-01-06T10:15:00.000Z",
	"started_at":  "2024-01-06T10:00:30.000Z",
	"finished_at": "2024-01-06T10:15:00.000Z",
	"duration":    870,
	"coverage":    "85.5",
}

FixturePipelineSuccess is a pipeline that completed successfully.

View Source
var FixtureProject = map[string]interface{}{
	"id":                  400,
	"name":                "Test Repository",
	"path":                "test-repo",
	"description":         "A test repository for GitLab CLI development and testing",
	"web_url":             "https://gitlab.com/test-owner/test-repo",
	"ssh_url_to_repo":     "git@gitlab.com:test-owner/test-repo.git",
	"http_url_to_repo":    "https://gitlab.com/test-owner/test-repo.git",
	"path_with_namespace": "test-owner/test-repo",
	"namespace": map[string]interface{}{
		"id":        100,
		"name":      "test-owner",
		"path":      "test-owner",
		"kind":      "user",
		"full_path": "test-owner",
	},
	"visibility":             "public",
	"default_branch":         "main",
	"created_at":             "2023-06-01T00:00:00.000Z",
	"last_activity_at":       "2024-01-06T14:00:00.000Z",
	"star_count":             42,
	"forks_count":            7,
	"open_issues_count":      5,
	"archived":               false,
	"topics":                 []string{"cli", "gitlab", "go"},
	"readme_url":             "https://gitlab.com/test-owner/test-repo/-/blob/main/README.md",
	"avatar_url":             "",
	"ci_config_path":         ".gitlab-ci.yml",
	"shared_runners_enabled": true,
}

FixtureProject is a typical project.

View Source
var FixtureProjectPrivate = map[string]interface{}{
	"id":                  401,
	"name":                "Private Project",
	"path":                "private-repo",
	"description":         "A private repository",
	"web_url":             "https://gitlab.com/test-owner/private-repo",
	"ssh_url_to_repo":     "git@gitlab.com:test-owner/private-repo.git",
	"http_url_to_repo":    "https://gitlab.com/test-owner/private-repo.git",
	"path_with_namespace": "test-owner/private-repo",
	"namespace": map[string]interface{}{
		"id":        100,
		"name":      "test-owner",
		"path":      "test-owner",
		"kind":      "user",
		"full_path": "test-owner",
	},
	"visibility":             "private",
	"default_branch":         "main",
	"created_at":             "2023-08-15T00:00:00.000Z",
	"last_activity_at":       "2024-01-05T10:00:00.000Z",
	"star_count":             0,
	"forks_count":            0,
	"open_issues_count":      2,
	"archived":               false,
	"topics":                 []string{},
	"readme_url":             "https://gitlab.com/test-owner/private-repo/-/blob/main/README.md",
	"avatar_url":             "",
	"ci_config_path":         ".gitlab-ci.yml",
	"shared_runners_enabled": true,
}

FixtureProjectPrivate is a private project.

View Source
var FixtureRelease = map[string]interface{}{
	"tag_name":    "v1.0.0",
	"name":        "Version 1.0.0",
	"description": "## What's Changed\n\n* Add new feature X\n* Fix bug Y\n* Improve performance Z\n\n**Full Changelog**: https://gitlab.com/test-owner/test-repo/-/compare/v0.9.0...v1.0.0",
	"created_at":  "2024-01-01T00:00:00.000Z",
	"released_at": "2024-01-01T12:00:00.000Z",
	"author": map[string]interface{}{
		"id":       1,
		"username": "test-user",
		"name":     "Test User",
	},
	"commit": map[string]interface{}{
		"id":         "abc123def456789012345678901234567890abcd",
		"short_id":   "abc123d",
		"title":      "Release v1.0.0",
		"message":    "Release v1.0.0\n\nFinal release for version 1.0.0",
		"created_at": "2024-01-01T00:00:00.000Z",
	},
	"assets": map[string]interface{}{
		"count": 2,
		"sources": []interface{}{
			map[string]string{
				"format": "zip",
				"url":    "https://gitlab.com/test-owner/test-repo/-/archive/v1.0.0/test-repo-v1.0.0.zip",
			},
			map[string]string{
				"format": "tar.gz",
				"url":    "https://gitlab.com/test-owner/test-repo/-/archive/v1.0.0/test-repo-v1.0.0.tar.gz",
			},
		},
		"links": []interface{}{},
	},
}

FixtureRelease is a typical release.

View Source
var FixtureSnippet = map[string]interface{}{
	"id":          600,
	"title":       "Example Go Function",
	"file_name":   "example.go",
	"description": "A simple example function in Go",
	"visibility":  "public",
	"web_url":     "https://gitlab.com/-/snippets/600",
	"raw_url":     "https://gitlab.com/-/snippets/600/raw",
	"author": map[string]interface{}{
		"id":       1,
		"username": "test-user",
		"name":     "Test User",
	},
	"created_at": "2024-01-02T15:00:00.000Z",
	"updated_at": "2024-01-02T15:30:00.000Z",
	"files": []interface{}{
		map[string]interface{}{
			"path":    "example.go",
			"raw_url": "https://gitlab.com/-/snippets/600/raw/main/example.go",
		},
	},
}

FixtureSnippet is a typical code snippet.

View Source
var FixtureSnippetPrivate = map[string]interface{}{
	"id":          601,
	"title":       "Private Configuration",
	"file_name":   "config.yml",
	"description": "Private configuration file",
	"visibility":  "private",
	"web_url":     "https://gitlab.com/-/snippets/601",
	"raw_url":     "https://gitlab.com/-/snippets/601/raw",
	"author": map[string]interface{}{
		"id":       1,
		"username": "test-user",
		"name":     "Test User",
	},
	"created_at": "2024-01-03T10:00:00.000Z",
	"updated_at": "2024-01-03T10:00:00.000Z",
	"files": []interface{}{
		map[string]interface{}{
			"path":    "config.yml",
			"raw_url": "https://gitlab.com/-/snippets/601/raw/main/config.yml",
		},
	},
}

FixtureSnippetPrivate is a private snippet.

View Source
var FixtureUser = map[string]interface{}{
	"id":           1,
	"username":     "test-user",
	"name":         "Test User",
	"email":        "test-user@example.com",
	"state":        "active",
	"web_url":      "https://gitlab.com/test-user",
	"avatar_url":   "https://www.gravatar.com/avatar/test",
	"bio":          "Software developer and GitLab enthusiast",
	"location":     "San Francisco, CA",
	"created_at":   "2020-01-01T00:00:00.000Z",
	"is_admin":     false,
	"public_email": "test-user@example.com",
}

FixtureUser is a typical user.

View Source
var FixtureUserAdmin = map[string]interface{}{
	"id":           10,
	"username":     "admin-user",
	"name":         "Admin User",
	"email":        "admin@example.com",
	"state":        "active",
	"web_url":      "https://gitlab.com/admin-user",
	"avatar_url":   "https://www.gravatar.com/avatar/admin",
	"bio":          "GitLab Administrator",
	"location":     "Remote",
	"created_at":   "2019-01-01T00:00:00.000Z",
	"is_admin":     true,
	"public_email": "admin@example.com",
}

FixtureUserAdmin is an admin user.

Functions

func AssertContains

func AssertContains(t *testing.T, str, substr string)

AssertContains fails the test if the string does not contain the substring.

func AssertEqual

func AssertEqual(t *testing.T, got, want interface{})

AssertEqual fails the test if the values are not equal.

func AssertJSONArray

func AssertJSONArray(t *testing.T, jsonString string, expectedLength int) []interface{}

AssertJSONArray verifies that JSON output is an array with expected length.

func AssertJSONContains

func AssertJSONContains(t *testing.T, jsonString string, expectedFields map[string]interface{})

AssertJSONContains verifies that JSON output contains specific key-value pairs. This is useful for partial matching when you don't want to verify the entire structure.

func AssertJSONOutput

func AssertJSONOutput(t *testing.T, jsonString string, expected interface{})

AssertJSONOutput parses JSON output and verifies it matches expected structure. The expected parameter can be a map[string]interface{} for objects or []interface{} for arrays.

func AssertNotContains

func AssertNotContains(t *testing.T, str, substr string)

AssertNotContains fails the test if the string contains the substring.

func CopyReader

func CopyReader(r io.Reader) (*bytes.Buffer, error)

CopyReader copies an io.Reader to a new bytes.Buffer and returns both the buffer and the original content.

func CreateIntegrationClient

func CreateIntegrationClient(t *testing.T, baseURL, token string) *api.Client

CreateIntegrationClient creates a GitLab API client configured for integration testing. It uses the provided test server URL and sets up proper authentication.

func ErrorResponse

func ErrorResponse(w http.ResponseWriter, statusCode int, message string)

ErrorResponse is a helper to write GitLab API error responses.

func FailTransport

func FailTransport(t *testing.T, targetHost string)

FailTransport replaces http.DefaultTransport so that all HTTPS requests to the given host return an immediate error instead of hitting real DNS. The cleanup is handled automatically via t.Cleanup.

func InterceptTransport

func InterceptTransport(t *testing.T, targetHost string, srv *httptest.Server)

InterceptTransport replaces http.DefaultTransport so that HTTPS requests to targetHost are rewritten to hit the test server instead. The cleanup is handled automatically via t.Cleanup.

func JSONResponse

func JSONResponse(w http.ResponseWriter, statusCode int, body interface{})

JSONResponse is a helper to write JSON responses in mock handlers.

func MockGitLabServer

func MockGitLabServer(t *testing.T, hostname string, handler http.HandlerFunc) *httptest.Server

MockGitLabServer creates a mock GitLab API server that intercepts requests to the given hostname and routes them to the provided handler. The server and transport cleanup are handled automatically via t.Cleanup.

func MockIssue

func MockIssue(id int, title, state string) map[string]interface{}

MockIssue returns a sample issue for testing.

func MockLabel

func MockLabel(id int, name, color string) map[string]interface{}

MockLabel returns a sample label for testing.

func MockMergeRequest

func MockMergeRequest(id int, title, state string) map[string]interface{}

MockMergeRequest returns a sample merge request for testing.

func MockPipeline

func MockPipeline(id int, ref, status string) map[string]interface{}

MockPipeline returns a sample pipeline for testing.

func MockProject

func MockProject(id int, name, path string) map[string]interface{}

MockProject returns a sample project for testing.

func MockRelease

func MockRelease(tagName, name, description string) map[string]interface{}

MockRelease returns a sample release for testing.

func MockServer

func MockServer(t *testing.T, handler http.HandlerFunc) *httptest.Server

MockServer creates an httptest.Server with a custom handler. The server is automatically closed via t.Cleanup.

func MockSnippet

func MockSnippet(id int, title, fileName string) map[string]interface{}

MockSnippet returns a sample snippet for testing.

func MockUser

func MockUser(id int, username, name string) map[string]interface{}

MockUser returns a sample user for testing.

func RunCommand

func RunCommand(t *testing.T, tf *TestFactory, cmd *cobra.Command, args ...string) (stdout, stderr string, err error)

RunCommand executes a cobra command with the given arguments and returns the output. It uses the TestFactory's captured IO for stdout and stderr.

func SetupAuthContext

func SetupAuthContext(t *testing.T, tf *TestFactory, hostname, token string)

SetupAuthContext configures authentication context for integration tests. It sets up the test environment with a valid token and hostname.

func SetupProjectContext

func SetupProjectContext(t *testing.T, tf *TestFactory, hostname, owner, repo string)

SetupProjectContext configures project context for integration tests. It sets up a git remote with the specified owner and repository.

func StubInput

func StubInput(t *testing.T, tf *TestFactory, input string)

StubInput sets the stdin content for testing interactive commands.

Types

type IntegrationTestFactory

type IntegrationTestFactory struct {
	*TestFactory
	APIServer   *http.Server
	APIBaseURL  string
	MockHandler http.Handler
}

IntegrationTestFactory extends TestFactory for integration testing scenarios.

type MockAPIRouter

type MockAPIRouter struct {
	// contains filtered or unexported fields
}

MockAPIRouter provides a multiplexer for routing mock API requests. Use this to create complex mock API servers that handle multiple endpoints.

func NewMockAPIRouter

func NewMockAPIRouter(t *testing.T) *MockAPIRouter

NewMockAPIRouter creates a new mock API router for integration tests.

func (*MockAPIRouter) Register

func (r *MockAPIRouter) Register(method, path string, handler http.HandlerFunc)

Register adds a handler for a specific path and method. The key format is "METHOD /path", e.g., "GET /api/v4/user" or "POST /api/v4/projects".

func (*MockAPIRouter) RegisterWithPattern

func (r *MockAPIRouter) RegisterWithPattern(method, pathPattern string, handler http.HandlerFunc)

RegisterWithPattern adds a handler that matches path patterns. Use this for dynamic paths like "/api/v4/projects/:id/merge_requests".

func (*MockAPIRouter) ServeHTTP

func (r *MockAPIRouter) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements http.Handler to route requests to registered handlers.

type RouterMux

type RouterMux struct {
	// contains filtered or unexported fields
}

RouterMux creates a simple request router for mock servers. It allows you to register handlers for different paths.

func NewRouterMux

func NewRouterMux() *RouterMux

NewRouterMux creates a new RouterMux.

func (*RouterMux) HandleFunc

func (rm *RouterMux) HandleFunc(path string, handler http.HandlerFunc)

HandleFunc registers a handler for the given path.

func (*RouterMux) ServeHTTP

func (rm *RouterMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler to route requests.

type TestFactory

type TestFactory struct {
	*cmdutil.Factory
	IO     *TestIO
	Config *config.Config
	Client *api.Client
	Remote *git.Remote
}

TestFactory creates a test Factory with controllable dependencies.

func NewTestFactory

func NewTestFactory(t *testing.T) *TestFactory

NewTestFactory creates a Factory suitable for testing with captured IO.

type TestIO

type TestIO struct {
	In     *bytes.Buffer
	Out    *bytes.Buffer
	ErrOut *bytes.Buffer
}

TestIO captures stdout, stderr, and provides stdin for command testing.

func NewTestIO

func NewTestIO() *TestIO

NewTestIO creates a new TestIO with empty buffers.

func (*TestIO) ErrString

func (tio *TestIO) ErrString() string

ErrString returns the captured stderr as a string.

func (*TestIO) IOStreams

func (tio *TestIO) IOStreams() *iostreams.IOStreams

IOStreams returns an iostreams.IOStreams backed by the test buffers.

func (*TestIO) String

func (tio *TestIO) String() string

String returns the captured stdout as a string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL