Skip to content

Commit 7acb3b9

Browse files
committed
Add test for setupHiddenBot
1 parent 1398ef0 commit 7acb3b9

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

troll_shield.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,24 +196,26 @@ func setupBot(envVar string) (*telegram.BotAPI, error) {
196196
return bot, nil
197197
}
198198

199-
func setupBots() (*telegram.BotAPI, *telegram.BotAPI, error) {
200-
var bot, botHidden *telegram.BotAPI
201-
var err error
202-
203-
log.Println("Setup the main bot")
204-
bot, err = setupBot("TELEGRAM_BOT_TOKEN")
205-
if err != nil {
206-
return nil, nil, err
207-
}
208-
199+
func setupHiddenBot(bot *telegram.BotAPI) *telegram.BotAPI {
209200
log.Println("Setup the hidden bot")
210-
botHidden, err = setupBot("TELEGRAM_BOT_HIDDEN_TOKEN")
201+
botHidden, err := setupBot("TELEGRAM_BOT_HIDDEN_TOKEN")
211202
if err != nil {
212203
log.Printf("Bot setup failed: %v. Fallback to main bot.", err)
213204
botHidden = bot
214205
}
215206

216-
return bot, botHidden, nil
207+
return botHidden
208+
209+
}
210+
211+
func setupBots() (*telegram.BotAPI, *telegram.BotAPI, error) {
212+
log.Println("Setup the main bot")
213+
bot, err := setupBot("TELEGRAM_BOT_TOKEN")
214+
if err != nil {
215+
return nil, nil, err
216+
}
217+
218+
return bot, setupHiddenBot(bot), nil
217219
}
218220

219221
func leaveChat(bot TrollShieldBot, update *telegram.Update, trollGroup string) {

troll_shield_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,14 @@ func TestSetupBot(t *testing.T) {
174174
}
175175

176176
func TestSetupBots(t *testing.T) {
177-
if _, _, err := setupBots(); err == nil {
177+
bot, _, err := setupBots()
178+
if err == nil {
178179
t.Errorf("setupBots fail with invalid tokens.")
179180
}
181+
botHidden := setupHiddenBot(bot)
182+
if botHidden != bot {
183+
t.Errorf("When botHidden fails to start, use bot as fallback")
184+
}
180185
}
181186

182187
func TestSetupLogging(t *testing.T) {

0 commit comments

Comments
 (0)