@@ -48,14 +48,18 @@ import (
4848 "testing"
4949 "unsafe"
5050
51+ "github.com/rokath/trice/internal/decoder"
52+ "github.com/rokath/trice/internal/trexDecoder"
53+ "github.com/rokath/trice/pkg/cipher"
5154 "github.com/rokath/trice/pkg/msg"
5255 "github.com/spf13/afero"
5356 "github.com/tj/assert"
5457)
5558
5659var (
57- triceDir string // triceDir holds the trice directory path.
58- testLines = - 1 // testLines is the common number of tested lines in triceCheck. The value -1 is for all lines, what takes time.
60+ triceDir string // triceDir holds the trice directory path.
61+ testLines = - 1 // testLines is the common number of tested lines in triceCheck. The value -1 is for all lines, what takes time.
62+ g globalDefaults // g holds global vars default values
5963)
6064
6165// https://stackoverflow.com/questions/23847003/golang-tests-and-working-directory
@@ -187,7 +191,7 @@ func triceLogTest(t *testing.T, triceLog logF, limit int) {
187191
188192// triceLogTest2 works like triceLogTest but additionally expects doubled output: direct and deferred.
189193func triceLogTest2 (t * testing.T , triceLog0 , triceLog1 logF , limit int ) {
190-
194+ g . GetGlobalVars () // read changed defaults
191195 osFSys := & afero.Afero {Fs : afero .NewOsFs ()}
192196
193197 // CopyFileIntoFSys(t, mmFSys, "til.json", osFSys, td+"./til.json") // needed for the trice log
@@ -213,6 +217,7 @@ func triceLogTest2(t *testing.T, triceLog0, triceLog1 logF, limit int) {
213217 buf := fmt .Sprint (bin )
214218 buffer := buf [1 : len (buf )- 1 ]
215219
220+ g .SetGlobalVars () // restore changed defaults
216221 act := triceLog0 (t , osFSys , buffer )
217222 triceClearOutBuffer ()
218223
@@ -228,10 +233,34 @@ func triceLogTest2(t *testing.T, triceLog0, triceLog1 logF, limit int) {
228233 buf := fmt .Sprint (bin )
229234 buffer := buf [1 : len (buf )- 1 ]
230235
236+ g .SetGlobalVars () // restore changed defaults
231237 act := triceLog1 (t , osFSys , buffer )
232238 triceClearOutBuffer ()
233239
234240 assert .Equal (t , r .exps , strings .TrimSuffix (act , "\n " ))
235241 }
236242 }
237243}
244+
245+ type globalDefaults struct {
246+ defaultPassword string
247+ defaultPackageFraming string
248+ defaultDoubled16BitID bool
249+ }
250+
251+ // Keep default values of global variables.
252+ func (p * globalDefaults ) GetGlobalVars () {
253+ p .defaultPassword = cipher .Password
254+ p .defaultPackageFraming = decoder .PackageFraming
255+ p .defaultDoubled16BitID = trexDecoder .Doubled16BitID
256+ }
257+
258+ // SetGlobalVars sets all global variables in a definitive state.
259+ // In Go, each package generates an individual test binary and they are tested parallel.
260+ // All package tests are executed sequentially but use the same global variables.
261+ // Therefore we have to reset the global variables in each test function.
262+ func (p * globalDefaults ) SetGlobalVars () {
263+ cipher .Password = p .defaultPassword
264+ decoder .PackageFraming = p .defaultPackageFraming
265+ trexDecoder .Doubled16BitID = p .defaultDoubled16BitID
266+ }
0 commit comments