@@ -25,6 +25,7 @@ import (
25
25
"github.com/stretchr/testify/require"
26
26
semver "go.bug.st/relaxed-semver"
27
27
"go.bug.st/testsuite"
28
+ "go.bug.st/testsuite/requirejson"
28
29
)
29
30
30
31
func TestHelp (t * testing.T ) {
@@ -69,8 +70,7 @@ func TestVersion(t *testing.T) {
69
70
// Checks if Application's value is arduino-cli
70
71
require .Equal (t , jsonMap ["Application" ], "arduino-cli" )
71
72
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
74
74
switch version := jsonMap ["VersionString" ]; version {
75
75
case "git-snapshot" :
76
76
require .Contains (t , version , "git-snapshot" )
@@ -85,9 +85,60 @@ func TestVersion(t *testing.T) {
85
85
require .NotEmpty (t , jsonMap ["Commit" ])
86
86
}
87
87
88
- func TestInventoryCreation (t * testing.T ) {
88
+ func TestLogOptions (t * testing.T ) {
89
89
// 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 )
90
103
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
91
142
env := testsuite .NewEnvironment (t )
92
143
defer env .CleanUp ()
93
144
0 commit comments