Skip to content

Commit

Permalink
ADD: unit test for commit message testing
Browse files Browse the repository at this point in the history
  • Loading branch information
EverythingSuckz committed Oct 16, 2022
1 parent 2b2602c commit ce582cc
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Test",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
},
{
"name": "Launch Package",
"type": "go",
Expand Down
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cSpell.words": [
"godotenv",
"joho"
]
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github-telegram-notify

go 1.18

require github.com/joho/godotenv v1.4.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
55 changes: 55 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"encoding/json"
"github-telegram-notify/types"
"github-telegram-notify/utils"
"io/ioutil"
"os"
"testing"

dotenv "github.com/joho/godotenv"
)

func loadEnvs(t *testing.T) (string, string) {
err := dotenv.Load()
if err != nil {
t.Fatal("Error loading .env file")
}
tg_token := os.Getenv("BOT_TOKEN")
if tg_token == "" {
t.Fatal("Bot token not specified in .env file")
}
chatID := os.Getenv("CHAT_ID")
if chatID == "" {
t.Fatal("Chat ID not specified in .env file")
}
return tg_token, chatID
}

func parse(t *testing.T, rawData []byte) (string, string, string) {
var gitEvent *types.Metadata
err := json.Unmarshal([]byte(rawData), &gitEvent)
if err != nil {
t.Fatal(err)
}

text, markupText, markupUrl, err := utils.CreateContents(gitEvent)
if err != nil {
t.Fatal(err)
}
return text, markupText, markupUrl
}

func TestCommitMessage(t *testing.T) {
token, chatID := loadEnvs(t)
data, err := ioutil.ReadFile("events/commit.json")
if err != nil {
t.Fatal(err)
}
text, markupText, markupUrl := parse(t, data)
error := utils.SendMessage(token, chatID, text, markupText, markupUrl)
if error.Description != "" {
t.Fatal(error.String())
}
}

0 comments on commit ce582cc

Please sign in to comment.