|
| 1 | +package store |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/lima-vm/lima/pkg/limayaml" |
| 9 | + "gotest.tools/v3/assert" |
| 10 | +) |
| 11 | + |
| 12 | +var instance Instance = Instance{ |
| 13 | + Name: "foo", |
| 14 | + Status: StatusStopped, |
| 15 | + VMType: limayaml.QEMU, |
| 16 | + Arch: limayaml.X8664, |
| 17 | + Dir: "dir", |
| 18 | +} |
| 19 | + |
| 20 | +var table string = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" + |
| 21 | + "foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B dir\n" |
| 22 | + |
| 23 | +// for width 60, everything is hidden |
| 24 | +var table60 string = "NAME STATUS SSH CPUS MEMORY DISK\n" + |
| 25 | + "foo Stopped 127.0.0.1:0 0 0B 0B\n" |
| 26 | + |
| 27 | +// for width 80, identical is hidden (type/arch) |
| 28 | +var table80 string = "NAME STATUS SSH CPUS MEMORY DISK DIR\n" + |
| 29 | + "foo Stopped 127.0.0.1:0 0 0B 0B dir\n" |
| 30 | + |
| 31 | +// for width 80, directory is hidden (if not identical) |
| 32 | +var tableTwo string = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK\n" + |
| 33 | + "foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B\n" + |
| 34 | + "bar Stopped 127.0.0.1:0 vz aarch64 0 0B 0B\n" |
| 35 | + |
| 36 | +var tableHome string = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" + |
| 37 | + "foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B ~/dir\n" |
| 38 | + |
| 39 | +func TestPrintInstanceTable(t *testing.T) { |
| 40 | + var buf bytes.Buffer |
| 41 | + instances := []*Instance{&instance} |
| 42 | + PrintInstances(&buf, instances, "table", nil) |
| 43 | + assert.Equal(t, buf.String(), table) |
| 44 | +} |
| 45 | + |
| 46 | +func TestPrintInstanceTable60(t *testing.T) { |
| 47 | + var buf bytes.Buffer |
| 48 | + instances := []*Instance{&instance} |
| 49 | + options := PrintOptions{IsTerminal: true, TerminalWidth: 60} |
| 50 | + PrintInstances(&buf, instances, "table", &options) |
| 51 | + assert.Equal(t, buf.String(), table60) |
| 52 | +} |
| 53 | + |
| 54 | +func TestPrintInstanceTable80(t *testing.T) { |
| 55 | + var buf bytes.Buffer |
| 56 | + instances := []*Instance{&instance} |
| 57 | + options := PrintOptions{IsTerminal: true, TerminalWidth: 80} |
| 58 | + PrintInstances(&buf, instances, "table", &options) |
| 59 | + assert.Equal(t, buf.String(), table80) |
| 60 | +} |
| 61 | + |
| 62 | +func TestPrintInstanceTable100(t *testing.T) { |
| 63 | + var buf bytes.Buffer |
| 64 | + instances := []*Instance{&instance} |
| 65 | + options := PrintOptions{IsTerminal: true, TerminalWidth: 100} |
| 66 | + PrintInstances(&buf, instances, "table", &options) |
| 67 | + assert.Equal(t, buf.String(), table) |
| 68 | +} |
| 69 | + |
| 70 | +func TestPrintInstanceTableAll(t *testing.T) { |
| 71 | + var buf bytes.Buffer |
| 72 | + instances := []*Instance{&instance} |
| 73 | + options := PrintOptions{IsTerminal: true, TerminalWidth: 40, AllFields: true} |
| 74 | + PrintInstances(&buf, instances, "table", &options) |
| 75 | + assert.Equal(t, buf.String(), table) |
| 76 | +} |
| 77 | + |
| 78 | +func TestPrintInstanceTableTwo(t *testing.T) { |
| 79 | + var buf bytes.Buffer |
| 80 | + instance1 := instance |
| 81 | + instance2 := instance |
| 82 | + instance2.Name = "bar" |
| 83 | + instance2.VMType = limayaml.VZ |
| 84 | + instance2.Arch = limayaml.AARCH64 |
| 85 | + instances := []*Instance{&instance1, &instance2} |
| 86 | + options := PrintOptions{IsTerminal: true, TerminalWidth: 80} |
| 87 | + PrintInstances(&buf, instances, "table", &options) |
| 88 | + assert.Equal(t, buf.String(), tableTwo) |
| 89 | +} |
| 90 | + |
| 91 | +func TestPrintInstanceTableHome(t *testing.T) { |
| 92 | + var buf bytes.Buffer |
| 93 | + homeDir, err := HomeDir() |
| 94 | + assert.NilError(t, err) |
| 95 | + instance1 := instance |
| 96 | + instance1.Dir = filepath.Join(homeDir, "dir") |
| 97 | + instances := []*Instance{&instance1} |
| 98 | + PrintInstances(&buf, instances, "table", nil) |
| 99 | + assert.Equal(t, buf.String(), tableHome) |
| 100 | +} |
0 commit comments