@@ -47,9 +47,9 @@ type AcFactory struct {
4747//
4848// If the test mail server has not standard configuration, you should set the custom configuration
4949// here.
50- func (self * AcFactory ) TearUp () {
51- if self .DefaultCfg == nil {
52- self .DefaultCfg = map [string ]option.Option [string ]{
50+ func (factory * AcFactory ) TearUp () {
51+ if factory .DefaultCfg == nil {
52+ factory .DefaultCfg = map [string ]option.Option [string ]{
5353 "mail_server" : option .Some ("localhost" ),
5454 "send_server" : option .Some ("localhost" ),
5555 "mail_port" : option .Some ("3143" ),
@@ -60,41 +60,43 @@ func (self *AcFactory) TearUp() {
6060 }
6161
6262 }
63- self .startTime = time .Now ().Unix ()
63+ factory .startTime = time .Now ().Unix ()
6464
6565 dir , err := os .MkdirTemp ("" , "" )
6666 if err != nil {
6767 panic (err )
6868 }
69- self .tempDir = dir
69+ factory .tempDir = dir
7070
71- self .tearUp = true
71+ factory .tearUp = true
7272}
7373
7474// Do cleanup, removing temporary directories and files created by the configured test accounts.
7575// Usually TearDown() is called with defer immediately after the creation of the AcFactory instance.
76- func (self * AcFactory ) TearDown () {
77- self .ensureTearUp ()
78- os .RemoveAll (self .tempDir )
76+ func (factory * AcFactory ) TearDown () {
77+ factory .ensureTearUp ()
78+ if err := os .RemoveAll (factory .tempDir ); err != nil {
79+ panic (err )
80+ }
7981}
8082
8183// MkdirTemp creates a new temporary directory. The directory is automatically removed on TearDown().
82- func (self * AcFactory ) MkdirTemp () string {
83- dir , err := os .MkdirTemp (self .tempDir , "" )
84+ func (factory * AcFactory ) MkdirTemp () string {
85+ dir , err := os .MkdirTemp (factory .tempDir , "" )
8486 if err != nil {
8587 panic (err )
8688 }
8789 return dir
8890}
8991
9092// Call the given function passing a new Rpc as parameter.
91- func (self * AcFactory ) WithRpc (callback func (* Rpc )) {
92- self .ensureTearUp ()
93+ func (factory * AcFactory ) WithRpc (callback func (* Rpc )) {
94+ factory .ensureTearUp ()
9395 trans := transport .NewIOTransport ()
94- if ! self .Debug {
96+ if ! factory .Debug {
9597 trans .Stderr = nil
9698 }
97- dir := self .MkdirTemp ()
99+ dir := factory .MkdirTemp ()
98100 trans .AccountsDir = filepath .Join (dir , "accounts" )
99101 err := trans .Open ()
100102 if err != nil {
@@ -106,25 +108,25 @@ func (self *AcFactory) WithRpc(callback func(*Rpc)) {
106108}
107109
108110// Get a new Account that is not yet configured, but it is ready to be configured.
109- func (self * AcFactory ) WithUnconfiguredAccount (callback func (* Rpc , AccountId )) {
110- self .WithRpc (func (rpc * Rpc ) {
111+ func (factory * AcFactory ) WithUnconfiguredAccount (callback func (* Rpc , AccountId )) {
112+ factory .WithRpc (func (rpc * Rpc ) {
111113 accId , err := rpc .AddAccount ()
112114 if err != nil {
113115 panic (err )
114116 }
115- self .serialMutex .Lock ()
116- self .serial ++
117- serial := self .serial
118- self .serialMutex .Unlock ()
117+ factory .serialMutex .Lock ()
118+ factory .serial ++
119+ serial := factory .serial
120+ factory .serialMutex .Unlock ()
119121
120- if len (self .DefaultCfg ) != 0 {
121- err = rpc .BatchSetConfig (accId , self .DefaultCfg )
122+ if len (factory .DefaultCfg ) != 0 {
123+ err = rpc .BatchSetConfig (accId , factory .DefaultCfg )
122124 if err != nil {
123125 panic (err )
124126 }
125127 }
126128 err = rpc .BatchSetConfig (accId , map [string ]option.Option [string ]{
127- "addr" : option .Some (fmt .Sprintf ("acc%v.%v@localhost" , serial , self .startTime )),
129+ "addr" : option .Some (fmt .Sprintf ("acc%v.%v@localhost" , serial , factory .startTime )),
128130 "mail_pw" : option .Some (fmt .Sprintf ("password%v" , serial )),
129131 })
130132 if err != nil {
@@ -136,8 +138,8 @@ func (self *AcFactory) WithUnconfiguredAccount(callback func(*Rpc, AccountId)) {
136138}
137139
138140// Get a new account configured and with I/O already started.
139- func (self * AcFactory ) WithOnlineAccount (callback func (* Rpc , AccountId )) {
140- self .WithUnconfiguredAccount (func (rpc * Rpc , accId AccountId ) {
141+ func (factory * AcFactory ) WithOnlineAccount (callback func (* Rpc , AccountId )) {
142+ factory .WithUnconfiguredAccount (func (rpc * Rpc , accId AccountId ) {
141143 err := rpc .Configure (accId )
142144 if err != nil {
143145 panic (err )
@@ -148,16 +150,16 @@ func (self *AcFactory) WithOnlineAccount(callback func(*Rpc, AccountId)) {
148150}
149151
150152// Get a new bot not yet configured, but its account is ready to be configured.
151- func (self * AcFactory ) WithUnconfiguredBot (callback func (* Bot , AccountId )) {
152- self .WithUnconfiguredAccount (func (rpc * Rpc , accId AccountId ) {
153+ func (factory * AcFactory ) WithUnconfiguredBot (callback func (* Bot , AccountId )) {
154+ factory .WithUnconfiguredAccount (func (rpc * Rpc , accId AccountId ) {
153155 bot := NewBot (rpc )
154156 callback (bot , accId )
155157 })
156158}
157159
158160// Get a new bot configured and with its account I/O already started. The bot is not running yet.
159- func (self * AcFactory ) WithOnlineBot (callback func (* Bot , AccountId )) {
160- self .WithUnconfiguredAccount (func (rpc * Rpc , accId AccountId ) {
161+ func (factory * AcFactory ) WithOnlineBot (callback func (* Bot , AccountId )) {
162+ factory .WithUnconfiguredAccount (func (rpc * Rpc , accId AccountId ) {
161163 addr , _ := rpc .GetConfig (accId , "addr" )
162164 pass , _ := rpc .GetConfig (accId , "mail_pw" )
163165 bot := NewBot (rpc )
@@ -172,14 +174,11 @@ func (self *AcFactory) WithOnlineBot(callback func(*Bot, AccountId)) {
172174
173175// Get a new bot configured and already listening to new events/messages.
174176// It is ensured that Bot.IsRunning() is true for the returned bot.
175- func (self * AcFactory ) WithRunningBot (callback func (* Bot , AccountId )) {
176- self .WithOnlineBot (func (bot * Bot , accId AccountId ) {
177+ func (factory * AcFactory ) WithRunningBot (callback func (* Bot , AccountId )) {
178+ factory .WithOnlineBot (func (bot * Bot , accId AccountId ) {
177179 var err error
178180 go func () { err = bot .Run () }()
179- for {
180- if bot .IsRunning () {
181- break
182- }
181+ for ! bot .IsRunning () {
183182 if err != nil {
184183 panic (err )
185184 }
@@ -190,8 +189,8 @@ func (self *AcFactory) WithRunningBot(callback func(*Bot, AccountId)) {
190189}
191190
192191// Wait for the next incoming message in the given account.
193- func (self * AcFactory ) NextMsg (rpc * Rpc , accId AccountId ) * MsgSnapshot {
194- event := self .WaitForEvent (rpc , accId , EventIncomingMsg {}).(EventIncomingMsg )
192+ func (factory * AcFactory ) NextMsg (rpc * Rpc , accId AccountId ) * MsgSnapshot {
193+ event := factory .WaitForEvent (rpc , accId , EventIncomingMsg {}).(EventIncomingMsg )
195194 msg , err := rpc .GetMessage (accId , event .MsgId )
196195 if err != nil {
197196 panic (err )
@@ -200,14 +199,14 @@ func (self *AcFactory) NextMsg(rpc *Rpc, accId AccountId) *MsgSnapshot {
200199}
201200
202201// Introduce two accounts to each other creating a 1:1 chat between them and exchanging messages.
203- func (self * AcFactory ) IntroduceEachOther (rpc1 * Rpc , accId1 AccountId , rpc2 * Rpc , accId2 AccountId ) {
204- chatId := self .CreateChat (rpc1 , accId1 , rpc2 , accId2 )
202+ func (factory * AcFactory ) IntroduceEachOther (rpc1 * Rpc , accId1 AccountId , rpc2 * Rpc , accId2 AccountId ) {
203+ chatId := factory .CreateChat (rpc1 , accId1 , rpc2 , accId2 )
205204 _ , err := rpc1 .MiscSendTextMessage (accId1 , chatId , "hi" )
206205 if err != nil {
207206 panic (err )
208207 }
209- self .WaitForEventInChat (rpc1 , accId1 , chatId , EventMsgsChanged {})
210- snapshot := self .NextMsg (rpc2 , accId2 )
208+ factory .WaitForEventInChat (rpc1 , accId1 , chatId , EventMsgsChanged {})
209+ snapshot := factory .NextMsg (rpc2 , accId2 )
211210 if snapshot .Text != "hi" {
212211 panic ("unexpected message: " + snapshot .Text )
213212 }
@@ -220,15 +219,15 @@ func (self *AcFactory) IntroduceEachOther(rpc1 *Rpc, accId1 AccountId, rpc2 *Rpc
220219 if err != nil {
221220 panic (err )
222221 }
223- self .WaitForEventInChat (rpc2 , accId2 , snapshot .ChatId , EventMsgsChanged {})
224- snapshot = self .NextMsg (rpc1 , accId1 )
222+ factory .WaitForEventInChat (rpc2 , accId2 , snapshot .ChatId , EventMsgsChanged {})
223+ snapshot = factory .NextMsg (rpc1 , accId1 )
225224 if snapshot .Text != "hello" {
226225 panic ("unexpected message: " + snapshot .Text )
227226 }
228227}
229228
230229// Create a 1:1 chat with accId2 in the chatlist of accId1.
231- func (self * AcFactory ) CreateChat (rpc1 * Rpc , accId1 AccountId , rpc2 * Rpc , accId2 AccountId ) ChatId {
230+ func (factory * AcFactory ) CreateChat (rpc1 * Rpc , accId1 AccountId , rpc2 * Rpc , accId2 AccountId ) ChatId {
232231 addr2 , err := rpc2 .GetConfig (accId2 , "configured_addr" )
233232 if err != nil {
234233 panic (err )
@@ -248,9 +247,9 @@ func (self *AcFactory) CreateChat(rpc1 *Rpc, accId1 AccountId, rpc2 *Rpc, accId2
248247}
249248
250249// Get a path to an image file that can be used for testing.
251- func (self * AcFactory ) TestImage () string {
250+ func (factory * AcFactory ) TestImage () string {
252251 var img string
253- self .WithOnlineAccount (func (rpc * Rpc , accId AccountId ) {
252+ factory .WithOnlineAccount (func (rpc * Rpc , accId AccountId ) {
254253 chatId , err := rpc .CreateChatByContactId (accId , ContactSelf )
255254 if err != nil {
256255 panic (err )
@@ -265,17 +264,26 @@ func (self *AcFactory) TestImage() string {
265264}
266265
267266// Get a path to a Webxdc file that can be used for testing.
268- func (self * AcFactory ) TestWebxdc () string {
269- self .ensureTearUp ()
270- dir := self .MkdirTemp ()
267+ func (factory * AcFactory ) TestWebxdc () string {
268+ factory .ensureTearUp ()
269+ dir := factory .MkdirTemp ()
271270 path := filepath .Join (dir , "test.xdc" )
272271 zipFile , err := os .Create (path )
273272 if err != nil {
274273 panic (err )
275274 }
276- defer zipFile .Close ()
275+ defer func () {
276+ if err := zipFile .Close (); err != nil {
277+ panic (err )
278+ }
279+ }()
280+
277281 writer := zip .NewWriter (zipFile )
278- defer writer .Close ()
282+ defer func () {
283+ if err := writer .Close (); err != nil {
284+ panic (err )
285+ }
286+ }()
279287
280288 var files = []struct {
281289 Name , Body string
@@ -304,17 +312,17 @@ func (self *AcFactory) TestWebxdc() string {
304312
305313// Wait for an event of the same type as the given event, the event must belong to the chat
306314// with the given ChatId.
307- func (self * AcFactory ) WaitForEventInChat (rpc * Rpc , accId AccountId , chatId ChatId , event Event ) Event {
315+ func (factory * AcFactory ) WaitForEventInChat (rpc * Rpc , accId AccountId , chatId ChatId , event Event ) Event {
308316 for {
309- event = self .WaitForEvent (rpc , accId , event )
317+ event = factory .WaitForEvent (rpc , accId , event )
310318 if getChatId (event ) == chatId {
311319 return event
312320 }
313321 }
314322}
315323
316324// Wait for an event of the same type as the given event.
317- func (self * AcFactory ) WaitForEvent (rpc * Rpc , accId AccountId , event Event ) Event {
325+ func (factory * AcFactory ) WaitForEvent (rpc * Rpc , accId AccountId , event Event ) Event {
318326 for {
319327 accId2 , ev , err := rpc .GetNextEvent ()
320328 if err != nil {
@@ -325,19 +333,19 @@ func (self *AcFactory) WaitForEvent(rpc *Rpc, accId AccountId, event Event) Even
325333 continue
326334 }
327335 if ev .eventType () == event .eventType () {
328- if self .Debug {
336+ if factory .Debug {
329337 fmt .Printf ("Got awaited event %v\n " , ev .eventType ())
330338 }
331339 return ev
332340 }
333- if self .Debug {
341+ if factory .Debug {
334342 fmt .Printf ("Waiting for event %v, got: %v\n " , event .eventType (), ev .eventType ())
335343 }
336344 }
337345}
338346
339- func (self * AcFactory ) ensureTearUp () {
340- if ! self .tearUp {
347+ func (factory * AcFactory ) ensureTearUp () {
348+ if ! factory .tearUp {
341349 panic ("TearUp() required" )
342350 }
343351}
0 commit comments