Skip to content

Commit

Permalink
Add tests for status command
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano committed Jan 14, 2022
1 parent 951242d commit b719f91
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func print(p *status.PackageStatus, w io.Writer) error {
}

bold.Fprintln(w, "Package Versions:")
table := tablewriter.NewWriter(os.Stdout)
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"Environment", "Version", "Title", "Description"})
table.SetHeaderColor(
twColor(tablewriter.Colors{tablewriter.Bold}),
Expand Down
113 changes: 113 additions & 0 deletions cmd/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package cmd

import (
"bytes"
"flag"
"io/ioutil"
"testing"

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

"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/packages/status"
)

var generateFlag = flag.Bool("generate", false, "Write golden files")

func fooPackage(version string) packages.PackageManifest {
return packages.PackageManifest{
Name: "foo",
Version: version,
Title: "Foo",
Description: "Foo integration",
}
}

func TestStatusFormatAndPrint(t *testing.T) {
cases := []struct {
title string
pkgStatus *status.PackageStatus
expected string
}{
{
title: "no versions",
pkgStatus: &status.PackageStatus{Name: "foo"},
expected: "./testdata/status-no-versions",
},
{
title: "version-one-stage",
pkgStatus: &status.PackageStatus{
Production: []packages.PackageManifest{
fooPackage("1.0.0"),
},
},
expected: "./testdata/status-version-one-stage",
},
{
title: "some versions",
pkgStatus: &status.PackageStatus{
Production: []packages.PackageManifest{
fooPackage("1.0.0"),
},
Staging: []packages.PackageManifest{
fooPackage("1.1.0-beta1"),
},
Snapshot: []packages.PackageManifest{
fooPackage("2.0.0-rc1"),
},
},
expected: "./testdata/status-some-versions",
},
{
title: "multiple versions in stage",
pkgStatus: &status.PackageStatus{
Production: []packages.PackageManifest{
fooPackage("1.0.0"),
fooPackage("1.0.1"),
fooPackage("1.0.2"),
},
Staging: []packages.PackageManifest{
fooPackage("1.0.0"),
fooPackage("1.0.1"),
fooPackage("1.0.2"),
fooPackage("1.1.0-beta1"),
},
Snapshot: []packages.PackageManifest{
fooPackage("1.0.0"),
fooPackage("1.0.1"),
fooPackage("1.0.2"),
fooPackage("1.1.0-beta1"),
fooPackage("2.0.0-rc1"),
},
},
expected: "./testdata/status-multiple-versions-in-stage",
},
}

for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
var buf bytes.Buffer
err := print(c.pkgStatus, &buf)
require.NoError(t, err)

assertOutputWithFile(t, c.expected, buf.String())
})
}
}

func assertOutputWithFile(t *testing.T, path string, out string) {
if *generateFlag {
err := ioutil.WriteFile(path, []byte(out), 0644)
require.NoError(t, err)
}

d, err := ioutil.ReadFile(path)
require.NoError(t, err)

assert.Equal(t, string(d), out)
}
13 changes: 13 additions & 0 deletions cmd/testdata/status-multiple-versions-in-stage
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Package:
Package Versions:
+-------------+--------------------------------+-------+-----------------+
| ENVIRONMENT | VERSION | TITLE | DESCRIPTION |
+-------------+--------------------------------+-------+-----------------+
| Snapshot | 2.0.0-rc1 (1.0.0, 1.0.1, | Foo | Foo integration |
| | 1.0.2, 1.1.0-beta1) | | |
+-------------+--------------------------------+-------+-----------------+
| Staging | 1.1.0-beta1 (1.0.0, 1.0.1, | Foo | Foo integration |
| | 1.0.2) | | |
+-------------+--------------------------------+-------+-----------------+
| Production | 1.0.2 (1.0.0, 1.0.1) | Foo | Foo integration |
+-------------+--------------------------------+-------+-----------------+
11 changes: 11 additions & 0 deletions cmd/testdata/status-no-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Package: foo
Package Versions:
+-------------+---------+-------+-------------+
| ENVIRONMENT | VERSION | TITLE | DESCRIPTION |
+-------------+---------+-------+-------------+
| Snapshot | - | - | - |
+-------------+---------+-------+-------------+
| Staging | - | - | - |
+-------------+---------+-------+-------------+
| Production | - | - | - |
+-------------+---------+-------+-------------+
11 changes: 11 additions & 0 deletions cmd/testdata/status-some-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Package:
Package Versions:
+-------------+-------------+-------+-----------------+
| ENVIRONMENT | VERSION | TITLE | DESCRIPTION |
+-------------+-------------+-------+-----------------+
| Snapshot | 2.0.0-rc1 | Foo | Foo integration |
+-------------+-------------+-------+-----------------+
| Staging | 1.1.0-beta1 | Foo | Foo integration |
+-------------+-------------+-------+-----------------+
| Production | 1.0.0 | Foo | Foo integration |
+-------------+-------------+-------+-----------------+
11 changes: 11 additions & 0 deletions cmd/testdata/status-version-one-stage
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Package:
Package Versions:
+-------------+---------+-------+-----------------+
| ENVIRONMENT | VERSION | TITLE | DESCRIPTION |
+-------------+---------+-------+-----------------+
| Snapshot | - | - | - |
+-------------+---------+-------+-----------------+
| Staging | - | - | - |
+-------------+---------+-------+-----------------+
| Production | 1.0.0 | Foo | Foo integration |
+-------------+---------+-------+-----------------+

0 comments on commit b719f91

Please sign in to comment.