-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hide identical columns and add unit tests
Signed-off-by: Anders F Björklund <[email protected]>
- Loading branch information
1 parent
0839624
commit d9aab45
Showing
2 changed files
with
133 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package store | ||
|
||
import ( | ||
"bytes" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/lima-vm/lima/pkg/limayaml" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
var instance Instance = Instance{ | ||
Name: "foo", | ||
Status: StatusStopped, | ||
VMType: limayaml.QEMU, | ||
Arch: limayaml.X8664, | ||
Dir: "dir", | ||
} | ||
|
||
var table string = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" + | ||
"foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B dir\n" | ||
|
||
// for width 60, everything is hidden | ||
var table60 string = "NAME STATUS SSH CPUS MEMORY DISK\n" + | ||
"foo Stopped 127.0.0.1:0 0 0B 0B\n" | ||
|
||
// for width 80, identical is hidden (type/arch) | ||
var table80 string = "NAME STATUS SSH CPUS MEMORY DISK DIR\n" + | ||
"foo Stopped 127.0.0.1:0 0 0B 0B dir\n" | ||
|
||
// for width 80, directory is hidden (if not identical) | ||
var tableTwo string = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK\n" + | ||
"foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B\n" + | ||
"bar Stopped 127.0.0.1:0 vz aarch64 0 0B 0B\n" | ||
|
||
const separator = string(filepath.Separator) | ||
|
||
var tableHome string = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" + | ||
"foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B ~" + separator + "dir\n" | ||
|
||
func TestPrintInstanceTable(t *testing.T) { | ||
var buf bytes.Buffer | ||
instances := []*Instance{&instance} | ||
PrintInstances(&buf, instances, "table", nil) | ||
assert.Equal(t, buf.String(), table) | ||
} | ||
|
||
func TestPrintInstanceTable60(t *testing.T) { | ||
var buf bytes.Buffer | ||
instances := []*Instance{&instance} | ||
options := PrintOptions{IsTerminal: true, TerminalWidth: 60} | ||
PrintInstances(&buf, instances, "table", &options) | ||
assert.Equal(t, buf.String(), table60) | ||
} | ||
|
||
func TestPrintInstanceTable80(t *testing.T) { | ||
var buf bytes.Buffer | ||
instances := []*Instance{&instance} | ||
options := PrintOptions{IsTerminal: true, TerminalWidth: 80} | ||
PrintInstances(&buf, instances, "table", &options) | ||
assert.Equal(t, buf.String(), table80) | ||
} | ||
|
||
func TestPrintInstanceTable100(t *testing.T) { | ||
var buf bytes.Buffer | ||
instances := []*Instance{&instance} | ||
options := PrintOptions{IsTerminal: true, TerminalWidth: 100} | ||
PrintInstances(&buf, instances, "table", &options) | ||
assert.Equal(t, buf.String(), table) | ||
} | ||
|
||
func TestPrintInstanceTableAll(t *testing.T) { | ||
var buf bytes.Buffer | ||
instances := []*Instance{&instance} | ||
options := PrintOptions{IsTerminal: true, TerminalWidth: 40, AllFields: true} | ||
PrintInstances(&buf, instances, "table", &options) | ||
assert.Equal(t, buf.String(), table) | ||
} | ||
|
||
func TestPrintInstanceTableTwo(t *testing.T) { | ||
var buf bytes.Buffer | ||
instance1 := instance | ||
instance2 := instance | ||
instance2.Name = "bar" | ||
instance2.VMType = limayaml.VZ | ||
instance2.Arch = limayaml.AARCH64 | ||
instances := []*Instance{&instance1, &instance2} | ||
options := PrintOptions{IsTerminal: true, TerminalWidth: 80} | ||
PrintInstances(&buf, instances, "table", &options) | ||
assert.Equal(t, buf.String(), tableTwo) | ||
} | ||
|
||
func TestPrintInstanceTableHome(t *testing.T) { | ||
var buf bytes.Buffer | ||
homeDir, err := HomeDir() | ||
assert.NilError(t, err) | ||
instance1 := instance | ||
instance1.Dir = filepath.Join(homeDir, "dir") | ||
instances := []*Instance{&instance1} | ||
PrintInstances(&buf, instances, "table", nil) | ||
assert.Equal(t, buf.String(), tableHome) | ||
} |