@@ -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 ) {
@@ -55,8 +56,8 @@ func TestVersion(t *testing.T) {
55
56
// Run version and check the output message
56
57
stdout , stderr , err := cli .Run ("version" )
57
58
require .NoError (t , err )
58
- require .Contains (t , string (stdout ), "Version" )
59
- require .Contains (t , string (stdout ), "Commit" )
59
+ require .Contains (t , string (stdout ), "Version: " )
60
+ require .Contains (t , string (stdout ), "Commit: " )
60
61
require .Empty (t , stderr )
61
62
62
63
// Checks if "version --format json" has a json as an output
@@ -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,64 @@ 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 ()
90
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
91
146
env := testsuite .NewEnvironment (t )
92
147
defer env .CleanUp ()
93
148
@@ -97,13 +152,15 @@ func TestInventoryCreation(t *testing.T) {
97
152
})
98
153
99
154
// no logs
100
- stdout , _ , _ := cli .Run ("version" )
155
+ stdout , _ , err := cli .Run ("version" )
156
+ require .NoError (t , err )
101
157
line := strings .TrimSpace (string (stdout ))
102
158
outLines := strings .Split (line , "\n " )
103
159
require .Len (t , outLines , 1 )
104
160
105
161
// parse inventory file
106
162
inventoryFile := cli .DataDir ().Join ("inventory.yaml" )
107
- stream , _ := inventoryFile .ReadFile ()
163
+ stream , err := inventoryFile .ReadFile ()
164
+ require .NoError (t , err )
108
165
require .True (t , strings .Contains (string (stream ), "installation" ))
109
166
}
0 commit comments