Ver Fonte

Makes WriteIssue actually capable of overwriting

arianagiroux há 2 semanas atrás
pai
commit
57ffe4ea69

+ 15 - 9
io.go

@@ -152,7 +152,7 @@ func InvokeEditor(path string) error {
 // after any modifications made by the editFunc callback and any encountered
 // errors.
 //
-// Note: A pre-built editor function is provided. See InvokeEditor for more.
+// Note: A pre-built editor callback is provided. See InvokeEditor for more.
 func EditTemplate(t Template, editFunc func(path string) error) (data []string, err error) {
 	// note: wraps writeText
 
@@ -247,15 +247,18 @@ func IsIssueCollection(path string) bool {
 }
 
 // Writes a issue to disk
-func WriteIssue(issue Issue, ignoreExisting bool) (success bool, err error) {
-	if IsIssue(issue.Path) && !ignoreExisting {
+func WriteIssue(issue Issue, overwrite bool) (success bool, err error) {
+	if IsIssue(issue.Path) && !overwrite {
 		return false, errors.New("path exists")
 	}
 
 	// make base directory (effectively, the title)
 	err = os.Mkdir(issue.Path, 0755)
-	if err != nil {
+
+	if err != nil && !overwrite {
 		return false, err
+	} else {
+		err = nil
 	}
 
 	// Write Fields
@@ -273,8 +276,8 @@ func WriteIssue(issue Issue, ignoreExisting bool) (success bool, err error) {
 	// Write Tags
 	if len(issue.Tags.Path) > 0 { // skip tags with no path init
 		err = os.Mkdir(filepath.Join(issue.Path, issue.Tags.Path), 0755)
-		if err != nil {
-			return false, err // test by overwrite path
+		if err != nil && !overwrite {
+			return false, err
 		}
 		for _, field := range issue.Tags.Fields {
 			if len(field.Path) > 0 { // skip empty paths
@@ -288,10 +291,10 @@ func WriteIssue(issue Issue, ignoreExisting bool) (success bool, err error) {
 	}
 
 	// Write Blockedby
-	if len(issue.Tags.Path) > 0 { // skip blockedby with no path init
+	if len(issue.Blockedby.Path) > 0 { // skip blockedby with no path init
 		err = os.Mkdir(filepath.Join(issue.Path, issue.Blockedby.Path), 0755)
-		if err != nil {
-			return false, err // test by overwrite path
+		if err != nil && !overwrite {
+			return false, err
 		}
 		for _, field := range issue.Blockedby.Fields {
 			if len(field.Path) > 0 { // skip empty paths
@@ -302,6 +305,9 @@ func WriteIssue(issue Issue, ignoreExisting bool) (success bool, err error) {
 				}
 			}
 		}
+		if err != nil && !overwrite {
+			return false, err
+		}
 	}
 
 	return true, nil

+ 27 - 1
io_test.go

@@ -70,7 +70,7 @@ func Test_WriteIssue_fails_on_bad_path(t *testing.T) {
 		Path:  "tests/cantexist/test",
 	}
 
-	_, err := WriteIssue(testIssue, true)
+	_, err := WriteIssue(testIssue, false)
 	assert.Error(t, err)
 }
 
@@ -145,6 +145,32 @@ func Test_WriteIssue_creates_variadic_fields(t *testing.T) {
 	assert.Nil(t, err)
 }
 
+func Test_WriteIssue_overwrites(t *testing.T) {
+	testIssue := Issue{
+		Path:        "tests/bugs/test-overwrite",
+		Title:       "test overwrite",
+		Status:      Field{Path: "/status", Data: "test"},
+		Description: Field{Path: "/description", Data: "test"},
+		Tags: VariadicField{
+			Path: "/tags",
+			Fields: []Field{
+				{Path: "test1"},
+				{Path: "test2"},
+			},
+		},
+		Blockedby: VariadicField{
+			Path: "/blockedby",
+			Fields: []Field{
+				{Path: "test1"},
+				{Path: "test2"},
+			},
+		},
+	}
+
+	_, err := WriteIssue(testIssue, true)
+	assert.Nil(t, err)
+}
+
 func Test_Generate_and_Read_Template(t *testing.T) {
 	testPath := "tests/desc1"
 

+ 0 - 0
tests/bugs/test-overwrite/blockedby/test1


+ 0 - 0
tests/bugs/test-overwrite/blockedby/test2


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

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

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

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

+ 0 - 0
tests/bugs/test-overwrite/tags/test1


+ 0 - 0
tests/bugs/test-overwrite/tags/test2