Wrapper for TunnelCommand to improve mocking of QuickTunnelRunner in testcase

This commit is contained in:
Nitin Kumar 2025-01-25 17:35:21 +05:30
parent 05d6f55abf
commit ac666dc5ca
2 changed files with 10 additions and 6 deletions

View File

@ -215,7 +215,6 @@ var (
"overwrite-dns",
"help",
}
runQuickTunnel = RunQuickTunnel
)
func Flags() []cli.Flag {
@ -287,7 +286,14 @@ See https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/in
}
}
// This is so that we can mock QuickTunnelRunner for TunnelCommand test cases
type QuickTunnelRunner func(*subcommandContext) error
func TunnelCommand(c *cli.Context) error {
return tunnelCommandImpl(c, RunQuickTunnel)
}
func tunnelCommandImpl(c *cli.Context, quickTunnelRunner QuickTunnelRunner) error {
sc, err := newSubcommandContext(c)
if err != nil {
return err
@ -316,7 +322,7 @@ func TunnelCommand(c *cli.Context) error {
// We don't support running proxy-dns and a quick tunnel at the same time as the same process
shouldRunQuickTunnel := c.IsSet("url") || c.IsSet("unix-socket") || c.IsSet(ingress.HelloWorldFlag)
if !c.IsSet("proxy-dns") && c.String("quick-service") != "" && shouldRunQuickTunnel {
return runQuickTunnel(sc)
return quickTunnelRunner(sc)
}
// If user provides a config, check to see if they meant to use `tunnel run` instead

View File

@ -55,10 +55,8 @@ func TestShouldRunQuickTunnel(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Mock RunQuickTunnel Function
originalRunQuickTunnel := runQuickTunnel
defer func() { runQuickTunnel = originalRunQuickTunnel }()
mockCalled := false
runQuickTunnel = func(sc *subcommandContext) error {
mockQuickTunnelRunner := func(sc *subcommandContext) error {
mockCalled = true
return nil
}
@ -69,7 +67,7 @@ func TestShouldRunQuickTunnel(t *testing.T) {
context := cli.NewContext(app, set, nil)
// Call TunnelCommand
err := TunnelCommand(context)
err := tunnelCommandImpl(context, mockQuickTunnelRunner)
// Validate
if tt.expectError {