Skip to content

Commit 91add99

Browse files
Migrated TestLogOptions to main_test.go and deleted test_main.py
1 parent b1b228a commit 91add99

File tree

2 files changed

+54
-55
lines changed

2 files changed

+54
-55
lines changed

internal/integrationtest/main/main_test.go

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/stretchr/testify/require"
2626
semver "go.bug.st/relaxed-semver"
2727
"go.bug.st/testsuite"
28+
"go.bug.st/testsuite/requirejson"
2829
)
2930

3031
func TestHelp(t *testing.T) {
@@ -69,8 +70,7 @@ func TestVersion(t *testing.T) {
6970
// Checks if Application's value is arduino-cli
7071
require.Equal(t, jsonMap["Application"], "arduino-cli")
7172

72-
// Checks if VersionString's value is git-snapshot,
73-
// nightly or a valid semantic versioning
73+
// Checks if VersionString's value is git-snapshot, nightly or a valid semantic versioning
7474
switch version := jsonMap["VersionString"]; version {
7575
case "git-snapshot":
7676
require.Contains(t, version, "git-snapshot")
@@ -85,9 +85,60 @@ func TestVersion(t *testing.T) {
8585
require.NotEmpty(t, jsonMap["Commit"])
8686
}
8787

88-
func TestInventoryCreation(t *testing.T) {
88+
func TestLogOptions(t *testing.T) {
8989
// Using version as a test command
90+
env := testsuite.NewEnvironment(t)
91+
defer env.CleanUp()
92+
93+
cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{
94+
ArduinoCLIPath: paths.New("..", "..", "..", "arduino-cli"),
95+
UseSharedStagingFolder: true,
96+
})
97+
98+
// No logs
99+
stdout, _, _ := cli.Run("version")
100+
trimOut := strings.TrimSpace(string(stdout))
101+
outLines := strings.Split(trimOut, "\n")
102+
require.Len(t, outLines, 1)
90103

104+
// Plain text logs on stdout
105+
stdout, _, _ = cli.Run("version", "-v")
106+
trimOut = strings.TrimSpace(string(stdout))
107+
outLines = strings.Split(trimOut, "\n")
108+
require.Greater(t, len(outLines), 1)
109+
require.True(t, strings.HasPrefix(outLines[0], "\x1b[36mINFO\x1b[0m")) // account for the colors
110+
111+
// Plain text logs on file
112+
logFile := cli.DataDir().Join("log.txt")
113+
_, _, _ = cli.Run("version", "--log-file", logFile.String())
114+
lines, _ := logFile.ReadFileAsLines()
115+
require.True(t, strings.HasPrefix(lines[0], "time=\""))
116+
117+
// json on stdout
118+
stdout, _, _ = cli.Run("version", "-v", "--log-format", "JSON")
119+
trimOut = strings.TrimSpace(string(stdout))
120+
outLines = strings.Split(trimOut, "\n")
121+
requirejson.Contains(t, []byte(outLines[0]), `{ "level" }`)
122+
123+
// Check if log.json contains readable json in each line
124+
var v interface{}
125+
logFileJson := cli.DataDir().Join("log.json")
126+
_, _, _ = cli.Run("version", "--log-format", "JSON", "--log-file", logFileJson.String())
127+
fileContent, err := logFileJson.ReadFileAsLines()
128+
require.NoError(t, err)
129+
for _, line := range fileContent {
130+
// it seems that vscode has problems reading a stream of JSONs. In this case, the last line of the file is empty and
131+
// causes the check to fail
132+
if line == "" {
133+
continue
134+
}
135+
err = json.Unmarshal([]byte(line), &v)
136+
require.NoError(t, err)
137+
}
138+
}
139+
140+
func TestInventoryCreation(t *testing.T) {
141+
// Using version as a test command
91142
env := testsuite.NewEnvironment(t)
92143
defer env.CleanUp()
93144

test/test_main.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)