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

[testing] add more tests #45

Merged
merged 4 commits into from
Feb 11, 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
60 changes: 27 additions & 33 deletions cmd/author.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
package cmd

import (
"fmt"

"github.com/cacoco/codemetagenerator/internal/model"
"github.com/cacoco/codemetagenerator/internal/utils"
"github.com/spf13/cobra"
)

func newAuthor(reader utils.Reader, writer utils.Writer) (*map[string]any, error) {
func author(reader utils.Reader, writer utils.Writer, basedir string) (*map[string]any, error) {
inProgressFilePath := utils.GetInProgressFilePath(basedir)

codemeta, err := utils.Unmarshal(inProgressFilePath)
if err != nil {
handleErr(writer, err)
return nil, writer.Errorf("unable to load the in-progress codemeta.json file for editing. Have you run \"codemetagenerator new\" yet?")

Check warning on line 15 in cmd/author.go

View check run for this annotation

Codecov / codecov/patch

cmd/author.go#L14-L15

Added lines #L14 - L15 were not covered by tests
}
mutateMap := *codemeta
currentValue := mutateMap[model.Author]
author, err := utils.NewPersonOrOrganizationPrompt(&reader, &writer, "Author")
if err != nil {
return nil, err
}
if currentValue == nil {
// set the new author as the value
mutateMap[model.Author] = []any{author}
} else {
// append new author to existing authors
mutateMap[model.Author] = append(currentValue.([]any), author)

Check warning on line 28 in cmd/author.go

View check run for this annotation

Codecov / codecov/patch

cmd/author.go#L28

Added line #L28 was not covered by tests
}

err = utils.Marshal(inProgressFilePath, mutateMap)
if err != nil {
handleErr(writer, err)
return nil, writer.Errorf("unable to save in-progress codemeta.json file after editing")

Check warning on line 34 in cmd/author.go

View check run for this annotation

Codecov / codecov/patch

cmd/author.go#L33-L34

Added lines #L33 - L34 were not covered by tests
} else {
writer.Println("⭐ Successfully updated in-progress codemeta.json file.")
}
return author, nil
}

Expand All @@ -32,36 +54,8 @@

When complete, run "generate" to generate the resultant 'codemeta.json' file.`,
RunE: func(cmd *cobra.Command, args []string) error {
reader := &utils.StdinReader{}
writer := &utils.StdoutWriter{}

inProgressFilePath := utils.GetInProgressFilePath(utils.UserHomeDir)

codemeta, err := utils.Unmarshal(inProgressFilePath)
if err != nil {
handleErr(writer, err)
return fmt.Errorf("unable to load the in-progress codemeta.json file for editing. Have you run \"codemetagenerator new\" yet?")
}
mutateMap := *codemeta
currentValue := mutateMap[model.Author]
author, err := newAuthor(reader, writer)
if err != nil {
return err
}
if currentValue == nil {
mutateMap[model.Author] = []any{author}
} else {
mutateMap[model.Author] = append(currentValue.([]any), author)
}

err = utils.Marshal(inProgressFilePath, mutateMap)
if err != nil {
handleErr(writer, err)
return fmt.Errorf("unable to save in-progress codemeta.json file after editing")
} else {
fmt.Println("⭐ Successfully updated in-progress codemeta.json file.")
}
return nil
_, err := author(&utils.StdinReader{}, &utils.StdoutWriter{}, utils.UserHomeDir)
return err

Check warning on line 58 in cmd/author.go

View check run for this annotation

Codecov / codecov/patch

cmd/author.go#L57-L58

Added lines #L57 - L58 were not covered by tests
},
}

Expand Down
99 changes: 94 additions & 5 deletions cmd/author_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
package cmd

import (
"bytes"
"os"
"testing"

"github.com/cacoco/codemetagenerator/internal/model"
"github.com/cacoco/codemetagenerator/internal/utils"
"github.com/ohler55/ojg/oj"
"github.com/onsi/gomega"
"github.com/spf13/cobra"
)

func TestNewAuthor(t *testing.T) {
func TestAddAuthor(t *testing.T) {
g := gomega.NewWithT(t)

temp := t.TempDir()
// setup
os.Mkdir(utils.GetHomeDir(temp), 0755)

testMap := map[string]any{
model.Context: model.DefaultContext,
model.Type: model.SoftwareSourceCodeType,
model.Description: "description",
model.ContinuousIntegration: "https://url.org",
}

inProgressFilePath := utils.GetInProgressFilePath(temp)
// need an in-progress code meta file
err := utils.Marshal(inProgressFilePath, testMap)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

var stack utils.Stack[string]
stack.Push("id\n")
stack.Push("person@email.org\n")
Expand All @@ -20,10 +42,7 @@ func TestNewAuthor(t *testing.T) {
reader := utils.TestReader{In: utils.TestStdin{Data: stack}}
writer := utils.TestWriter{}

author, err := newAuthor(&reader, &writer)
if author == nil {
t.Errorf("Expected author to not be nil")
}
author, err := author(&reader, &writer, temp)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
Expand All @@ -36,3 +55,73 @@ func TestNewAuthor(t *testing.T) {
}
g.Ω(*author).Should(gomega.Equal(expected))
}

func Test_ExecuteAuthorCmd(t *testing.T) {
g := gomega.NewWithT(t)

temp := t.TempDir()
// setup
os.Mkdir(utils.GetHomeDir(temp), 0755)

testMap := map[string]any{
model.Context: model.DefaultContext,
model.Type: model.SoftwareSourceCodeType,
model.Description: "description",
model.ContinuousIntegration: "https://url.org",
}

inProgressFilePath := utils.GetInProgressFilePath(temp)
// need an in-progress code meta file
err := utils.Marshal(inProgressFilePath, testMap)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

var stack utils.Stack[string]
stack.Push("id\n")
stack.Push("person@email.org\n")
stack.Push("familyName\n")
stack.Push("givenName\n")
stack.Push("\n") // enter to select the first option
reader := utils.TestReader{In: utils.TestStdin{Data: stack}}
writer := utils.TestWriter{}

authorCmd := &cobra.Command{Use: "author", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error {
_, err := author(&reader, &writer, temp)
return err
},
}
buf := bytes.NewBufferString("")
authorCmd.SetOut(buf)
authorCmd.SetErr(buf)
authorCmd.SetArgs([]string{})

err = authorCmd.Execute()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

// check file
fileBytes, err := utils.LoadFile(utils.GetInProgressFilePath(temp))
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var m = make(map[string]any)
oj.Unmarshal(fileBytes, &m)

expected := map[string]any{
model.Context: model.DefaultContext,
model.Type: model.SoftwareSourceCodeType,
model.Description: "description",
model.ContinuousIntegration: "https://url.org",
model.Author: []any{map[string]any{
model.Type: model.PersonType,
model.FamilyName: "familyName",
model.GivenName: "givenName",
model.Email: "person@email.org",
model.Id: "id",
}},
}

g.Ω(m).Should(gomega.Equal(expected))
}
61 changes: 27 additions & 34 deletions cmd/contributor.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
package cmd

import (
"fmt"

"github.com/cacoco/codemetagenerator/internal/model"
"github.com/cacoco/codemetagenerator/internal/utils"
"github.com/spf13/cobra"
)

func newContributor(reader utils.Reader, writer utils.Writer) (*map[string]any, error) {
func contributor(reader utils.Reader, writer utils.Writer, basedir string) (*map[string]any, error) {
inProgressFilePath := utils.GetInProgressFilePath(basedir)

codemeta, err := utils.Unmarshal(inProgressFilePath)
if err != nil {
handleErr(writer, err)
return nil, writer.Errorf("unable to load the in-progress codemeta.json file for editing. Have you run \"codemetagenerator new\" yet?")

Check warning on line 15 in cmd/contributor.go

View check run for this annotation

Codecov / codecov/patch

cmd/contributor.go#L14-L15

Added lines #L14 - L15 were not covered by tests
}
mutateMap := *codemeta
currrentValue := mutateMap[model.Contributor]
contributor, err := utils.NewPersonOrOrganizationPrompt(&reader, &writer, "Contributor")
if err != nil {
return nil, err
}
if currrentValue == nil {
// set the new contributor as the value
mutateMap[model.Contributor] = []any{contributor}
} else {
// append new contributor to existing contributors
mutateMap[model.Contributor] = append(currrentValue.([]any), contributor)

Check warning on line 28 in cmd/contributor.go

View check run for this annotation

Codecov / codecov/patch

cmd/contributor.go#L28

Added line #L28 was not covered by tests
}

err = utils.Marshal(inProgressFilePath, mutateMap)
if err != nil {
handleErr(writer, err)
return nil, writer.Errorf("unable to save in-progress codemeta.json file after editing")

Check warning on line 34 in cmd/contributor.go

View check run for this annotation

Codecov / codecov/patch

cmd/contributor.go#L33-L34

Added lines #L33 - L34 were not covered by tests
} else {
writer.Println("⭐ Successfully updated in-progress codemeta.json file.")
}
return contributor, nil
}

Expand All @@ -32,37 +54,8 @@

When complete, run "generate" to generate the resultant 'codemeta.json' file.`,
RunE: func(cmd *cobra.Command, args []string) error {
reader := &utils.StdinReader{}
writer := &utils.StdoutWriter{}

inProgressFilePath := utils.GetInProgressFilePath(utils.UserHomeDir)

codemeta, err := utils.Unmarshal(inProgressFilePath)
if err != nil {
return fmt.Errorf("unable to load the in-progress codemeta.json file for editing. Have you run \"codemetagenerator new\" yet?")
}

mutateMap := *codemeta
currrentValue := mutateMap[model.Contributor]
contributor, err := newContributor(reader, writer)
if err != nil {
handleErr(writer, err)
return err
}
if currrentValue == nil {
mutateMap[model.Contributor] = []any{contributor}
} else {
mutateMap[model.Contributor] = append(currrentValue.([]any), contributor)
}

err = utils.Marshal(inProgressFilePath, mutateMap)
if err != nil {
handleErr(writer, err)
return fmt.Errorf("unable to save in-progress codemeta.json file after editing")
} else {
fmt.Println("⭐ Successfully updated in-progress codemeta.json file.")
}
return nil
_, err := contributor(&utils.StdinReader{}, &utils.StdoutWriter{}, utils.UserHomeDir)
return err

Check warning on line 58 in cmd/contributor.go

View check run for this annotation

Codecov / codecov/patch

cmd/contributor.go#L57-L58

Added lines #L57 - L58 were not covered by tests
},
}

Expand Down
Loading
Loading