refactor: format shell name better

This commit is contained in:
Jan De Dobbeleer 2020-09-24 19:11:56 +02:00 committed by Jan De Dobbeleer
parent f478255bbf
commit 6c17689966
5 changed files with 14 additions and 76 deletions

View file

@ -26,7 +26,7 @@ type environmentInfo interface {
lastErrorCode() int
getArgs() *args
getBatteryInfo() (*battery.Battery, error)
getParentProcess() (ps.Process, error)
getShellName() string
}
type environment struct {
@ -95,9 +95,15 @@ func (env *environment) getBatteryInfo() (*battery.Battery, error) {
return battery.Get(0)
}
func (env *environment) getParentProcess() (ps.Process, error) {
func (env *environment) getShellName() string {
pid := os.Getppid()
return ps.FindProcess(pid)
p, err := ps.FindProcess(pid)
if err != nil {
return "unknown"
}
shell := strings.Replace(p.Executable(), ".exe", "", 1)
return strings.Trim(shell, " ")
}
func cleanHostName(hostName string) string {

View file

@ -41,11 +41,7 @@ func main() {
colorWriter := &Renderer{
Buffer: new(bytes.Buffer),
}
var shell string
if parentProcess, err := env.getParentProcess(); err != nil {
shell = parentProcess.Executable()
}
colorWriter.init(shell)
colorWriter.init(env.getShellName())
engine := &engine{
settings: settings,
env: env,

View file

@ -7,7 +7,6 @@ import (
"testing"
"github.com/distatus/battery"
ps "github.com/mitchellh/go-ps"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
@ -81,9 +80,9 @@ func (env *MockedEnvironment) getBatteryInfo() (*battery.Battery, error) {
return args.Get(0).(*battery.Battery), args.Error(1)
}
func (env *MockedEnvironment) getParentProcess() (ps.Process, error) {
func (env *MockedEnvironment) getShellName() string {
args := env.Called(nil)
return args.Get(0).(ps.Process), args.Error(1)
return args.String(0)
}
func TestWorkingDir(t *testing.T) {

View file

@ -1,7 +1,5 @@
package main
import "strings"
type shell struct {
props *properties
env environmentInfo
@ -12,13 +10,7 @@ func (s *shell) enabled() bool {
}
func (s *shell) string() string {
p, err := s.env.getParentProcess()
if err != nil {
return "unknown"
}
shell := strings.Replace(p.Executable(), ".exe", "", 1)
return shell
return s.env.getShellName()
}
func (s *shell) init(props *properties, env environmentInfo) {

View file

@ -1,42 +1,15 @@
package main
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type process struct {
mock.Mock
}
// Pid is the process ID for this process.
func (p *process) Pid() int {
args := p.Called(nil)
return args.Int(0)
}
// PPid is the parent process ID for this process.
func (p *process) PPid() int {
args := p.Called(nil)
return args.Int(0)
}
// Executable name running this process. This is not a path to the
// executable.
func (p *process) Executable() string {
args := p.Called(nil)
return args.String(0)
}
func TestWriteCurrentShell(t *testing.T) {
expected := "zsh"
env := new(MockedEnvironment)
process := new(process)
process.On("Executable", nil).Return(expected)
env.On("getParentProcess", nil).Return(process, nil)
env.On("getShellName", nil).Return(expected, nil)
props := &properties{}
s := &shell{
env: env,
@ -44,31 +17,3 @@ func TestWriteCurrentShell(t *testing.T) {
}
assert.Equal(t, expected, s.string())
}
func TestWriteCurrentShellWindowsExe(t *testing.T) {
expected := "pwsh"
env := new(MockedEnvironment)
process := new(process)
parentProcess := expected + ".exe"
process.On("Executable", nil).Return(parentProcess)
env.On("getParentProcess", nil).Return(process, nil)
props := &properties{}
s := &shell{
env: env,
props: props,
}
assert.Equal(t, expected, s.string())
}
func TestWriteCurrentShellError(t *testing.T) {
err := errors.New("Oh no, what shell is this?")
env := new(MockedEnvironment)
process := new(process)
env.On("getParentProcess", nil).Return(process, err)
props := &properties{}
s := &shell{
env: env,
props: props,
}
assert.Equal(t, "unknown", s.string())
}