MCPcopy
hub / github.com/larksuite/cli / TestFactory

Function TestFactory

internal/cmdutil/testing.go:32–77  ·  view source on GitHub ↗

TestFactory creates a Factory for testing. Returns (factory, stdout buffer, stderr buffer, http mock registry).

(t *testing.T, config *core.CliConfig)

Source from the content-addressed store, hash-verified

30// TestFactory creates a Factory for testing.
31// Returns (factory, stdout buffer, stderr buffer, http mock registry).
32func TestFactory(t *testing.T, config *core.CliConfig) (*Factory, *bytes.Buffer, *bytes.Buffer, *httpmock.Registry) {
33 t.Helper()
34
35 reg := &httpmock.Registry{}
36 t.Cleanup(func() { reg.Verify(t) })
37
38 stdoutBuf := &bytes.Buffer{}
39 stderrBuf := &bytes.Buffer{}
40
41 mockClient := httpmock.NewClient(reg)
42 sdkMockClient := &http.Client{
43 Transport: &UserAgentTransport{Base: reg},
44 }
45
46 var testLarkClient *lark.Client
47 if config != nil && config.AppID != "" {
48 opts := []lark.ClientOptionFunc{
49 lark.WithEnableTokenCache(false),
50 lark.WithLogLevel(larkcore.LogLevelError),
51 lark.WithHttpClient(sdkMockClient),
52 lark.WithHeaders(BaseSecurityHeaders()),
53 }
54 if config.Brand != "" {
55 opts = append(opts, lark.WithOpenBaseUrl(core.ResolveOpenBaseURL(config.Brand)))
56 }
57 testLarkClient = lark.NewClient(config.AppID, credential.RuntimeAppSecret(config.AppSecret), opts...)
58 }
59
60 testCred := credential.NewCredentialProvider(
61 nil,
62 &testDefaultAcct{config: config},
63 &testDefaultToken{},
64 func() (*http.Client, error) { return mockClient, nil },
65 )
66
67 f := &Factory{
68 Config: func() (*core.CliConfig, error) { return config, nil },
69 HttpClient: func() (*http.Client, error) { return mockClient, nil },
70 LarkClient: func() (*lark.Client, error) { return testLarkClient, nil },
71 IOStreams: &IOStreams{In: nil, Out: stdoutBuf, ErrOut: stderrBuf},
72 Keychain: &noopKeychain{},
73 Credential: testCred,
74 FileIOProvider: fileio.GetProvider(),
75 }
76 return f, stdoutBuf, stderrBuf, reg
77}
78
79type testDefaultAcct struct {
80 config *core.CliConfig

Calls 8

VerifyMethod · 0.95
NewClientFunction · 0.92
ResolveOpenBaseURLFunction · 0.92
RuntimeAppSecretFunction · 0.92
NewCredentialProviderFunction · 0.92
GetProviderFunction · 0.92
BaseSecurityHeadersFunction · 0.85
CleanupMethod · 0.65

Tested by

no test coverage detected