@@ -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 ) {
@@ -84,9 +85,64 @@ func TestVersion(t *testing.T) {
84
85
require .NotEmpty (t , jsonMap ["Commit" ])
85
86
}
86
87
87
- func TestInventoryCreation (t * testing.T ) {
88
+ func TestLogOptions (t * testing.T ) {
88
89
// Using version as a test command
90
+ env := testsuite .NewEnvironment (t )
91
+ defer env .CleanUp ()
89
92
93
+ cli := integrationtest .NewArduinoCliWithinEnvironment (env , & integrationtest.ArduinoCLIConfig {
94
+ ArduinoCLIPath : paths .New (".." , ".." , ".." , "arduino-cli" ),
95
+ UseSharedStagingFolder : true ,
96
+ })
97
+
98
+ // No logs
99
+ stdout , _ , err := cli .Run ("version" )
100
+ require .NoError (t , err )
101
+ trimOut := strings .TrimSpace (string (stdout ))
102
+ outLines := strings .Split (trimOut , "\n " )
103
+ require .Len (t , outLines , 1 )
104
+
105
+ // Plain text logs on stdout
106
+ stdout , _ , err = cli .Run ("version" , "-v" )
107
+ require .NoError (t , err )
108
+ trimOut = strings .TrimSpace (string (stdout ))
109
+ outLines = strings .Split (trimOut , "\n " )
110
+ require .Greater (t , len (outLines ), 1 )
111
+ require .True (t , strings .HasPrefix (outLines [0 ], "\x1b [36mINFO\x1b [0m" )) // account for the colors
112
+
113
+ // Plain text logs on file
114
+ logFile := cli .DataDir ().Join ("log.txt" )
115
+ _ , _ , err = cli .Run ("version" , "--log-file" , logFile .String ())
116
+ require .NoError (t , err )
117
+ lines , _ := logFile .ReadFileAsLines ()
118
+ require .True (t , strings .HasPrefix (lines [0 ], "time=\" " ))
119
+
120
+ // json on stdout
121
+ stdout , _ , err = cli .Run ("version" , "-v" , "--log-format" , "JSON" )
122
+ require .NoError (t , err )
123
+ trimOut = strings .TrimSpace (string (stdout ))
124
+ outLines = strings .Split (trimOut , "\n " )
125
+ requirejson .Contains (t , []byte (outLines [0 ]), `{ "level" }` )
126
+
127
+ // Check if log.json contains readable json in each line
128
+ var v interface {}
129
+ logFileJson := cli .DataDir ().Join ("log.json" )
130
+ _ , _ , err = cli .Run ("version" , "--log-format" , "JSON" , "--log-file" , logFileJson .String ())
131
+ require .NoError (t , err )
132
+ fileContent , err := logFileJson .ReadFileAsLines ()
133
+ require .NoError (t , err )
134
+ for _ , line := range fileContent {
135
+ // exclude empty lines since they are not valid json
136
+ if line == "" {
137
+ continue
138
+ }
139
+ err = json .Unmarshal ([]byte (line ), & v )
140
+ require .NoError (t , err )
141
+ }
142
+ }
143
+
144
+ func TestInventoryCreation (t * testing.T ) {
145
+ // Using version as a test command
90
146
env := testsuite .NewEnvironment (t )
91
147
defer env .CleanUp ()
92
148
@@ -96,13 +152,15 @@ func TestInventoryCreation(t *testing.T) {
96
152
})
97
153
98
154
// no logs
99
- stdout , _ , _ := cli .Run ("version" )
155
+ stdout , _ , err := cli .Run ("version" )
156
+ require .NoError (t , err )
100
157
line := strings .TrimSpace (string (stdout ))
101
158
outLines := strings .Split (line , "\n " )
102
159
require .Len (t , outLines , 1 )
103
160
104
161
// parse inventory file
105
162
inventoryFile := cli .DataDir ().Join ("inventory.yaml" )
106
- stream , _ := inventoryFile .ReadFile ()
163
+ stream , err := inventoryFile .ReadFile ()
164
+ require .NoError (t , err )
107
165
require .True (t , strings .Contains (string (stream ), "installation" ))
108
166
}
0 commit comments