Skip to content

Commit

Permalink
Merge pull request #4 from ivanilves/shell-coverage
Browse files Browse the repository at this point in the history
test(pkg/shell): add test coverage
  • Loading branch information
ivanilves authored Oct 15, 2022
2 parents 7c8a5b2 + 3ca0067 commit 102edd1
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml → .github/workflows/golang.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Go
name: Golang

on:
push:
Expand All @@ -15,7 +15,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.19

- name: Build
run: go build -v ./...
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ main: dep build
all: dep build install

dep:
go mod tidy
go mod vendor

build:
Expand Down
12 changes: 9 additions & 3 deletions cmd/ttg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main
import (
"flag"
"log"
"os"

"github.com/ivanilves/ttg/pkg/directory"
"github.com/ivanilves/ttg/pkg/file"
"github.com/ivanilves/ttg/pkg/filter"
"github.com/ivanilves/ttg/pkg/menu"
"github.com/ivanilves/ttg/pkg/scm"
"github.com/ivanilves/ttg/pkg/shell"
"github.com/ivanilves/ttg/pkg/terminal"
)

var appVersion = "default"
Expand Down Expand Up @@ -38,7 +40,9 @@ func main() {
flag.Parse()

if version {
shell.PrintAndExit(appVersion)
println(appVersion)

os.Exit(0)
}

matches := flag.Args()
Expand All @@ -59,16 +63,18 @@ func main() {
log.Fatalf("invalid filter: %s", err.Error())
}

selected, err := menu.Build(filter.Apply(entries, matches))
selected, err := menu.Build(filter.Apply(entries, matches), terminal.Height())

if err != nil {
log.Fatalf("failed to build menu: %s", err.Error())
}

if outFile != "" {
if err := file.WriteAndExit(outFile, entries[selected]); err != nil {
if err := file.Write(outFile, entries[selected]); err != nil {
log.Fatalf("failed to write output file: %s", err.Error())
}

os.Exit(0)
}

shell.Spawn(entries[selected])
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-git/go-git/v5 v5.4.2
github.com/manifoldco/promptui v0.9.0
github.com/stretchr/testify v1.8.0
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
)

require (
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9w
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
6 changes: 1 addition & 5 deletions pkg/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import (
"os"
)

func WriteAndExit(outFile string, path string) error {
func Write(outFile string, path string) error {
err := os.WriteFile(outFile, []byte(path), 0644)

if err == nil {
os.Exit(0)
}

return err
}
22 changes: 20 additions & 2 deletions pkg/menu/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,26 @@ import (

const label = "Select Terragrunt project to travel"

// Overhead shows how many lines are occupied by menu control elements
const Overhead = 3

// MinSize stands for minimal menu size
const MinSize = 5

func getSize(itemCount int, size int) int {
if itemCount <= size {
return itemCount
}

if size <= MinSize {
return MinSize
}

return size
}

// Build creates an interactive menu to chose Terragrunt project from
func Build(items []string) (selected string, err error) {
func Build(items []string, maxSize int) (selected string, err error) {
if len(items) == 0 {
return "", fmt.Errorf("no items")
}
Expand All @@ -26,7 +44,7 @@ func Build(items []string) (selected string, err error) {
prompt := promptui.Select{
Label: label,
Items: items,
Size: len(items),
Size: getSize(len(items), maxSize-Overhead),
Searcher: searcher,
}

Expand Down
18 changes: 10 additions & 8 deletions pkg/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ import (
"os"
"os/exec"
"strconv"
"strings"
)

const defaultShell = "bash"

func detectShell() string {
shell := os.Getenv("SHELL")

if len(shell) != 0 {
if len(shell) == 0 {
return defaultShell
}

if strings.HasSuffix(shell, "/bash") || strings.HasSuffix(shell, "/zsh") {
return shell
}

if shell == "/bin/true" || shell == "/bin/false" {
return shell
}

Expand Down Expand Up @@ -51,10 +60,3 @@ func Spawn(path string) error {

return cmd.Run()
}

// PrintAndExit prints a string passed and exits after
func PrintAndExit(s string) {
fmt.Printf("%s\n", s)

os.Exit(0)
}
86 changes: 86 additions & 0 deletions pkg/shell/shell_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package shell

import (
"os"

"testing"

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

func TestDetectShell(t *testing.T) {
assert := assert.New(t)

cases := map[string]string{
"/bin/bash": "/bin/bash",
"/bin/zsh": "/bin/zsh",
"/bin/yolo": defaultShell,
"": defaultShell,
}

for input, expected := range cases {
os.Setenv("SHELL", input)

assert.Equal(expected, detectShell())
}
}

func TestName(t *testing.T) {
assert := assert.New(t)

assert.Equal(os.Args[0], Name())
}

func TestIsRunningInside(t *testing.T) {
assert := assert.New(t)

cases := map[string]bool{
"true": true,
"false": false,
"whatever": false,
}

for input, expected := range cases {
os.Setenv("TTG", input)

assert.Equal(expected, IsRunningInside())
}

os.Setenv("TTG", "")

assert.Equal(false, IsRunningInside())
}

func TestGetppid(t *testing.T) {
assert := assert.New(t)

cases := map[string]int{
"42424242": 42424242,
"xyz": 0,
}

for input, expected := range cases {
os.Setenv("TTG_PID", input)

assert.Equal(expected, Getppid())
}

os.Setenv("TTG_PID", "")

assert.Equal(0, Getppid())
}

func TestSpawn(t *testing.T) {
assert := assert.New(t)

cases := map[string]bool{
"/bin/true": true,
"/bin/false": false,
}

for input, expected := range cases {
os.Setenv("SHELL", input)

assert.Equal(expected, Spawn("/") == nil)
}
}
18 changes: 18 additions & 0 deletions pkg/terminal/terminal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package terminal

import (
"golang.org/x/term"
)

const defautHeight = 20

// Height reveals a number of lines we have in our terminal
func Height() int {
_, h, err := term.GetSize(0)

if err != nil {
return defautHeight
}

return h
}

0 comments on commit 102edd1

Please sign in to comment.