Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for the utils package #34

Merged
merged 5 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/spf13/cobra"

"github.com/theredditbandit/pman/pkg/db"
"github.com/theredditbandit/pman/pkg/utils"
)

Expand All @@ -23,7 +24,7 @@ var infoCmd = &cobra.Command{
return ErrBadUsageInfoCmd
}
projectName := args[0]
infoData, err := utils.ReadREADME(projectName)
infoData, err := utils.ReadREADME(db.DBName, projectName)
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"github.com/theredditbandit/pman/pkg/db"
)

const dbname = db.DBTestName
const bucketName = "testBucket"
const key = "testKey"
const (
dbname = db.DBTestName
bucketName = "testBucket"
key = "testKey"
)

func Test_GetDBLoc(t *testing.T) {
t.Run("Test getDBLoc", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/interactiveTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func RenderInteractiveTable(data map[string]string) error {
var rows []table.Row
for proj, status := range data {
alias, err := db.GetRecord(db.DBName, proj, pkg.ProjectAliasBucket)
lastEdited := utils.GetLastModifiedTime(proj)
lastEdited := utils.GetLastModifiedTime(db.DBName, proj)
if err == nil {
pname := fmt.Sprintf("%s (%s)", proj, alias)
row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited
Expand Down
3 changes: 2 additions & 1 deletion pkg/ui/pager/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"

"github.com/theredditbandit/pman/pkg/db"
"github.com/theredditbandit/pman/pkg/utils"
)

Expand Down Expand Up @@ -101,7 +102,7 @@ func (m model) footerView() string {
}

func LaunchRenderer(file string) error {
content, err := utils.ReadREADME(file)
content, err := utils.ReadREADME(db.DBName, file)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/statusTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func RenderTable(data map[string]string) error {
var tableData [][]string
for p, status := range data {
alias, err := db.GetRecord(db.DBName, p, pkg.ProjectAliasBucket)
lastEdited := utils.GetLastModifiedTime(p)
lastEdited := utils.GetLastModifiedTime(db.DBName, p)
if err == nil {
pname := fmt.Sprintf("%s (%s)", p, alias)
row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited
Expand Down
38 changes: 13 additions & 25 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (

var (
ErrBeautifyMD = errors.New("error beautifying markdown")
ErrGetAlias = errors.New("error getting alias")
ErrGetProject = errors.New("error getting project")
ErrReadREADME = errors.New("error reading README")
)

Expand All @@ -38,28 +40,16 @@ func FilterByStatuses(data map[string]string, status []string) map[string]string
return filteredData
}

// Deprecated: Use ui.RenderTable instead
func PrintData(data map[string]string) {
for k, v := range data {
alias, err := db.GetRecord(db.DBName, k, pkg.ProjectAliasBucket)
if err == nil {
log.Printf("%s : %s (%s) \n", TitleCase(v), k, alias)
} else {
log.Printf("%s : %s \n", TitleCase(v), k)
}
}
}

func GetLastModifiedTime(pname string) string {
func GetLastModifiedTime(dbname, pname string) string {
var lastModTime time.Time
var lastModFile string
today := time.Now()
_ = lastModFile
pPath, err := db.GetRecord(db.DBName, pname, pkg.ProjectPaths)
pPath, err := db.GetRecord(dbname, pname, pkg.ProjectPaths)
if err != nil {
return "Something went wrong"
}
err = filepath.Walk(pPath, func(_ string, info os.FileInfo, err error) error {
_ = filepath.Walk(pPath, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
Expand All @@ -69,9 +59,7 @@ func GetLastModifiedTime(pname string) string {
}
return nil
})
if err != nil {
return "Something went wrong"
}

switch fmt.Sprint(lastModTime.Date()) {
case fmt.Sprint(today.Date()):
return fmt.Sprintf("Today %s", lastModTime.Format("15:04"))
Expand All @@ -97,25 +85,25 @@ func BeautifyMD(data []byte) (string, error) {
}

// ReadREADME: returns the byte array of README.md of a project
func ReadREADME(projectName string) ([]byte, error) {
path, err := db.GetRecord(db.DBName, projectName, pkg.ProjectPaths)
func ReadREADME(dbname, projectName string) ([]byte, error) {
path, err := db.GetRecord(dbname, projectName, pkg.ProjectPaths)
if err != nil {
actualName, err := db.GetRecord(db.DBName, projectName, pkg.ProjectAliasBucket)
actualName, err := db.GetRecord(dbname, projectName, pkg.ProjectAliasBucket)
if err != nil {
log.Printf("project: %v not a valid project\n", projectName)
return nil, errors.Join(ErrReadREADME, err)
return nil, errors.Join(ErrGetAlias, err)
}
projectName = actualName
path, err = db.GetRecord(db.DBName, projectName, pkg.ProjectPaths)
path, err = db.GetRecord(dbname, projectName, pkg.ProjectPaths)
if err != nil {
log.Printf("project: %v not a valid project\n", projectName)
return nil, errors.Join(ErrReadREADME, err)
return nil, errors.Join(ErrGetProject, err)
}
}
pPath := filepath.Join(path, "README.md")
data, err := os.ReadFile(pPath)
if err != nil {
return nil, fmt.Errorf("something went wrong while reading README for %s: %w", projectName, err)
return nil, errors.Join(ErrReadREADME, fmt.Errorf("something went wrong while reading README for %s: %w", projectName, err))
}
return data, nil
}
Loading
Loading