Wrapper for TunnelCommand to improve mocking of QuickTunnelRunner in testcase
This commit is contained in:
parent
05d6f55abf
commit
ac666dc5ca
|
@ -215,7 +215,6 @@ var (
|
||||||
"overwrite-dns",
|
"overwrite-dns",
|
||||||
"help",
|
"help",
|
||||||
}
|
}
|
||||||
runQuickTunnel = RunQuickTunnel
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Flags() []cli.Flag {
|
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 {
|
func TunnelCommand(c *cli.Context) error {
|
||||||
|
return tunnelCommandImpl(c, RunQuickTunnel)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tunnelCommandImpl(c *cli.Context, quickTunnelRunner QuickTunnelRunner) error {
|
||||||
sc, err := newSubcommandContext(c)
|
sc, err := newSubcommandContext(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
// 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)
|
shouldRunQuickTunnel := c.IsSet("url") || c.IsSet("unix-socket") || c.IsSet(ingress.HelloWorldFlag)
|
||||||
if !c.IsSet("proxy-dns") && c.String("quick-service") != "" && shouldRunQuickTunnel {
|
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
|
// If user provides a config, check to see if they meant to use `tunnel run` instead
|
||||||
|
|
|
@ -55,10 +55,8 @@ func TestShouldRunQuickTunnel(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
// Mock RunQuickTunnel Function
|
// Mock RunQuickTunnel Function
|
||||||
originalRunQuickTunnel := runQuickTunnel
|
|
||||||
defer func() { runQuickTunnel = originalRunQuickTunnel }()
|
|
||||||
mockCalled := false
|
mockCalled := false
|
||||||
runQuickTunnel = func(sc *subcommandContext) error {
|
mockQuickTunnelRunner := func(sc *subcommandContext) error {
|
||||||
mockCalled = true
|
mockCalled = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -69,7 +67,7 @@ func TestShouldRunQuickTunnel(t *testing.T) {
|
||||||
context := cli.NewContext(app, set, nil)
|
context := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
// Call TunnelCommand
|
// Call TunnelCommand
|
||||||
err := TunnelCommand(context)
|
err := tunnelCommandImpl(context, mockQuickTunnelRunner)
|
||||||
|
|
||||||
// Validate
|
// Validate
|
||||||
if tt.expectError {
|
if tt.expectError {
|
||||||
|
|
Loading…
Reference in New Issue