Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ jobs:
with:
go-version: ${{ matrix.go-version }}
- name: build go
run: go build ./cmd/app/main.go
run: go build ./cmd/app
- name: build noui (headless)
run: CGO_ENABLED=0 go build -tags=noui -o console-headless ./cmd/app/main.go
run: CGO_ENABLED=0 go build -tags=noui -o console-headless ./cmd/app
- name: Install Test Converter and run tests
run: |
export GOPATH="$HOME/go/"
Expand Down
57 changes: 57 additions & 0 deletions cmd/app/browser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//go:build !noui

package main

import (
"context"
"os/exec"
"runtime"

"github.com/device-management-toolkit/console/config"
)

func launchBrowser(cfg *config.Config) {
scheme := "http"
if cfg.TLS.Enabled {
scheme = "https"
}

if err := openBrowser(scheme+"://localhost:"+cfg.Port, runtime.GOOS); err != nil {
panic(err)
}
}

// CommandExecutor is an interface to allow for mocking exec.Command in tests.
type CommandExecutor interface {
Execute(name string, arg ...string) error
}

// RealCommandExecutor is a real implementation of CommandExecutor.
type RealCommandExecutor struct{}

func (e *RealCommandExecutor) Execute(name string, arg ...string) error {
return exec.CommandContext(context.Background(), name, arg...).Start()
}

// Global command executor, can be replaced in tests.
var cmdExecutor CommandExecutor = &RealCommandExecutor{}

func openBrowser(url, currentOS string) error {
var cmd string

var args []string

switch currentOS {
case "darwin":
cmd = "open"
args = []string{url}
case "windows":
cmd = "cmd"
args = []string{"/c", "start", url}
default:
cmd = "xdg-open"
args = []string{url}
}

return cmdExecutor.Execute(cmd, args...)
}
8 changes: 8 additions & 0 deletions cmd/app/browser_noui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build noui

package main

import "github.com/device-management-toolkit/console/config"

// launchBrowser is a no-op in noui builds; there is no UI to open.
func launchBrowser(_ *config.Config) {}
53 changes: 53 additions & 0 deletions cmd/app/browser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//go:build !noui

package main

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

type MockCommandExecutor struct {
mock.Mock
}

func (m *MockCommandExecutor) Execute(name string, arg ...string) error {
args := m.Called(name, arg)

return args.Error(0)
}

func TestOpenBrowserWindows(t *testing.T) { //nolint:paralleltest // cannot have simultaneous tests modifying executor.
mockCmdExecutor := new(MockCommandExecutor)
cmdExecutor = mockCmdExecutor

mockCmdExecutor.On("Execute", "cmd", []string{"/c", "start", "http://localhost:8080"}).Return(nil)

err := openBrowser("http://localhost:8080", "windows")
assert.NoError(t, err)
mockCmdExecutor.AssertExpectations(t)
}

func TestOpenBrowserDarwin(t *testing.T) { //nolint:paralleltest // cannot have simultaneous tests modifying executor.
mockCmdExecutor := new(MockCommandExecutor)
cmdExecutor = mockCmdExecutor

mockCmdExecutor.On("Execute", "open", []string{"http://localhost:8080"}).Return(nil)

err := openBrowser("http://localhost:8080", "darwin")
assert.NoError(t, err)
mockCmdExecutor.AssertExpectations(t)
}

func TestOpenBrowserLinux(t *testing.T) { //nolint:paralleltest // cannot have simultaneous tests modifying executor.
mockCmdExecutor := new(MockCommandExecutor)
cmdExecutor = mockCmdExecutor

mockCmdExecutor.On("Execute", "xdg-open", []string{"http://localhost:8080"}).Return(nil)

err := openBrowser("http://localhost:8080", "ubuntu")
assert.NoError(t, err)
mockCmdExecutor.AssertExpectations(t)
}
49 changes: 0 additions & 49 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package main

import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"runtime"

"github.com/device-management-toolkit/go-wsman-messages/v2/pkg/security"

Expand Down Expand Up @@ -98,17 +95,6 @@ func handleDebugMode(cfg *config.Config, l logger.Interface) {
}
}

func launchBrowser(cfg *config.Config) {
scheme := "http"
if cfg.TLS.Enabled {
scheme = "https"
}

if err := openBrowser(scheme+"://localhost:"+cfg.Port, runtime.GOOS); err != nil {
panic(err)
}
}

func handleOpenAPIGeneration(l logger.Interface) {
usecases := usecase.Usecases{}

Expand Down Expand Up @@ -302,38 +288,3 @@ func handleKeyNotFound(toolkitCrypto security.Crypto, _, _ security.Storager) st

return toolkitCrypto.GenerateKey()
}

// CommandExecutor is an interface to allow for mocking exec.Command in tests.
type CommandExecutor interface {
Execute(name string, arg ...string) error
}

// RealCommandExecutor is a real implementation of CommandExecutor.
type RealCommandExecutor struct{}

func (e *RealCommandExecutor) Execute(name string, arg ...string) error {
return exec.CommandContext(context.Background(), name, arg...).Start()
}

// Global command executor, can be replaced in tests.
var cmdExecutor CommandExecutor = &RealCommandExecutor{}

func openBrowser(url, currentOS string) error {
var cmd string

var args []string

switch currentOS {
case "darwin":
cmd = "open"
args = []string{url}
case "windows":
cmd = "cmd"
args = []string{"/c", "start", url}
default:
cmd = "xdg-open"
args = []string{url}
}

return cmdExecutor.Execute(cmd, args...)
}
43 changes: 0 additions & 43 deletions cmd/app/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ import (
"github.com/device-management-toolkit/console/pkg/logger"
)

type MockCommandExecutor struct {
mock.Mock
}

func (m *MockCommandExecutor) Execute(name string, arg ...string) error {
args := m.Called(name, arg)

return args.Error(0)
}

func TestMainFunction(_ *testing.T) { //nolint:paralleltest // cannot have simultaneous tests modifying env variables.
os.Setenv("GIN_MODE", "debug")

Expand Down Expand Up @@ -54,39 +44,6 @@ func TestMainFunction(_ *testing.T) { //nolint:paralleltest // cannot have simul
main()
}

func TestOpenBrowserWindows(t *testing.T) { //nolint:paralleltest // cannot have simultaneous tests modifying executor.
mockCmdExecutor := new(MockCommandExecutor)
cmdExecutor = mockCmdExecutor

mockCmdExecutor.On("Execute", "cmd", []string{"/c", "start", "http://localhost:8080"}).Return(nil)

err := openBrowser("http://localhost:8080", "windows")
assert.NoError(t, err)
mockCmdExecutor.AssertExpectations(t)
}

func TestOpenBrowserDarwin(t *testing.T) { //nolint:paralleltest // cannot have simultaneous tests modifying executor.
mockCmdExecutor := new(MockCommandExecutor)
cmdExecutor = mockCmdExecutor

mockCmdExecutor.On("Execute", "open", []string{"http://localhost:8080"}).Return(nil)

err := openBrowser("http://localhost:8080", "darwin")
assert.NoError(t, err)
mockCmdExecutor.AssertExpectations(t)
}

func TestOpenBrowserLinux(t *testing.T) { //nolint:paralleltest // cannot have simultaneous tests modifying executor.
mockCmdExecutor := new(MockCommandExecutor)
cmdExecutor = mockCmdExecutor

mockCmdExecutor.On("Execute", "xdg-open", []string{"http://localhost:8080"}).Return(nil)

err := openBrowser("http://localhost:8080", "ubuntu")
assert.NoError(t, err)
mockCmdExecutor.AssertExpectations(t)
}

type MockGenerator struct {
mock.Mock
}
Expand Down
Loading