Skip to content

Commit 4d1a1b7

Browse files
committed
Cleanup, unit testing example, and automated unit testing
1 parent 308c192 commit 4d1a1b7

File tree

12 files changed

+124
-77
lines changed

12 files changed

+124
-77
lines changed

.github/workflows/go.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ jobs:
3333
- name: Build
3434
run: make build
3535

36+
- name: Test
37+
run: make test
38+
3639
build_windows:
3740
runs-on: windows-latest
3841
steps:
@@ -45,5 +48,11 @@ jobs:
4548
with:
4649
go-version: '1.23'
4750

48-
- name: Check & Build
49-
run: .\build.bat
51+
- name: Check
52+
run: .\build.bat check
53+
54+
- name: Build
55+
run: .\build.bat build
56+
57+
- name: Test
58+
run: .\build.bat test

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@ check:
1515
build: ./main.go
1616
go build -o $(EXEC_NAME) ./main.go
1717

18+
test:
19+
go test ./...
20+
1821
clean: $(EXEC_NAME)
1922
rm $(EXEC_NAME)

build.bat

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
@echo off
2-
32
::vars
43
set EXEC_NAME=api-tools.exe
54

6-
::setup
5+
::simulate makefile-like branching
6+
set skip=1
7+
if "%1"=="setup" goto %1
8+
if "%1"=="check" goto %1
9+
if "%1"=="build" goto %1
10+
if "%1"=="test" goto %1
11+
set skip=0
12+
13+
:setup
714
echo Performing setup...
815
go install honnef.co/go/tools/cmd/staticcheck@latest && ^
916
go install golang.org/x/tools/cmd/goimports@latest
1017
if ERRORLEVEL 1 exit /b %ERRORLEVEL% :: fail if error occurred
1118
echo Setup done!
19+
if %skip%==1 exit
1220
echo[
1321

14-
::checks
22+
:check
1523
echo Performing checks...
1624
go mod tidy && ^
1725
go vet ./... && ^
@@ -20,10 +28,21 @@ gofmt -w ./.. && ^
2028
goimports -w ./..
2129
if ERRORLEVEL 1 exit /b %ERRORLEVEL% :: fail if error occurred
2230
echo Checks done!
31+
if %skip%==1 exit
2332
echo[
2433

25-
::build
34+
:build
2635
echo Building...
2736
go build -o %EXEC_NAME% ./main.go
2837
if ERRORLEVEL 1 exit /b %ERRORLEVEL% :: fail if error occurred
29-
echo Build complete!
38+
echo Build complete!
39+
if %skip%==1 exit
40+
echo[
41+
42+
:test
43+
echo Testing...
44+
go test ./...
45+
if ERRORLEVEL 1 exit /b %ERRORLEVEL% :: fail if error occurred
46+
echo Testing complete!
47+
if %skip%==1 exit
48+
echo[

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ import (
1111
"github.com/UTDNebula/api-tools/scrapers"
1212
"github.com/UTDNebula/api-tools/uploader"
1313
"github.com/UTDNebula/api-tools/utils"
14+
"github.com/joho/godotenv"
1415
)
1516

1617
func main() {
18+
// Load environment variables
19+
godotenv.Load()
1720

1821
// Setup flags
1922

scrapers/astra.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,12 @@ import (
1313
"time"
1414

1515
"github.com/UTDNebula/api-tools/utils"
16-
"github.com/joho/godotenv"
1716
"github.com/valyala/fastjson"
1817
)
1918

2019
var MAX_EVENTS_PER_DAY = 5000
2120

2221
func ScrapeAstra(outDir string) {
23-
24-
// Load env vars
25-
if err := godotenv.Load(); err != nil {
26-
log.Panic("Error loading .env file")
27-
}
28-
2922
// Start chromedp
3023
chromedpCtx, cancel := utils.InitChromeDp()
3124

scrapers/coursebook.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,9 @@ import (
1414
"time"
1515

1616
"github.com/UTDNebula/api-tools/utils"
17-
"github.com/joho/godotenv"
1817
)
1918

2019
func ScrapeCoursebook(term string, startPrefix string, outDir string) {
21-
22-
// Load env vars
23-
if err := godotenv.Load(); err != nil {
24-
log.Panic("Error loading .env file")
25-
}
26-
2720
// Start chromedp
2821
chromedpCtx, cancel := utils.InitChromeDp()
2922
defer cancel()

scrapers/mazevo.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,17 @@ import (
1414
"os"
1515
"time"
1616

17-
"github.com/joho/godotenv"
17+
"github.com/UTDNebula/api-tools/utils"
1818
)
1919

2020
func ScrapeMazevo(outDir string) {
21-
22-
// Load env vars
23-
if err := godotenv.Load(); err != nil {
24-
log.Panic("Error loading .env file")
25-
}
26-
apikey, present := os.LookupEnv("MAZEVO_API_KEY")
27-
if !present {
28-
log.Panic("MAZEVO_API_KEY is missing from .env!")
21+
apikey, err := utils.GetEnv("MAZEVO_API_KEY")
22+
if err != nil {
23+
panic(err)
2924
}
3025

3126
// Make output folder
32-
err := os.MkdirAll(outDir, 0777)
27+
err = os.MkdirAll(outDir, 0777)
3328
if err != nil {
3429
panic(err)
3530
}

scrapers/organizations.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"encoding/base64"
1111
"encoding/csv"
1212
"encoding/json"
13-
"errors"
1413
"fmt"
1514
"io"
1615
"log"
@@ -26,7 +25,6 @@ import (
2625
"github.com/chromedp/cdproto/browser"
2726
"github.com/chromedp/cdproto/network"
2827
"github.com/chromedp/chromedp"
29-
"github.com/joho/godotenv"
3028
"go.mongodb.org/mongo-driver/bson/primitive"
3129
)
3230

@@ -46,16 +44,7 @@ var (
4644

4745
func ScrapeOrganizations(outdir string) {
4846
log.Println("Scraping SOC ...")
49-
if err := godotenv.Load(); err != nil {
50-
log.Panic("error loading .env file")
51-
}
52-
53-
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Flag("headless", false))
54-
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
55-
defer cancel()
56-
57-
ctx, cancel := chromedp.NewContext(allocCtx)
58-
// ensure cleanup occurs
47+
ctx, cancel := utils.InitChromeDp()
5948
defer cancel()
6049

6150
if err := loginToSoc(ctx); err != nil {
@@ -66,21 +55,13 @@ func ScrapeOrganizations(outdir string) {
6655
}
6756
}
6857

69-
func lookupEnvWithError(name string) (string, error) {
70-
value, exists := os.LookupEnv(name)
71-
if !exists {
72-
return "", errors.New(name + " is missing from .env!")
73-
}
74-
return value, nil
75-
}
76-
7758
func loginToSoc(ctx context.Context) error {
7859
log.Println("Logging into SOC ...")
79-
netID, err := lookupEnvWithError("LOGIN_NETID")
60+
netID, err := utils.GetEnv("LOGIN_NETID")
8061
if err != nil {
8162
return err
8263
}
83-
password, err := lookupEnvWithError("LOGIN_PASSWORD")
64+
password, err := utils.GetEnv("LOGIN_PASSWORD")
8465
if err != nil {
8566
return err
8667
}

uploader/database.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"time"
1414

15+
"github.com/UTDNebula/api-tools/utils"
1516
"go.mongodb.org/mongo-driver/mongo"
1617
"go.mongodb.org/mongo-driver/mongo/options"
1718
)
@@ -47,10 +48,9 @@ func getCollection(client *mongo.Client, collectionName string) *mongo.Collectio
4748
}
4849

4950
func getEnvMongoURI() string {
50-
uri, exist := os.LookupEnv("MONGODB_URI")
51-
if !exist {
52-
log.Panic("Error loading 'MONGODB_URI' from the .env file")
53-
os.Exit(1)
51+
uri, err := utils.GetEnv("MONGODB_URI")
52+
if err != nil {
53+
panic(err)
5454
}
5555
return uri
5656
}

uploader/uploader.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"go.mongodb.org/mongo-driver/mongo/options"
2121

2222
"github.com/UTDNebula/nebula-api/api/schema"
23-
"github.com/joho/godotenv"
2423
)
2524

2625
// It's important to note that all of the files must be updated/uploaded TOGETHER!
@@ -33,12 +32,6 @@ import (
3332
var filesToUpload [3]string = [3]string{"courses.json", "professors.json", "sections.json"}
3433

3534
func Upload(inDir string, replace bool) {
36-
37-
//Load env vars
38-
if err := godotenv.Load(); err != nil {
39-
log.Panic("Error loading .env file")
40-
}
41-
4235
//Connect to mongo
4336
client := connectDB()
4437

0 commit comments

Comments
 (0)