2023-06-21 15:28:15 +03:00
|
|
|
package cli_test
|
2022-12-30 20:10:56 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-21 15:28:15 +03:00
|
|
|
"encoding/json"
|
2022-12-30 20:10:56 +03:00
|
|
|
"os"
|
2023-06-21 15:28:15 +03:00
|
|
|
"runtime"
|
2022-12-30 20:10:56 +03:00
|
|
|
"testing"
|
|
|
|
|
2023-06-21 15:28:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/ioz"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/buildinfo"
|
|
|
|
"github.com/neilotoole/sq/cli/testrun"
|
|
|
|
|
2023-06-23 06:07:27 +03:00
|
|
|
"github.com/ecnepsnai/osquery"
|
2023-06-21 15:28:15 +03:00
|
|
|
|
2022-12-30 20:10:56 +03:00
|
|
|
"golang.org/x/mod/semver"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetVersionFromBrewFormula(t *testing.T) {
|
|
|
|
f, err := os.ReadFile("testdata/sq-0.20.0.rb")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-06-21 15:28:15 +03:00
|
|
|
vers, err := cli.GetVersionFromBrewFormula(f)
|
2022-12-30 20:10:56 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "0.20.0", vers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchBrewVersion(t *testing.T) {
|
2023-06-21 15:28:15 +03:00
|
|
|
latest, err := cli.FetchBrewVersion(context.Background())
|
2022-12-30 20:10:56 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, semver.IsValid("v"+latest))
|
|
|
|
}
|
2023-06-21 15:28:15 +03:00
|
|
|
|
|
|
|
func TestOSQuery(t *testing.T) {
|
|
|
|
info, err := osquery.Get()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Logf("%+v", info)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCmdVersion(t *testing.T) {
|
|
|
|
bi := buildinfo.Get()
|
|
|
|
ctx := context.Background()
|
|
|
|
tr := testrun.New(ctx, t, nil)
|
|
|
|
|
|
|
|
// --text
|
|
|
|
err := tr.Exec("version", "--text")
|
|
|
|
require.NoError(t, err)
|
|
|
|
text := tr.Out.String()
|
|
|
|
require.Contains(t, text, bi.Version)
|
|
|
|
|
|
|
|
tr = testrun.New(ctx, t, nil)
|
|
|
|
err = tr.Exec("version", "--text", "--verbose")
|
|
|
|
require.NoError(t, err)
|
|
|
|
text = tr.Out.String()
|
|
|
|
|
|
|
|
checkStringsFn := func(text string) {
|
|
|
|
require.Contains(t, text, bi.Version)
|
|
|
|
require.Contains(t, text, runtime.GOOS)
|
|
|
|
require.Contains(t, text, runtime.GOARCH)
|
|
|
|
}
|
|
|
|
checkStringsFn(text)
|
|
|
|
|
|
|
|
// --json
|
|
|
|
tr = testrun.New(ctx, t, nil)
|
|
|
|
err = tr.Exec("version", "--json")
|
|
|
|
require.NoError(t, err)
|
|
|
|
text = tr.Out.String()
|
|
|
|
checkStringsFn(text)
|
|
|
|
|
|
|
|
m := map[string]any{}
|
|
|
|
err = json.Unmarshal(tr.Out.Bytes(), &m)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, runtime.GOOS, m["host"].(map[string]any)["platform"])
|
|
|
|
|
|
|
|
// --yaml
|
|
|
|
tr = testrun.New(ctx, t, nil)
|
|
|
|
err = tr.Exec("version", "--yaml")
|
|
|
|
require.NoError(t, err)
|
|
|
|
text = tr.Out.String()
|
|
|
|
checkStringsFn(text)
|
|
|
|
|
|
|
|
m = map[string]any{}
|
|
|
|
err = ioz.UnmarshallYAML(tr.Out.Bytes(), &m)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, runtime.GOOS, m["host"].(map[string]any)["platform"])
|
|
|
|
}
|