Skip to content

Commit

Permalink
🥰 Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bullshitsoftware committed Jul 10, 2022
1 parent 2bf7163 commit f82413e
Show file tree
Hide file tree
Showing 17 changed files with 708 additions and 113 deletions.
53 changes: 36 additions & 17 deletions cmd/yttadd/main.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,55 @@
package main

import (
"errors"
"flag"
"fmt"
"os"
"strings"

"github.com/bullshitsoftware/youtimetrack/internal/app"
yt "github.com/bullshitsoftware/youtimetrack/internal/pkg/youtrack"
)

type App interface {
Load()
NewYoutrack() app.Youtrack
}

var a App = &app.App{}

func main() {
if len(os.Args) != 4 {
app.ExitOnError(errors.New("invalid arguments number"))
flag.Usage = func() {
fmt.Printf("Usage of %s type issue duration comment:\n", os.Args[0])
fmt.Println(" - type, work type prefix (e.g., develop)")
fmt.Println(" - issue, issue number (e.g., XY-123)")
fmt.Println(" - duration, spent time in YouTrack format (e.g., 1h 30m)")
fmt.Println(" - comment, work item description (e.g., did something cool)")
}
flag.Parse()
if flag.NArg() != 4 {
flag.Usage()
return
}

a := app.Default()
err := a.ReadConfig()
app.ExitOnError(err)
typeName := strings.ToLower(os.Args[0])
types, err := a.Youtrack.WorkItemTypes()
a.Load()
yt := a.NewYoutrack()

typeName := strings.ToLower(os.Args[1])
issue := os.Args[2]
duration := os.Args[3]
text := os.Args[4]

types, err := yt.WorkItemTypes()
app.ExitOnError(err)
var t yt.Type
aTypes := []string{}
for _, i := range types {
s := strings.ToLower(i.Name)
if strings.HasPrefix(s, typeName) {
t = i
break
err = yt.Add(i, issue, duration, text)
app.ExitOnError(err)
fmt.Println("Time tracked")
return
}
aTypes = append(aTypes, i.Name)
}
issue := os.Args[1]
duration := os.Args[2]
text := os.Args[3]

a.Youtrack.Add(t, issue, duration, text)
fmt.Println("No work type found, available:", strings.Join(aTypes, ", "))
}
80 changes: 80 additions & 0 deletions cmd/yttadd/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"os"
"time"

"github.com/bullshitsoftware/youtimetrack/internal/app"
"github.com/bullshitsoftware/youtimetrack/internal/pkg/youtrack"
)

type AppStub struct{}

func (s *AppStub) Load() {}

func (s *AppStub) NewYoutrack() app.Youtrack {
return &YoutrackStub{}
}

type YoutrackStub struct{}

func (yt *YoutrackStub) WorkItems(start, end time.Time) ([]youtrack.WorkItem, error) {
panic("unexpected call")
}

func (yt *YoutrackStub) WorkItemTypes() ([]youtrack.Type, error) {
types := []youtrack.Type{
{
Id: "123",
Name: "Development",
},
{
Id: "456",
Name: "DevOps",
},
}

return types, nil
}

func (yt *YoutrackStub) Add(itemType youtrack.Type, issueId, duration, text string) error {
if itemType.Id != "123" {
panic("unexpected type")
}

if issueId != "XY-123" {
panic("unexpected issue")
}

if duration != "1h" {
panic("unexpected dureation")
}

if text != "did something" {
panic("unexpected text")
}

return nil
}

func Example() {
a = &AppStub{}

os.Args = []string{"yttadd"}
main()

os.Args = []string{"yttadd", "deve", "XY-123", "1h", "did something"}
main()

os.Args = []string{"yttadd", "strange", "XY-123", "1h", "did something"}
main()

// Output:
// Usage of yttadd type issue duration comment:
// - type, work type prefix (e.g., develop)
// - issue, issue number (e.g., XY-123)
// - duration, spent time in YouTrack format (e.g., 1h 30m)
// - comment, work item description (e.g., did something cool)
// Time tracked
// No work type found, available: Development, DevOps
}
10 changes: 7 additions & 3 deletions cmd/yttconf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import (
"github.com/bullshitsoftware/youtimetrack/internal/app"
)

type App interface {
Save() string
}

var a App = &app.App{}

func main() {
a := app.Default()
p, err := a.SaveConfig()
app.ExitOnError(err)
p := a.Save()
fmt.Println("Created", p, "edit it with your favorite text editor")
}
14 changes: 14 additions & 0 deletions cmd/yttconf/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

type AppStub struct{}

func (s *AppStub) Save() string {
return "/home/john/.config/ytt/config.json"
}

func Example() {
a = &AppStub{}
main()

// Output: Created /home/john/.config/ytt/config.json edit it with your favorite text editor
}
31 changes: 20 additions & 11 deletions cmd/yttdet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@ package main
import (
"flag"
"fmt"
"os"
"strings"
"time"

"github.com/bullshitsoftware/youtimetrack/internal/app"
)

type App interface {
Load()
NewCalendar() app.Calendar
NewYoutrack() app.Youtrack
}

var (
a App = &app.App{}
now time.Time = time.Now()
)

func main() {
a := app.Default()
err := a.ReadConfig()
app.ExitOnError(err)
a.Load()

cal := a.NewCalendar()
yt := a.NewYoutrack()

now := time.Now()
period := a.Calendar.Period(now)
fs := flag.NewFlagSet("details", flag.ExitOnError)
fs.Func("start", "start date (2006-01-02)", period.ParseStart)
fs.Func("end", "end date (2006-01-02)", period.ParseEnd)
fs.Parse(os.Args)
period := cal.Period(now)
flag.Func("start", "start date (2006-01-02)", period.ParseStart)
flag.Func("end", "end date (2006-01-02)", period.ParseEnd)
flag.Parse()

items, err := a.Youtrack.WorkItems(period.Start, period.End)
items, err := yt.WorkItems(period.Start, period.End)
app.ExitOnError(err)
for _, i := range items {
date := time.Unix(i.Date/1000, 0)
Expand Down
82 changes: 82 additions & 0 deletions cmd/yttdet/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"time"

"github.com/bullshitsoftware/youtimetrack/internal/app"
"github.com/bullshitsoftware/youtimetrack/internal/pkg/calendar"
"github.com/bullshitsoftware/youtimetrack/internal/pkg/youtrack"
)

type AppStub struct{}

func (s *AppStub) Load() {}

func (s *AppStub) NewCalendar() app.Calendar {
return &CalendarStub{}
}

func (s *AppStub) NewYoutrack() app.Youtrack {
return &YoutrackStub{}
}

type CalendarStub struct{}

func (s *CalendarStub) Period(now time.Time) *calendar.Period {
if !now.Equal(time.Date(2007, 1, 2, 3, 4, 5, 0, time.UTC)) {
panic("enexpected now")
}

return &calendar.Period{
Start: time.Date(2007, 1, 1, 0, 0, 0, 0, time.UTC),
End: time.Date(2007, 1, 31, 23, 59, 59, 0, time.UTC),
}
}

func (s *CalendarStub) Calc(start, end time.Time) int {
panic("unexpected call")
}

type YoutrackStub struct{}

func (yt *YoutrackStub) WorkItems(start, end time.Time) ([]youtrack.WorkItem, error) {
issue := &youtrack.Issue{IdReadable: "XY-123", Summary: "Do something cool"}
items := []youtrack.WorkItem{
{
Issue: issue,
Date: time.Date(2007, 1, 10, 3, 4, 5, 0, time.UTC).UnixMilli(),
Duration: youtrack.Duration{Minutes: 30},
Type: youtrack.Type{Id: "123", Name: "Development"},
Text: "did something cool",
},
{
Issue: issue,
Date: time.Date(2007, 1, 15, 3, 4, 5, 0, time.UTC).UnixMilli(),
Duration: youtrack.Duration{Minutes: 30},
Type: youtrack.Type{Id: "321", Name: "DevOps"},
Text: "opsed something cool",
},
}

return items, nil
}

func (yt *YoutrackStub) WorkItemTypes() ([]youtrack.Type, error) {
panic("unexpected call")
}

func (yt *YoutrackStub) Add(itemType youtrack.Type, issueId, duration, text string) error {
panic("unexpected call")
}

func Example() {
a = &AppStub{}
now = time.Date(2007, 1, 2, 3, 4, 5, 0, time.UTC)
main()

// Output:
// 2007-01-10 0h30m XY-123 Do something cool
// did something cool
// 2007-01-15 0h30m XY-123 Do something cool
// opsed something cool
}
35 changes: 22 additions & 13 deletions cmd/yttsum/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@ package main
import (
"flag"
"fmt"
"os"
"time"

"github.com/bullshitsoftware/youtimetrack/internal/app"
)

type App interface {
Load()
NewCalendar() app.Calendar
NewYoutrack() app.Youtrack
}

var (
a App = &app.App{}
now time.Time = time.Now()
)

func main() {
a := app.Default()
err := a.ReadConfig()
app.ExitOnError(err)
a.Load()

cal := a.NewCalendar()
yt := a.NewYoutrack()

now := time.Now()
period := a.Calendar.Period(now)
fs := flag.NewFlagSet("summary", flag.ExitOnError)
fs.Func("start", "start date (2006-01-02)", period.ParseStart)
fs.Func("end", "end date (2006-01-02)", period.ParseEnd)
fs.Parse(os.Args)
period := cal.Period(now)
flag.Func("start", "start date (2006-01-02)", period.ParseStart)
flag.Func("end", "end date (2006-01-02)", period.ParseEnd)
flag.Parse()

month := a.Calendar.Calc(period.Start, period.End)
items, err := a.Youtrack.WorkItems(period.Start, period.End)
month := cal.Calc(period.Start, period.End)
items, err := yt.WorkItems(period.Start, period.End)
app.ExitOnError(err)
worked := 0
for _, i := range items {
Expand All @@ -38,7 +47,7 @@ func main() {
return
}

today := a.Calendar.Calc(period.Start, now)
today := cal.Calc(period.Start, now)
fmt.Printf("%s / %s / %s (worked / today / month)\n",
app.FormatMinutes(worked),
app.FormatMinutes(today),
Expand Down
Loading

0 comments on commit f82413e

Please sign in to comment.