Skip to content

Commit

Permalink
action tests moved
Browse files Browse the repository at this point in the history
  • Loading branch information
dundee committed Mar 7, 2021
1 parent 3809cf4 commit bbf8b74
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 121 deletions.
122 changes: 122 additions & 0 deletions tui/actions_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,138 @@
package tui

import (
"runtime"
"testing"

"github.com/dundee/gdu/v4/analyze"
"github.com/dundee/gdu/v4/device"
"github.com/dundee/gdu/v4/internal/testanalyze"
"github.com/dundee/gdu/v4/internal/testapp"
"github.com/dundee/gdu/v4/internal/testdir"
"github.com/gdamore/tcell/v2"
"github.com/stretchr/testify/assert"
)

func TestShowDevices(t *testing.T) {
app, simScreen := testapp.CreateTestAppWithSimScreen(50, 50)
defer simScreen.Fini()

ui := CreateUI(app, true, true)
ui.ListDevices(getDevicesInfoMock())
ui.table.Draw(simScreen)
simScreen.Show()

b, _, _ := simScreen.GetContents()

text := []byte("Device name")
for i, r := range b[0:11] {
assert.Equal(t, text[i], r.Bytes[0])
}
}

func TestShowDevicesBW(t *testing.T) {
app, simScreen := testapp.CreateTestAppWithSimScreen(50, 50)
defer simScreen.Fini()

ui := CreateUI(app, false, false)
ui.ListDevices(getDevicesInfoMock())
ui.table.Draw(simScreen)
simScreen.Show()

b, _, _ := simScreen.GetContents()

text := []byte("Device name")
for i, r := range b[0:11] {
assert.Equal(t, text[i], r.Bytes[0])
}
}

func TestShowDevicesWithError(t *testing.T) {
if runtime.GOOS != "linux" {
return
}

app, simScreen := testapp.CreateTestAppWithSimScreen(50, 50)
defer simScreen.Fini()

getter := device.LinuxDevicesInfoGetter{MountsPath: "/xyzxyz"}

ui := CreateUI(app, false, false)
err := ui.ListDevices(getter)

assert.Contains(t, err.Error(), "no such file")
}

func TestDeviceSelected(t *testing.T) {
app := testapp.CreateMockedApp(false)
ui := CreateUI(app, true, true)
ui.analyzer = &testanalyze.MockedAnalyzer{}
ui.done = make(chan struct{})
ui.SetIgnoreDirPaths([]string{"/xxx"})
ui.ListDevices(getDevicesInfoMock())

assert.Equal(t, 3, ui.table.GetRowCount())

ui.deviceItemSelected(1, 0)

<-ui.done // wait for analyzer

assert.Equal(t, "test_dir", ui.currentDir.Name)

for _, f := range ui.app.(*testapp.MockedApp).UpdateDraws {
f()
}

assert.Equal(t, 5, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "/..")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "aaa")
}

func TestAnalyzePath(t *testing.T) {
ui := getAnalyzedPathMockedApp(t, true, true, true)

assert.Equal(t, 5, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "/..")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "aaa")
}

func TestAnalyzePathBW(t *testing.T) {
ui := getAnalyzedPathMockedApp(t, false, true, true)

assert.Equal(t, 5, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "/..")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "aaa")
}

func TestAnalyzePathWithParentDir(t *testing.T) {
parentDir := &analyze.Dir{
File: &analyze.File{
Name: "parent",
},
Files: make([]analyze.Item, 0, 1),
}

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, false, true)
ui.analyzer = &testanalyze.MockedAnalyzer{}
ui.topDir = parentDir
ui.done = make(chan struct{})
ui.AnalyzePath("test_dir", parentDir)

<-ui.done // wait for analyzer

assert.Equal(t, "test_dir", ui.currentDir.Name)
assert.Equal(t, parentDir, ui.currentDir.Parent)

for _, f := range ui.app.(*testapp.MockedApp).UpdateDraws {
f()
}

assert.Equal(t, 5, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "/..")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "aaa")
}

func TestViewDirContents(t *testing.T) {
app := testapp.CreateMockedApp(true)
ui := CreateUI(app, false, true)
Expand Down
121 changes: 0 additions & 121 deletions tui/tui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tui

import (
"errors"
"runtime"
"testing"

"github.com/dundee/gdu/v4/analyze"
Expand Down Expand Up @@ -110,81 +109,6 @@ func TestHelpBw(t *testing.T) {
}
}

func TestShowDevices(t *testing.T) {
app, simScreen := testapp.CreateTestAppWithSimScreen(50, 50)
defer simScreen.Fini()

ui := CreateUI(app, true, true)
ui.ListDevices(getDevicesInfoMock())
ui.table.Draw(simScreen)
simScreen.Show()

b, _, _ := simScreen.GetContents()

text := []byte("Device name")
for i, r := range b[0:11] {
assert.Equal(t, text[i], r.Bytes[0])
}
}

func TestShowDevicesBW(t *testing.T) {
app, simScreen := testapp.CreateTestAppWithSimScreen(50, 50)
defer simScreen.Fini()

ui := CreateUI(app, false, false)
ui.ListDevices(getDevicesInfoMock())
ui.table.Draw(simScreen)
simScreen.Show()

b, _, _ := simScreen.GetContents()

text := []byte("Device name")
for i, r := range b[0:11] {
assert.Equal(t, text[i], r.Bytes[0])
}
}

func TestShowDevicesWithError(t *testing.T) {
if runtime.GOOS != "linux" {
return
}

app, simScreen := testapp.CreateTestAppWithSimScreen(50, 50)
defer simScreen.Fini()

getter := device.LinuxDevicesInfoGetter{MountsPath: "/xyzxyz"}

ui := CreateUI(app, false, false)
err := ui.ListDevices(getter)

assert.Contains(t, err.Error(), "no such file")
}

func TestDeviceSelected(t *testing.T) {
app := testapp.CreateMockedApp(false)
ui := CreateUI(app, true, true)
ui.analyzer = &testanalyze.MockedAnalyzer{}
ui.done = make(chan struct{})
ui.SetIgnoreDirPaths([]string{"/xxx"})
ui.ListDevices(getDevicesInfoMock())

assert.Equal(t, 3, ui.table.GetRowCount())

ui.deviceItemSelected(1, 0)

<-ui.done // wait for analyzer

assert.Equal(t, "test_dir", ui.currentDir.Name)

for _, f := range ui.app.(*testapp.MockedApp).UpdateDraws {
f()
}

assert.Equal(t, 5, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "/..")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "aaa")
}

func TestAppRun(t *testing.T) {
app := testapp.CreateMockedApp(false)
ui := CreateUI(app, false, true)
Expand All @@ -203,51 +127,6 @@ func TestAppRunWithErr(t *testing.T) {
assert.Equal(t, "Fail", err.Error())
}

func TestAnalyzePath(t *testing.T) {
ui := getAnalyzedPathMockedApp(t, true, true, true)

assert.Equal(t, 5, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "/..")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "aaa")
}

func TestAnalyzePathBW(t *testing.T) {
ui := getAnalyzedPathMockedApp(t, false, true, true)

assert.Equal(t, 5, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "/..")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "aaa")
}

func TestAnalyzePathWithParentDir(t *testing.T) {
parentDir := &analyze.Dir{
File: &analyze.File{
Name: "parent",
},
Files: make([]analyze.Item, 0, 1),
}

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, false, true)
ui.analyzer = &testanalyze.MockedAnalyzer{}
ui.topDir = parentDir
ui.done = make(chan struct{})
ui.AnalyzePath("test_dir", parentDir)

<-ui.done // wait for analyzer

assert.Equal(t, "test_dir", ui.currentDir.Name)
assert.Equal(t, parentDir, ui.currentDir.Parent)

for _, f := range ui.app.(*testapp.MockedApp).UpdateDraws {
f()
}

assert.Equal(t, 5, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "/..")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "aaa")
}

func TestRescanDir(t *testing.T) {
parentDir := &analyze.Dir{
File: &analyze.File{
Expand Down

0 comments on commit bbf8b74

Please sign in to comment.