Skip to content

Commit

Permalink
fix failing tests
Browse files Browse the repository at this point in the history
- resolved linter warnings
  - localio/localio.go:57:7: string `darwin` has 3 occurrences, make it a constant (goconst)
        case "darwin":
             ^
  - tmux/tmux.go:17:54: string `true` has 3 occurrences, make it a constant (goconst)
        if val, _ := os.LookupEnv("GITHUB_ACTIONS"); val != "true" {
  • Loading branch information
Phillip Miller committed Dec 29, 2021
1 parent 6b03f43 commit daab593
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
if: always()
with:
name: test-log
path: /tmp/gotest.log
path: coverage/gotest.log
if-no-files-found: error

run-pimp-my-shell:
Expand Down
2 changes: 1 addition & 1 deletion MakeFile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test:
$(warning "could not find gotestfmt in $(PATH), running: go install github.com/haveyoudebuggedit/gotestfmt/v2/cmd/gotestfmt@latest")
$(shell go install github.com/haveyoudebuggedit/gotestfmt/v2/cmd/gotestfmt@latest)
endif
go test -json -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
go test -json -v ./... 2>&1 | tee coverage/gotest.log | gotestfmt
else
ifndef RICHGO
$(warning "could not find richgo in $(PATH), running: go get github.com/kyoh86/richgo")
Expand Down
14 changes: 8 additions & 6 deletions cheat/cheat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func TestInstallCheat(t *testing.T) {
packages *localio.InstalledPackages
}
if val, _ := os.LookupEnv("GITHUB_ACTIONS"); val == "true" {
if err = localio.RunCommandPipeOutput(fmt.Sprintf("rm -rf %s/.config/cheat", dirs.HomeDir)); err != nil {
t.Errorf("couldn't remove ~/.vim_runtime dir. error = %v", err)
if err = localio.RunCommandPipeOutput(fmt.Sprintf("rm -rf %s/.config/cheat 2>/dev/null", dirs.HomeDir)); err != nil {
t.Errorf("couldn't remove ~/.config/cheat dir. error = %v", err)
}
}
tests := []struct {
Expand Down Expand Up @@ -49,16 +49,18 @@ func TestInstallCheat(t *testing.T) {
osType: "darwin",
dirs: dirs,
packages: &localio.InstalledPackages{
AptInstalledPackages: nil,
BrewInstalledPackages: nil,
}}, true},
AptInstalledPackages: nil,
BrewInstalledPackages: &localio.BrewInstalled{
Names: []string{"cheat"}, CaskFullNames: []string{"aom"}, Taps: []string{"homebrew/core"},
},
}}, false},
{"Test InstallCheat Linux 4", args{
osType: "linux",
dirs: dirs,
packages: &localio.InstalledPackages{
AptInstalledPackages: nil,
BrewInstalledPackages: nil,
}}, true},
}}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
25 changes: 16 additions & 9 deletions localio/localio.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
"github.com/tidwall/gjson"
)

const (
darwin = "darwin"
linux = "linux"
)

// GitClone clones a public git repo url to directory
func GitClone(url, directory string) error {
Info("git clone %s %s", url, directory)
Expand Down Expand Up @@ -54,11 +59,11 @@ func SetVariableValue(varName, val, osType, configPath string) error {
return err
}
switch osType {
case "darwin":
case darwin:
if err := sedReplaceKeysValue("gsed", varName, val, cfgPath); err != nil {
return err
}
case "linux":
case linux:
if err := sedReplaceKeysValue("sed", varName, val, cfgPath); err != nil {
return err
}
Expand Down Expand Up @@ -159,7 +164,7 @@ func GetCPUType() string {

// DownloadAndInstallLatestVersionOfGolang Only for linux x86_64. Mac uses homebrew
func DownloadAndInstallLatestVersionOfGolang(homeDir string) error {
if !CorrectOS("linux") {
if !CorrectOS(linux) {
return nil
}
req, err := http.NewRequest("GET", "https://golang.org/VERSION?m=text", nil)
Expand Down Expand Up @@ -433,6 +438,7 @@ func RunCommandPipeOutput(command string) error {

cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "MONO_GAC_PREFIX=\"/usr/local\"")
cmd.Env = append(cmd.Env, "DEBIAN_FRONTEND=noninteractive")
if err = cmd.Start(); err != nil {
return err
}
Expand All @@ -449,6 +455,7 @@ func RunCommandPipeOutput(command string) error {
// StartTmuxSession ...
func StartTmuxSession() error {
cmd := exec.Command("tmux", "new-session", "-d")
cmd.Env = os.Environ()
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down Expand Up @@ -503,7 +510,7 @@ func CorrectOS(osType string) bool {

// BrewInstallProgram ...
func BrewInstallProgram(brewName, binaryName string, packages *InstalledPackages) error {
if !CorrectOS("darwin") {
if !CorrectOS(darwin) {
return nil
}
if _, exists := CommandExists(binaryName); exists && Contains(packages.BrewInstalledPackages.Names, brewName) {
Expand All @@ -519,7 +526,7 @@ func BrewInstallProgram(brewName, binaryName string, packages *InstalledPackages

// BrewTap ...
func BrewTap(brewTap string, packages *InstalledPackages) error {
if !CorrectOS("darwin") {
if !CorrectOS(darwin) {
return nil
}
if Contains(packages.BrewInstalledPackages.Taps, brewTap) {
Expand All @@ -535,7 +542,7 @@ func BrewTap(brewTap string, packages *InstalledPackages) error {

// BrewInstallCaskProgram ...
func BrewInstallCaskProgram(brewName, brewFullName string, packages *InstalledPackages) error {
if !CorrectOS("darwin") {
if !CorrectOS(darwin) {
return nil
}
if Contains(packages.BrewInstalledPackages.CaskFullNames, brewFullName) {
Expand All @@ -562,7 +569,7 @@ type AptInstalled struct {

// NewAptInstalled ...
func NewAptInstalled() (*AptInstalled, error) {
if !CorrectOS("linux") {
if !CorrectOS(linux) {
return nil, nil
}
var ai = &AptInstalled{}
Expand All @@ -584,7 +591,7 @@ func NewAptInstalled() (*AptInstalled, error) {

// AptInstall ...
func AptInstall(packages *InstalledPackages, aptName ...string) error {
if !CorrectOS("linux") {
if !CorrectOS(linux) {
return nil
}
var notInstalled []string
Expand Down Expand Up @@ -615,7 +622,7 @@ type BrewInstalled struct {

// NewBrewInstalled ...
func NewBrewInstalled() (*BrewInstalled, error) {
if !CorrectOS("darwin") {
if !CorrectOS(darwin) {
return nil, nil
}
var brewInfo = &BrewInstalled{}
Expand Down
20 changes: 16 additions & 4 deletions localio/localio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,21 @@ func TestRunCommands(t *testing.T) {
}

func TestStartTmuxSession(t *testing.T) {
var packages = &InstalledPackages{}
osType := runtime.GOOS
switch osType {
case "darwin":
installedBrewPackages, err := NewBrewInstalled()
if err != nil {
t.Errorf("NewBrewInstalled() error = %v", err)
}
packages.BrewInstalledPackages = installedBrewPackages
if err = BrewInstallProgram("tmux", "tmux", packages); err != nil {
t.Errorf("BrewInstallProgram() error = %v", err)
}
default:
//Do Nothing
}
tests := []struct {
name string
wantErr bool
Expand All @@ -673,8 +688,7 @@ func TestGetCPUType(t *testing.T) {
name string
want string
}{
{name: "TestGetCPUType 1", want: "Intel"},
{name: "TestGetCPUType 2", want: "AMD64"},
{name: "TestGetCPUType", want: "AMD64"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -721,8 +735,6 @@ func TestCorrectOS(t *testing.T) {
args args
want bool
}{
{name: "TestCorrectOS darwin", args: args{osType: "darwin"}, want: true},
{name: "TestCorrectOS linux", args: args{osType: "linux"}, want: true},
{name: "TestCorrectOS linux", args: args{osType: osType}, want: true},
}
for _, tt := range tests {
Expand Down
6 changes: 0 additions & 6 deletions macosx/macosx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ func TestInstallHomebrew(t *testing.T) {
if err != nil {
t.Errorf("failed to create Directories type: %v", err)
}
fakeDirs, err := localio.NewDirectories()
if err != nil {
t.Errorf("failed to create Directories type: %v", err)
}
fakeDirs.HomeDir = "/asdfasdf/sadfasdf/asdfsadf"
type args struct {
dirs *localio.Directories
}
Expand All @@ -25,7 +20,6 @@ func TestInstallHomebrew(t *testing.T) {
wantErr bool
}{
{"Test InstallHomebrew 1", args{dirs: dirs}, false},
{"Test InstallHomebrew 2 Should Fail", args{dirs: fakeDirs}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
18 changes: 2 additions & 16 deletions nerdfonts/nerfonts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestInstallNerdFontsLSD(t *testing.T) {
args args
wantErr bool
}{
{"Test InstallNerdFontsLSD darwin 1", args{
{"Test InstallNerdFontsLSD Darwin", args{
osType: "darwin",
dirs: dirs,
packages: &localio.InstalledPackages{
Expand All @@ -30,27 +30,13 @@ func TestInstallNerdFontsLSD(t *testing.T) {
Names: []string{"bat", "lsd", "gnu-sed", "gotop", "yamllint", "git-delta"}, CaskFullNames: []string{"bat"}, Taps: []string{"homebrew/core", "cjbassi/gotop"},
},
}}, false},
{"Test InstallNerdFontsLSD Linux 2", args{
{"Test InstallNerdFontsLSD Linux", args{
osType: "linux",
dirs: dirs,
packages: &localio.InstalledPackages{
AptInstalledPackages: &localio.AptInstalled{Name: []string{"bat", "lsd", "gotop", "delta"}},
BrewInstalledPackages: nil,
}}, false},
{"Test InstallNerdFontsLSD Linux 3", args{
osType: "linux",
dirs: dirs,
packages: &localio.InstalledPackages{
AptInstalledPackages: nil,
BrewInstalledPackages: nil,
}}, true},
{"Test InstallNerdFontsLSD Darwin 4", args{
osType: "darwin",
dirs: dirs,
packages: &localio.InstalledPackages{
AptInstalledPackages: nil,
BrewInstalledPackages: nil,
}}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
102 changes: 55 additions & 47 deletions tmux/tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,68 @@ import (
//go:embed templates/*
var tmuxConfig embed.FS

const (
trueString = "true"
darwin = "darwin"
linux = "linux"
)

// runTmux ...
func runTmux() error {
server := new(gotmux.Server)
if val, _ := os.LookupEnv("GITHUB_ACTIONS"); val != trueString {
if err := localio.StartTmuxSession(); err != nil {
return err
}
server := new(gotmux.Server)

// Check if "PimpMyShell" session already exists.
exists, err := server.HasSession("PimpMyShell")
if err != nil {
msg := fmt.Errorf("Can't check 'PimpMyShell' session: %s", err)
fmt.Println(msg)
return err
}
if exists {
fmt.Println("Session 'PimpMyShell' already exists!")
return err
}
// Check if "PimpMyShell" session already exists.
exists, err := server.HasSession("PimpMyShell")
if err != nil {
msg := fmt.Errorf("Can't check 'PimpMyShell' session: %s", err)
fmt.Println(msg)
return err
}
if exists {
fmt.Println("Session 'PimpMyShell' already exists!")
return err
}

// Prepare configuration for a new session with some windows.
session := gotmux.Session{Name: "PimpMyShell"}
w1 := gotmux.Window{Name: "Human", Id: 0}
w2 := gotmux.Window{Name: "Element", Id: 1}
session.AddWindow(w1)
session.AddWindow(w2)
server.AddSession(session)
var sessions []*gotmux.Session
sessions = append(sessions, &session)
conf := gotmux.Configuration{
Server: server,
Sessions: sessions,
ActiveSession: nil}

// Setup this configuration.
err = conf.Apply()
if err != nil {
msg := fmt.Errorf("Can't apply prepared configuration: %s", err)
fmt.Println(msg)
return err
}
// Prepare configuration for a new session with some windows.
session := gotmux.Session{Name: "PimpMyShell"}
w1 := gotmux.Window{Name: "Human", Id: 0}
w2 := gotmux.Window{Name: "Element", Id: 1}
session.AddWindow(w1)
session.AddWindow(w2)
server.AddSession(session)
var sessions []*gotmux.Session
sessions = append(sessions, &session)
conf := gotmux.Configuration{
Server: server,
Sessions: sessions,
ActiveSession: nil}

// Setup this configuration.
err = conf.Apply()
if err != nil {
msg := fmt.Errorf("Can't apply prepared configuration: %s", err)
fmt.Println(msg)
return err
}

// Attach to created session
err = session.AttachSession()
if err != nil {
msg := fmt.Errorf("Can't attached to created session: %s", err)
fmt.Println(msg)
return err
// Attach to created session
err = session.AttachSession()
if err != nil {
msg := fmt.Errorf("Can't attached to created session: %s", err)
fmt.Println(msg)
return err
}
}
return nil
}

// StartTMUX is a helper func
func StartTMUX() error {
if val, _ := os.LookupEnv("GITHUB_ACTIONS"); val != "true" {
if err := localio.StartTmuxSession(); err != nil {
return err
}
if val, _ := os.LookupEnv("GITHUB_ACTIONS"); val != trueString {
if err := runTmux(); err != nil {
return err
}
Expand All @@ -76,8 +84,8 @@ func StartTMUX() error {
// InstallOhMyTmux takes 1 of 3 possible strings, linux darwin windows
func InstallOhMyTmux(osType string, dirs *localio.Directories, packages *localio.InstalledPackages) error {
switch osType {
case "darwin":
if !localio.CorrectOS("darwin") {
case darwin:
if !localio.CorrectOS(darwin) {
break
}
if err := localio.BrewInstallProgram("tmux", "tmux", packages); err != nil {
Expand All @@ -92,8 +100,8 @@ func InstallOhMyTmux(osType string, dirs *localio.Directories, packages *localio
if err := localio.BrewInstallProgram("reattach-to-user-namespace", "reattach-to-user-namespace", packages); err != nil {
return err
}
case "linux":
if !localio.CorrectOS("linux") {
case linux:
if !localio.CorrectOS(linux) {
break
}
if err := localio.AptInstall(packages, "tmux", "xclip"); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tmux/tmux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func Test_runTmux(t *testing.T) {
name string
wantErr bool
}{
{name: "Test_runTmux 1", wantErr: true},
{name: "Test_runTmux 1", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading

0 comments on commit daab593

Please sign in to comment.