Skip to content

Commit ce582cc

Browse files
ADD: unit test for commit message testing
1 parent 2b2602c commit ce582cc

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

.vscode/launch.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"name": "Test",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "test",
12+
"program": "${workspaceFolder}",
13+
},
714
{
815
"name": "Launch Package",
916
"type": "go",

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cSpell.words": [
3+
"godotenv",
4+
"joho"
5+
]
6+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github-telegram-notify
22

33
go 1.18
4+
5+
require github.com/joho/godotenv v1.4.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
2+
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

main_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"github-telegram-notify/types"
6+
"github-telegram-notify/utils"
7+
"io/ioutil"
8+
"os"
9+
"testing"
10+
11+
dotenv "github.com/joho/godotenv"
12+
)
13+
14+
func loadEnvs(t *testing.T) (string, string) {
15+
err := dotenv.Load()
16+
if err != nil {
17+
t.Fatal("Error loading .env file")
18+
}
19+
tg_token := os.Getenv("BOT_TOKEN")
20+
if tg_token == "" {
21+
t.Fatal("Bot token not specified in .env file")
22+
}
23+
chatID := os.Getenv("CHAT_ID")
24+
if chatID == "" {
25+
t.Fatal("Chat ID not specified in .env file")
26+
}
27+
return tg_token, chatID
28+
}
29+
30+
func parse(t *testing.T, rawData []byte) (string, string, string) {
31+
var gitEvent *types.Metadata
32+
err := json.Unmarshal([]byte(rawData), &gitEvent)
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
37+
text, markupText, markupUrl, err := utils.CreateContents(gitEvent)
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
return text, markupText, markupUrl
42+
}
43+
44+
func TestCommitMessage(t *testing.T) {
45+
token, chatID := loadEnvs(t)
46+
data, err := ioutil.ReadFile("events/commit.json")
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
text, markupText, markupUrl := parse(t, data)
51+
error := utils.SendMessage(token, chatID, text, markupText, markupUrl)
52+
if error.Description != "" {
53+
t.Fatal(error.String())
54+
}
55+
}

0 commit comments

Comments
 (0)