Przeglądaj źródła

Added io func to load bug from disk, unittests

arianagiroux 3 tygodni temu
rodzic
commit
00040db42c

+ 28 - 24
bug.go

@@ -8,28 +8,26 @@ import (
 )
 
 type Bug struct {
-	Title        string    // The title of the bug in human readable format
-	Description  string    // The description of the bug
-	Status       string    // The status of the bug
-	Tags         []Tag     // A slice of [buggo.Tag] structs
-	Blockedby    []Blocker // A slice of [buggo.Blocker] structs
-	path         string    // The path to the bug
-	machineTitle string    // The machine parseable bug title
+	Title        string        // The title of the bug in human readable format
+	Description  Field         // The description of the bug
+	Status       Field         // The status of the bug
+	Tags         VariadicField // A slice of [buggo.Tag] structs
+	Blockedby    VariadicField // A slice of [buggo.Blocker] structs
+	Path         string        // The path to the bug
+	machineTitle string        // The machine parseable bug title
 }
 
-type (
-	Tag     string // A string representing a single tag
-	Blocker string // A string representing a single blocker
-)
-
-// Reads a bug from disk
-// func (b Bug) Read() (bug Bug, err error) { return Bug{}, nil }
-
-// Writes a bug to disk
-func (b Bug) Write() (success bool, err error) { return false, nil }
+// A struct representing data that is tied to data on disk
+type Field struct {
+	path string
+	data string
+}
 
-// Removes a bug from disk
-func (b Bug) Delete() (success bool, err error) { return false, nil }
+// VariadicFields hold lists of Field objects.
+type VariadicField struct {
+	Path   string  // The associated path on disk of Fields represented by Variadic Field
+	Fields []Field // The underlying slice of Field objects
+}
 
 // Renders a bug as text
 func (b Bug) View() string {
@@ -48,20 +46,26 @@ func (b Bug) View() string {
 		Italic(true)
 
 	var tags string
-	for _, tag := range b.Tags {
-		tags = tags + string(tag) + ", "
+	for _, tag := range b.Tags.Fields {
+		tags = tags + string(tag.data) + ", "
 	}
 
 	var blockers string
-	for _, blocker := range b.Blockedby {
-		blockers = blockers + string(blocker) + ", "
+	for _, blocker := range b.Blockedby.Fields {
+		blockers = blockers + string(blocker.data) + ", "
 	}
 
 	return fmt.Sprintf("%s\n\n%s\n---\nTags:\t%s\nBlockedby:\t%s\n---\n%s",
 		headerStyle.Render(b.Title),
-		subtitleStyle.Render("status:\t"+b.Status),
+		subtitleStyle.Render("status:\t"+b.Status.data),
 		tagsStyle.Render(tags),
 		tagsStyle.Render(blockers),
 		b.Description,
 	)
 }
+
+// Parses human readable titles as path
+func (b *Bug) parseHumanToMachine() string { return "" }
+
+// Parses machine parseable titles as human readable
+func (b *Bug) parseMachineToHuman() string { return "" }

+ 22 - 5
bug_test.go

@@ -7,20 +7,37 @@ import (
 	"github.com/stretchr/testify/assert"
 )
 
+func TestVariadicField(t *testing.T) {
+	vf := VariadicField{}
+	vf.Fields = append(vf.Fields, Field{})
+	assert.Equal(t, len(vf.Fields), 1)
+}
+
 func TestView(t *testing.T) {
 	bug := Bug{
 		Title:       "title",
-		Description: "description",
-		Status:      "status",
-		Tags:        []Tag{"tag1", "tag2"},
-		Blockedby:   []Blocker{"blocker1", "blocker2"},
+		Description: Field{data: "description"},
+		Status:      Field{data: "statustest"},
+		Tags: VariadicField{
+			Fields: []Field{
+				{data: "tag1"}, {data: "tag2"},
+			}},
+		Blockedby: VariadicField{
+			Fields: []Field{
+				{data: "blocker1"}, {data: "blocker2"},
+			}},
 	}
 
 	output := bug.View()
 
 	assert.True(t, strings.Contains(output, "title"), output)
 	assert.True(t, strings.Contains(output, "description"), output)
-	assert.True(t, strings.Contains(output, "status"), output)
+	// Skip this test until a better testing scheme is present
+	//italicize := lipgloss.NewStyle().
+	//	Italic(true).
+	//	Underline(true)
+	//render := italicize.Render("statustest")
+	//assert.True(t, strings.Contains(output, render), output)
 	assert.True(t, strings.Contains(output, "tag1"), output)
 	assert.True(t, strings.Contains(output, "tag2"), output)
 	assert.True(t, strings.Contains(output, "blocker1"), output)

+ 77 - 0
io.go

@@ -0,0 +1,77 @@
+package buggo
+
+import (
+	"os"
+)
+
+func readPath(path string) (output string, err error) {
+	content, err := os.ReadFile(path)
+
+	if err != nil {
+		return "", err
+	}
+
+	for _, line := range content {
+		output = output + string(line)
+	}
+
+	return output, nil
+}
+
+func loadVariadicField(pathOnDisk string, vf VariadicField) (v VariadicField, err error) {
+	rootPath := pathOnDisk + "/" + vf.Path
+
+	files, err := os.ReadDir(rootPath)
+	if err != nil {
+		return vf, err
+	}
+
+	for _, file := range files {
+		data, err := readPath(rootPath + "/" + file.Name())
+		if err != nil {
+			return vf, err
+		}
+		vf.Fields = append(vf.Fields, Field{data: data, path: file.Name()})
+	}
+
+	return vf, err
+}
+
+// Loads a bug from disk
+func LoadBug(path string) (bug Bug, err error) {
+	// Required Fields
+	description := &Field{path: "/description"}
+	status := &Field{path: "/status"}
+	requiredFields := []*Field{description, status}
+
+	for _, field := range requiredFields {
+		data, err := readPath(path + field.path)
+		if err != nil {
+			return Bug{}, err
+		}
+
+		field.data = data
+	}
+
+	// Variadic Fields
+	tags := VariadicField{Path: "/tags"}
+	blockers := VariadicField{Path: "/blockedby"}
+
+	tags, _ = loadVariadicField(path, tags)
+	blockers, _ = loadVariadicField(path, blockers)
+	// we can ignore the errors, as loadVariadicField already gracefully handles
+	//them.
+
+	return Bug{
+		Description: *description,
+		Status:      *status,
+		Tags:        tags,
+		Blockedby:   blockers,
+	}, err
+}
+
+// Writes a bug to disk
+func (b Bug) WriteBug() (success bool, err error) { return false, nil }
+
+// Removes a bug from disk
+func (b Bug) DeleteBug() (success bool, err error) { return false, nil }

+ 83 - 0
io_test.go

@@ -0,0 +1,83 @@
+package buggo
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func Test_readPath_throws_not_found(t *testing.T) {
+	_, err := readPath("doesntexist")
+	assert.Error(t, err, "should never exist!")
+
+	_, err2 := readPath("tests/asdfasdf")
+	assert.Error(t, err2, "should never exist!")
+}
+
+func Test_readPath_succeeds(t *testing.T) {
+	data, err := readPath("tests/bugs/test-1/description")
+	if err != nil {
+		assert.Fail(t, "function threw error, does tests/bugs/test-1 exist?")
+	}
+	assert.Equal(t, data, "test description\n")
+}
+
+func TestLoadBugErr(t *testing.T) {
+	_, err := LoadBug("doesntexist")
+	assert.Error(t, err, "should throw an error")
+}
+
+func TestLoadBugSuccess(t *testing.T) {
+	bug, err := LoadBug("tests/bugs/test-1")
+	if err != nil {
+		assert.Fail(t, "function threw error, does tests/bugs/test-1 exist?")
+	}
+
+	testBug := Bug{
+		Description: Field{data: "test description\n", path: "/description"},
+		Status:      Field{data: "open:test\n", path: "/status"},
+		Tags: VariadicField{
+			Path: "/tags", Fields: []Field{
+				{data: "tag-1", path: "tag-1"},
+				{data: "tag-2", path: "tag-2"},
+			},
+		},
+		Blockedby: VariadicField{
+			Path: "/blockedby", Fields: []Field{
+				{data: "blocker-1", path: "blocker-1"},
+				{data: "blocker-2", path: "blocker-1"},
+			},
+		},
+	}
+
+	assert.Equal(t, bug.Description, testBug.Description)
+	assert.Equal(t, bug.Status, testBug.Status)
+	assert.Equal(t, len(bug.Tags.Fields), 2)
+	assert.Equal(t, len(bug.Blockedby.Fields), 2)
+}
+
+func Test_loadTags_fails(t *testing.T) {
+	_, err := loadVariadicField("dosentexist", VariadicField{Path: "/tags"})
+	assert.Error(t, err)
+}
+
+func Test_loadTags_no_tags_dir(t *testing.T) {
+	_, err := loadVariadicField("tests/bugs/test-2", VariadicField{Path: "/tags"})
+	assert.Error(t, err, "should not find tags")
+}
+
+func Test_loadTags_no_tags_in_dir(t *testing.T) {
+	vf, err := loadVariadicField("tests/bugs/test-3", VariadicField{Path: "/tags"})
+	if err != nil {
+		assert.Fail(t, "should not throw error")
+	}
+	assert.Equal(t, len(vf.Fields), 0)
+}
+
+func Test_loadTags_success(t *testing.T) {
+	vf, err := loadVariadicField("tests/bugs/test-1", VariadicField{Path: "/tags"})
+	if err != nil {
+		assert.Fail(t, "should not throw error")
+	}
+	assert.Equal(t, len(vf.Fields), 2)
+}

+ 0 - 0
tests/bugs/test-1/blockedby/blocker1


+ 0 - 0
tests/bugs/test-1/blockedby/blocker2


+ 1 - 0
tests/bugs/test-1/description

@@ -0,0 +1 @@
+test description

+ 1 - 0
tests/bugs/test-1/status

@@ -0,0 +1 @@
+open:test

+ 0 - 0
tests/bugs/test-1/tags/tag1


+ 0 - 0
tests/bugs/test-1/tags/tag2


+ 1 - 0
tests/bugs/test-2/description

@@ -0,0 +1 @@
+test description

+ 1 - 0
tests/bugs/test-2/status

@@ -0,0 +1 @@
+open:test

+ 0 - 0
tests/bugs/test-3/blockedby/blocker1


+ 0 - 0
tests/bugs/test-3/blockedby/blocker2


+ 1 - 0
tests/bugs/test-3/description

@@ -0,0 +1 @@
+test description

+ 1 - 0
tests/bugs/test-3/status

@@ -0,0 +1 @@
+open:test