Skip to content

Commit a30a158

Browse files
committed
Added test and fixed formatting.
1 parent 18aada4 commit a30a158

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

Diff for: internal/cli/monitor/monitor.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"os"
2525
"sort"
2626
"strings"
27-
"time"
27+
"time"
2828

2929
"github.com/arduino/arduino-cli/commands/monitor"
3030
"github.com/arduino/arduino-cli/configuration"
@@ -250,33 +250,33 @@ func contains(s []string, searchterm string) bool {
250250
}
251251

252252
type timeStampWriter struct {
253-
writer io.Writer
253+
writer io.Writer
254254
sendTimeStampNext bool
255255
}
256256

257257
func newTimeStampWriter(writer io.Writer) *timeStampWriter {
258258
return &timeStampWriter{
259-
writer: writer,
259+
writer: writer,
260260
sendTimeStampNext: true,
261261
}
262262
}
263263

264264
func (t *timeStampWriter) Write(p []byte) (int, error) {
265265
written := 0
266266
for _, b := range p {
267-
if (t.sendTimeStampNext) {
267+
if t.sendTimeStampNext {
268268
_, err := t.writer.Write([]byte(time.Now().Format("[2006-01-02 15:04:05] ")))
269269
if err != nil {
270270
return written, err
271271
}
272-
t.sendTimeStampNext = false;
272+
t.sendTimeStampNext = false
273273
}
274274
n, err := t.writer.Write([]byte{b})
275275
written += n
276276
if err != nil {
277277
return written, err
278278
}
279-
t.sendTimeStampNext = b == '\n';
279+
t.sendTimeStampNext = b == '\n'
280280
}
281281
return written, nil
282282
}

Diff for: internal/cli/monitor/monitor_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2023 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package monitor
17+
18+
import (
19+
"bytes"
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestTimeStampWriter(t *testing.T) {
26+
buf := &bytes.Buffer{}
27+
writer := newTimeStampWriter(buf)
28+
29+
writer.Write([]byte("foo"))
30+
// The first received bytes get a timestamp prepended
31+
require.Regexp(t, `^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] foo$`, buf)
32+
33+
buf.Reset()
34+
writer.Write([]byte("\nbar\n"))
35+
// A timestamp should be inserted before the first char of the next line
36+
require.Regexp(t, "^\n"+`\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] bar` + "\n$", buf)
37+
}

0 commit comments

Comments
 (0)