Skip to content

Commit

Permalink
Added some tests for history repository
Browse files Browse the repository at this point in the history
  • Loading branch information
barthr committed Apr 25, 2022
1 parent 79a9ead commit dfc729f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
4 changes: 3 additions & 1 deletion repository/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repository
import (
"bufio"
"os"
"strings"
)

var (
Expand Down Expand Up @@ -33,7 +34,8 @@ func (repository *HistoryRepository) GetHistory() ([]string, error) {

var result []string
for fileScanner.Scan() {
result = append(result, fileScanner.Text())
parsedLine := strings.Split(fileScanner.Text(), ";")
result = append(result, parsedLine[len(parsedLine)-1])
}
// reverse array
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
Expand Down
34 changes: 34 additions & 0 deletions repository/history_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package repository

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetHistoryZsh(t *testing.T) {
InitHistoryRepository("testdata/zsh.txt")

repository := GetHistoryRepository()
history, err := repository.GetHistory()

assert.NoError(t, err)
assert.Len(t, history, 3)

assert.Contains(t, history, "htop")
assert.Contains(t, history, `echo "test"`)
assert.Contains(t, history, `docker system prune`)
}

func TestGetHistoryBash(t *testing.T) {
InitHistoryRepository("testdata/bash.txt")

repository := GetHistoryRepository()
history, err := repository.GetHistory()

assert.NoError(t, err)
assert.Len(t, history, 3)

assert.Contains(t, history, "htop")
assert.Contains(t, history, `echo "test"`)
assert.Contains(t, history, `docker system prune`)
}
3 changes: 3 additions & 0 deletions repository/testdata/bash.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
htop
echo "test"
docker system prune
3 changes: 3 additions & 0 deletions repository/testdata/zsh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
: 1650622819:0;htop
: 1650622898:0;echo "test"
: 1650622911:0;docker system prune
6 changes: 2 additions & 4 deletions ui/history_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"io"
"strings"
)

var (
Expand All @@ -18,12 +17,11 @@ var (
)

type HistoryItem struct {
Command string
Command string
}

func NewHistoryItem(historyEntry string) *HistoryItem {
historyParts := strings.Split(historyEntry, ";")
return &HistoryItem{Command: historyParts[len(historyParts)-1]}
return &HistoryItem{Command: historyEntry}
}

func (h HistoryItem) FilterValue() string {
Expand Down

0 comments on commit dfc729f

Please sign in to comment.