Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilGal committed Aug 18, 2024
1 parent b93bf9c commit d64086f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
name: "Run all tests"
strategy:
matrix:
go-version: [1.14.x]
go-version: [1.23.x]
platform: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"mode": "auto",
"program": "${workspaceFolder}",
"env": {},
"args": ["log", "-j","TEST-01", "-t", "30m"]
"args": ["log", "TEST-01", "-t", "30m"]
},
{
"name": "jtl push -p",
Expand Down
4 changes: 2 additions & 2 deletions cmd/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func SetDataFilePath(p string) {
const (
DefaultDateTimePattern = "02 Jan 2006 15:04"
DefaultDatePattern = "02 Jan 2006"
DataFileHeader = "id,date,activity,hours,jira"
DataFileHeader = "id,date,activity,hours"
)

func Init() {
Expand All @@ -61,7 +61,7 @@ func Init() {
viper.SetConfigName(configName)
viper.SetConfigType(configType)

viper.Set("dataFileHeader", "id,date,activity,hours,jira")
viper.Set("dataFileHeader", "id,date,activity,hours")
viper.SetDefault("host", "")
viper.SetDefault("credentials", map[string]string{
"username": "",
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (f *CsvFile) AddRecord(rec CsvRec) {
// UpdateRecord replaces record at the given index with the new record.
func (f *CsvFile) UpdateRecord(idx int, rec CsvRec) error {
if idx < 0 || idx > len(f.Records) {
return errors.New("ndex is out of range")
return errors.New("index is out of range")
}
rec._idx = idx
f.Records[idx] = rec
Expand Down
8 changes: 4 additions & 4 deletions cmd/internal/csv/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestCsvFile_Write(t *testing.T) {
writtenFile := CsvFile{
Path: path,
Header: GetCsvHeader(),
Records: CsvRecords{newCsvRecord(0, []string{"1", "14 Apr 2020 11:30", "US demo", "10m", "TICKET-1", "jira"})},
Records: CsvRecords{newCsvRecord(0, []string{"1", "14 Apr 2020 11:30", "US demo", "10m", "TICKET-1"})},
}
writtenFile.Write()

Expand All @@ -80,8 +80,8 @@ func TestCsvFile_ReadAll(t *testing.T) {
Path: "./csv_testdata/not_empty.csv",
Header: GetCsvHeader(),
Records: CsvRecords{
newCsvRecord(0, []string{"1", "14 Apr 2020 11:30", "Row with ID", "10m", "TICKET-1", "jira"}),
newCsvRecord(1, []string{"", "15 Apr 2020 11:30", "Row without ID", "10m", "TICKET-2", "jira"}),
newCsvRecord(0, []string{"1", "14 Apr 2020 11:30", "Row with ID", "10m", "TICKET-1"}),
newCsvRecord(1, []string{"", "15 Apr 2020 11:30", "Row without ID", "10m", "TICKET-2"}),
},
},
rowsExpected: 2,
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestUpdateRecord(t *testing.T) {
expectedError error
}{
{"Should update record at the given index", updatedRec._idx, updatedRec, nil},
{"Should return error if the given index is out of range", idxOutOfBounds, okCsvRecord, errors.New("Index is out of range")},
{"Should return error if the given index is out of range", idxOutOfBounds, okCsvRecord, errors.New("index is out of range")},
}

for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/model/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type JiraRequest []JiraRequestRow
// NewJiraRequest creates JiraRequest from CsvRecords
func NewJiraRequest(recs *csv.CsvRecords) JiraRequest {
jr := JiraRequest{}
for _, row := range recs.Filter(func(r csv.CsvRec) bool { return r.IsPushed() }) {
for _, row := range recs.Filter(func(r csv.CsvRec) bool { return !r.IsPushed() }) {
//Rows with IDs are pushed, don't them into request
req := JiraRequestRow{
_rowIdx: row.GetIdx(),
Expand Down
39 changes: 30 additions & 9 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package cmd

import (
"errors"
"log"
"slices"
"time"

"github.com/philgal/jtl/cmd/internal/config"
Expand Down Expand Up @@ -45,7 +48,7 @@ Examples:
`,
Run: func(cmd *cobra.Command, args []string) {
//use App for testing
runLogCommand(cmd)
runLogCommand(cmd, args)
displayReport()
},
}
Expand All @@ -59,17 +62,25 @@ const (

func init() {
rootCmd.AddCommand(logCmd)
logCmd.Flags().StringP(ticketCmdStr, "j", "", "[Required] Jira ticket. Ticket aliases can be used. See > jtl help log")
logCmd.MarkFlagRequired(ticketCmdStr)
//todo improve duration parsing
//todo improve duration parsing
logCmd.Flags().StringP(timeCmdStr, "t", "4h", "[Required] Time spent. Default - 4h")
logCmd.Flags().StringP(messageCmdStr, "m", "", "Comment to the work log. Will be displayed in Jira. Default - empty")
logCmd.Flags().StringP(messageCmdStr, "m", "wip", "Comment to the work log. Will be displayed in Jira. Default - \"wip\"")
logCmd.Flags().StringP(dateCmdStr, "d", time.Now().Format(config.DefaultDateTimePattern), "Date and time when the work has been started. Default - current timestamp")
}

func runLogCommand(cmd *cobra.Command) {
func runLogCommand(cmd *cobra.Command, args []string) {

if len(args) < 1 {
log.Fatal("too few arguments")
}

var ticket, timeSpent, date, comment string
ticket, _ = cmd.Flags().GetString(ticketCmdStr)

ticket = args[0]
if len(ticket) < 5 {
log.Fatalf("invalid ticket %s", ticket)
}

timeSpent, _ = cmd.Flags().GetString(timeCmdStr)
comment, _ = cmd.Flags().GetString(messageCmdStr)
date, _ = cmd.Flags().GetString(dateCmdStr)
Expand All @@ -79,10 +90,18 @@ func runLogCommand(cmd *cobra.Command) {
// total: 4h before pause, 4h after
// traverse from most recent record
// sum hours logged in the same day as `date`
// case =0: log 4h from 8:45, 4h from 13:00
// case =0: log 4h from 8:45, 4h from 13:00
// case >0:


// calculate how much is left of the date
slices.SortFunc(fcsv.Records, func(a csv.CsvRec, b csv.CsvRec) int {
atime, aerr := time.Parse(config.DefaultDateTimePattern, a.StartedTs)
btime, berr := time.Parse(config.DefaultDateTimePattern, b.StartedTs)
if err := errors.Join(aerr, berr); err != nil {
log.Fatal("cannot compare log records: ", err)
}
return atime.Compare(btime)
})

fcsv.AddRecord(csv.CsvRec{
ID: "",
Expand All @@ -91,5 +110,7 @@ func runLogCommand(cmd *cobra.Command) {
TimeSpent: timeSpent,
Ticket: ticket,
})

log.Print(fcsv.Records)
fcsv.Write()
}

0 comments on commit d64086f

Please sign in to comment.