diff --git a/.vscode/launch.json b/.vscode/launch.json index 6deb9a6..da4c0fe 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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", diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b79996f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "cSpell.words": [ + "godotenv", + "joho" + ] +} \ No newline at end of file diff --git a/go.mod b/go.mod index f398c0b..7908da1 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github-telegram-notify go 1.18 + +require github.com/joho/godotenv v1.4.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8c9f290 --- /dev/null +++ b/go.sum @@ -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= diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..e6eeb51 --- /dev/null +++ b/main_test.go @@ -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()) + } +}