Skip to content

Commit

Permalink
Added basic unit tests and cleaned up package refs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jelloeater committed Sep 22, 2024
1 parent d2d5c12 commit 2f6a6f6
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 48 deletions.
6 changes: 1 addition & 5 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tasks:
- go vet .
- go clean .
- task: build
- stampy
- ./dist/stampy_linux_386/stampy
silent: false

go_build:
Expand All @@ -20,10 +20,6 @@ tasks:
- go build -o ./build .
- ./build/stampy

rm_deb:
ignore_error: true
cmds:
- sudo dpkg -r stampy -y
build:
- goreleaser --snapshot --clean

Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
module stampy
module github.com/jelloeater/stampy

go 1.22

require (
github.com/atotto/clipboard v0.1.4
github.com/beevik/ntp v1.4.3
github.com/stretchr/testify v1.9.0
github.com/urfave/cli/v2 v2.27.4

)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
42 changes: 0 additions & 42 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os"
"time"

"github.com/atotto/clipboard"
"github.com/beevik/ntp"
"github.com/urfave/cli/v2"
)

Expand All @@ -17,46 +15,6 @@ var ( // Create by GoRelease at compile time
//date = "unknown"
)

func writeDate(format string, timezone string, ntpServer string, diaryFormat bool) {

if os.Getenv("STAMPY_TZ") != "" {
timezone = os.Getenv("STAMPY_TZ")
}
if os.Getenv("STAMPY_FORMAT") != "" {
format = os.Getenv("STAMPY_FORMAT")
}
if os.Getenv("STAMPY_NTP") != "" {
ntpServer = os.Getenv("STAMPY_NTP")
}
if diaryFormat {
format = "Monday January 2 2006 3:04PM"
}

now := time.Time{} // Declare now outside the if-else block

if timezone != "" {
now = time.Now()
} else {
loc, e := time.LoadLocation(timezone)
if e != nil {
log.Fatal(e)
}
now = time.Now().In(loc)
}

if ntpServer != "" { // Override local time with NTP server
ntpTime, err := ntp.Time(ntpServer)
if err == nil { // Check for errors when getting NTP time
now = ntpTime
println("NTP from " + ntpServer)
}
}
timestamp := now.Format(format)
clip := timestamp
println(clip + " copied to clipboard")
_ = clipboard.WriteAll(clip)
}

func mainCliApp() error {
authors := []*cli.Author{
{
Expand Down
42 changes: 42 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func TestWriteDate(t *testing.T) {
// Backup original environment variables
originalTZ := os.Getenv("STAMPY_TZ")
originalFormat := os.Getenv("STAMPY_FORMAT")
originalNTP := os.Getenv("STAMPY_NTP")
defer func() {
os.Setenv("STAMPY_TZ", originalTZ)
os.Setenv("STAMPY_FORMAT", originalFormat)
os.Setenv("STAMPY_NTP", originalNTP)
}()

t.Run("Default behavior", func(t *testing.T) {
writeDate("", "", "", false)
// Add assertions based on expected output
})

t.Run("With environment variables", func(t *testing.T) {
os.Setenv("STAMPY_TZ", "America/New_York")
os.Setenv("STAMPY_FORMAT", "2006-01-02")
os.Setenv("STAMPY_NTP", "pool.ntp.org")
writeDate("", "", "", false)
// Add assertions based on expected output
})

t.Run("Diary format", func(t *testing.T) {
writeDate("", "", "", true)
// Add assertions based on expected output
})

t.Run("Error loading location", func(t *testing.T) {
os.Setenv("STAMPY_TZ", "Invalid/Timezone")
assert.Panics(t, func() { writeDate("", "", "", false) })
})
}
48 changes: 48 additions & 0 deletions worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"github.com/atotto/clipboard"
"github.com/beevik/ntp"
"log"
"os"
"time"
)

func writeDate(format string, timezone string, ntpServer string, diaryFormat bool) {

if os.Getenv("STAMPY_TZ") != "" {
timezone = os.Getenv("STAMPY_TZ")
}
if os.Getenv("STAMPY_FORMAT") != "" {
format = os.Getenv("STAMPY_FORMAT")
}
if os.Getenv("STAMPY_NTP") != "" {
ntpServer = os.Getenv("STAMPY_NTP")
}
if diaryFormat {
format = "Monday January 2 2006 3:04PM"
}

now := time.Time{} // Declare now outside the if-else block

if timezone != "" {
loc, err := time.LoadLocation(timezone)
if err != nil {
log.Panic(err)
}
now = time.Now().In(loc)
} else {
now = time.Now()
}
if ntpServer != "" { // Override local time with NTP server
ntpTime, err := ntp.Time(ntpServer)
if err == nil { // Check for errors when getting NTP time
now = ntpTime
println("NTP from " + ntpServer)
}
}
timestamp := now.Format(format)
clip := timestamp
println(clip + " copied to clipboard")
_ = clipboard.WriteAll(clip)
}

0 comments on commit 2f6a6f6

Please sign in to comment.