@@ -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
3031func TestHelp (t * testing.T ) {
@@ -84,9 +85,64 @@ func TestVersion(t *testing.T) {
8485 require .NotEmpty (t , jsonMap ["Commit" ])
8586}
8687
87- func TestInventoryCreation (t * testing.T ) {
88+ func TestLogOptions (t * testing.T ) {
8889 // Using version as a test command
90+ env := testsuite .NewEnvironment (t )
91+ defer env .CleanUp ()
8992
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
90146 env := testsuite .NewEnvironment (t )
91147 defer env .CleanUp ()
92148
@@ -96,13 +152,15 @@ func TestInventoryCreation(t *testing.T) {
96152 })
97153
98154 // no logs
99- stdout , _ , _ := cli .Run ("version" )
155+ stdout , _ , err := cli .Run ("version" )
156+ require .NoError (t , err )
100157 line := strings .TrimSpace (string (stdout ))
101158 outLines := strings .Split (line , "\n " )
102159 require .Len (t , outLines , 1 )
103160
104161 // parse inventory file
105162 inventoryFile := cli .DataDir ().Join ("inventory.yaml" )
106- stream , _ := inventoryFile .ReadFile ()
163+ stream , err := inventoryFile .ReadFile ()
164+ require .NoError (t , err )
107165 require .True (t , strings .Contains (string (stream ), "installation" ))
108166}
0 commit comments